1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2025-12-10 22:16:33 +00:00

Compare commits

...

3 commits

Author SHA1 Message Date
Merlin 767721748e
Merge d515b9f16e into 8a78eb0443 2025-04-29 10:48:48 +02:00
ayn2op 8a78eb0443 fix(api): change field name to login 2025-04-28 23:05:05 -07:00
Merlin d515b9f16e sleep on rate limit 2025-03-21 19:58:51 +01:00
3 changed files with 34 additions and 4 deletions

View file

@ -15,12 +15,13 @@ type LoginResponse struct {
Token string `json:"token"`
}
func (c *Client) Login(email, password string) (*LoginResponse, error) {
// login is the user's email or E.164-formatted phone number
func (c *Client) Login(login, password string) (*LoginResponse, error) {
var param struct {
Email string `json:"email"`
Login string `json:"login"`
Password string `json:"password"`
}
param.Email = email
param.Login = login
param.Password = password
var r *LoginResponse

View file

@ -2,3 +2,4 @@ golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/term v0.24.0/go.mod h1:lOBK/LVxemqiMij05LGJ0tzNr8xlmwBRJ81PX6wVLH8=
golang.org/x/term v0.28.0/go.mod h1:Sw/lC2IAUZ92udQNf3WodGtn4k/XoLyZoh8v/8uiwek=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/time v0.9.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=

View file

@ -17,6 +17,10 @@ import (
// StatusTooManyRequests is the HTTP status code discord sends on rate-limiting.
const StatusTooManyRequests = 429
// StatusNirnTimeout is the HTTP status code Nirn Proxy sends when the request
// times out.
const StatusNirnTimeout = 408
// Retries is the default attempts to retry if the API returns an error before
// giving up. If the value is smaller than 1, then requests will retry forever.
var Retries uint = 5
@ -230,10 +234,34 @@ func (c *Client) request(
continue
}
if status = r.GetStatus(); status == StatusTooManyRequests || status >= 500 {
status = r.GetStatus()
if status == StatusNirnTimeout || status >= 500 {
continue
}
if status == StatusTooManyRequests {
var body = r.GetBody()
defer body.Close()
buf := bytes.Buffer{}
buf.ReadFrom(body)
var errBody struct {
RetryAfter float32 `json:"retry_after"`
}
err := json.Unmarshal(buf.Bytes(), &errBody)
if err == nil {
time.Sleep(time.Duration(errBody.RetryAfter) * time.Second)
continue
} else {
doErr = fmt.Errorf("failed to parse rate limit body: %w", err)
continue
}
}
break
}