Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use a custom *http.Client #74

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading