-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
47 lines (39 loc) · 951 Bytes
/
client.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
package client
import (
"net/http"
"time"
"github.com/newrelic/go-agent/v3/newrelic"
)
// Client contains functions for youtube api client.
type Client struct {
host string
http *http.Client
maxAge time.Time
}
// New to create new youtube client.
func New(key string, maxAge int) *Client {
return &Client{
host: "https://www.googleapis.com/youtube/v3",
http: &http.Client{
Timeout: 10 * time.Second,
Transport: newrelic.NewRoundTripper(&transportWithKey{
key: key,
}),
},
maxAge: time.Now().Add(time.Duration(maxAge*-24) * time.Hour),
}
}
type transportWithKey struct {
transport http.RoundTripper
key string
}
// RoundTrip is http roundtrip.
func (t *transportWithKey) RoundTrip(req *http.Request) (*http.Response, error) {
if t.transport == nil {
t.transport = http.DefaultTransport
}
q := req.URL.Query()
q.Add("key", t.key)
req.URL.RawQuery = q.Encode()
return t.transport.RoundTrip(req)
}