Add logging of requests to ek

This commit is contained in:
Peter Stuifzand 2018-08-18 11:50:38 +02:00
parent 03c7375064
commit c23802216e
Signed by: peter
GPG Key ID: 374322D56E5209E8

View File

@ -3,7 +3,9 @@ package client
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"net/http" "net/http"
"net/http/httputil"
"net/url" "net/url"
"strings" "strings"
@ -14,6 +16,8 @@ type Client struct {
Me *url.URL Me *url.URL
MicrosubEndpoint *url.URL MicrosubEndpoint *url.URL
Token string Token string
logging bool
} }
func (c *Client) microsubGetRequest(action string, args map[string]string) (*http.Response, error) { func (c *Client) microsubGetRequest(action string, args map[string]string) (*http.Response, error) {
@ -34,7 +38,19 @@ func (c *Client) microsubGetRequest(action string, args map[string]string) (*htt
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.Token)) req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.Token))
return client.Do(req) if c.logging {
x, _ := httputil.DumpRequestOut(req, true)
log.Printf("REQUEST:\n\n%s\n\n", x)
}
res, err := client.Do(req)
if c.logging {
x, _ := httputil.DumpResponse(res, true)
log.Printf("RESPONSE:\n\n%s\n\n", x)
}
return res, err
} }
func (c *Client) microsubPostRequest(action string, args map[string]string) (*http.Response, error) { func (c *Client) microsubPostRequest(action string, args map[string]string) (*http.Response, error) {
@ -55,7 +71,19 @@ func (c *Client) microsubPostRequest(action string, args map[string]string) (*ht
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.Token)) req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.Token))
return client.Do(req) if c.logging {
x, _ := httputil.DumpRequestOut(req, true)
log.Printf("REQUEST:\n\n%s\n\n", x)
}
res, err := client.Do(req)
if c.logging {
x, _ := httputil.DumpResponse(res, true)
log.Printf("RESPONSE:\n\n%s\n\n", x)
}
return res, err
} }
func (c *Client) microsubPostFormRequest(action string, args map[string]string, data url.Values) (*http.Response, error) { func (c *Client) microsubPostFormRequest(action string, args map[string]string, data url.Values) (*http.Response, error) {
@ -94,11 +122,8 @@ func (c *Client) ChannelsGetList() ([]microsub.Channel, error) {
dec := json.NewDecoder(res.Body) dec := json.NewDecoder(res.Body)
var channels channelsResponse var channels channelsResponse
err = dec.Decode(&channels) err = dec.Decode(&channels)
if err != nil {
return channels.Channels, err
}
return channels.Channels, nil return channels.Channels, err
} }
func (c *Client) TimelineGet(before, after, channel string) (microsub.Timeline, error) { func (c *Client) TimelineGet(before, after, channel string) (microsub.Timeline, error) {