bbgo_origin/pkg/datasource/glassnode/glassnodeapi/client.go

56 lines
1.1 KiB
Go
Raw Permalink Normal View History

2022-04-25 08:44:57 +00:00
package glassnodeapi
2022-04-03 04:13:29 +00:00
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
2022-04-03 04:13:29 +00:00
apiKey string
}
2022-04-25 08:44:57 +00:00
func NewRestClient() *RestClient {
2022-04-03 04:13:29 +00:00
u, err := url.Parse(glassnodeBaseURL)
if err != nil {
panic(err)
}
return &RestClient{
BaseAPIClient: requestgen.BaseAPIClient{
BaseURL: u,
HttpClient: &http.Client{
Timeout: defaultHTTPTimeout,
},
2022-04-03 04:13:29 +00:00
},
}
}
func (c *RestClient) Auth(apiKey string) {
2022-06-17 07:04:23 +00:00
// pragma: allowlist nextline secret
2022-04-03 04:13:29 +00:00
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)
2022-04-03 04:13:29 +00:00
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
2022-04-03 04:13:29 +00:00
}