From cb16d45ecb5cff1c52fe24936d130ef482fa4092 Mon Sep 17 00:00:00 2001 From: Christian Date: Sat, 25 Jun 2016 12:15:58 +0200 Subject: [PATCH 01/25] Better abstraction & https://golang.org/doc/effective_go.html#commentary --- api_methods.go | 8 +++--- client.go | 46 ++++++++++++++++++++++++++------- req_fixture.go | 10 +++---- req_fixtures.go | 14 +++++----- req_soccerseason.go | 6 ++--- req_soccerseason_fixtures.go | 14 +++++----- req_soccerseason_leaguetable.go | 10 +++---- req_soccerseason_teams.go | 6 ++--- req_soccerseasons.go | 10 +++---- req_team.go | 6 ++--- req_team_fixtures.go | 18 ++++++------- req_team_players.go | 6 ++--- 12 files changed, 91 insertions(+), 63 deletions(-) diff --git a/api_methods.go b/api_methods.go index f10ab2a..8e61b90 100644 --- a/api_methods.go +++ b/api_methods.go @@ -6,11 +6,11 @@ import ( ) type request struct { - c *Client - p string - v url.Values + fdClient *client + path string + urlValues url.Values } func (r request) doJson(method string) (*json.Decoder, ResponseMeta, error) { - return r.c.doJson(method, r.p, r.v) + return r.fdClient.doJson(method, r.path, r.urlValues) } diff --git a/client.go b/client.go index b6eb582..d19c415 100644 --- a/client.go +++ b/client.go @@ -15,24 +15,52 @@ 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 struct { - h *http.Client +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). +*/ +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 } -// Creates a new Client instance that wraps around the given HTTP client. -func NewClient(h *http.Client) *Client { - return &Client{h: h} +// 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} } -func (c *Client) req(path string, pathValues ...interface{}) request { +// SetToken Set the authentication token +// Calling this method is *optional* +func (c* client) SetToken(authToken string) { + c.AuthToken=authToken +} + + +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, @@ -48,7 +76,7 @@ func (c *Client) doJson(method string, path string, values url.Values) (j *json. req.Header.Set("User-Agent", "go-footballdata/0.0") // Execute request - resp, err := c.h.Do(req) + resp, err := c.httpClient.Do(req) if err != nil { return } diff --git a/req_fixture.go b/req_fixture.go index 897f491..8d456e6 100644 --- a/req_fixture.go +++ b/req_fixture.go @@ -4,13 +4,13 @@ import "fmt" type FixtureRequest struct{ request } -// Modifies the request to specify the number of former games to be analyzed (normally 10). +// Head2Head Modifies the request to specify the number of former games to be analyzed (normally 10). func (r FixtureRequest) Head2Head(num uint16) FixtureRequest { - r.v.Set("head2head", fmt.Sprintf("%d", num)) + r.urlValues.Set("head2head", fmt.Sprintf("%d", num)) return r } -// Executes the request. +// Do Executes the request. func (r FixtureRequest) Do() (s Fixture, err error) { d, _, err := r.doJson("GET") if err != nil { @@ -21,7 +21,7 @@ func (r FixtureRequest) Do() (s Fixture, err error) { return } -// Prepares a request to fetch the fixtures of a soccer season. -func (c *Client) Fixture(id uint64) FixtureRequest { +// Fixture Prepares a request to fetch the fixtures of a soccer season. +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 8ba685a..dac81dc 100644 --- a/req_fixtures.go +++ b/req_fixtures.go @@ -7,19 +7,19 @@ import ( type FixturesRequest struct{ request } -// Modifies the request to specify a specific time frame. +// TimeFrame Modifies the request to specify a specific time frame. func (r FixturesRequest) TimeFrame(timeframe time.Duration) FixturesRequest { - r.v.Set("timeFrame", durationToTimeFrame(timeframe)) + r.urlValues.Set("timeFrame", durationToTimeFrame(timeframe)) return r } -// Modifies the request to specify a list of leagues by their code. +// League Modifies the request to specify a list of leagues by their code. func (r FixturesRequest) League(leagueCodes ...string) FixturesRequest { - r.v.Set("league", strings.Join(leagueCodes, ",")) + r.urlValues.Set("league", strings.Join(leagueCodes, ",")) return r } -// Executes the request. +// Do Executes the request. func (r FixturesRequest) Do() (s FixturesResponse, err error) { d, _, err := r.doJson("GET") if err != nil { @@ -30,7 +30,7 @@ func (r FixturesRequest) Do() (s FixturesResponse, err error) { return } -// Prepares a request to fetch the fixtures of a soccer season. -func (c *Client) Fixtures() FixturesRequest { +// Fixtures Prepares a request to fetch the fixtures of a soccer season. +func (c *client) Fixtures() FixturesRequest { return FixturesRequest{c.req("fixtures")} } diff --git a/req_soccerseason.go b/req_soccerseason.go index 85008ff..3fa03df 100644 --- a/req_soccerseason.go +++ b/req_soccerseason.go @@ -2,7 +2,7 @@ package footballdata type SoccerSeasonRequest struct{ request } -// Executes the request. +// Do Executes the request. func (r SoccerSeasonRequest) Do() (s SoccerSeason, err error) { d, _, err := r.doJson("GET") if err != nil { @@ -13,7 +13,7 @@ func (r SoccerSeasonRequest) Do() (s SoccerSeason, err error) { return } -// Prepares a request to fetch the complete list of soccer seasons. -func (c *Client) SoccerSeason(id uint64) SoccerSeasonRequest { +// SoccerSeason Prepares a request to fetch the complete list of soccer seasons. +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 b4d0f93..74010d7 100644 --- a/req_soccerseason_fixtures.go +++ b/req_soccerseason_fixtures.go @@ -7,19 +7,19 @@ import ( type SoccerSeasonFixturesRequest struct{ request } -// Modifies the request to specify a match day. +// Matchday Modifies the request to specify a match day. func (r SoccerSeasonFixturesRequest) Matchday(matchday uint16) SoccerSeasonFixturesRequest { - r.v.Set("matchday", fmt.Sprintf("%d", matchday)) + r.urlValues.Set("matchday", fmt.Sprintf("%d", matchday)) return r } -// Modifies the request to specify a specific time frame. +// TimeFrame Modifies the request to specify a specific time frame. func (r SoccerSeasonFixturesRequest) TimeFrame(timeframe time.Duration) SoccerSeasonFixturesRequest { - r.v.Set("timeFrame", durationToTimeFrame(timeframe)) + r.urlValues.Set("timeFrame", durationToTimeFrame(timeframe)) return r } -// Executes the request. +// Do Executes the request. func (r SoccerSeasonFixturesRequest) Do() (s FixtureList, err error) { d, _, err := r.doJson("GET") if err != nil { @@ -30,7 +30,7 @@ func (r SoccerSeasonFixturesRequest) Do() (s FixtureList, err error) { return } -// Prepares a request to fetch the fixtures of a soccer season. -func (c *Client) FixturesOfSoccerSeason(soccerSeasonId uint64) SoccerSeasonFixturesRequest { +// FixturesOfSoccerSeason Prepares a request to fetch the fixtures of a soccer season. +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 1c811d9..cda202c 100644 --- a/req_soccerseason_leaguetable.go +++ b/req_soccerseason_leaguetable.go @@ -4,13 +4,13 @@ import "fmt" type SoccerSeasonLeagueTableRequest struct{ request } -// Modifies the request to specify a match day. +// Matchday Modifies the request to specify a match day. func (r SoccerSeasonLeagueTableRequest) Matchday(matchday uint16) SoccerSeasonLeagueTableRequest { - r.v.Set("matchday", fmt.Sprintf("%d", matchday)) + r.urlValues.Set("matchday", fmt.Sprintf("%d", matchday)) return r } -// Executes the request. +// Do Executes the request. func (r SoccerSeasonLeagueTableRequest) Do() (s SoccerSeason, err error) { d, _, err := r.doJson("GET") if err != nil { @@ -21,7 +21,7 @@ func (r SoccerSeasonLeagueTableRequest) Do() (s SoccerSeason, err error) { return } -// Prepares a new request to fetch the league table of a given soccer season. -func (c *Client) LeagueTableOfSoccerSeason(soccerSeasonId uint64) SoccerSeasonLeagueTableRequest { +// LeagueTableOfSoccerSeason Prepares a new request to fetch the league table of a given soccer season. +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 bfa214e..cea43dd 100644 --- a/req_soccerseason_teams.go +++ b/req_soccerseason_teams.go @@ -2,7 +2,7 @@ package footballdata type SoccerSeasonTeamsRequest struct{ request } -// Executes the request. +// Do Executes the request. func (r SoccerSeasonTeamsRequest) Do() (s TeamList, err error) { d, _, err := r.doJson("GET") if err != nil { @@ -13,7 +13,7 @@ func (r SoccerSeasonTeamsRequest) Do() (s TeamList, err error) { return } -// Prepares a new request to fetch the league table of a given soccer season. -func (c *Client) TeamsOfSoccerSeason(soccerSeasonId uint64) SoccerSeasonTeamsRequest { +// TeamsOfSoccerSeason Prepares a new request to fetch the league table of a given soccer season. +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 8f83fa7..529cb23 100644 --- a/req_soccerseasons.go +++ b/req_soccerseasons.go @@ -4,13 +4,13 @@ import "fmt" type SoccerSeasonsRequest struct{ request } -// Modifies the request to specify a season. +// Season Modifies the request to specify a season. func (r SoccerSeasonsRequest) Season(num uint32) SoccerSeasonsRequest { - r.v.Set("season", fmt.Sprintf("%d", num)) + r.urlValues.Set("season", fmt.Sprintf("%d", num)) return r } -// Executes the request. +// Do Executes the request. func (r SoccerSeasonsRequest) Do() (s SoccerSeasonList, err error) { d, _, err := r.doJson("GET") if err != nil { @@ -21,7 +21,7 @@ func (r SoccerSeasonsRequest) Do() (s SoccerSeasonList, err error) { return } -// Prepares a request to fetch the complete list of soccer seasons. -func (c *Client) SoccerSeasons() SoccerSeasonsRequest { +// SoccerSeasons Prepares a request to fetch the complete list of soccer seasons. +func (c *client) SoccerSeasons() SoccerSeasonsRequest { return SoccerSeasonsRequest{c.req("soccerseasons")} } diff --git a/req_team.go b/req_team.go index db46823..3940862 100644 --- a/req_team.go +++ b/req_team.go @@ -5,7 +5,7 @@ type TeamRequest struct { id uint64 } -// Executes the request. +// Do Executes the request. func (r TeamRequest) Do() (s Team, err error) { d, _, err := r.doJson("GET") if err != nil { @@ -20,7 +20,7 @@ func (r TeamRequest) Do() (s Team, err error) { return } -// Prepares a request to fetch a team's information. -func (c *Client) Team(id uint64) TeamRequest { +// Team Prepares a request to fetch a team's information. +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 4bb8c09..2f552a7 100644 --- a/req_team_fixtures.go +++ b/req_team_fixtures.go @@ -7,25 +7,25 @@ import ( type TeamFixturesRequest struct{ request } -// Modifies the request to specify a specific time frame. +// TimeFrame Modifies the request to specify a specific time frame. func (r TeamFixturesRequest) TimeFrame(timeframe time.Duration) TeamFixturesRequest { - r.v.Set("timeFrame", durationToTimeFrame(timeframe)) + r.urlValues.Set("timeFrame", durationToTimeFrame(timeframe)) return r } -// Modifies the request to specify a list of leagues by their code. +// Season Modifies the request to specify a list of leagues by their code. func (r TeamFixturesRequest) Season(season uint64) TeamFixturesRequest { - r.v.Set("season", fmt.Sprintf("%d", season)) + r.urlValues.Set("season", fmt.Sprintf("%d", season)) return r } -// Modifies the request to specify a venue. +// Venue Modifies the request to specify a venue. func (r TeamFixturesRequest) Venue(venue Venue) TeamFixturesRequest { - r.v.Set("venue", string(venue)) + r.urlValues.Set("venue", string(venue)) return r } -// Executes the request. +// Do Executes the request. func (r TeamFixturesRequest) Do() (s FixturesResponse, err error) { d, _, err := r.doJson("GET") if err != nil { @@ -36,7 +36,7 @@ func (r TeamFixturesRequest) Do() (s FixturesResponse, err error) { return } -// Prepares a request to fetch the fixtures of a soccer season. -func (c *Client) FixturesOfTeam(id uint64) TeamFixturesRequest { +// FixturesOfTeam Prepares a request to fetch the fixtures of a soccer season. +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 285af22..5508132 100644 --- a/req_team_players.go +++ b/req_team_players.go @@ -2,7 +2,7 @@ package footballdata type TeamPlayersRequest struct{ request } -// Executes the request. +// Do Executes the request. func (r TeamPlayersRequest) Do() (s PlayerList, err error) { d, _, err := r.doJson("GET") if err != nil { @@ -13,7 +13,7 @@ func (r TeamPlayersRequest) Do() (s PlayerList, err error) { return } -// Prepares a request to fetch a team's players. -func (c *Client) PlayersOfTeam(id uint64) TeamPlayersRequest { +// PlayersOfTeam Prepares a request to fetch a team's players. +func (c *client) PlayersOfTeam(id uint64) TeamPlayersRequest { return TeamPlayersRequest{c.req("teams/%d/players", id)} } From 0ae7a5f5b6c1afbc6527053dd0acf1b6f5dc2cbd Mon Sep 17 00:00:00 2001 From: Christian Date: Sat, 25 Jun 2016 12:29:32 +0200 Subject: [PATCH 02/25] Failing test due to API divergence --- example_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_test.go b/example_test.go index ff662a6..d37119f 100644 --- a/example_test.go +++ b/example_test.go @@ -12,7 +12,7 @@ func Example() { client := footballdata.NewClient(http.DefaultClient) // Tell it to use our API token - client.AuthToken = "" + client.SetToken("") // Get list of seasons... seasons, err := client.SoccerSeasons().Do() From 65ccba4d66f889a1d7a6198698d799868d1714ef Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Sat, 25 Jun 2016 23:20:59 +0200 Subject: [PATCH 03/25] Normalize code whitespace. --- client.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/client.go b/client.go index d19c415..ad9edae 100644 --- a/client.go +++ b/client.go @@ -38,7 +38,7 @@ 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. @@ -50,11 +50,10 @@ func NewClient(h *http.Client) Client { // SetToken Set 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 { return request{c, fmt.Sprintf(path, pathValues...), url.Values{}} } From c65cf6f06cde4c05211648beaa8d5169e2b75eb2 Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Sat, 25 Jun 2016 23:22:52 +0200 Subject: [PATCH 04/25] Complements to pull request for improving comments. - Fix casing of method commentary. - Fix grammar and punctuation for client.SetToken. --- client.go | 6 +++--- req_fixture.go | 6 +++--- req_fixtures.go | 8 ++++---- req_soccerseason.go | 4 ++-- req_soccerseason_fixtures.go | 8 ++++---- req_soccerseason_leaguetable.go | 6 +++--- req_soccerseason_teams.go | 4 ++-- req_soccerseasons.go | 6 +++--- req_team.go | 4 ++-- req_team_fixtures.go | 10 +++++----- req_team_players.go | 4 ++-- 11 files changed, 33 insertions(+), 33 deletions(-) diff --git a/client.go b/client.go index ad9edae..c801491 100644 --- a/client.go +++ b/client.go @@ -41,15 +41,15 @@ type client struct { AuthToken string } -// NewClient Creates a new Client instance that wraps around the given HTTP client. +// 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} } -// SetToken Set the authentication token -// Calling this method is *optional* +// SetToken sets the authentication token. +// Calling this method is *optional*. func (c *client) SetToken(authToken string) { c.AuthToken = authToken } diff --git a/req_fixture.go b/req_fixture.go index 8d456e6..3c43651 100644 --- a/req_fixture.go +++ b/req_fixture.go @@ -4,13 +4,13 @@ import "fmt" type FixtureRequest struct{ request } -// Head2Head Modifies the request to specify the number of former games to be analyzed (normally 10). +// Head2Head modifies the request to specify the number of former games to be analyzed (normally 10). func (r FixtureRequest) Head2Head(num uint16) FixtureRequest { r.urlValues.Set("head2head", fmt.Sprintf("%d", num)) return r } -// Do Executes the request. +// Do executes the request. func (r FixtureRequest) Do() (s Fixture, err error) { d, _, err := r.doJson("GET") if err != nil { @@ -21,7 +21,7 @@ func (r FixtureRequest) Do() (s Fixture, err error) { return } -// Fixture Prepares a request to fetch the fixtures of a soccer season. +// Fixture prepares a request to fetch the fixtures of a soccer season. 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 dac81dc..e0f0c6e 100644 --- a/req_fixtures.go +++ b/req_fixtures.go @@ -7,19 +7,19 @@ import ( type FixturesRequest struct{ request } -// TimeFrame Modifies the request to specify a specific time frame. +// TimeFrame modifies the request to specify a specific time frame. func (r FixturesRequest) TimeFrame(timeframe time.Duration) FixturesRequest { r.urlValues.Set("timeFrame", durationToTimeFrame(timeframe)) return r } -// League Modifies the request to specify a list of leagues by their code. +// League modifies the request to specify a list of leagues by their code. func (r FixturesRequest) League(leagueCodes ...string) FixturesRequest { r.urlValues.Set("league", strings.Join(leagueCodes, ",")) return r } -// Do Executes the request. +// Do executes the request. func (r FixturesRequest) Do() (s FixturesResponse, err error) { d, _, err := r.doJson("GET") if err != nil { @@ -30,7 +30,7 @@ func (r FixturesRequest) Do() (s FixturesResponse, err error) { return } -// Fixtures Prepares a request to fetch the fixtures of a soccer season. +// Fixtures prepares a request to fetch the fixtures of a soccer season. func (c *client) Fixtures() FixturesRequest { return FixturesRequest{c.req("fixtures")} } diff --git a/req_soccerseason.go b/req_soccerseason.go index 3fa03df..fccaad9 100644 --- a/req_soccerseason.go +++ b/req_soccerseason.go @@ -2,7 +2,7 @@ package footballdata type SoccerSeasonRequest struct{ request } -// Do Executes the request. +// Do executes the request. func (r SoccerSeasonRequest) Do() (s SoccerSeason, err error) { d, _, err := r.doJson("GET") if err != nil { @@ -13,7 +13,7 @@ func (r SoccerSeasonRequest) Do() (s SoccerSeason, err error) { return } -// SoccerSeason Prepares a request to fetch the complete list of soccer seasons. +// SoccerSeason prepares a request to fetch the complete list of soccer seasons. 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 74010d7..c026a1d 100644 --- a/req_soccerseason_fixtures.go +++ b/req_soccerseason_fixtures.go @@ -7,19 +7,19 @@ import ( type SoccerSeasonFixturesRequest struct{ request } -// Matchday Modifies the request to specify a match day. +// Matchday modifies the request to specify a match day. func (r SoccerSeasonFixturesRequest) Matchday(matchday uint16) SoccerSeasonFixturesRequest { r.urlValues.Set("matchday", fmt.Sprintf("%d", matchday)) return r } -// TimeFrame Modifies the request to specify a specific time frame. +// TimeFrame modifies the request to specify a specific time frame. func (r SoccerSeasonFixturesRequest) TimeFrame(timeframe time.Duration) SoccerSeasonFixturesRequest { r.urlValues.Set("timeFrame", durationToTimeFrame(timeframe)) return r } -// Do Executes the request. +// Do executes the request. func (r SoccerSeasonFixturesRequest) Do() (s FixtureList, err error) { d, _, err := r.doJson("GET") if err != nil { @@ -30,7 +30,7 @@ func (r SoccerSeasonFixturesRequest) Do() (s FixtureList, err error) { return } -// FixturesOfSoccerSeason Prepares a request to fetch the fixtures of a soccer season. +// FixturesOfSoccerSeason prepares a request to fetch the fixtures of a soccer season. 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 cda202c..737653b 100644 --- a/req_soccerseason_leaguetable.go +++ b/req_soccerseason_leaguetable.go @@ -4,13 +4,13 @@ import "fmt" type SoccerSeasonLeagueTableRequest struct{ request } -// Matchday Modifies the request to specify a match day. +// Matchday modifies the request to specify a match day. func (r SoccerSeasonLeagueTableRequest) Matchday(matchday uint16) SoccerSeasonLeagueTableRequest { r.urlValues.Set("matchday", fmt.Sprintf("%d", matchday)) return r } -// Do Executes the request. +// Do executes the request. func (r SoccerSeasonLeagueTableRequest) Do() (s SoccerSeason, err error) { d, _, err := r.doJson("GET") if err != nil { @@ -21,7 +21,7 @@ func (r SoccerSeasonLeagueTableRequest) Do() (s SoccerSeason, err error) { return } -// LeagueTableOfSoccerSeason Prepares a new request to fetch the league table of a given soccer season. +// LeagueTableOfSoccerSeason prepares a new request to fetch the league table of a given soccer season. 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 cea43dd..e60a479 100644 --- a/req_soccerseason_teams.go +++ b/req_soccerseason_teams.go @@ -2,7 +2,7 @@ package footballdata type SoccerSeasonTeamsRequest struct{ request } -// Do Executes the request. +// Do executes the request. func (r SoccerSeasonTeamsRequest) Do() (s TeamList, err error) { d, _, err := r.doJson("GET") if err != nil { @@ -13,7 +13,7 @@ func (r SoccerSeasonTeamsRequest) Do() (s TeamList, err error) { return } -// TeamsOfSoccerSeason Prepares a new request to fetch the league table of a given soccer season. +// TeamsOfSoccerSeason prepares a new request to fetch the league table of a given soccer season. 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 529cb23..6b1408e 100644 --- a/req_soccerseasons.go +++ b/req_soccerseasons.go @@ -4,13 +4,13 @@ import "fmt" type SoccerSeasonsRequest struct{ request } -// Season Modifies the request to specify a season. +// Season modifies the request to specify a season. func (r SoccerSeasonsRequest) Season(num uint32) SoccerSeasonsRequest { r.urlValues.Set("season", fmt.Sprintf("%d", num)) return r } -// Do Executes the request. +// Do executes the request. func (r SoccerSeasonsRequest) Do() (s SoccerSeasonList, err error) { d, _, err := r.doJson("GET") if err != nil { @@ -21,7 +21,7 @@ func (r SoccerSeasonsRequest) Do() (s SoccerSeasonList, err error) { return } -// SoccerSeasons Prepares a request to fetch the complete list of soccer seasons. +// SoccerSeasons prepares a request to fetch the complete list of soccer seasons. func (c *client) SoccerSeasons() SoccerSeasonsRequest { return SoccerSeasonsRequest{c.req("soccerseasons")} } diff --git a/req_team.go b/req_team.go index 3940862..c961502 100644 --- a/req_team.go +++ b/req_team.go @@ -5,7 +5,7 @@ type TeamRequest struct { id uint64 } -// Do Executes the request. +// Do executes the request. func (r TeamRequest) Do() (s Team, err error) { d, _, err := r.doJson("GET") if err != nil { @@ -20,7 +20,7 @@ func (r TeamRequest) Do() (s Team, err error) { return } -// Team Prepares a request to fetch a team's information. +// Team prepares a request to fetch a team's information. 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 2f552a7..d63e03e 100644 --- a/req_team_fixtures.go +++ b/req_team_fixtures.go @@ -7,25 +7,25 @@ import ( type TeamFixturesRequest struct{ request } -// TimeFrame Modifies the request to specify a specific time frame. +// TimeFrame modifies the request to specify a specific time frame. func (r TeamFixturesRequest) TimeFrame(timeframe time.Duration) TeamFixturesRequest { r.urlValues.Set("timeFrame", durationToTimeFrame(timeframe)) return r } -// Season Modifies the request to specify a list of leagues by their code. +// Season modifies the request to specify a list of leagues by their code. func (r TeamFixturesRequest) Season(season uint64) TeamFixturesRequest { r.urlValues.Set("season", fmt.Sprintf("%d", season)) return r } -// Venue Modifies the request to specify a venue. +// Venue modifies the request to specify a venue. func (r TeamFixturesRequest) Venue(venue Venue) TeamFixturesRequest { r.urlValues.Set("venue", string(venue)) return r } -// Do Executes the request. +// Do executes the request. func (r TeamFixturesRequest) Do() (s FixturesResponse, err error) { d, _, err := r.doJson("GET") if err != nil { @@ -36,7 +36,7 @@ func (r TeamFixturesRequest) Do() (s FixturesResponse, err error) { return } -// FixturesOfTeam Prepares a request to fetch the fixtures of a soccer season. +// FixturesOfTeam prepares a request to fetch the fixtures of a soccer season. 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 5508132..099de3b 100644 --- a/req_team_players.go +++ b/req_team_players.go @@ -2,7 +2,7 @@ package footballdata type TeamPlayersRequest struct{ request } -// Do Executes the request. +// Do executes the request. func (r TeamPlayersRequest) Do() (s PlayerList, err error) { d, _, err := r.doJson("GET") if err != nil { @@ -13,7 +13,7 @@ func (r TeamPlayersRequest) Do() (s PlayerList, err error) { return } -// PlayersOfTeam Prepares a request to fetch a team's players. +// PlayersOfTeam prepares a request to fetch a team's players. func (c *client) PlayersOfTeam(id uint64) TeamPlayersRequest { return TeamPlayersRequest{c.req("teams/%d/players", id)} } From 8f5d035e92b6777bf2c6039ad31513a2ea755351 Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Sat, 25 Jun 2016 23:35:38 +0200 Subject: [PATCH 05/25] Move commentary for client methods to the actual interface. --- client.go | 34 ++++++++++++++++++++++++++------- req_fixture.go | 1 - req_fixtures.go | 1 - req_soccerseason.go | 1 - req_soccerseason_fixtures.go | 1 - req_soccerseason_leaguetable.go | 1 - req_soccerseason_teams.go | 1 - req_soccerseasons.go | 1 - req_team.go | 1 - req_team_fixtures.go | 1 - req_team_players.go | 1 - 11 files changed, 27 insertions(+), 17 deletions(-) diff --git a/client.go b/client.go index c801491..f689d06 100644 --- a/client.go +++ b/client.go @@ -16,16 +16,38 @@ 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 prepares a request to fetch the fixtures of a soccer season. Fixture(id uint64) FixtureRequest + + // Fixtures prepares a request to fetch the fixtures of a soccer season. Fixtures() FixturesRequest - SoccerSeason(id uint64) SoccerSeasonRequest + + // FixturesOfSoccerSeason prepares a request to fetch the fixtures of a soccer season. FixturesOfSoccerSeason(soccerSeasonId uint64) SoccerSeasonFixturesRequest - LeagueTableOfSoccerSeason(soccerSeasonId uint64) SoccerSeasonLeagueTableRequest - TeamsOfSoccerSeason(soccerSeasonId uint64) SoccerSeasonTeamsRequest - SoccerSeasons() SoccerSeasonsRequest - Team(id uint64) TeamRequest + + // FixturesOfTeam prepares a request to fetch the fixtures of a soccer season. FixturesOfTeam(id uint64) TeamFixturesRequest + // LeagueTableOfSoccerSeason prepares a new request to fetch the league table of a given soccer season. + LeagueTableOfSoccerSeason(soccerSeasonId uint64) SoccerSeasonLeagueTableRequest + + // PlayersOfTeam prepares a request to fetch a team's players. + PlayersOfTeam(id uint64) TeamPlayersRequest + + // SoccerSeason prepares a request to fetch the complete list of soccer seasons. + SoccerSeason(id uint64) SoccerSeasonRequest + + // SoccerSeasons prepares a request to fetch the complete list of soccer seasons. + SoccerSeasons() SoccerSeasonsRequest + + // Team prepares a request to fetch a team's information. + Team(id uint64) TeamRequest + + // TeamsOfSoccerSeason prepares a new request to fetch the league table of a given soccer season. + TeamsOfSoccerSeason(soccerSeasonId uint64) SoccerSeasonTeamsRequest + + // SetToken sets the authentication token. + // Calling this method is *optional*. SetToken(authToken string) } @@ -48,8 +70,6 @@ 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 } diff --git a/req_fixture.go b/req_fixture.go index 3c43651..34c1e66 100644 --- a/req_fixture.go +++ b/req_fixture.go @@ -21,7 +21,6 @@ func (r FixtureRequest) Do() (s Fixture, err error) { return } -// Fixture prepares a request to fetch the fixtures of a soccer season. 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..de2426c 100644 --- a/req_fixtures.go +++ b/req_fixtures.go @@ -30,7 +30,6 @@ func (r FixturesRequest) Do() (s FixturesResponse, err error) { return } -// Fixtures prepares a request to fetch the fixtures of a soccer season. func (c *client) Fixtures() FixturesRequest { return FixturesRequest{c.req("fixtures")} } diff --git a/req_soccerseason.go b/req_soccerseason.go index fccaad9..2675d3a 100644 --- a/req_soccerseason.go +++ b/req_soccerseason.go @@ -13,7 +13,6 @@ func (r SoccerSeasonRequest) Do() (s SoccerSeason, err error) { return } -// SoccerSeason prepares a request to fetch the complete list of soccer seasons. 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..90b67a7 100644 --- a/req_soccerseason_fixtures.go +++ b/req_soccerseason_fixtures.go @@ -30,7 +30,6 @@ func (r SoccerSeasonFixturesRequest) Do() (s FixtureList, err error) { return } -// FixturesOfSoccerSeason prepares a request to fetch the fixtures of a soccer season. 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..60ca8c4 100644 --- a/req_soccerseason_leaguetable.go +++ b/req_soccerseason_leaguetable.go @@ -21,7 +21,6 @@ func (r SoccerSeasonLeagueTableRequest) Do() (s SoccerSeason, err error) { return } -// LeagueTableOfSoccerSeason prepares a new request to fetch the league table of a given soccer season. 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..d821173 100644 --- a/req_soccerseason_teams.go +++ b/req_soccerseason_teams.go @@ -13,7 +13,6 @@ func (r SoccerSeasonTeamsRequest) Do() (s TeamList, err error) { return } -// TeamsOfSoccerSeason prepares a new request to fetch the league table of a given soccer season. 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..79cef56 100644 --- a/req_soccerseasons.go +++ b/req_soccerseasons.go @@ -21,7 +21,6 @@ func (r SoccerSeasonsRequest) Do() (s SoccerSeasonList, err error) { return } -// SoccerSeasons prepares a request to fetch the complete list of soccer seasons. func (c *client) SoccerSeasons() SoccerSeasonsRequest { return SoccerSeasonsRequest{c.req("soccerseasons")} } diff --git a/req_team.go b/req_team.go index c961502..67c06fd 100644 --- a/req_team.go +++ b/req_team.go @@ -20,7 +20,6 @@ func (r TeamRequest) Do() (s Team, err error) { return } -// Team prepares a request to fetch a team's information. 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..575f02a 100644 --- a/req_team_fixtures.go +++ b/req_team_fixtures.go @@ -36,7 +36,6 @@ func (r TeamFixturesRequest) Do() (s FixturesResponse, err error) { return } -// FixturesOfTeam prepares a request to fetch the fixtures of a soccer season. 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..4f8656b 100644 --- a/req_team_players.go +++ b/req_team_players.go @@ -13,7 +13,6 @@ func (r TeamPlayersRequest) Do() (s PlayerList, err error) { return } -// PlayersOfTeam prepares a request to fetch a team's players. func (c *client) PlayersOfTeam(id uint64) TeamPlayersRequest { return TeamPlayersRequest{c.req("teams/%d/players", id)} } From 5418ecc69ef882ab1541862bb417a3e4a980b0f6 Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Sun, 26 Jun 2016 12:02:53 +0200 Subject: [PATCH 06/25] Add separate examples for Client. --- example_test.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/example_test.go b/example_test.go index d37119f..b6202dd 100644 --- a/example_test.go +++ b/example_test.go @@ -25,3 +25,25 @@ func Example() { fmt.Println(season.Id, season.Caption) } } + +func ExampleClient() { + // Create client + client := footballdata.NewClient(http.DefaultClient) + + /* Do something with the client instance... */ + seasons, err := client.SoccerSeasons().Do() + if err != nil { + panic(err) + } + for _, season := range seasons { + fmt.Println(season.Id, season.Caption) + } +} + +func ExampleClient_withToken() { + // Create client + client := footballdata.NewClient(http.DefaultClient) + + // Tell it to use our API token + client.SetToken("") +} From 9d3290ff36bbd44d0a4fdf3d30b08134306a8e55 Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Sun, 26 Jun 2016 12:11:24 +0200 Subject: [PATCH 07/25] Update example code in README - Replaced AuthToken member assignment to SetToken call. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ba5cb89..7a0e026 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ func main() { client := footballdata.NewClient(http.DefaultClient) // Tell it to use our API token - client.AuthToken = "" + client.SetToken("") // Get list of seasons... seasons, err := client.SoccerSeasons().Do() @@ -44,4 +44,4 @@ func main() { } } -``` \ No newline at end of file +``` From 1d952c49e937ee6d53117bb169a1109ae9294852 Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Sun, 26 Jun 2016 12:24:59 +0200 Subject: [PATCH 08/25] Revert "Move commentary for client methods to the actual interface." This reverts commit 8f5d035e92b6777bf2c6039ad31513a2ea755351. --- client.go | 34 +++++++-------------------------- req_fixture.go | 1 + req_fixtures.go | 1 + req_soccerseason.go | 1 + req_soccerseason_fixtures.go | 1 + req_soccerseason_leaguetable.go | 1 + req_soccerseason_teams.go | 1 + req_soccerseasons.go | 1 + req_team.go | 1 + req_team_fixtures.go | 1 + req_team_players.go | 1 + 11 files changed, 17 insertions(+), 27 deletions(-) diff --git a/client.go b/client.go index f689d06..c801491 100644 --- a/client.go +++ b/client.go @@ -16,38 +16,16 @@ 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 prepares a request to fetch the fixtures of a soccer season. Fixture(id uint64) FixtureRequest - - // Fixtures prepares a request to fetch the fixtures of a soccer season. Fixtures() FixturesRequest - - // FixturesOfSoccerSeason prepares a request to fetch the fixtures of a soccer season. + SoccerSeason(id uint64) SoccerSeasonRequest FixturesOfSoccerSeason(soccerSeasonId uint64) SoccerSeasonFixturesRequest - - // FixturesOfTeam prepares a request to fetch the fixtures of a soccer season. + LeagueTableOfSoccerSeason(soccerSeasonId uint64) SoccerSeasonLeagueTableRequest + TeamsOfSoccerSeason(soccerSeasonId uint64) SoccerSeasonTeamsRequest + SoccerSeasons() SoccerSeasonsRequest + Team(id uint64) TeamRequest FixturesOfTeam(id uint64) TeamFixturesRequest - // LeagueTableOfSoccerSeason prepares a new request to fetch the league table of a given soccer season. - LeagueTableOfSoccerSeason(soccerSeasonId uint64) SoccerSeasonLeagueTableRequest - - // PlayersOfTeam prepares a request to fetch a team's players. - PlayersOfTeam(id uint64) TeamPlayersRequest - - // SoccerSeason prepares a request to fetch the complete list of soccer seasons. - SoccerSeason(id uint64) SoccerSeasonRequest - - // SoccerSeasons prepares a request to fetch the complete list of soccer seasons. - SoccerSeasons() SoccerSeasonsRequest - - // Team prepares a request to fetch a team's information. - Team(id uint64) TeamRequest - - // TeamsOfSoccerSeason prepares a new request to fetch the league table of a given soccer season. - TeamsOfSoccerSeason(soccerSeasonId uint64) SoccerSeasonTeamsRequest - - // SetToken sets the authentication token. - // Calling this method is *optional*. SetToken(authToken string) } @@ -70,6 +48,8 @@ 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 } diff --git a/req_fixture.go b/req_fixture.go index 34c1e66..3c43651 100644 --- a/req_fixture.go +++ b/req_fixture.go @@ -21,6 +21,7 @@ func (r FixtureRequest) Do() (s Fixture, err error) { return } +// Fixture prepares a request to fetch the fixtures of a soccer season. 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 de2426c..e0f0c6e 100644 --- a/req_fixtures.go +++ b/req_fixtures.go @@ -30,6 +30,7 @@ func (r FixturesRequest) Do() (s FixturesResponse, err error) { return } +// Fixtures prepares a request to fetch the fixtures of a soccer season. func (c *client) Fixtures() FixturesRequest { return FixturesRequest{c.req("fixtures")} } diff --git a/req_soccerseason.go b/req_soccerseason.go index 2675d3a..fccaad9 100644 --- a/req_soccerseason.go +++ b/req_soccerseason.go @@ -13,6 +13,7 @@ func (r SoccerSeasonRequest) Do() (s SoccerSeason, err error) { return } +// SoccerSeason prepares a request to fetch the complete list of soccer seasons. 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 90b67a7..c026a1d 100644 --- a/req_soccerseason_fixtures.go +++ b/req_soccerseason_fixtures.go @@ -30,6 +30,7 @@ func (r SoccerSeasonFixturesRequest) Do() (s FixtureList, err error) { return } +// FixturesOfSoccerSeason prepares a request to fetch the fixtures of a soccer season. 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 60ca8c4..737653b 100644 --- a/req_soccerseason_leaguetable.go +++ b/req_soccerseason_leaguetable.go @@ -21,6 +21,7 @@ func (r SoccerSeasonLeagueTableRequest) Do() (s SoccerSeason, err error) { return } +// LeagueTableOfSoccerSeason prepares a new request to fetch the league table of a given soccer season. 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 d821173..e60a479 100644 --- a/req_soccerseason_teams.go +++ b/req_soccerseason_teams.go @@ -13,6 +13,7 @@ func (r SoccerSeasonTeamsRequest) Do() (s TeamList, err error) { return } +// TeamsOfSoccerSeason prepares a new request to fetch the league table of a given soccer season. 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 79cef56..6b1408e 100644 --- a/req_soccerseasons.go +++ b/req_soccerseasons.go @@ -21,6 +21,7 @@ func (r SoccerSeasonsRequest) Do() (s SoccerSeasonList, err error) { return } +// SoccerSeasons prepares a request to fetch the complete list of soccer seasons. func (c *client) SoccerSeasons() SoccerSeasonsRequest { return SoccerSeasonsRequest{c.req("soccerseasons")} } diff --git a/req_team.go b/req_team.go index 67c06fd..c961502 100644 --- a/req_team.go +++ b/req_team.go @@ -20,6 +20,7 @@ func (r TeamRequest) Do() (s Team, err error) { return } +// Team prepares a request to fetch a team's information. 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 575f02a..d63e03e 100644 --- a/req_team_fixtures.go +++ b/req_team_fixtures.go @@ -36,6 +36,7 @@ func (r TeamFixturesRequest) Do() (s FixturesResponse, err error) { return } +// FixturesOfTeam prepares a request to fetch the fixtures of a soccer season. 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 4f8656b..099de3b 100644 --- a/req_team_players.go +++ b/req_team_players.go @@ -13,6 +13,7 @@ func (r TeamPlayersRequest) Do() (s PlayerList, err error) { return } +// PlayersOfTeam prepares a request to fetch a team's players. func (c *client) PlayersOfTeam(id uint64) TeamPlayersRequest { return TeamPlayersRequest{c.req("teams/%d/players", id)} } From e6526da3e09170f8618d91595cc0018481f8d846 Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Sun, 26 Jun 2016 12:57:14 +0200 Subject: [PATCH 09/25] 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)} } From 850e9e7bbf59de09ed69ee46d6f2114cf80e3921 Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Sun, 26 Jun 2016 12:58:50 +0200 Subject: [PATCH 10/25] Update example code in README. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7a0e026..78c3bbf 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ import ( func main() { // Create client - client := footballdata.NewClient(http.DefaultClient) + client := new(footballdata.Client) // Tell it to use our API token client.SetToken("") From cb21cdb829435a0cf36cdbc8b42a66beac499cb9 Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Sun, 26 Jun 2016 13:04:04 +0200 Subject: [PATCH 11/25] 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. --- README.md | 8 +++----- client.go | 24 +++++++++++++++++++++++- example_test.go | 26 +++++++++++++++++++------- 3 files changed, 45 insertions(+), 13 deletions(-) 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 +} From c3d9a59b0d2a435e9cc0e043a4a2fd1479efdbba Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Sat, 9 Jul 2016 20:50:13 +0200 Subject: [PATCH 12/25] Fix typo in SoccerSeason definition. --- api_types.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api_types.go b/api_types.go index a361b83..ceaaed1 100644 --- a/api_types.go +++ b/api_types.go @@ -33,7 +33,7 @@ type SoccerSeason struct { NumberOfMatchdays uint16 NumberOfTeams uint16 NumberOfGames uint16 - LastUpdates time.Time + LastUpdated time.Time } // Contains the fixture and the head to head information delivered by the API From 782a1683b32be7435ab2af438d69ff0447a65412 Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Sat, 9 Jul 2016 20:57:58 +0200 Subject: [PATCH 13/25] Add missing states POSTPONED, CANCELED and SCHEDULED for FixtureStatus. --- api_types.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/api_types.go b/api_types.go index ceaaed1..673f7e3 100644 --- a/api_types.go +++ b/api_types.go @@ -5,9 +5,12 @@ import "time" type FixtureStatus string const ( - FixtureStatus_Timed FixtureStatus = "TIMED" - FixtureStatus_InPlay FixtureStatus = "IN_PLAY" - FixtureStatus_Finished FixtureStatus = "FINISHED" + FixtureStatus_Scheduled FixtureStatus = "SCHEDULED" + FixtureStatus_Timed FixtureStatus = "TIMED" + FixtureStatus_Postponed FixtureStatus = "POSTPONED" + FixtureStatus_InPlay FixtureStatus = "IN_PLAY" + FixtureStatus_Canceled FixtureStatus = "CANCELED" + FixtureStatus_Finished FixtureStatus = "FINISHED" ) // Describes the venue. From d0afff06fcc0f78fce5343d11c6f5c921b13c5e3 Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Sat, 9 Jul 2016 20:59:24 +0200 Subject: [PATCH 14/25] Deprecate Soccerseason methods and structs in favor of the Competition model. Deprecated structs and methods will be removed before tagging v1 of the implementation. --- api_types.go | 20 ++++++++++++++++++ req_competition.go | 19 +++++++++++++++++ req_competition_fixtures.go | 36 +++++++++++++++++++++++++++++++++ req_competition_leaguetable.go | 27 +++++++++++++++++++++++++ req_competition_teams.go | 19 +++++++++++++++++ req_competitions.go | 27 +++++++++++++++++++++++++ req_soccerseason.go | 6 ++++++ req_soccerseason_fixtures.go | 10 +++++++++ req_soccerseason_leaguetable.go | 6 ++++++ req_soccerseason_teams.go | 6 ++++++ req_soccerseasons.go | 8 ++++++++ 11 files changed, 184 insertions(+) create mode 100644 req_competition.go create mode 100644 req_competition_fixtures.go create mode 100644 req_competition_leaguetable.go create mode 100644 req_competition_teams.go create mode 100644 req_competitions.go diff --git a/api_types.go b/api_types.go index 673f7e3..a0bbe98 100644 --- a/api_types.go +++ b/api_types.go @@ -23,9 +23,13 @@ const ( Venue_Away Venue = "away" ) +// DEPRECATED. +// // Contains the list of soccer seasons returned by the API. type SoccerSeasonList []SoccerSeason +// DEPRECATED. +// // Contains information about a soccer season. type SoccerSeason struct { Id uint64 @@ -39,6 +43,22 @@ type SoccerSeason struct { LastUpdated time.Time } +// Contains the list of competitions returned by the API. +type CompetitionList []Competitions + +// Contains information about a competition. +type Competition struct { + Id uint64 + Caption string + League string + Year string + CurrentMatchday uint16 + NumberOfMatchdays uint16 + NumberOfTeams uint16 + NumberOfGames uint16 + LastUpdated time.Time +} + // Contains the fixture and the head to head information delivered by the API // for a wanted fixture. type FixtureResponse struct { diff --git a/req_competition.go b/req_competition.go new file mode 100644 index 0000000..b4aa6ea --- /dev/null +++ b/req_competition.go @@ -0,0 +1,19 @@ +package footballdata + +type CompetitionRequest struct{ request } + +// Do executes the request. +func (r CompetitionRequest) Do() (s Competition, err error) { + d, _, err := r.doJson("GET") + if err != nil { + return + } + + err = d.Decode(&s) + return +} + +// Competition prepares a request to fetch the complete list of soccer seasons. +func (c *Client) Competition(id uint64) CompetitionRequest { + return CompetitionRequest{c.req("competitions/%d", id)} +} diff --git a/req_competition_fixtures.go b/req_competition_fixtures.go new file mode 100644 index 0000000..ac41957 --- /dev/null +++ b/req_competition_fixtures.go @@ -0,0 +1,36 @@ +package footballdata + +import ( + "fmt" + "time" +) + +type CompetitionFixturesRequest struct{ request } + +// Matchday modifies the request to specify a match day. +func (r CompetitionFixturesRequest) Matchday(matchday uint16) CompetitionFixturesRequest { + r.urlValues.Set("matchday", fmt.Sprintf("%d", matchday)) + return r +} + +// TimeFrame modifies the request to specify a specific time frame. +func (r CompetitionFixturesRequest) TimeFrame(timeframe time.Duration) CompetitionFixturesRequest { + r.urlValues.Set("timeFrame", durationToTimeFrame(timeframe)) + return r +} + +// Do executes the request. +func (r CompetitionFixturesRequest) Do() (s FixtureList, err error) { + d, _, err := r.doJson("GET") + if err != nil { + return + } + + err = d.Decode(&s) + return +} + +// FixturesOfCompetition prepares a request to fetch the fixtures of a soccer season. +func (c *Client) FixturesOfCompetition(soccerSeasonId uint64) CompetitionFixturesRequest { + return CompetitionFixturesRequest{c.req("competitions/%d/fixtures", soccerSeasonId)} +} diff --git a/req_competition_leaguetable.go b/req_competition_leaguetable.go new file mode 100644 index 0000000..db9f046 --- /dev/null +++ b/req_competition_leaguetable.go @@ -0,0 +1,27 @@ +package footballdata + +import "fmt" + +type CompetitionLeagueTableRequest struct{ request } + +// Matchday modifies the request to specify a match day. +func (r CompetitionLeagueTableRequest) Matchday(matchday uint16) CompetitionLeagueTableRequest { + r.urlValues.Set("matchday", fmt.Sprintf("%d", matchday)) + return r +} + +// Do executes the request. +func (r CompetitionLeagueTableRequest) Do() (s Competition, err error) { + d, _, err := r.doJson("GET") + if err != nil { + return + } + + err = d.Decode(&s) + return +} + +// LeagueTableOfCompetition prepares a new request to fetch the league table of a given soccer season. +func (c *Client) LeagueTableOfCompetition(soccerSeasonId uint64) CompetitionLeagueTableRequest { + return CompetitionLeagueTableRequest{c.req("competitions/%d/leagueTable", soccerSeasonId)} +} diff --git a/req_competition_teams.go b/req_competition_teams.go new file mode 100644 index 0000000..7e9a659 --- /dev/null +++ b/req_competition_teams.go @@ -0,0 +1,19 @@ +package footballdata + +type CompetitionTeamsRequest struct{ request } + +// Do executes the request. +func (r CompetitionTeamsRequest) Do() (s TeamList, err error) { + d, _, err := r.doJson("GET") + if err != nil { + return + } + + err = d.Decode(&s) + return +} + +// TeamsOfCompetition prepares a new request to fetch the league table of a given soccer season. +func (c *Client) TeamsOfCompetition(soccerSeasonId uint64) CompetitionTeamsRequest { + return CompetitionTeamsRequest{c.req("competitions/%d/leagueTable", soccerSeasonId)} +} diff --git a/req_competitions.go b/req_competitions.go new file mode 100644 index 0000000..c414f66 --- /dev/null +++ b/req_competitions.go @@ -0,0 +1,27 @@ +package footballdata + +import "fmt" + +type CompetitionsRequest struct{ request } + +// Season modifies the request to specify a season. +func (r CompetitionsRequest) Season(num uint32) CompetitionsRequest { + r.urlValues.Set("season", fmt.Sprintf("%d", num)) + return r +} + +// Do executes the request. +func (r CompetitionsRequest) Do() (s CompetitionList, err error) { + d, _, err := r.doJson("GET") + if err != nil { + return + } + + err = d.Decode(&s) + return +} + +// Competitions prepares a request to fetch the complete list of soccer seasons. +func (c *Client) Competitions() CompetitionsRequest { + return CompetitionsRequest{c.req("competitions")} +} diff --git a/req_soccerseason.go b/req_soccerseason.go index 1bfa417..5e14367 100644 --- a/req_soccerseason.go +++ b/req_soccerseason.go @@ -1,7 +1,11 @@ package footballdata +// DEPRECATED. +// type SoccerSeasonRequest struct{ request } +// DEPRECATED. +// // Do executes the request. func (r SoccerSeasonRequest) Do() (s SoccerSeason, err error) { d, _, err := r.doJson("GET") @@ -13,6 +17,8 @@ func (r SoccerSeasonRequest) Do() (s SoccerSeason, err error) { return } +// DEPRECATED. +// // SoccerSeason prepares a request to fetch the complete list of soccer seasons. 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 7f95b47..58b5c90 100644 --- a/req_soccerseason_fixtures.go +++ b/req_soccerseason_fixtures.go @@ -5,20 +5,28 @@ import ( "time" ) +// DEPRECATED. +// type SoccerSeasonFixturesRequest struct{ request } +// DEPRECATED. +// // Matchday modifies the request to specify a match day. func (r SoccerSeasonFixturesRequest) Matchday(matchday uint16) SoccerSeasonFixturesRequest { r.urlValues.Set("matchday", fmt.Sprintf("%d", matchday)) return r } +// DEPRECATED. +// // TimeFrame modifies the request to specify a specific time frame. func (r SoccerSeasonFixturesRequest) TimeFrame(timeframe time.Duration) SoccerSeasonFixturesRequest { r.urlValues.Set("timeFrame", durationToTimeFrame(timeframe)) return r } +// DEPRECATED. +// // Do executes the request. func (r SoccerSeasonFixturesRequest) Do() (s FixtureList, err error) { d, _, err := r.doJson("GET") @@ -30,6 +38,8 @@ func (r SoccerSeasonFixturesRequest) Do() (s FixtureList, err error) { return } +// DEPRECATED. +// // FixturesOfSoccerSeason prepares a request to fetch the fixtures of a soccer season. 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 7fd6cbf..51c31e3 100644 --- a/req_soccerseason_leaguetable.go +++ b/req_soccerseason_leaguetable.go @@ -4,12 +4,16 @@ import "fmt" type SoccerSeasonLeagueTableRequest struct{ request } +// DEPRECATED. +// // Matchday modifies the request to specify a match day. func (r SoccerSeasonLeagueTableRequest) Matchday(matchday uint16) SoccerSeasonLeagueTableRequest { r.urlValues.Set("matchday", fmt.Sprintf("%d", matchday)) return r } +// DEPRECATED. +// // Do executes the request. func (r SoccerSeasonLeagueTableRequest) Do() (s SoccerSeason, err error) { d, _, err := r.doJson("GET") @@ -21,6 +25,8 @@ func (r SoccerSeasonLeagueTableRequest) Do() (s SoccerSeason, err error) { return } +// DEPRECATED. +// // LeagueTableOfSoccerSeason prepares a new request to fetch the league table of a given soccer season. 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 1ec633c..480634a 100644 --- a/req_soccerseason_teams.go +++ b/req_soccerseason_teams.go @@ -1,7 +1,11 @@ package footballdata +// DEPRECATED. +// type SoccerSeasonTeamsRequest struct{ request } +// DEPRECATED. +// // Do executes the request. func (r SoccerSeasonTeamsRequest) Do() (s TeamList, err error) { d, _, err := r.doJson("GET") @@ -13,6 +17,8 @@ func (r SoccerSeasonTeamsRequest) Do() (s TeamList, err error) { return } +// DEPRECATED. +// // TeamsOfSoccerSeason prepares a new request to fetch the league table of a given soccer season. 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 88d0fe7..bd61a82 100644 --- a/req_soccerseasons.go +++ b/req_soccerseasons.go @@ -2,14 +2,20 @@ package footballdata import "fmt" +// DEPRECATED. +// type SoccerSeasonsRequest struct{ request } +// DEPRECATED. +// // Season modifies the request to specify a season. func (r SoccerSeasonsRequest) Season(num uint32) SoccerSeasonsRequest { r.urlValues.Set("season", fmt.Sprintf("%d", num)) return r } +// DEPRECATED. +// // Do executes the request. func (r SoccerSeasonsRequest) Do() (s SoccerSeasonList, err error) { d, _, err := r.doJson("GET") @@ -21,6 +27,8 @@ func (r SoccerSeasonsRequest) Do() (s SoccerSeasonList, err error) { return } +// DEPRECATED. +// // SoccerSeasons prepares a request to fetch the complete list of soccer seasons. func (c *Client) SoccerSeasons() SoccerSeasonsRequest { return SoccerSeasonsRequest{c.req("soccerseasons")} From 883bfcf113d7654ddd832c2155b0036ebeff99dc Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Sat, 9 Jul 2016 21:06:13 +0200 Subject: [PATCH 15/25] Update examples to use Competition instead of SoccerSeason. --- README.md | 6 +++--- example_test.go | 14 +++++++------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index e67ef31..ab947cc 100644 --- a/README.md +++ b/README.md @@ -31,14 +31,14 @@ func main() { WithToken("") // Get list of seasons... - seasons, err := client.SoccerSeasons().Do() + competitions, err := client.Competitions().Do() if err != nil { panic(err) } // ...and print them - for _, season := range seasons { - fmt.Println(season.Id, season.Caption) + for _, competition := range competitions { + fmt.Println(competition.Id, competition.Caption) } } diff --git a/example_test.go b/example_test.go index d3a7103..9057dcc 100644 --- a/example_test.go +++ b/example_test.go @@ -13,14 +13,14 @@ func Example() { WithToken("") // Get list of seasons... - seasons, err := client.SoccerSeasons().Do() + competitions, err := client.Competitions().Do() if err != nil { panic(err) } - // ...and print them - for _, season := range seasons { - fmt.Println(season.Id, season.Caption) + // ... and print them. + for _, competition := range competitions { + fmt.Println(competition.Id, competition.Caption) } } @@ -34,12 +34,12 @@ func ExampleClient() { // Do something with the client instance... // Here we just fetch the listed soccer seasons on the API - seasons, err := client.SoccerSeasons().Do() + competitions, err := client.Competitions().Do() if err != nil { panic(err) } - for _, season := range seasons { - fmt.Println(season.Id, season.Caption) + for _, competition := range competitions { + fmt.Println(competition.Id, competition.Caption) } } From fca8f382218269b858392785f7ba9f4a5446725c Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Sat, 9 Jul 2016 21:10:14 +0200 Subject: [PATCH 16/25] Fix typo in api_types.go. --- api_types.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api_types.go b/api_types.go index a0bbe98..431aa67 100644 --- a/api_types.go +++ b/api_types.go @@ -44,7 +44,7 @@ type SoccerSeason struct { } // Contains the list of competitions returned by the API. -type CompetitionList []Competitions +type CompetitionList []Competition // Contains information about a competition. type Competition struct { From 0c3d78739e7a7d73c75253ae35581eee3f267e74 Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Sat, 9 Jul 2016 21:26:07 +0200 Subject: [PATCH 17/25] Fix return value of LeagueTable methods. --- api_types.go | 20 ++++++++++++++++++++ req_competition_leaguetable.go | 2 +- req_soccerseason_leaguetable.go | 2 +- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/api_types.go b/api_types.go index 431aa67..bdf5457 100644 --- a/api_types.go +++ b/api_types.go @@ -153,3 +153,23 @@ type Player struct { ContractUntil time.Time MarketValue string } + +// Contains the league table for a season. +type LeagueTable struct { + LeagueCaption string + Matchday uint16 + Standing []TeamLeagueStatistics +} + +// Contains statistical information about a team's performance in a league. +type TeamLeagueStatistics struct { + Rank uint8 + Team string + TeamId uint64 + PlayedGames uint16 + CrestURI string + Points uint16 + Goals uint16 + GoalsAgainst uint16 + GoalDifference uint8 +} diff --git a/req_competition_leaguetable.go b/req_competition_leaguetable.go index db9f046..80714c1 100644 --- a/req_competition_leaguetable.go +++ b/req_competition_leaguetable.go @@ -11,7 +11,7 @@ func (r CompetitionLeagueTableRequest) Matchday(matchday uint16) CompetitionLeag } // Do executes the request. -func (r CompetitionLeagueTableRequest) Do() (s Competition, err error) { +func (r CompetitionLeagueTableRequest) Do() (s LeagueTable, err error) { d, _, err := r.doJson("GET") if err != nil { return diff --git a/req_soccerseason_leaguetable.go b/req_soccerseason_leaguetable.go index 51c31e3..8ea058b 100644 --- a/req_soccerseason_leaguetable.go +++ b/req_soccerseason_leaguetable.go @@ -15,7 +15,7 @@ func (r SoccerSeasonLeagueTableRequest) Matchday(matchday uint16) SoccerSeasonLe // DEPRECATED. // // Do executes the request. -func (r SoccerSeasonLeagueTableRequest) Do() (s SoccerSeason, err error) { +func (r SoccerSeasonLeagueTableRequest) Do() (s LeagueTable, err error) { d, _, err := r.doJson("GET") if err != nil { return From 6eb67ba54119d404a98cda8546d8db80073b013a Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Sat, 9 Jul 2016 21:35:29 +0200 Subject: [PATCH 18/25] Clarification of documentation for "Competitions" and "SoccerSeasons" methods. This commit makes clear that the list methods for the Competition/SoccerSeason resource only return data for the current season or for any targeted season. Fetching the complete list is not possible. --- req_competitions.go | 2 +- req_soccerseasons.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/req_competitions.go b/req_competitions.go index c414f66..f9339db 100644 --- a/req_competitions.go +++ b/req_competitions.go @@ -21,7 +21,7 @@ func (r CompetitionsRequest) Do() (s CompetitionList, err error) { return } -// Competitions prepares a request to fetch the complete list of soccer seasons. +// Competitions prepares a request to fetch the list of competitions for the current season or for any specified season (via the "Season" submethod). func (c *Client) Competitions() CompetitionsRequest { return CompetitionsRequest{c.req("competitions")} } diff --git a/req_soccerseasons.go b/req_soccerseasons.go index bd61a82..5cc2983 100644 --- a/req_soccerseasons.go +++ b/req_soccerseasons.go @@ -29,7 +29,7 @@ func (r SoccerSeasonsRequest) Do() (s SoccerSeasonList, err error) { // DEPRECATED. // -// SoccerSeasons prepares a request to fetch the complete list of soccer seasons. +// SoccerSeasons prepares a request to fetch the list of soccer seasons for the current season or for any specified season (via the "Season" submethod). func (c *Client) SoccerSeasons() SoccerSeasonsRequest { return SoccerSeasonsRequest{c.req("soccerseasons")} } From eeff7fb54b195ebbb52781123f1eb8c7309ac2f0 Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Sat, 9 Jul 2016 21:41:07 +0200 Subject: [PATCH 19/25] Make TeamLeagueStatistics.PlayedGames a uint8 instead of a uint16. --- api_types.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api_types.go b/api_types.go index bdf5457..3a5f709 100644 --- a/api_types.go +++ b/api_types.go @@ -166,7 +166,7 @@ type TeamLeagueStatistics struct { Rank uint8 Team string TeamId uint64 - PlayedGames uint16 + PlayedGames uint8 CrestURI string Points uint16 Goals uint16 From daa2878117469909da4c90fb5a294d1f5ba12df5 Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Sat, 9 Jul 2016 21:41:55 +0200 Subject: [PATCH 20/25] Implement "timeFrameStart" and "timeFrameEnd" filters for fixtures. --- req_competition_fixtures.go | 18 +++++++++++++++++- req_fixtures.go | 18 +++++++++++++++++- util.go | 2 ++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/req_competition_fixtures.go b/req_competition_fixtures.go index ac41957..c255da6 100644 --- a/req_competition_fixtures.go +++ b/req_competition_fixtures.go @@ -13,12 +13,28 @@ func (r CompetitionFixturesRequest) Matchday(matchday uint16) CompetitionFixture return r } -// TimeFrame modifies the request to specify a specific time frame. +// TimeFrame modifies the request to specify a specific relative time frame. func (r CompetitionFixturesRequest) TimeFrame(timeframe time.Duration) CompetitionFixturesRequest { r.urlValues.Set("timeFrame", durationToTimeFrame(timeframe)) return r } +// TimeFrameStart modifies the request to specify the beginning of the time frame filter for the returned results. +// +// Only the year, month and day of the Time value will be used for the request. +func (r CompetitionFixturesRequest) TimeFrameStart(date time.Time) CompetitionFixturesRequest { + r.urlValues.Set("timeFrameStart", date.Format(timeFrameLayout)) + return r +} + +// TimeFrameEnd modifies the request to specify the end of the time frame filter for the returned results. +// +// Only the year, month and day of the Time value will be used for the request. +func (r CompetitionFixturesRequest) TimeFrameStart(date time.Time) CompetitionFixturesRequest { + r.urlValues.Set("timeFrameEnd", date.Format(timeFrameLayout)) + return r +} + // Do executes the request. func (r CompetitionFixturesRequest) Do() (s FixtureList, err error) { d, _, err := r.doJson("GET") diff --git a/req_fixtures.go b/req_fixtures.go index b51af64..54eadbd 100644 --- a/req_fixtures.go +++ b/req_fixtures.go @@ -7,12 +7,28 @@ import ( type FixturesRequest struct{ request } -// TimeFrame modifies the request to specify a specific time frame. +// TimeFrame modifies the request to specify a specific relative time frame. func (r FixturesRequest) TimeFrame(timeframe time.Duration) FixturesRequest { r.urlValues.Set("timeFrame", durationToTimeFrame(timeframe)) return r } +// TimeFrameStart modifies the request to specify the beginning of the time frame filter for the returned results. +// +// Only the year, month and day of the Time value will be used for the request. +func (r FixturesRequest) TimeFrameStart(date time.Time) FixturesRequest { + r.urlValues.Set("timeFrameStart", date.Format(timeFrameLayout)) + return r +} + +// TimeFrameEnd modifies the request to specify the end of the time frame filter for the returned results. +// +// Only the year, month and day of the Time value will be used for the request. +func (r FixturesRequest) TimeFrameStart(date time.Time) FixturesRequest { + r.urlValues.Set("timeFrameEnd", date.Format(timeFrameLayout)) + return r +} + // League modifies the request to specify a list of leagues by their code. func (r FixturesRequest) League(leagueCodes ...string) FixturesRequest { r.urlValues.Set("league", strings.Join(leagueCodes, ",")) diff --git a/util.go b/util.go index 4b42cae..ebd1739 100644 --- a/util.go +++ b/util.go @@ -8,6 +8,8 @@ import ( const ( day = 24 * time.Hour week = 7 * day + + timeFrameLayout = "2006-01-02" ) func durationToTimeFrame(d time.Duration) (r string) { From 1d2adea2d60d8df91d20ac1eddafc3a023a2fea0 Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Sat, 9 Jul 2016 21:46:45 +0200 Subject: [PATCH 21/25] Fix copypasta fail. --- req_competition_fixtures.go | 2 +- req_fixtures.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/req_competition_fixtures.go b/req_competition_fixtures.go index c255da6..c3953ef 100644 --- a/req_competition_fixtures.go +++ b/req_competition_fixtures.go @@ -30,7 +30,7 @@ func (r CompetitionFixturesRequest) TimeFrameStart(date time.Time) CompetitionFi // TimeFrameEnd modifies the request to specify the end of the time frame filter for the returned results. // // Only the year, month and day of the Time value will be used for the request. -func (r CompetitionFixturesRequest) TimeFrameStart(date time.Time) CompetitionFixturesRequest { +func (r CompetitionFixturesRequest) TimeFrameEnd(date time.Time) CompetitionFixturesRequest { r.urlValues.Set("timeFrameEnd", date.Format(timeFrameLayout)) return r } diff --git a/req_fixtures.go b/req_fixtures.go index 54eadbd..6eaecfa 100644 --- a/req_fixtures.go +++ b/req_fixtures.go @@ -24,7 +24,7 @@ func (r FixturesRequest) TimeFrameStart(date time.Time) FixturesRequest { // TimeFrameEnd modifies the request to specify the end of the time frame filter for the returned results. // // Only the year, month and day of the Time value will be used for the request. -func (r FixturesRequest) TimeFrameStart(date time.Time) FixturesRequest { +func (r FixturesRequest) TimeFrameEnd(date time.Time) FixturesRequest { r.urlValues.Set("timeFrameEnd", date.Format(timeFrameLayout)) return r } From b3ec22e7e5681a6b75fe464568494b084560258e Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Sun, 4 Sep 2016 13:28:39 +0200 Subject: [PATCH 22/25] Add two quick Client construction tests. --- client_test.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 client_test.go diff --git a/client_test.go b/client_test.go new file mode 100644 index 0000000..f64989f --- /dev/null +++ b/client_test.go @@ -0,0 +1,20 @@ +package footballdata + +import ( + "net/http" + "testing" + + "github.com/stretchr/testify/require" +) + +func Test_Client_New(t *testing.T) { + client := NewClient(http.DefaultClient) + _, err := client.Competitions().Do() + require.Nil(t, err) +} + +func Test_Client_GoNew(t *testing.T) { + client := new(Client) + _, err := client.Competitions().Do() + require.Nil(t, err) +} From 91b031b34c6018271cabba5f55dd855bca679798 Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Sun, 4 Sep 2016 13:43:15 +0200 Subject: [PATCH 23/25] Travis: Remove invalid "release" Go version and add Go 1.7 for testing. --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 3b95c07..ceea99d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ go: - 1.4.3 - 1.5.4 - 1.6.2 - - release + - 1.7 - tip install: From 17a5b33953d20ca52909cc7a251e8838872be3ee Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Sun, 4 Sep 2016 19:37:03 +0200 Subject: [PATCH 24/25] Send v0.1 in User-Agent request header. --- client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client.go b/client.go index 404bac0..1bffa51 100644 --- a/client.go +++ b/client.go @@ -88,7 +88,7 @@ func (c *Client) doJson(method string, path string, values url.Values) (j *json. req.Header.Set("X-Auth-Token", c.authToken) } req.Header.Set("X-Response-Control", "minified") - req.Header.Set("User-Agent", "go-footballdata/0.0") + req.Header.Set("User-Agent", "go-footballdata/0.1") // Execute request resp, err := c.httpClient.Do(req) From 132fef37953982304061bf5d94194690e766223f Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Sun, 23 Oct 2016 15:11:17 +0200 Subject: [PATCH 25/25] Update TeamLeagueStatistics struct. --- api_types.go | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/api_types.go b/api_types.go index 3a5f709..f74d602 100644 --- a/api_types.go +++ b/api_types.go @@ -163,13 +163,25 @@ type LeagueTable struct { // Contains statistical information about a team's performance in a league. type TeamLeagueStatistics struct { - Rank uint8 - Team string - TeamId uint64 - PlayedGames uint8 CrestURI string - Points uint16 + Draws uint16 + GoalDifference int16 Goals uint16 GoalsAgainst uint16 - GoalDifference uint8 + Losses uint16 + PlayedGames uint8 + Points uint16 + Position uint8 + TeamName string + Wins uint16 + Home ShortTeamLeagueStatistics + Away ShortTeamLeagueStatistics +} + +type ShortTeamLeagueStatistics struct { + Draws uint16 + Goals uint16 + GoalsAgainst uint16 + Losses uint16 + Wins uint16 }