Provide fluent-style configuration methods as alternatives to the Set methods.

- Allows for quick and easy one-liners and also allows for temporarily changing configuration for a few requests.
- Update example codes to promote usage of fluent-style configuration.
api/rm-interface
Icedream 2016-06-26 13:04:04 +02:00
parent 850e9e7bbf
commit cb21cdb829
3 changed files with 45 additions and 13 deletions

View File

@ -26,11 +26,9 @@ import (
)
func main() {
// Create client
client := new(footballdata.Client)
// Tell it to use our API token
client.SetToken("<insert your api token here>")
// Create client (optionally with auth token)
client := new(footballdata.Client).
WithToken("<insert your api token here>")
// Get list of seasons...
seasons, err := client.SoccerSeasons().Do()

View File

@ -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{}}
}

View File

@ -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("<insert your api token here>")
// Create client (optionally with auth token)
client := new(footballdata.Client).
WithToken("<insert your api token here>")
// 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("<insert your api token here>")
// Tell it to use our API token (optional)
client.SetToken("<insert your api token here>")
@ -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("<insert your api token here>").
WithHttpClient(&http.Client{
Transport: &http.Transport{
DisableCompression: true,
}})
// Do something with the client instance here...
_ = client
}