Add retries to Twitch API calls
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
95e802706f
commit
5a47332258
2 changed files with 40 additions and 46 deletions
5
irc.go
5
irc.go
|
@ -5,17 +5,12 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/go-irc/irc"
|
"github.com/go-irc/irc"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
twitchRequestTimeout = 2 * time.Second
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
badgeBroadcaster = "broadcaster"
|
badgeBroadcaster = "broadcaster"
|
||||||
badgeFounder = "founder"
|
badgeFounder = "founder"
|
||||||
|
|
81
twitch.go
81
twitch.go
|
@ -8,11 +8,17 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/Luzifer/go_helpers/v2/backoff"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
const timeDay = 24 * time.Hour
|
const (
|
||||||
|
timeDay = 24 * time.Hour
|
||||||
|
|
||||||
|
twitchRequestRetries = 5
|
||||||
|
twitchRequestTimeout = 2 * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
var twitch = newTwitchClient()
|
var twitch = newTwitchClient()
|
||||||
|
|
||||||
|
@ -27,9 +33,6 @@ func newTwitchClient() *twitchClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t twitchClient) getAuthorizedUsername() (string, error) {
|
func (t twitchClient) getAuthorizedUsername() (string, error) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), twitchRequestTimeout)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
var payload struct {
|
var payload struct {
|
||||||
Data []struct {
|
Data []struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
|
@ -37,7 +40,13 @@ func (t twitchClient) getAuthorizedUsername() (string, error) {
|
||||||
} `json:"data"`
|
} `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := t.request(ctx, http.MethodGet, "https://api.twitch.tv/helix/users", nil, &payload); err != nil {
|
if err := t.request(
|
||||||
|
context.Background(),
|
||||||
|
http.MethodGet,
|
||||||
|
"https://api.twitch.tv/helix/users",
|
||||||
|
nil,
|
||||||
|
&payload,
|
||||||
|
); err != nil {
|
||||||
return "", errors.Wrap(err, "request channel info")
|
return "", errors.Wrap(err, "request channel info")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,9 +63,6 @@ func (t twitchClient) GetDisplayNameForUser(username string) (string, error) {
|
||||||
return d.(string), nil
|
return d.(string), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), twitchRequestTimeout)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
var payload struct {
|
var payload struct {
|
||||||
Data []struct {
|
Data []struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
|
@ -66,7 +72,7 @@ func (t twitchClient) GetDisplayNameForUser(username string) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := t.request(
|
if err := t.request(
|
||||||
ctx,
|
context.Background(),
|
||||||
http.MethodGet,
|
http.MethodGet,
|
||||||
fmt.Sprintf("https://api.twitch.tv/helix/users?login=%s", username),
|
fmt.Sprintf("https://api.twitch.tv/helix/users?login=%s", username),
|
||||||
nil,
|
nil,
|
||||||
|
@ -100,9 +106,6 @@ func (t twitchClient) GetFollowDate(from, to string) (time.Time, error) {
|
||||||
return time.Time{}, errors.Wrap(err, "getting id for 'to' user")
|
return time.Time{}, errors.Wrap(err, "getting id for 'to' user")
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), twitchRequestTimeout)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
var payload struct {
|
var payload struct {
|
||||||
Data []struct {
|
Data []struct {
|
||||||
FollowedAt time.Time `json:"followed_at"`
|
FollowedAt time.Time `json:"followed_at"`
|
||||||
|
@ -110,7 +113,7 @@ func (t twitchClient) GetFollowDate(from, to string) (time.Time, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := t.request(
|
if err := t.request(
|
||||||
ctx,
|
context.Background(),
|
||||||
http.MethodGet,
|
http.MethodGet,
|
||||||
fmt.Sprintf("https://api.twitch.tv/helix/users/follows?to_id=%s&from_id=%s", toID, fromID),
|
fmt.Sprintf("https://api.twitch.tv/helix/users/follows?to_id=%s&from_id=%s", toID, fromID),
|
||||||
nil,
|
nil,
|
||||||
|
@ -135,9 +138,6 @@ func (t twitchClient) HasLiveStream(username string) (bool, error) {
|
||||||
return d.(bool), nil
|
return d.(bool), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), twitchRequestTimeout)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
var payload struct {
|
var payload struct {
|
||||||
Data []struct {
|
Data []struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
|
@ -147,7 +147,7 @@ func (t twitchClient) HasLiveStream(username string) (bool, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := t.request(
|
if err := t.request(
|
||||||
ctx,
|
context.Background(),
|
||||||
http.MethodGet,
|
http.MethodGet,
|
||||||
fmt.Sprintf("https://api.twitch.tv/helix/streams?user_login=%s", username),
|
fmt.Sprintf("https://api.twitch.tv/helix/streams?user_login=%s", username),
|
||||||
nil,
|
nil,
|
||||||
|
@ -168,9 +168,6 @@ func (t twitchClient) getIDForUsername(username string) (string, error) {
|
||||||
return d.(string), nil
|
return d.(string), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), twitchRequestTimeout)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
var payload struct {
|
var payload struct {
|
||||||
Data []struct {
|
Data []struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
|
@ -179,7 +176,7 @@ func (t twitchClient) getIDForUsername(username string) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := t.request(
|
if err := t.request(
|
||||||
ctx,
|
context.Background(),
|
||||||
http.MethodGet,
|
http.MethodGet,
|
||||||
fmt.Sprintf("https://api.twitch.tv/helix/users?login=%s", username),
|
fmt.Sprintf("https://api.twitch.tv/helix/users?login=%s", username),
|
||||||
nil,
|
nil,
|
||||||
|
@ -204,9 +201,6 @@ func (t twitchClient) GetRecentStreamInfo(username string) (string, string, erro
|
||||||
return d.([2]string)[0], d.([2]string)[1], nil
|
return d.([2]string)[0], d.([2]string)[1], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), twitchRequestTimeout)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
id, err := t.getIDForUsername(username)
|
id, err := t.getIDForUsername(username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", errors.Wrap(err, "getting ID for username")
|
return "", "", errors.Wrap(err, "getting ID for username")
|
||||||
|
@ -222,7 +216,7 @@ func (t twitchClient) GetRecentStreamInfo(username string) (string, string, erro
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := t.request(
|
if err := t.request(
|
||||||
ctx,
|
context.Background(),
|
||||||
http.MethodGet,
|
http.MethodGet,
|
||||||
fmt.Sprintf("https://api.twitch.tv/helix/channels?broadcaster_id=%s", id),
|
fmt.Sprintf("https://api.twitch.tv/helix/channels?broadcaster_id=%s", id),
|
||||||
nil,
|
nil,
|
||||||
|
@ -247,22 +241,27 @@ func (twitchClient) request(ctx context.Context, method, url string, body io.Rea
|
||||||
"url": url,
|
"url": url,
|
||||||
}).Trace("Execute Twitch API request")
|
}).Trace("Execute Twitch API request")
|
||||||
|
|
||||||
req, err := http.NewRequestWithContext(ctx, method, url, body)
|
return backoff.NewBackoff().WithMaxIterations(twitchRequestRetries).Retry(func() error {
|
||||||
if err != nil {
|
reqCtx, cancel := context.WithTimeout(ctx, twitchRequestTimeout)
|
||||||
return errors.Wrap(err, "assemble request")
|
defer cancel()
|
||||||
}
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
|
||||||
req.Header.Set("Client-Id", cfg.TwitchClient)
|
|
||||||
req.Header.Set("Authorization", "Bearer "+cfg.TwitchToken)
|
|
||||||
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
req, err := http.NewRequestWithContext(reqCtx, method, url, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "execute request")
|
return errors.Wrap(err, "assemble request")
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
req.Header.Set("Client-Id", cfg.TwitchClient)
|
||||||
|
req.Header.Set("Authorization", "Bearer "+cfg.TwitchToken)
|
||||||
|
|
||||||
return errors.Wrap(
|
resp, err := http.DefaultClient.Do(req)
|
||||||
json.NewDecoder(resp.Body).Decode(out),
|
if err != nil {
|
||||||
"parse user info",
|
return errors.Wrap(err, "execute request")
|
||||||
)
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
return errors.Wrap(
|
||||||
|
json.NewDecoder(resp.Body).Decode(out),
|
||||||
|
"parse user info",
|
||||||
|
)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue