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 {
|
2022-05-05 03:05:27 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-05-05 03:05:27 +00:00
|
|
|
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) {
|
|
|
|
c.apiKey = apiKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RestClient) NewAuthenticatedRequest(ctx context.Context, method, refURL string, params url.Values, payload interface{}) (*http.Request, error) {
|
2022-05-04 17:39:57 +00:00
|
|
|
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)
|
2022-05-04 17:39:57 +00:00
|
|
|
|
|
|
|
return req, nil
|
2022-04-03 04:13:29 +00:00
|
|
|
}
|