add api key support + misc corrections
This commit is contained in:
parent
2e1b00f306
commit
cf28f6c8df
4
api.go
4
api.go
@ -97,6 +97,10 @@ func (s *Sorare) Debug() *Sorare {
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Sorare) SetApiKey(apiKey string) {
|
||||
s.Client.SetApiKey(apiKey)
|
||||
}
|
||||
|
||||
func (s *Sorare) SetJWTToken(token graphql.JwtToken, audience string) {
|
||||
s.Client.SetJWTToken(token, audience)
|
||||
}
|
||||
|
@ -27,6 +27,7 @@ type Football struct {
|
||||
Season *graphql.Query[Season, SeasonParams]
|
||||
|
||||
So5 *So5
|
||||
Rivals *Rivals
|
||||
}
|
||||
|
||||
func NewFootball(c *graphql.Client) *Football {
|
||||
@ -117,5 +118,6 @@ func NewFootball(c *graphql.Client) *Football {
|
||||
),
|
||||
|
||||
So5: NewSo5(c),
|
||||
Rivals: NewRivals(c),
|
||||
}
|
||||
}
|
||||
|
11
football/rivals.go
Normal file
11
football/rivals.go
Normal file
@ -0,0 +1,11 @@
|
||||
package football
|
||||
|
||||
import "git.lehouerou.net/laurent/sorare/graphql"
|
||||
|
||||
type Rivals struct {
|
||||
c *graphql.Client
|
||||
}
|
||||
|
||||
func NewRivals(c *graphql.Client) *Rivals {
|
||||
return &Rivals{c: c}
|
||||
}
|
@ -58,9 +58,9 @@ type JwtToken struct {
|
||||
type SignIn struct {
|
||||
CurrentUser struct {
|
||||
Slug string
|
||||
JwtToken JwtToken `graphql:"jwtToken(aud: $aud)"`
|
||||
} `graphql:"currentUser"`
|
||||
OtpSessionChallenge string
|
||||
JwtToken JwtToken `graphql:"jwtToken(aud: $aud)"`
|
||||
Errors []struct {
|
||||
Message string
|
||||
} `graphql:"errors"`
|
||||
@ -116,12 +116,12 @@ func (c *Client) getNewToken(
|
||||
}
|
||||
|
||||
if len(res.Errors) == 0 {
|
||||
if res.CurrentUser.JwtToken.Token == "" {
|
||||
if res.JwtToken.Token == "" {
|
||||
return JwtToken{}, errors.New(
|
||||
"no errors & no jwt token returned on email/password mutation",
|
||||
)
|
||||
}
|
||||
return res.CurrentUser.JwtToken, nil
|
||||
return res.JwtToken, nil
|
||||
}
|
||||
|
||||
if res.Errors[0].Message == "invalid" {
|
||||
@ -146,10 +146,10 @@ func (c *Client) getNewToken(
|
||||
}
|
||||
|
||||
if len(resOtp.Errors) == 0 {
|
||||
if resOtp.CurrentUser.JwtToken.Token == "" {
|
||||
if resOtp.JwtToken.Token == "" {
|
||||
return JwtToken{}, errors.New("no errors & no jwt token returned on otp mutation")
|
||||
}
|
||||
return resOtp.CurrentUser.JwtToken, nil
|
||||
return resOtp.JwtToken, nil
|
||||
}
|
||||
return JwtToken{}, errors.Errorf("validating otp: %s", resOtp.Errors[0].Message)
|
||||
}
|
||||
|
@ -22,9 +22,11 @@ type Client struct {
|
||||
httpClient *http.Client
|
||||
gql *graphql.Client
|
||||
rl *rate.Limiter
|
||||
apirl *rate.Limiter
|
||||
lock *sync.Mutex
|
||||
authenticated bool
|
||||
token JwtToken
|
||||
apiKey string
|
||||
}
|
||||
|
||||
func NewClient(httpclient *http.Client, baseUrl string) *Client {
|
||||
@ -32,6 +34,7 @@ func NewClient(httpclient *http.Client, baseUrl string) *Client {
|
||||
httpClient: httpclient,
|
||||
gql: graphql.NewClient(baseUrl, httpclient),
|
||||
rl: rate.NewLimiter(rate.Every(rateLimitPeriod), rateLimitBurst),
|
||||
apirl: rate.NewLimiter(rate.Every(rateLimitPeriod/10), rateLimitBurst*10),
|
||||
lock: &sync.Mutex{},
|
||||
authenticated: false,
|
||||
}
|
||||
@ -50,6 +53,13 @@ func (c *Client) SetJWTToken(token JwtToken, audience string) {
|
||||
c.authenticated = true
|
||||
}
|
||||
|
||||
func (c *Client) SetApiKey(apiKey string) {
|
||||
c.gql = c.gql.WithRequestModifier(func(request *http.Request) {
|
||||
request.Header.Set("APIKEY", apiKey)
|
||||
})
|
||||
c.apiKey = apiKey
|
||||
}
|
||||
|
||||
func (c *Client) MaxComplexity() int {
|
||||
if c.authenticated {
|
||||
return MaxAuthenticatedQueryComplexity
|
||||
@ -71,10 +81,17 @@ func (c *Client) Query(
|
||||
variables interface{},
|
||||
options ...graphql.Option,
|
||||
) error {
|
||||
if c.apiKey != "" {
|
||||
err := c.apirl.Wait(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "waiting for rate limit")
|
||||
}
|
||||
} else {
|
||||
err := c.rl.Wait(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "waiting for rate limit")
|
||||
}
|
||||
}
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
return c.gql.Query(ctx, q, variables, options...)
|
||||
@ -86,10 +103,17 @@ func (c *Client) QueryRaw(
|
||||
variables interface{},
|
||||
options ...graphql.Option,
|
||||
) ([]byte, error) {
|
||||
if c.apiKey != "" {
|
||||
err := c.apirl.Wait(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "waiting for rate limit")
|
||||
}
|
||||
} else {
|
||||
err := c.rl.Wait(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "waiting for rate limit")
|
||||
}
|
||||
}
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
return c.gql.QueryRaw(ctx, q, variables, options...)
|
||||
@ -101,10 +125,17 @@ func (c *Client) Mutate(
|
||||
variables interface{},
|
||||
options ...graphql.Option,
|
||||
) error {
|
||||
if c.apiKey != "" {
|
||||
err := c.apirl.Wait(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "waiting for rate limit")
|
||||
}
|
||||
} else {
|
||||
err := c.rl.Wait(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "waiting for rate limit")
|
||||
}
|
||||
}
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
return c.gql.Mutate(ctx, q, variables, options...)
|
||||
|
4
user.go
4
user.go
@ -13,7 +13,7 @@ import (
|
||||
|
||||
type baseUser struct {
|
||||
Active bool `graphql:"active"`
|
||||
CardCounts struct {
|
||||
FootballCardCounts struct {
|
||||
Common int `graphql:"common"`
|
||||
CustomSeries int `graphql:"customSeries"`
|
||||
Limited int `graphql:"limited"`
|
||||
@ -21,7 +21,7 @@ type baseUser struct {
|
||||
SuperRare int `graphql:"superRare"`
|
||||
Unique int `graphql:"unique"`
|
||||
Total int `graphql:"total"`
|
||||
} `graphql:"cardCounts"`
|
||||
} `graphql:"footballCardCounts"`
|
||||
CreatedAt time.Time `graphql:"createdAt"`
|
||||
EthVaultId int `graphql:"ethVaultId"`
|
||||
EthereumAddress string `graphql:"ethereumAddress"`
|
||||
|
Loading…
Reference in New Issue
Block a user