mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-16 10:09:28 +02:00
Funkwhale: Handle rate limit headers
This commit is contained in:
parent
9b5a087974
commit
bef482fd68
1 changed files with 23 additions and 9 deletions
|
@ -23,7 +23,9 @@ package funkwhale
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/go-resty/resty/v2"
|
||||
)
|
||||
|
@ -36,17 +38,29 @@ type Client struct {
|
|||
}
|
||||
|
||||
func NewClient(serverUrl string, token string) Client {
|
||||
resty := resty.New()
|
||||
resty.SetBaseURL(serverUrl)
|
||||
resty.SetAuthScheme("Bearer")
|
||||
resty.SetAuthToken(token)
|
||||
resty.SetHeader("Accept", "application/json")
|
||||
client := Client{
|
||||
HttpClient: resty,
|
||||
client := resty.New()
|
||||
client.SetBaseURL(serverUrl)
|
||||
client.SetAuthScheme("Bearer")
|
||||
client.SetAuthToken(token)
|
||||
client.SetHeader("Accept", "application/json")
|
||||
|
||||
// Handle rate limiting (see https://docs.funkwhale.audio/developer/api/rate-limit.html)
|
||||
client.SetRetryCount(5)
|
||||
client.AddRetryCondition(
|
||||
func(r *resty.Response, err error) bool {
|
||||
return r.StatusCode() == http.StatusTooManyRequests
|
||||
},
|
||||
)
|
||||
client.SetRetryMaxWaitTime(time.Duration(1 * time.Minute))
|
||||
client.SetRetryAfter(func(client *resty.Client, resp *resty.Response) (time.Duration, error) {
|
||||
retryAfter, err := strconv.Atoi(resp.Header().Get("X-RateLimit-Reset-In"))
|
||||
return time.Duration(retryAfter * int(time.Second)), err
|
||||
})
|
||||
|
||||
return Client{
|
||||
HttpClient: client,
|
||||
token: token,
|
||||
}
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
func (c Client) GetHistoryListenings(user string, page int, perPage int) (result ListeningsResult, err error) {
|
||||
|
|
Loading…
Add table
Reference in a new issue