From 98a35a485f96ca00c3cb6f674ca194995ee2d091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=AA=E3=82=8B=E3=81=BF?= Date: Thu, 5 May 2022 11:05:27 +0800 Subject: [PATCH] glassnode: use requestgen.BaseAPIClient --- go.mod | 2 +- go.sum | 4 +- .../glassnode/glassnodeapi/client.go | 76 ++----------------- 3 files changed, 10 insertions(+), 72 deletions(-) diff --git a/go.mod b/go.mod index 8223e8961..564adf731 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 7741a9028..8089b4465 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/datasource/glassnode/glassnodeapi/client.go b/pkg/datasource/glassnode/glassnodeapi/client.go index 3a26b29ec..c1631a015 100644 --- a/pkg/datasource/glassnode/glassnodeapi/client.go +++ b/pkg/datasource/glassnode/glassnodeapi/client.go @@ -1,10 +1,7 @@ package glassnodeapi import ( - "bytes" "context" - "encoding/json" - "errors" "net/http" "net/url" "time" @@ -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 } @@ -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 { @@ -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 -}