Skip to content

Commit

Permalink
Pass separate client objects for awsSigner and default http
Browse files Browse the repository at this point in the history
  • Loading branch information
RMPall committed Feb 16, 2022
1 parent cfd4908 commit 6a57d38
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
28 changes: 11 additions & 17 deletions http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,6 @@ var DefaultTransport = &http.Transport{
IdleConnTimeout: 30 * time.Second,
}

// DefaultClient is a dp-net specific http client with sensible timeouts,
// exponential backoff, and a contextual dialer.
var DefaultClient = &Client{
MaxRetries: 3,
RetryTime: 20 * time.Millisecond,

HTTPClient: &http.Client{
Timeout: 10 * time.Second,
Transport: DefaultTransport,
},
}

// Clienter provides an interface for methods on an HTTP Client.
type Clienter interface {
SetTimeout(timeout time.Duration)
Expand All @@ -68,7 +56,7 @@ type Clienter interface {

// NewClient returns a copy of DefaultClient.
func NewClient() Clienter {
newClient := &Client{
return &Client{
MaxRetries: 3,
RetryTime: 20 * time.Millisecond,

Expand All @@ -77,18 +65,25 @@ func NewClient() Clienter {
Transport: DefaultTransport,
},
}
return newClient
}

// NewClientWithAwsSigner return a new client with aws signer profile.
func NewClientWithAwsSigner(awsFilename, awsProfile, awsRegion, awsService string) (Clienter, error) {
newClient := *DefaultClient
newClient := &Client{
MaxRetries: 3,
RetryTime: 20 * time.Millisecond,

HTTPClient: &http.Client{
Timeout: 10 * time.Second,
Transport: DefaultTransport,
},
}
awsRoundTripper, err := NewAWSSignerRoundTripper(awsFilename, awsProfile, awsRegion, awsService, DefaultTransport)
if err != nil {
return nil, err
}
newClient.HTTPClient.Transport = awsRoundTripper
return &newClient, nil
return newClient, nil
}

// ClientWithTimeout facilitates creating a client and setting request timeout.
Expand All @@ -102,7 +97,6 @@ func ClientWithTimeout(c Clienter, timeout time.Duration) Clienter {

// Clienter roundtripper calls the httpclient roundtripper.
func (c *Client) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("test", "001")
return c.HTTPClient.Transport.RoundTrip(req)
}

Expand Down
2 changes: 0 additions & 2 deletions http/custom_roundtripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ type AwsSignerRoundTripper struct {
}

func NewAWSSignerRoundTripper(awsFilename, awsProfile, awsRegion, awsService string, customTransport http.RoundTripper) (*AwsSignerRoundTripper, error) {
fmt.Println("inside aws signer..........................")
var roundTripper http.RoundTripper
if awsRegion == "" || awsService == "" {
return nil, fmt.Errorf("aws region and service should be valid options")
Expand All @@ -38,7 +37,6 @@ func NewAWSSignerRoundTripper(awsFilename, awsProfile, awsRegion, awsService str
}

func (srt *AwsSignerRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
fmt.Println("inside round tripper..........................")
var body []byte
var err error
if req.Body != nil {
Expand Down

0 comments on commit 6a57d38

Please sign in to comment.