Skip to content

Commit c505d32

Browse files
committed
refactor(coachless): centralize endpoints
1 parent fddd15d commit c505d32

3 files changed

Lines changed: 34 additions & 40 deletions

File tree

internal/auth/source.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/chromedp/chromedp"
1515

1616
"github.com/controlado/lol-autobuild/internal/autobuild/domain"
17+
"github.com/controlado/lol-autobuild/internal/coachless"
1718
)
1819

1920
var ErrAccessTokenUnavailable = errors.New("unable to acquire valid access token")
@@ -75,7 +76,7 @@ func (s BrowserSource) Acquire(ctx context.Context) (domain.TokenPair, error) {
7576
chromedp.ListenTarget(ctx, func(ev any) {
7677
switch e := ev.(type) {
7778
case *network.EventResponseReceived:
78-
if e.Response == nil || !strings.Contains(e.Response.URL, "/api/Auth/login") {
79+
if e.Response == nil || !strings.Contains(e.Response.URL, coachless.AuthLoginPath) {
7980
return
8081
}
8182
go onAuthResponse(e)

internal/coachless/client.go

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,11 @@ func (c *Client) Refresh(ctx context.Context, refreshToken string) (domain.Token
3636
}
3737

3838
var (
39-
endpoint = "/api/Auth/refresh"
40-
reqBody = map[string]string{"refreshToken": refreshToken}
41-
out apiRefreshResponse
39+
reqBody = map[string]string{"refreshToken": refreshToken}
40+
out apiRefreshResponse
4241
)
4342

44-
if err := c.doJSON(ctx, http.MethodPost, endpoint, "", reqBody, &out); err != nil {
43+
if err := c.doJSON(ctx, http.MethodPost, authRefreshPath, "", reqBody, &out); err != nil {
4544
return domain.TokenPair{}, err
4645
}
4746

@@ -58,90 +57,69 @@ func (c *Client) Refresh(ctx context.Context, refreshToken string) (domain.Token
5857
}
5958

6059
func (c *Client) GetPatches(ctx context.Context, accessToken string) ([]domain.PatchInfo, error) {
61-
var (
62-
endpoint = "/api/ChampionWinprob/GetPatches"
63-
out []apiPatchInfo
64-
)
60+
var out []apiPatchInfo
6561

66-
if err := c.doJSON(ctx, http.MethodGet, endpoint, accessToken, nil, &out); err != nil {
62+
if err := c.doJSON(ctx, http.MethodGet, championWinprobPatchesPath, accessToken, nil, &out); err != nil {
6763
return nil, err
6864
}
6965

7066
return patchInfosFromAPI(out), nil
7167
}
7268

7369
func (c *Client) GetKeystoneData(ctx context.Context, accessToken string, req domain.KeystoneRequest) ([]domain.KeystoneStat, error) {
74-
var (
75-
endpoint = "/api/Rune/GetKeystoneData"
76-
out []apiKeystoneStat
77-
)
70+
var out []apiKeystoneStat
7871

79-
if err := c.doJSON(ctx, http.MethodPost, endpoint, accessToken, apiKeystoneRequestFromDomain(req), &out); err != nil {
72+
if err := c.doJSON(ctx, http.MethodPost, runeKeystoneDataPath, accessToken, apiKeystoneRequestFromDomain(req), &out); err != nil {
8073
return nil, err
8174
}
8275

8376
return keystoneStatsFromAPI(out), nil
8477
}
8578

8679
func (c *Client) GetSecondaryTreePlaycount(ctx context.Context, accessToken string, req domain.SecondaryTreePlaycountRequest) ([]domain.RuneTreePlaycount, error) {
87-
var (
88-
endpoint = "/api/Rune/GetSecondaryTreePlaycount"
89-
out []apiRuneTreePlaycount
90-
)
80+
var out []apiRuneTreePlaycount
9181

92-
if err := c.doJSON(ctx, http.MethodPost, endpoint, accessToken, apiSecondaryTreePlaycountRequestFromDomain(req), &out); err != nil {
82+
if err := c.doJSON(ctx, http.MethodPost, runeSecondaryTreePlaycountPath, accessToken, apiSecondaryTreePlaycountRequestFromDomain(req), &out); err != nil {
9383
return nil, err
9484
}
9585

9686
return runeTreePlaycountsFromAPI(out), nil
9787
}
9888

9989
func (c *Client) GetRuneStatsForKeystoneAndTree(ctx context.Context, accessToken string, req domain.RuneStatsRequest) (domain.RuneStatsByRow, error) {
100-
var (
101-
endpoint = "/api/Rune/GetRunesForKeystoneAndTree"
102-
out apiRuneStatsByRow
103-
)
90+
var out apiRuneStatsByRow
10491

105-
if err := c.doJSON(ctx, http.MethodPost, endpoint, accessToken, apiRuneStatsRequestFromDomain(req), &out); err != nil {
92+
if err := c.doJSON(ctx, http.MethodPost, runeStatsForKeystoneAndTreePath, accessToken, apiRuneStatsRequestFromDomain(req), &out); err != nil {
10693
return domain.RuneStatsByRow{}, err
10794
}
10895

10996
return runeStatsByRowFromAPI(out), nil
11097
}
11198

11299
func (c *Client) GetShardStatsForKeystoneAndTree(ctx context.Context, accessToken string, req domain.ShardStatsRequest) (domain.ShardStats, error) {
113-
var (
114-
endpoint = "/api/Rune/GetShardsForKeystoneAndTree"
115-
out apiShardStats
116-
)
100+
var out apiShardStats
117101

118-
if err := c.doJSON(ctx, http.MethodPost, endpoint, accessToken, apiShardStatsRequestFromDomain(req), &out); err != nil {
102+
if err := c.doJSON(ctx, http.MethodPost, runeShardsForKeystoneAndTreePath, accessToken, apiShardStatsRequestFromDomain(req), &out); err != nil {
119103
return domain.ShardStats{}, err
120104
}
121105

122106
return shardStatsFromAPI(out), nil
123107
}
124108

125109
func (c *Client) GetSummonerSpellStats(ctx context.Context, accessToken string, req domain.SummonerSpellStatsRequest) ([]domain.SummonerSpellStat, error) {
126-
var (
127-
out []apiSummonerSpellStat
128-
endpoint = "/api/ChampionWinprob/GetGlobalSummonerSpellStatistics"
129-
)
110+
var out []apiSummonerSpellStat
130111

131-
if err := c.doJSON(ctx, http.MethodPost, endpoint, accessToken, apiSummonerSpellStatsRequestFromDomain(req), &out); err != nil {
112+
if err := c.doJSON(ctx, http.MethodPost, championWinprobSummonerSpellStatsPath, accessToken, apiSummonerSpellStatsRequestFromDomain(req), &out); err != nil {
132113
return nil, err
133114
}
134115

135116
return summonerSpellStatsFromAPI(out), nil
136117
}
137118

138119
func (c *Client) GetItemStats(ctx context.Context, accessToken string, req domain.ItemStatsRequest) ([]domain.ItemStat, error) {
139-
var (
140-
out []apiItemStat
141-
endpoint = "/api/ChampionWinprob/GetGlobalItemStatistics"
142-
)
120+
var out []apiItemStat
143121

144-
if err := c.doJSON(ctx, http.MethodPost, endpoint, accessToken, apiItemStatsRequestFromDomain(req), &out); err != nil {
122+
if err := c.doJSON(ctx, http.MethodPost, championWinprobItemStatsPath, accessToken, apiItemStatsRequestFromDomain(req), &out); err != nil {
145123
return nil, err
146124
}
147125

internal/coachless/endpoints.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package coachless
2+
3+
const (
4+
AuthLoginPath = "/api/Auth/login"
5+
authRefreshPath = "/api/Auth/refresh"
6+
7+
championWinprobPatchesPath = "/api/ChampionWinprob/GetPatches"
8+
championWinprobSummonerSpellStatsPath = "/api/ChampionWinprob/GetGlobalSummonerSpellStatistics"
9+
championWinprobItemStatsPath = "/api/ChampionWinprob/GetGlobalItemStatistics"
10+
11+
runeKeystoneDataPath = "/api/Rune/GetKeystoneData"
12+
runeSecondaryTreePlaycountPath = "/api/Rune/GetSecondaryTreePlaycount"
13+
runeStatsForKeystoneAndTreePath = "/api/Rune/GetRunesForKeystoneAndTree"
14+
runeShardsForKeystoneAndTreePath = "/api/Rune/GetShardsForKeystoneAndTree"
15+
)

0 commit comments

Comments
 (0)