Skip to content

Commit

Permalink
Allow to use a custom *http.Client
Browse files Browse the repository at this point in the history
  • Loading branch information
jybp committed Dec 9, 2023
1 parent 7403840 commit 8d40040
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 26 deletions.
19 changes: 10 additions & 9 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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")
Expand All @@ -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,
}
Expand Down
16 changes: 10 additions & 6 deletions ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const (
type ipfsClient struct {
server string
projectId string
client *retryablehttp.Client
client *http.Client
routines int
}

Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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,
}
Expand Down
13 changes: 2 additions & 11 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit 8d40040

Please sign in to comment.