mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-18 19:19: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 (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/go-resty/resty/v2"
|
"github.com/go-resty/resty/v2"
|
||||||
)
|
)
|
||||||
|
@ -36,17 +38,29 @@ type Client struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(serverUrl string, token string) Client {
|
func NewClient(serverUrl string, token string) Client {
|
||||||
resty := resty.New()
|
client := resty.New()
|
||||||
resty.SetBaseURL(serverUrl)
|
client.SetBaseURL(serverUrl)
|
||||||
resty.SetAuthScheme("Bearer")
|
client.SetAuthScheme("Bearer")
|
||||||
resty.SetAuthToken(token)
|
client.SetAuthToken(token)
|
||||||
resty.SetHeader("Accept", "application/json")
|
client.SetHeader("Accept", "application/json")
|
||||||
client := Client{
|
|
||||||
HttpClient: resty,
|
// 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,
|
token: token,
|
||||||
}
|
}
|
||||||
|
|
||||||
return client
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Client) GetHistoryListenings(user string, page int, perPage int) (result ListeningsResult, err error) {
|
func (c Client) GetHistoryListenings(user string, page int, perPage int) (result ListeningsResult, err error) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue