diff --git a/README.md b/README.md index 78c3bbf..e67ef31 100644 --- a/README.md +++ b/README.md @@ -26,11 +26,9 @@ import ( ) func main() { - // Create client - client := new(footballdata.Client) - - // Tell it to use our API token - client.SetToken("") + // Create client (optionally with auth token) + client := new(footballdata.Client). + WithToken("") // Get list of seasons... seasons, err := client.SoccerSeasons().Do() diff --git a/client.go b/client.go index 69212bb..404bac0 100644 --- a/client.go +++ b/client.go @@ -16,7 +16,8 @@ Provides a high-level client implementation to talk to the API that football-dat A new instance of Client will by default use the default HTTP client and no authentication token. To configure this, Client provides methods to set the token and the HTTP client. For more information, see the respective documentation -of SetHttpClient and SetToken. +of SetHttpClient and SetToken, or take a look at the fluent-style companion +methods WithHttpClient and WithToken. */ type Client struct { httpClient http.Client @@ -39,6 +40,15 @@ func (c *Client) SetToken(authToken string) { c.authToken = authToken } +// WithToken sets the authentication token on a copy of the current Client +// instance. +// +// This method allows for easy fluent-style usage. +func (c Client) WithToken(authToken string) *Client { + c.authToken = authToken + return &c +} + // SetHttpClient sets the client that should be used to send out requests. // Calling this method is *optional*. func (c *Client) SetHttpClient(client *http.Client) { @@ -48,6 +58,18 @@ func (c *Client) SetHttpClient(client *http.Client) { c.httpClient = *client } +// WithHttpClient sets the client that should be used to send out requests on +// a copy of the current Client instance. +// +// This method allows for easy fluent-style usage. +func (c Client) WithHttpClient(client *http.Client) *Client { + if client == nil { + panic("client must not be nil") + } + c.httpClient = *client + return &c +} + func (c *Client) req(path string, pathValues ...interface{}) request { return request{c, fmt.Sprintf(path, pathValues...), url.Values{}} } diff --git a/example_test.go b/example_test.go index 892f58e..d3a7103 100644 --- a/example_test.go +++ b/example_test.go @@ -8,11 +8,9 @@ import ( ) func Example() { - // Create client and tell it to use our API token - client := new(footballdata.Client) - - // Tell it to use our API token (optional) - client.SetToken("") + // Create client (optionally with auth token) + client := new(footballdata.Client). + WithToken("") // Get list of seasons... seasons, err := client.SoccerSeasons().Do() @@ -27,8 +25,9 @@ func Example() { } func ExampleClient() { - // Create client - client := footballdata.NewClient(http.DefaultClient) + // Create client (optionally with auth token) + client := new(footballdata.Client). + WithToken("") // Tell it to use our API token (optional) client.SetToken("") @@ -57,3 +56,16 @@ func ExampleClient_setTokenAndHttpClient() { DisableCompression: true, }}) } + +func ExampleClient_withCustomConfiguration() { + // Create client with custom token and wrapping a custom HTTP client + client := new(footballdata.Client). + WithToken(""). + WithHttpClient(&http.Client{ + Transport: &http.Transport{ + DisableCompression: true, + }}) + + // Do something with the client instance here... + _ = client +}