From e6526da3e09170f8618d91595cc0018481f8d846 Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Sun, 26 Jun 2016 12:57:14 +0200 Subject: [PATCH] Remove intermediate interface and provide Set methods for HttpClient and Token. - Make the "Client" struct construction-friendly so it can be easily initialized with "new(footballdata.Client)". - Rewrite example_test.go to show the respective new usage. Make it clear here that the SetToken call is optional. This would fix #2. --- api_methods.go | 2 +- client.go | 58 +++++++++++++++------------------ example_test.go | 24 ++++++++++---- req_fixture.go | 2 +- req_fixtures.go | 2 +- req_soccerseason.go | 2 +- req_soccerseason_fixtures.go | 2 +- req_soccerseason_leaguetable.go | 2 +- req_soccerseason_teams.go | 2 +- req_soccerseasons.go | 2 +- req_team.go | 2 +- req_team_fixtures.go | 2 +- req_team_players.go | 2 +- 13 files changed, 54 insertions(+), 50 deletions(-) diff --git a/api_methods.go b/api_methods.go index 8e61b90..b984b0a 100644 --- a/api_methods.go +++ b/api_methods.go @@ -6,7 +6,7 @@ import ( ) type request struct { - fdClient *client + fdClient *Client path string urlValues url.Values } diff --git a/client.go b/client.go index c801491..69212bb 100644 --- a/client.go +++ b/client.go @@ -10,56 +10,50 @@ import ( "net/url" ) -/* -Provides a high-level client to talk to the API that football-data.org offers. - -To create an instance please use NewClient(h). -*/ -type Client interface { - Fixture(id uint64) FixtureRequest - Fixtures() FixturesRequest - SoccerSeason(id uint64) SoccerSeasonRequest - FixturesOfSoccerSeason(soccerSeasonId uint64) SoccerSeasonFixturesRequest - LeagueTableOfSoccerSeason(soccerSeasonId uint64) SoccerSeasonLeagueTableRequest - TeamsOfSoccerSeason(soccerSeasonId uint64) SoccerSeasonTeamsRequest - SoccerSeasons() SoccerSeasonsRequest - Team(id uint64) TeamRequest - FixturesOfTeam(id uint64) TeamFixturesRequest - - SetToken(authToken string) -} - /* Provides a high-level client implementation to talk to the API that football-data.org offers. -To create an instance please use NewClient(h). +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. */ -type client struct { - httpClient *http.Client +type Client struct { + httpClient http.Client // Insert an API token here if you have one. It will be sent across with all requests. - AuthToken string + authToken string } // NewClient creates a new Client instance that wraps around the given HTTP client. // -// Call SetToken to add your token. -func NewClient(h *http.Client) Client { - return &client{httpClient: h} +// A call to this method is not necessary in order to create a working instance +// of Client. `new(footballdata.Client)` works just as fine. +func NewClient(h *http.Client) *Client { + return &Client{httpClient: *h} } // SetToken sets the authentication token. // Calling this method is *optional*. -func (c *client) SetToken(authToken string) { - c.AuthToken = authToken +func (c *Client) SetToken(authToken string) { + c.authToken = authToken } -func (c *client) req(path string, pathValues ...interface{}) request { +// SetHttpClient sets the client that should be used to send out requests. +// Calling this method is *optional*. +func (c *Client) SetHttpClient(client *http.Client) { + if client == nil { + panic("client must not be nil") + } + c.httpClient = *client +} + +func (c *Client) req(path string, pathValues ...interface{}) request { return request{c, fmt.Sprintf(path, pathValues...), url.Values{}} } // Executes an HTTP request with given parameters and on success returns the response wrapped in a JSON decoder. -func (c *client) doJson(method string, path string, values url.Values) (j *json.Decoder, meta ResponseMeta, err error) { +func (c *Client) doJson(method string, path string, values url.Values) (j *json.Decoder, meta ResponseMeta, err error) { // Create request req := &http.Request{ Method: method, @@ -68,8 +62,8 @@ func (c *client) doJson(method string, path string, values url.Values) (j *json. } // Set request headers - if len(c.AuthToken) > 0 { - req.Header.Set("X-Auth-Token", c.AuthToken) + if len(c.authToken) > 0 { + req.Header.Set("X-Auth-Token", c.authToken) } req.Header.Set("X-Response-Control", "minified") req.Header.Set("User-Agent", "go-footballdata/0.0") diff --git a/example_test.go b/example_test.go index b6202dd..892f58e 100644 --- a/example_test.go +++ b/example_test.go @@ -8,10 +8,10 @@ import ( ) func Example() { - // Create client - client := footballdata.NewClient(http.DefaultClient) + // Create client and tell it to use our API token + client := new(footballdata.Client) - // Tell it to use our API token + // Tell it to use our API token (optional) client.SetToken("") // Get list of seasons... @@ -30,7 +30,11 @@ func ExampleClient() { // Create client client := footballdata.NewClient(http.DefaultClient) - /* Do something with the client instance... */ + // Tell it to use our API token (optional) + client.SetToken("") + + // Do something with the client instance... + // Here we just fetch the listed soccer seasons on the API seasons, err := client.SoccerSeasons().Do() if err != nil { panic(err) @@ -40,10 +44,16 @@ func ExampleClient() { } } -func ExampleClient_withToken() { +func ExampleClient_setTokenAndHttpClient() { // Create client - client := footballdata.NewClient(http.DefaultClient) + client := new(footballdata.Client) - // Tell it to use our API token + // If you have an API token, you can tell the Client instance to use it client.SetToken("") + + // The Client instance also allows you to use your own HTTP client configuration + client.SetHttpClient(&http.Client{ + Transport: &http.Transport{ + DisableCompression: true, + }}) } diff --git a/req_fixture.go b/req_fixture.go index 3c43651..3458737 100644 --- a/req_fixture.go +++ b/req_fixture.go @@ -22,6 +22,6 @@ func (r FixtureRequest) Do() (s Fixture, err error) { } // Fixture prepares a request to fetch the fixtures of a soccer season. -func (c *client) Fixture(id uint64) FixtureRequest { +func (c *Client) Fixture(id uint64) FixtureRequest { return FixtureRequest{c.req("fixture/%d", id)} } diff --git a/req_fixtures.go b/req_fixtures.go index e0f0c6e..b51af64 100644 --- a/req_fixtures.go +++ b/req_fixtures.go @@ -31,6 +31,6 @@ func (r FixturesRequest) Do() (s FixturesResponse, err error) { } // Fixtures prepares a request to fetch the fixtures of a soccer season. -func (c *client) Fixtures() FixturesRequest { +func (c *Client) Fixtures() FixturesRequest { return FixturesRequest{c.req("fixtures")} } diff --git a/req_soccerseason.go b/req_soccerseason.go index fccaad9..1bfa417 100644 --- a/req_soccerseason.go +++ b/req_soccerseason.go @@ -14,6 +14,6 @@ func (r SoccerSeasonRequest) Do() (s SoccerSeason, err error) { } // SoccerSeason prepares a request to fetch the complete list of soccer seasons. -func (c *client) SoccerSeason(id uint64) SoccerSeasonRequest { +func (c *Client) SoccerSeason(id uint64) SoccerSeasonRequest { return SoccerSeasonRequest{c.req("soccerseasons/%d", id)} } diff --git a/req_soccerseason_fixtures.go b/req_soccerseason_fixtures.go index c026a1d..7f95b47 100644 --- a/req_soccerseason_fixtures.go +++ b/req_soccerseason_fixtures.go @@ -31,6 +31,6 @@ func (r SoccerSeasonFixturesRequest) Do() (s FixtureList, err error) { } // FixturesOfSoccerSeason prepares a request to fetch the fixtures of a soccer season. -func (c *client) FixturesOfSoccerSeason(soccerSeasonId uint64) SoccerSeasonFixturesRequest { +func (c *Client) FixturesOfSoccerSeason(soccerSeasonId uint64) SoccerSeasonFixturesRequest { return SoccerSeasonFixturesRequest{c.req("soccerseasons/%d/fixtures", soccerSeasonId)} } diff --git a/req_soccerseason_leaguetable.go b/req_soccerseason_leaguetable.go index 737653b..7fd6cbf 100644 --- a/req_soccerseason_leaguetable.go +++ b/req_soccerseason_leaguetable.go @@ -22,6 +22,6 @@ func (r SoccerSeasonLeagueTableRequest) Do() (s SoccerSeason, err error) { } // LeagueTableOfSoccerSeason prepares a new request to fetch the league table of a given soccer season. -func (c *client) LeagueTableOfSoccerSeason(soccerSeasonId uint64) SoccerSeasonLeagueTableRequest { +func (c *Client) LeagueTableOfSoccerSeason(soccerSeasonId uint64) SoccerSeasonLeagueTableRequest { return SoccerSeasonLeagueTableRequest{c.req("soccerseasons/%d/leagueTable", soccerSeasonId)} } diff --git a/req_soccerseason_teams.go b/req_soccerseason_teams.go index e60a479..1ec633c 100644 --- a/req_soccerseason_teams.go +++ b/req_soccerseason_teams.go @@ -14,6 +14,6 @@ func (r SoccerSeasonTeamsRequest) Do() (s TeamList, err error) { } // TeamsOfSoccerSeason prepares a new request to fetch the league table of a given soccer season. -func (c *client) TeamsOfSoccerSeason(soccerSeasonId uint64) SoccerSeasonTeamsRequest { +func (c *Client) TeamsOfSoccerSeason(soccerSeasonId uint64) SoccerSeasonTeamsRequest { return SoccerSeasonTeamsRequest{c.req("soccerseasons/%d/leagueTable", soccerSeasonId)} } diff --git a/req_soccerseasons.go b/req_soccerseasons.go index 6b1408e..88d0fe7 100644 --- a/req_soccerseasons.go +++ b/req_soccerseasons.go @@ -22,6 +22,6 @@ func (r SoccerSeasonsRequest) Do() (s SoccerSeasonList, err error) { } // SoccerSeasons prepares a request to fetch the complete list of soccer seasons. -func (c *client) SoccerSeasons() SoccerSeasonsRequest { +func (c *Client) SoccerSeasons() SoccerSeasonsRequest { return SoccerSeasonsRequest{c.req("soccerseasons")} } diff --git a/req_team.go b/req_team.go index c961502..e7aed1a 100644 --- a/req_team.go +++ b/req_team.go @@ -21,6 +21,6 @@ func (r TeamRequest) Do() (s Team, err error) { } // Team prepares a request to fetch a team's information. -func (c *client) Team(id uint64) TeamRequest { +func (c *Client) Team(id uint64) TeamRequest { return TeamRequest{c.req("teams/%d", id), id} } diff --git a/req_team_fixtures.go b/req_team_fixtures.go index d63e03e..11c3642 100644 --- a/req_team_fixtures.go +++ b/req_team_fixtures.go @@ -37,6 +37,6 @@ func (r TeamFixturesRequest) Do() (s FixturesResponse, err error) { } // FixturesOfTeam prepares a request to fetch the fixtures of a soccer season. -func (c *client) FixturesOfTeam(id uint64) TeamFixturesRequest { +func (c *Client) FixturesOfTeam(id uint64) TeamFixturesRequest { return TeamFixturesRequest{c.req("teams/%d/fixtures", id)} } diff --git a/req_team_players.go b/req_team_players.go index 099de3b..7d6089e 100644 --- a/req_team_players.go +++ b/req_team_players.go @@ -14,6 +14,6 @@ func (r TeamPlayersRequest) Do() (s PlayerList, err error) { } // PlayersOfTeam prepares a request to fetch a team's players. -func (c *client) PlayersOfTeam(id uint64) TeamPlayersRequest { +func (c *Client) PlayersOfTeam(id uint64) TeamPlayersRequest { return TeamPlayersRequest{c.req("teams/%d/players", id)} }