-
Notifications
You must be signed in to change notification settings - Fork 2
/
client_logger.go
50 lines (42 loc) · 1.08 KB
/
client_logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package radarr
import (
"fmt"
"net/http"
"net/http/httputil"
)
// Custom http transport to log request
type transport struct {
transport http.RoundTripper
apiKey string
verbose bool
}
func newTransport(apiKey string, verbose bool) *transport {
return &transport{
apiKey: apiKey,
transport: http.DefaultTransport,
verbose: verbose,
}
}
// Custom RoundTrip method to insert custom headers on each requests
func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("User-Agent", "SkYNewZ-Go-http-client/1.1")
req.Header.Add("Content-Type", "application/json; charset=utf-8")
req.Header.Add("X-Api-Key", t.apiKey)
if t.verbose {
data, err := httputil.DumpRequest(req, true)
if err != nil {
return nil, err
}
fmt.Printf("[DEBUG] Request: %s\n", string(data))
}
// Send request to the orignal transport
resp, err := t.transport.RoundTrip(req)
if t.verbose {
data, err := httputil.DumpResponse(resp, false)
if err != nil {
return nil, err
}
fmt.Printf("[DEBUG] Response: %s\n", string(data))
}
return resp, err
}