mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 08:45:16 +00:00
glassnode: simplify NewAuthenticatedRequest
This commit is contained in:
parent
463c6ee01f
commit
9c66930537
|
@ -43,11 +43,6 @@ func (c *RestClient) Auth(apiKey string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *RestClient) NewRequest(ctx context.Context, method string, refURL string, params url.Values, payload interface{}) (*http.Request, error) {
|
func (c *RestClient) NewRequest(ctx context.Context, method string, refURL string, params url.Values, payload interface{}) (*http.Request, error) {
|
||||||
body, err := castPayload(payload)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
rel, err := url.Parse(refURL)
|
rel, err := url.Parse(refURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -58,6 +53,12 @@ func (c *RestClient) NewRequest(ctx context.Context, method string, refURL strin
|
||||||
}
|
}
|
||||||
|
|
||||||
pathURL := c.BaseURL.ResolveReference(rel)
|
pathURL := c.BaseURL.ResolveReference(rel)
|
||||||
|
|
||||||
|
body, err := castPayload(payload)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return http.NewRequestWithContext(ctx, method, pathURL.String(), bytes.NewReader(body))
|
return http.NewRequestWithContext(ctx, method, pathURL.String(), bytes.NewReader(body))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,28 +83,7 @@ func (c *RestClient) SendRequest(req *http.Request) (*requestgen.Response, error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *RestClient) NewAuthenticatedRequest(ctx context.Context, method, refURL string, params url.Values, payload interface{}) (*http.Request, error) {
|
func (c *RestClient) NewAuthenticatedRequest(ctx context.Context, method, refURL string, params url.Values, payload interface{}) (*http.Request, error) {
|
||||||
rel, err := url.Parse(refURL)
|
req, err := c.NewRequest(ctx, method, refURL, params, payload)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if params != nil {
|
|
||||||
rel.RawQuery = params.Encode()
|
|
||||||
}
|
|
||||||
|
|
||||||
pathURL := c.BaseURL.ResolveReference(rel)
|
|
||||||
|
|
||||||
path := pathURL.Path
|
|
||||||
if rel.RawQuery != "" {
|
|
||||||
path += "?" + rel.RawQuery
|
|
||||||
}
|
|
||||||
|
|
||||||
body, err := castPayload(payload)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
req, err := http.NewRequestWithContext(ctx, method, pathURL.String(), bytes.NewReader(body))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -111,14 +91,10 @@ func (c *RestClient) NewAuthenticatedRequest(ctx context.Context, method, refURL
|
||||||
req.Header.Add("Content-Type", "application/json")
|
req.Header.Add("Content-Type", "application/json")
|
||||||
req.Header.Add("Accept", "application/json")
|
req.Header.Add("Accept", "application/json")
|
||||||
|
|
||||||
// Build authentication headers
|
|
||||||
c.attachAuthHeaders(req, method, path, body)
|
|
||||||
return req, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *RestClient) attachAuthHeaders(req *http.Request, method string, path string, body []byte) {
|
|
||||||
// Attch API Key to header. https://docs.glassnode.com/basic-api/api-key#usage
|
// Attch API Key to header. https://docs.glassnode.com/basic-api/api-key#usage
|
||||||
req.Header.Add("X-Api-Key", c.apiKey)
|
req.Header.Add("X-Api-Key", c.apiKey)
|
||||||
|
|
||||||
|
return req, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func castPayload(payload interface{}) ([]byte, error) {
|
func castPayload(payload interface{}) ([]byte, error) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user