Skip to content

Commit d942e20

Browse files
Add new RiotID routes; Capture RiotIDGameName from MatchV5 (#60)
* added get account by puuid from account v1 api * added get by riot id from account v1 api * fixed fmt and removed files that should not be pushed * added account client test fixed linter * Capture new riot game name * Fixes * Fix log field * implement accountclient functions directly on client * fix test --------- Co-authored-by: Nicolas ROBERT <rbtnicolas57@gmail.com>
1 parent dbe1751 commit d942e20

File tree

8 files changed

+188
-6
lines changed

8 files changed

+188
-6
lines changed

riot/account/account.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package account
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/KnutZuidema/golio/api"
7+
8+
log "github.com/sirupsen/logrus"
9+
)
10+
11+
// GetByPUUID returns the account matching the PUUID
12+
func (ac *Client) GetByPUUID(puuid string) (*Account, error) {
13+
logger := ac.logger().WithField("method", "GetByPUUID")
14+
var account Account
15+
c := *ac.c
16+
c.Region = api.Region(api.RegionToRoute[c.Region])
17+
18+
if err := c.GetInto(
19+
fmt.Sprintf(endpointGetByPUUID, puuid),
20+
&account,
21+
); err != nil {
22+
logger.Debug(err)
23+
return nil, err
24+
}
25+
return &account, nil
26+
}
27+
28+
// GetByRiotID returns the account matching the riot id
29+
func (ac *Client) GetByRiotID(gameName, tagLine string) (*Account, error) {
30+
logger := ac.logger().WithField("method", "GetByRiotID")
31+
var account Account
32+
c := *ac.c
33+
c.Region = api.Region(api.RegionToRoute[c.Region])
34+
35+
if err := c.GetInto(
36+
fmt.Sprintf(endpointGetByRiotID, gameName, tagLine),
37+
&account,
38+
); err != nil {
39+
logger.Debug(err)
40+
return nil, err
41+
}
42+
return &account, nil
43+
}
44+
45+
func (ac *Client) logger() log.FieldLogger {
46+
return ac.c.Logger().WithField("category", "account")
47+
}

riot/account/account_test.go

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package account
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"testing"
7+
8+
"github.com/sirupsen/logrus"
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
12+
"github.com/KnutZuidema/golio/api"
13+
"github.com/KnutZuidema/golio/internal"
14+
"github.com/KnutZuidema/golio/internal/mock"
15+
)
16+
17+
func TestAccountClient_GetByPUUID(t *testing.T) {
18+
t.Parallel()
19+
tests := []struct {
20+
name string
21+
want *Account
22+
doer internal.Doer
23+
wantErr error
24+
}{
25+
{
26+
name: "get response",
27+
want: &Account{},
28+
doer: mock.NewJSONMockDoer(Account{}, 200),
29+
},
30+
{
31+
name: "not found",
32+
wantErr: api.ErrNotFound,
33+
doer: mock.NewStatusMockDoer(http.StatusNotFound),
34+
},
35+
}
36+
for _, tt := range tests {
37+
t.Run(
38+
tt.name, func(t *testing.T) {
39+
client := internal.NewClient(api.RegionEuropeWest, "API_KEY", tt.doer, logrus.StandardLogger())
40+
got, err := (&Client{c: client}).GetByPUUID("")
41+
require.Equal(t, err, tt.wantErr, fmt.Sprintf("want err %v, got %v", tt.wantErr, err))
42+
if tt.wantErr == nil {
43+
assert.Equal(t, got, tt.want)
44+
}
45+
},
46+
)
47+
}
48+
}
49+
50+
func TestAccountClient_GetByRiotID(t *testing.T) {
51+
t.Parallel()
52+
tests := []struct {
53+
name string
54+
want *Account
55+
doer internal.Doer
56+
wantErr error
57+
}{
58+
{
59+
name: "get response",
60+
want: &Account{},
61+
doer: mock.NewJSONMockDoer(Account{}, 200),
62+
},
63+
{
64+
name: "not found",
65+
wantErr: api.ErrNotFound,
66+
doer: mock.NewStatusMockDoer(http.StatusNotFound),
67+
},
68+
}
69+
for _, tt := range tests {
70+
t.Run(
71+
tt.name, func(t *testing.T) {
72+
client := internal.NewClient(api.RegionEuropeWest, "API_KEY", tt.doer, logrus.StandardLogger())
73+
got, err := (&Client{c: client}).GetByRiotID("", "")
74+
require.Equal(t, err, tt.wantErr, fmt.Sprintf("want err %v, got %v", tt.wantErr, err))
75+
if tt.wantErr == nil {
76+
assert.Equal(t, got, tt.want)
77+
}
78+
},
79+
)
80+
}
81+
}

riot/account/client.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package account
2+
3+
import "github.com/KnutZuidema/golio/internal"
4+
5+
// Client pools all methods for endpoints of the League of Legends API.
6+
type Client struct {
7+
c *internal.Client
8+
}
9+
10+
// NewClient returns a new instance of a League of Legends client.
11+
func NewClient(base *internal.Client) *Client {
12+
return &Client{
13+
base,
14+
}
15+
}

riot/account/client_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package account
2+
3+
import (
4+
"testing"
5+
6+
"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+
c := NewClient(internal.NewClient(api.RegionBrasil, "key", mock.NewStatusMockDoer(200), logrus.StandardLogger()))
15+
if c == nil {
16+
t.Error("returned nil")
17+
}
18+
}

riot/account/constants.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package account
2+
3+
const (
4+
endpointBase = "/riot"
5+
endpointAccountBase = endpointBase + "/account/v1"
6+
endpointAccountsBase = endpointAccountBase + "/accounts"
7+
endpointGetByPUUID = endpointAccountsBase + "/by-puuid/%s"
8+
endpointGetByRiotID = endpointAccountsBase + "/by-riot-id/%s/%s"
9+
)

riot/account/model.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package account
2+
3+
// Account contains information about a user account
4+
type Account struct {
5+
Puuid string `json:"puuid"`
6+
GameName string `json:"gameName"`
7+
TagLine string `json:"tagLine"`
8+
}

riot/client.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/KnutZuidema/golio/api"
99
"github.com/KnutZuidema/golio/internal"
10+
"github.com/KnutZuidema/golio/riot/account"
1011
"github.com/KnutZuidema/golio/riot/lol"
1112
"github.com/KnutZuidema/golio/riot/lor"
1213
"github.com/KnutZuidema/golio/riot/val"
@@ -33,18 +34,20 @@ type Client struct {
3334
// Deprecated: Use Client.LoL.Tournament instead. Will be removed in a future release.
3435
Tournament *lol.TournamentClient
3536

36-
LoL *lol.Client
37-
LoR *lor.Client
38-
Val *val.Client
37+
Account *account.Client
38+
LoL *lol.Client
39+
LoR *lor.Client
40+
Val *val.Client
3941
}
4042

4143
// NewClient returns a new api client for the Riot API
4244
func NewClient(region api.Region, apiKey string, client internal.Doer, logger log.FieldLogger) *Client {
4345
baseClient := internal.NewClient(region, apiKey, client, logger)
4446
c := &Client{
45-
LoL: lol.NewClient(baseClient),
46-
LoR: lor.NewClient(baseClient),
47-
Val: val.NewClient(baseClient),
47+
Account: account.NewClient(baseClient),
48+
LoL: lol.NewClient(baseClient),
49+
LoR: lor.NewClient(baseClient),
50+
Val: val.NewClient(baseClient),
4851
}
4952

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

riot/lol/model.go

+1
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ type Participant struct {
301301
ProfileIcon int `json:"profileIcon"`
302302
PUUID string `json:"puuid"`
303303
QuadraKills int `json:"quadraKills"`
304+
RiotIDGameName string `json:"riotIdGameName"`
304305
RiotIDName string `json:"riotIdName"`
305306
RiotIDTagline string `json:"riotIdTagline"`
306307
Role string `json:"role"`

0 commit comments

Comments
 (0)