-
-
Notifications
You must be signed in to change notification settings - Fork 296
/
client.go
55 lines (43 loc) · 1.11 KB
/
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
48
49
50
51
52
53
54
55
package glassnodeapi
import (
"context"
"net/http"
"net/url"
"time"
"github.com/c9s/requestgen"
)
const defaultHTTPTimeout = time.Second * 15
const glassnodeBaseURL = "https://api.glassnode.com"
type RestClient struct {
requestgen.BaseAPIClient
apiKey string
}
func NewRestClient() *RestClient {
u, err := url.Parse(glassnodeBaseURL)
if err != nil {
panic(err)
}
return &RestClient{
BaseAPIClient: requestgen.BaseAPIClient{
BaseURL: u,
HttpClient: &http.Client{
Timeout: defaultHTTPTimeout,
},
},
}
}
func (c *RestClient) Auth(apiKey string) {
// pragma: allowlist nextline secret
c.apiKey = apiKey
}
func (c *RestClient) NewAuthenticatedRequest(ctx context.Context, method, refURL string, params url.Values, payload interface{}) (*http.Request, error) {
req, err := c.NewRequest(ctx, method, refURL, params, payload)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
// Attch API Key to header. https://docs.glassnode.com/basic-api/api-key#usage
req.Header.Add("X-Api-Key", c.apiKey)
return req, nil
}