diff --git a/client.go b/client.go index 03c314a..eb862b2 100644 --- a/client.go +++ b/client.go @@ -11,15 +11,10 @@ import ( type apiClient struct { server string projectId string - client *retryablehttp.Client + client *http.Client routines int } -// HttpRequestDoer defines methods for a http client. -type HttpRequestDoer interface { - Do(req *http.Request) (*http.Response, error) -} - // APIClientOptions contains optios used to initialize an API Client using // NewAPIClient type APIClientOptions struct { @@ -32,6 +27,9 @@ type APIClientOptions struct { // Max number of routines to use for *All methods MaxRoutines int + + // Underlying http client to use. If not set, default github.com/hashicorp/go-retryablehttp is used. + Client *http.Client } // NewAPICLient creates a client from APIClientOptions. If no options are provided, @@ -41,8 +39,11 @@ func NewAPIClient(options APIClientOptions) APIClient { options.Server = CardanoMainNet } - retryclient := retryablehttp.NewClient() - retryclient.Logger = nil + if options.Client == nil { + retryclient := retryablehttp.NewClient() + retryclient.Logger = nil + options.Client = retryclient.StandardClient() + } if options.ProjectID == "" { options.ProjectID = os.Getenv("BLOCKFROST_PROJECT_ID") @@ -54,7 +55,7 @@ func NewAPIClient(options APIClientOptions) APIClient { client := &apiClient{ server: options.Server, - client: retryclient, + client: options.Client, projectId: options.ProjectID, routines: options.MaxRoutines, } diff --git a/ipfs.go b/ipfs.go index 76709e1..459270a 100644 --- a/ipfs.go +++ b/ipfs.go @@ -28,7 +28,7 @@ const ( type ipfsClient struct { server string projectId string - client *retryablehttp.Client + client *http.Client routines int } @@ -38,10 +38,10 @@ type IPFSClientOptions struct { ProjectID string // Configures server to use. Can be toggled for test servers Server string - // Interface implementing Do method such *http.Client - Client *retryablehttp.Client // Max goroutines to use for *All Methods MaxRoutines int + // Underlying http client to use. If not set, default github.com/hashicorp/go-retryablehttp is used. + Client *http.Client } // IPFSObject contains information on an IPFS object @@ -86,8 +86,12 @@ func NewIPFSClient(options IPFSClientOptions) IPFSClient { if options.Server == "" { options.Server = IPFSNet } - retryclient := retryablehttp.NewClient() - retryclient.Logger = nil + + if options.Client == nil { + retryclient := retryablehttp.NewClient() + retryclient.Logger = nil + options.Client = retryclient.StandardClient() + } if options.ProjectID == "" { options.ProjectID = os.Getenv("BLOCKFROST_IPFS_PROJECT_ID") @@ -99,7 +103,7 @@ func NewIPFSClient(options IPFSClientOptions) IPFSClient { client := &ipfsClient{ server: options.Server, - client: retryclient, + client: options.Client, projectId: options.ProjectID, routines: options.MaxRoutines, } diff --git a/util.go b/util.go index 78b02b3..71b128a 100644 --- a/util.go +++ b/util.go @@ -8,7 +8,6 @@ import ( "net/url" "github.com/blockfrost/blockfrost-go/internal/version" - "github.com/hashicorp/go-retryablehttp" ) func handleAPIErrorResponse(res *http.Response) error { @@ -99,11 +98,7 @@ func (c *apiClient) handleRequest(req *http.Request) (res *http.Response, err er userAgent := fmt.Sprintf("%s/%s", "blockfrost-go", version.String()) req.Header.Set("User-Agent", userAgent) - rreq, err := retryablehttp.FromRequest(req) - if err != nil { - return - } - res, err = c.client.Do(rreq) + res, err = c.client.Do(req) if err != nil { return } @@ -119,11 +114,7 @@ func (ip *ipfsClient) handleRequest(req *http.Request) (res *http.Response, err req.Header.Add("project_id", ip.projectId) userAgent := fmt.Sprintf("%s/%s", "blockfrost-go", version.String()) req.Header.Set("User-Agent", userAgent) - rreq, err := retryablehttp.FromRequest(req) - if err != nil { - return - } - res, err = ip.client.Do(rreq) + res, err = ip.client.Do(req) if err != nil { return }