Skip to content

Commit d0831c6

Browse files
authored
Adding TFT API support (#69)
* TFT, wip * Finsh TFT draft * Add more docs * Fix compilation errors
1 parent d88bcf3 commit d0831c6

18 files changed

+1361
-15
lines changed

internal/client.go

+17-14
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ func NewClient(region api.Region, key string, client Doer, logger log.FieldLogge
4040
}
4141

4242
// GetInto processes a GET request and saves the response body into the given target.
43-
func (c *Client) GetInto(endpoint string, target interface{}) error {
43+
func (c *Client) GetInto(endpoint string, target interface{}, reqOptions ...RequestOption) error {
4444
logger := c.Logger().WithFields(
4545
log.Fields{
4646
"method": "GetInto",
4747
"endpoint": endpoint,
4848
},
4949
)
50-
response, err := c.Get(endpoint)
50+
response, err := c.Get(endpoint, reqOptions...)
5151
if err != nil {
5252
logger.Debug(err)
5353
return err
@@ -60,14 +60,14 @@ func (c *Client) GetInto(endpoint string, target interface{}) error {
6060
}
6161

6262
// PostInto processes a POST request and saves the response body into the given target.
63-
func (c *Client) PostInto(endpoint string, body, target interface{}) error {
63+
func (c *Client) PostInto(endpoint string, body, target interface{}, reqOptions ...RequestOption) error {
6464
logger := c.Logger().WithFields(
6565
log.Fields{
6666
"method": "PostInto",
6767
"endpoint": endpoint,
6868
},
6969
)
70-
response, err := c.Post(endpoint, body)
70+
response, err := c.Post(endpoint, body, reqOptions...)
7171
if err != nil {
7272
logger.Debug(err)
7373
return err
@@ -80,7 +80,7 @@ func (c *Client) PostInto(endpoint string, body, target interface{}) error {
8080
}
8181

8282
// Put processes a PUT request.
83-
func (c *Client) Put(endpoint string, body interface{}) error {
83+
func (c *Client) Put(endpoint string, body interface{}, reqOptions ...RequestOption) error {
8484
logger := c.Logger().WithFields(
8585
log.Fields{
8686
"method": "Put",
@@ -92,17 +92,17 @@ func (c *Client) Put(endpoint string, body interface{}) error {
9292
logger.Debug(err)
9393
return err
9494
}
95-
_, err := c.DoRequest("PUT", endpoint, buf)
95+
_, err := c.DoRequest("PUT", endpoint, buf, reqOptions)
9696
return err
9797
}
9898

9999
// Get processes a GET request.
100-
func (c *Client) Get(endpoint string) (*http.Response, error) {
101-
return c.DoRequest("GET", endpoint, nil)
100+
func (c *Client) Get(endpoint string, reqOptions ...RequestOption) (*http.Response, error) {
101+
return c.DoRequest("GET", endpoint, nil, reqOptions)
102102
}
103103

104104
// Post processes a POST request.
105-
func (c *Client) Post(endpoint string, body interface{}) (*http.Response, error) {
105+
func (c *Client) Post(endpoint string, body interface{}, reqOptions ...RequestOption) (*http.Response, error) {
106106
logger := c.Logger().WithFields(
107107
log.Fields{
108108
"method": "Post",
@@ -114,19 +114,19 @@ func (c *Client) Post(endpoint string, body interface{}) (*http.Response, error)
114114
logger.Debug(err)
115115
return nil, err
116116
}
117-
return c.DoRequest("POST", endpoint, buf)
117+
return c.DoRequest("POST", endpoint, buf, reqOptions)
118118
}
119119

120120
// DoRequest processes a http.Request and returns the response.
121121
// Rate-Limiting and retrying is handled via the corresponding response headers.
122-
func (c *Client) DoRequest(method, endpoint string, body io.Reader) (*http.Response, error) {
122+
func (c *Client) DoRequest(method, endpoint string, body io.Reader, reqOptions []RequestOption) (*http.Response, error) {
123123
logger := c.Logger().WithFields(
124124
log.Fields{
125125
"method": "DoRequest",
126126
"endpoint": endpoint,
127127
},
128128
)
129-
request, err := c.NewRequest(method, endpoint, body)
129+
request, err := c.NewRequest(method, endpoint, body, reqOptions...)
130130
if err != nil {
131131
logger.Debug(err)
132132
return nil, err
@@ -154,7 +154,7 @@ func (c *Client) DoRequest(method, endpoint string, body io.Reader) (*http.Respo
154154
}
155155
logger.Infof("rate limited, waiting %d seconds", seconds)
156156
time.Sleep(time.Duration(seconds) * time.Second)
157-
return c.DoRequest(method, endpoint, body)
157+
return c.DoRequest(method, endpoint, body, reqOptions)
158158
}
159159
if response.StatusCode < 200 || response.StatusCode > 299 {
160160
logger.Debugf("error response: %v", response.Status)
@@ -171,7 +171,7 @@ func (c *Client) DoRequest(method, endpoint string, body io.Reader) (*http.Respo
171171
}
172172

173173
// NewRequest returns a new http.Request with necessary headers et.
174-
func (c *Client) NewRequest(method, endpoint string, body io.Reader) (*http.Request, error) {
174+
func (c *Client) NewRequest(method, endpoint string, body io.Reader, reqOptions ...RequestOption) (*http.Request, error) {
175175
logger := c.Logger().WithFields(
176176
log.Fields{
177177
"method": "NewRequest",
@@ -185,6 +185,9 @@ func (c *Client) NewRequest(method, endpoint string, body io.Reader) (*http.Requ
185185
}
186186
request.Header.Add(apiTokenHeaderKey, c.APIKey)
187187
request.Header.Add("Accept", "application/json")
188+
for _, opt := range reqOptions {
189+
opt(request)
190+
}
188191
return request, nil
189192
}
190193

internal/client_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func TestClient_DoRequest(t *testing.T) {
9393
t.Run(
9494
tt.name, func(t *testing.T) {
9595
c := NewClient(api.RegionEuropeNorthEast, "", tt.doer, logrus.StandardLogger())
96-
_, err := c.DoRequest(tt.args.method, tt.args.endpoint, tt.args.body)
96+
_, err := c.DoRequest(tt.args.method, tt.args.endpoint, tt.args.body, nil)
9797
assert.Equal(t, err != nil, tt.wantErr)
9898
},
9999
)

internal/request_options.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package internal
2+
3+
import (
4+
"net/http"
5+
)
6+
7+
type RequestOption func(r *http.Request)
8+
9+
func WithHeader(key, value string) RequestOption {
10+
return func(r *http.Request) {
11+
r.Header.Add(key, value)
12+
}
13+
}

riot/client.go

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/KnutZuidema/golio/riot/account"
1111
"github.com/KnutZuidema/golio/riot/lol"
1212
"github.com/KnutZuidema/golio/riot/lor"
13+
"github.com/KnutZuidema/golio/riot/tft"
1314
"github.com/KnutZuidema/golio/riot/val"
1415
)
1516

@@ -38,6 +39,7 @@ type Client struct {
3839
LoL *lol.Client
3940
LoR *lor.Client
4041
Val *val.Client
42+
TFT *tft.Client
4143
}
4244

4345
// NewClient returns a new api client for the Riot API
@@ -48,6 +50,7 @@ func NewClient(region api.Region, apiKey string, client internal.Doer, logger lo
4850
LoL: lol.NewClient(baseClient),
4951
LoR: lor.NewClient(baseClient),
5052
Val: val.NewClient(baseClient),
53+
TFT: tft.NewClient(baseClient),
5154
}
5255

5356
// TODO: deprecated, remove in a future release

riot/tft/client.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Package tft allows you to interact with the Teamfight Tactics API
2+
package tft
3+
4+
import "github.com/KnutZuidema/golio/internal"
5+
6+
// Client pools all methods for endpoints of the League of Legends TFT API.
7+
type Client struct {
8+
Spectator *SpectatorClient
9+
League *LeagueClient
10+
Match *MatchClient
11+
Status *StatusClient
12+
Summoner *SummonerClient
13+
}
14+
15+
// NewClient returns a new instance of a League of Legends TFT client.
16+
func NewClient(base *internal.Client) *Client {
17+
return &Client{
18+
Spectator: &SpectatorClient{c: base},
19+
League: &LeagueClient{c: base},
20+
Match: &MatchClient{c: base},
21+
Status: &StatusClient{c: base},
22+
Summoner: &SummonerClient{c: base},
23+
}
24+
}

riot/tft/client_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package tft
2+
3+
import (
4+
"testing"
5+
6+
log "github.com/sirupsen/logrus"
7+
8+
"github.com/KnutZuidema/golio/api"
9+
"github.com/KnutZuidema/golio/internal"
10+
"github.com/KnutZuidema/golio/internal/mock"
11+
)
12+
13+
func TestNewClient(t *testing.T) {
14+
t.Parallel()
15+
c := NewClient(internal.NewClient(api.RegionEuropeNorthEast, "key", mock.NewStatusMockDoer(200), log.StandardLogger()))
16+
if c == nil {
17+
t.Error("returned nil")
18+
}
19+
}

riot/tft/constants.go

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package tft
2+
3+
const (
4+
endpointBase = "/tft"
5+
endpointSpectatorBase = "/lol/spectator" + endpointBase
6+
7+
endpointSpectatorActiveGamedByPUUID = endpointSpectatorBase + "/v5/active-games/by-puuid/%s"
8+
endpointSpectatorFeaturedGames = endpointSpectatorBase + "/v5/featured-games"
9+
10+
endpointLeagueBase = endpointBase + "/league/v1"
11+
endpointLeagueChallenger = endpointLeagueBase + "/challenger?queue=%s"
12+
endpointLeagueEntriesBySummoner = endpointLeagueBase + "/entries/by-summoner/%s"
13+
endpointLeagueEntries = endpointLeagueBase + "/entries/%s/%s"
14+
endpointLeagueGrandMaster = endpointLeagueBase + "/grandmaster?queue=%s"
15+
endpointLeagueLeagues = endpointLeagueBase + "/leagues/%s"
16+
endpointLeagueMaster = endpointLeagueBase + "/master?queue=%s"
17+
endpointLeagueRatedLattersByQueue = endpointLeagueBase + "/rated-ladders/%s/top"
18+
19+
endpointMatchBase = endpointBase + "/match/v1/matches"
20+
endpointMatchesByPUUID = endpointMatchBase + "/by-puuid/%s/ids"
21+
endpointMatchByMatchID = endpointMatchBase + "/%s"
22+
23+
endpointStatusBase = endpointBase + "/status/v1"
24+
endpointStatusPlatformData = endpointStatusBase + "/platform-data"
25+
26+
endpointSummonerBase = endpointBase + "/summoner/v1/summoners"
27+
endpointSummonerByAccount = endpointSummonerBase + "/by-account/%s"
28+
endpointSummonerByPUUID = endpointSummonerBase + "/by-puuid/%s"
29+
endpointSummonerByMe = endpointSummonerBase + "/me"
30+
endpointSummonerBySummonerID = endpointSummonerBase + "/%s"
31+
)
32+
33+
type queue string
34+
35+
const (
36+
QueueRankedTFT queue = "RANKED_TFT"
37+
QueueRankedTFTDoubleUp queue = "RANKED_TFT_DOUBLE_UP"
38+
QueueRankedTFTTurbo queue = "RANKED_TFT_TURBO"
39+
)
40+
41+
type tier string
42+
43+
// All possible Tiers
44+
const (
45+
TierIron tier = "IRON"
46+
TierBronze tier = "BRONZE"
47+
TierSilver tier = "SILVER"
48+
TierGold tier = "GOLD"
49+
TierPlatinum tier = "PLATINUM"
50+
TierEmerald tier = "EMERALD"
51+
TierDiamond tier = "DIAMOND"
52+
TierMaster tier = "MASTER"
53+
TierGrandMaster tier = "GRANDMASTER"
54+
TierChallenger tier = "CHALLENGER"
55+
)
56+
57+
type division string
58+
59+
// All possible divisions
60+
const (
61+
DivisionOne division = "I"
62+
DivisionTwo division = "II"
63+
DivisionThree division = "III"
64+
DivisionFour division = "IV"
65+
)

riot/tft/league.go

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package tft
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/KnutZuidema/golio/internal"
7+
log "github.com/sirupsen/logrus"
8+
)
9+
10+
// LeagueClient provides methods for league endpoints of the League of Legends TFT API.
11+
type LeagueClient struct {
12+
c *internal.Client
13+
}
14+
15+
// GetChallenger returns the current Challenger league for the Region
16+
func (lc *LeagueClient) GetChallenger(queue queue) (*LeagueList, error) {
17+
logger := lc.logger().WithField("method", "GetChallenger")
18+
if queue == "" {
19+
queue = QueueRankedTFT
20+
}
21+
url := fmt.Sprintf(endpointLeagueChallenger, queue)
22+
var out *LeagueList
23+
if err := lc.c.GetInto(url, &out); err != nil {
24+
logger.Debug(err)
25+
return nil, err
26+
}
27+
return out, nil
28+
}
29+
30+
// GetEntriesBySummoner returns league entries for a given summoner ID
31+
func (lc *LeagueClient) GetEntriesBySummoner(summonerID string) ([]*LeagueEntry, error) {
32+
logger := lc.logger().WithField("method", "GetEntriesBySummoner")
33+
url := fmt.Sprintf(endpointLeagueEntriesBySummoner, summonerID)
34+
var out []*LeagueEntry
35+
if err := lc.c.GetInto(url, &out); err != nil {
36+
logger.Debug(err)
37+
return nil, err
38+
}
39+
return out, nil
40+
}
41+
42+
// GetEntries returns all the league entries
43+
func (lc *LeagueClient) GetEntries(tier tier, division division) ([]*LeagueEntry, error) {
44+
logger := lc.logger().WithField("method", "GetEntries")
45+
url := fmt.Sprintf(endpointLeagueEntries, tier, division)
46+
var out []*LeagueEntry
47+
if err := lc.c.GetInto(url, &out); err != nil {
48+
logger.Debug(err)
49+
return nil, err
50+
}
51+
return out, nil
52+
}
53+
54+
// GetGrandMaster returns the current GrandMaster league for the Region
55+
func (lc *LeagueClient) GetGrandMaster(queue queue) (*LeagueList, error) {
56+
logger := lc.logger().WithField("method", "GetGrandMaster")
57+
if queue == "" {
58+
queue = QueueRankedTFT
59+
}
60+
url := fmt.Sprintf(endpointLeagueGrandMaster, queue)
61+
var out *LeagueList
62+
if err := lc.c.GetInto(url, &out); err != nil {
63+
logger.Debug(err)
64+
return nil, err
65+
}
66+
return out, nil
67+
}
68+
69+
// GetLeagues returns league with given ID, including inactive entries
70+
func (lc *LeagueClient) GetLeagues(leagueID string) (*LeagueList, error) {
71+
logger := lc.logger().WithField("method", "GetLeagues")
72+
url := fmt.Sprintf(endpointLeagueLeagues, leagueID)
73+
var out *LeagueList
74+
if err := lc.c.GetInto(url, &out); err != nil {
75+
logger.Debug(err)
76+
return nil, err
77+
}
78+
return out, nil
79+
}
80+
81+
// GetMaster returns the current Master league for the Region
82+
func (lc *LeagueClient) GetMaster(queue queue) (*LeagueList, error) {
83+
logger := lc.logger().WithField("method", "GetMaster")
84+
if queue == "" {
85+
queue = QueueRankedTFT
86+
}
87+
url := fmt.Sprintf(endpointLeagueMaster, queue)
88+
var out *LeagueList
89+
if err := lc.c.GetInto(url, &out); err != nil {
90+
logger.Debug(err)
91+
return nil, err
92+
}
93+
return out, nil
94+
}
95+
96+
// GetRatedLaddersByQueue returns the top rated ladder for given queue
97+
func (lc *LeagueClient) GetRatedLaddersByQueue(queue queue) ([]*TopRatedLadderEntry, error) {
98+
logger := lc.logger().WithField("method", "GetRatedLaddersByQueue")
99+
url := fmt.Sprintf(endpointLeagueRatedLattersByQueue, queue)
100+
var out []*TopRatedLadderEntry
101+
if err := lc.c.GetInto(url, &out); err != nil {
102+
logger.Debug(err)
103+
return nil, err
104+
}
105+
return out, nil
106+
}
107+
108+
func (lc *LeagueClient) logger() log.FieldLogger {
109+
return lc.c.Logger().WithField("category", "league")
110+
}

0 commit comments

Comments
 (0)