glassnode: use requestgen.BaseAPIClient

This commit is contained in:
なるみ 2022-05-05 11:05:27 +08:00
parent 9c66930537
commit 98a35a485f
3 changed files with 10 additions and 72 deletions

2
go.mod
View File

@ -7,7 +7,7 @@ go 1.17
require ( require (
github.com/DATA-DOG/go-sqlmock v1.5.0 github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/adshao/go-binance/v2 v2.3.5 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/c9s/rockhopper v1.2.1-0.20220426104534-f27cbb09846c
github.com/codingconcepts/env v0.0.0-20200821220118-a8fbf8d84482 github.com/codingconcepts/env v0.0.0-20200821220118-a8fbf8d84482
github.com/fatih/color v1.13.0 github.com/fatih/color v1.13.0

4
go.sum
View File

@ -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/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 h1:biVzkmvwrH8WK8raXaxBx6fRVTlJILwEwQGL1I/ByEI=
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= 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.3.0 h1:3cTHvWIlrc37nGEdJLIO07XaVidDeOwcew06csBz++U=
github.com/c9s/requestgen v1.1.1-0.20211230171502-c042072e23cd/go.mod h1:5n9FU3hr5307IiXAmbMiZbHYaPiys1u9jCWYexZr9qA= 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 h1:I3AHs+/fxnWX6eSRxzqQ/vp4jXW+ecVMGy1oy5d6fJ8=
github.com/c9s/rockhopper v1.2.1-0.20220426104534-f27cbb09846c/go.mod h1:EKObf66Cp7erWxym2de+07qNN5T1N9PXxHdh97N44EQ= 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= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=

View File

@ -1,10 +1,7 @@
package glassnodeapi package glassnodeapi
import ( import (
"bytes"
"context" "context"
"encoding/json"
"errors"
"net/http" "net/http"
"net/url" "net/url"
"time" "time"
@ -16,8 +13,7 @@ const defaultHTTPTimeout = time.Second * 15
const glassnodeBaseURL = "https://api.glassnode.com" const glassnodeBaseURL = "https://api.glassnode.com"
type RestClient struct { type RestClient struct {
BaseURL *url.URL requestgen.BaseAPIClient
Client *http.Client
apiKey string apiKey string
} }
@ -28,60 +24,20 @@ func NewRestClient() *RestClient {
panic(err) panic(err)
} }
client := &RestClient{ return &RestClient{
BaseURL: u, BaseAPIClient: requestgen.BaseAPIClient{
Client: &http.Client{ BaseURL: u,
Timeout: defaultHTTPTimeout, HttpClient: &http.Client{
Timeout: defaultHTTPTimeout,
},
}, },
} }
return client
} }
func (c *RestClient) Auth(apiKey string) { func (c *RestClient) Auth(apiKey string) {
c.apiKey = apiKey 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) { 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) req, err := c.NewRequest(ctx, method, refURL, params, payload)
if err != nil { if err != nil {
@ -96,21 +52,3 @@ func (c *RestClient) NewAuthenticatedRequest(ctx context.Context, method, refURL
return req, nil 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
}