Compare commits

...

2 Commits

Author SHA1 Message Date
c10bf83c41
Add -verbose option to enable logging
All checks were successful
the build was successful
2018-08-18 11:58:57 +02:00
c23802216e
Add logging of requests to ek 2018-08-18 11:50:38 +02:00
2 changed files with 41 additions and 7 deletions

View File

@ -19,6 +19,7 @@ package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/url"
@ -29,6 +30,10 @@ import (
"p83.nl/go/ekster/pkg/microsub"
)
var (
verbose = flag.Bool("verbose", false, "show verbose logging")
)
func init() {
log.SetFlags(log.Lshortfile | log.Ldate | log.Ltime)
}
@ -102,6 +107,8 @@ func loadEndpoints(c *client.Client, me *url.URL, filename string) error {
}
func main() {
flag.Parse()
configDir := fmt.Sprintf("%s/.config/microsub", os.Getenv("HOME"))
if len(os.Args) == 3 && os.Args[1] == "connect" {
@ -156,7 +163,9 @@ func main() {
log.Fatal(err)
}
performCommands(&c, os.Args[1:])
c.Logging = *verbose
performCommands(&c, flag.Args())
}
func performCommands(sub microsub.Microsub, commands []string) {

View File

@ -3,7 +3,9 @@ package client
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"
"strings"
@ -14,6 +16,8 @@ type Client struct {
Me *url.URL
MicrosubEndpoint *url.URL
Token string
Logging bool
}
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))
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) {
@ -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))
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) {
@ -94,11 +122,8 @@ func (c *Client) ChannelsGetList() ([]microsub.Channel, error) {
dec := json.NewDecoder(res.Body)
var channels channelsResponse
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) {