This repository has been archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #349 from jabielecki/with_header
add WithHeader and convert NewClient to use options
- Loading branch information
Showing
4 changed files
with
115 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package packngo | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
// ClientOpt is an option usable as an argument to NewClient constructor. | ||
type ClientOpt func(*Client) error | ||
|
||
// WithAuth configures Client with a specific consumerToken and apiKey for subsequent HTTP requests. | ||
func WithAuth(consumerToken string, apiKey string) ClientOpt { | ||
return func(c *Client) error { | ||
c.ConsumerToken = consumerToken | ||
c.APIKey = apiKey | ||
c.apiKeySet = true | ||
|
||
return nil | ||
} | ||
} | ||
|
||
// WithHTTPClient configures Client to use a specific httpClient for subsequent HTTP requests. | ||
func WithHTTPClient(httpClient *http.Client) ClientOpt { | ||
return func(c *Client) error { | ||
c.client = httpClient | ||
|
||
return nil | ||
} | ||
} | ||
|
||
// WithBaseURL configures Client to use a nonstandard API URL, e.g. for mocking the remote API. | ||
func WithBaseURL(apiBaseURL string) ClientOpt { | ||
return func(c *Client) error { | ||
u, err := url.Parse(apiBaseURL) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
c.BaseURL = u | ||
|
||
return nil | ||
} | ||
} | ||
|
||
// WithHeader configures Client to use the given HTTP header set. | ||
// The headers X-Auth-Token, X-Consumer-Token, User-Agent will be ignored even if provided in the set. | ||
func WithHeader(header http.Header) ClientOpt { | ||
return func(c *Client) error { | ||
for k, v := range header { | ||
c.header[k] = v | ||
} | ||
|
||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters