Skip to content

Commit

Permalink
glassnode: use requestgen.BaseAPIClient
Browse files Browse the repository at this point in the history
  • Loading branch information
narumiruna committed May 5, 2022
1 parent 9c66930 commit 98a35a4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 72 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ go 1.17
require (
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/adshao/go-binance/v2 v2.3.5
github.com/c9s/requestgen v1.1.1-0.20211230171502-c042072e23cd
github.com/c9s/requestgen v1.3.0
github.com/c9s/rockhopper v1.2.1-0.20220426104534-f27cbb09846c
github.com/codingconcepts/env v0.0.0-20200821220118-a8fbf8d84482
github.com/fatih/color v1.13.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4Yn
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc h1:biVzkmvwrH8WK8raXaxBx6fRVTlJILwEwQGL1I/ByEI=
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/c9s/requestgen v1.1.1-0.20211230171502-c042072e23cd h1:o87kZ8aHtxA1ZduOV2Z55T4PX2xM4OTGH/Z9W5AjKnU=
github.com/c9s/requestgen v1.1.1-0.20211230171502-c042072e23cd/go.mod h1:5n9FU3hr5307IiXAmbMiZbHYaPiys1u9jCWYexZr9qA=
github.com/c9s/requestgen v1.3.0 h1:3cTHvWIlrc37nGEdJLIO07XaVidDeOwcew06csBz++U=
github.com/c9s/requestgen v1.3.0/go.mod h1:5n9FU3hr5307IiXAmbMiZbHYaPiys1u9jCWYexZr9qA=
github.com/c9s/rockhopper v1.2.1-0.20220426104534-f27cbb09846c h1:I3AHs+/fxnWX6eSRxzqQ/vp4jXW+ecVMGy1oy5d6fJ8=
github.com/c9s/rockhopper v1.2.1-0.20220426104534-f27cbb09846c/go.mod h1:EKObf66Cp7erWxym2de+07qNN5T1N9PXxHdh97N44EQ=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down
76 changes: 7 additions & 69 deletions pkg/datasource/glassnode/glassnodeapi/client.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package glassnodeapi

import (
"bytes"
"context"
"encoding/json"
"errors"
"net/http"
"net/url"
"time"
Expand All @@ -16,8 +13,7 @@ const defaultHTTPTimeout = time.Second * 15
const glassnodeBaseURL = "https://api.glassnode.com"

type RestClient struct {
BaseURL *url.URL
Client *http.Client
requestgen.BaseAPIClient

apiKey string
}
Expand All @@ -28,60 +24,20 @@ func NewRestClient() *RestClient {
panic(err)
}

client := &RestClient{
BaseURL: u,
Client: &http.Client{
Timeout: defaultHTTPTimeout,
return &RestClient{
BaseAPIClient: requestgen.BaseAPIClient{
BaseURL: u,
HttpClient: &http.Client{
Timeout: defaultHTTPTimeout,
},
},
}

return client
}

func (c *RestClient) Auth(apiKey string) {
c.apiKey = apiKey
}

func (c *RestClient) NewRequest(ctx context.Context, method string, refURL string, params url.Values, payload interface{}) (*http.Request, error) {
rel, err := url.Parse(refURL)
if err != nil {
return nil, err
}

if params != nil {
rel.RawQuery = params.Encode()
}

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))
}

func (c *RestClient) SendRequest(req *http.Request) (*requestgen.Response, error) {
resp, err := c.Client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

response, err := requestgen.NewResponse(resp)
if err != nil {
return response, err
}

// Check error, if there is an error, return the ErrorResponse struct type
if response.IsError() {
return response, errors.New(string(response.Body))
}

return response, nil
}

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 {
Expand All @@ -96,21 +52,3 @@ func (c *RestClient) NewAuthenticatedRequest(ctx context.Context, method, refURL

return req, nil
}

func castPayload(payload interface{}) ([]byte, error) {
if payload != nil {
switch v := payload.(type) {
case string:
return []byte(v), nil

case []byte:
return v, nil

default:
body, err := json.Marshal(v)
return body, err
}
}

return nil, nil
}

0 comments on commit 98a35a4

Please sign in to comment.