[core] Export Twitch client Request

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2023-07-01 16:48:21 +02:00
parent f5f8feb730
commit fc93c6e63b
Signed by: luzifer
GPG key ID: D91C3E91E4CAD6F5
12 changed files with 71 additions and 71 deletions

View file

@ -23,8 +23,8 @@ func (c *Client) AddChannelVIP(ctx context.Context, broadcasterName, userName st
} }
return errors.Wrap( return errors.Wrap(
c.request(clientRequestOpts{ c.Request(ClientRequestOpts{
AuthType: authTypeBearerToken, AuthType: AuthTypeBearerToken,
Context: ctx, Context: ctx,
Method: http.MethodPost, Method: http.MethodPost,
OKStatus: http.StatusNoContent, OKStatus: http.StatusNoContent,
@ -96,8 +96,8 @@ func (c *Client) ModifyChannelInformation(ctx context.Context, broadcasterName s
} }
return errors.Wrap( return errors.Wrap(
c.request(clientRequestOpts{ c.Request(ClientRequestOpts{
AuthType: authTypeBearerToken, AuthType: AuthTypeBearerToken,
Body: body, Body: body,
Context: ctx, Context: ctx,
Method: http.MethodPatch, Method: http.MethodPatch,
@ -120,8 +120,8 @@ func (c *Client) RemoveChannelVIP(ctx context.Context, broadcasterName, userName
} }
return errors.Wrap( return errors.Wrap(
c.request(clientRequestOpts{ c.Request(ClientRequestOpts{
AuthType: authTypeBearerToken, AuthType: AuthTypeBearerToken,
Context: ctx, Context: ctx,
Method: http.MethodDelete, Method: http.MethodDelete,
OKStatus: http.StatusNoContent, OKStatus: http.StatusNoContent,
@ -152,8 +152,8 @@ func (c *Client) RunCommercial(ctx context.Context, channel string, duration int
} }
return errors.Wrap( return errors.Wrap(
c.request(clientRequestOpts{ c.Request(ClientRequestOpts{
AuthType: authTypeBearerToken, AuthType: AuthTypeBearerToken,
Body: body, Body: body,
Context: ctx, Context: ctx,
Method: http.MethodPost, Method: http.MethodPost,

View file

@ -40,8 +40,8 @@ func (c *Client) SendChatAnnouncement(channel, color, message string) error {
} }
return errors.Wrap( return errors.Wrap(
c.request(clientRequestOpts{ c.Request(ClientRequestOpts{
AuthType: authTypeBearerToken, AuthType: AuthTypeBearerToken,
Context: context.Background(), Context: context.Background(),
Method: http.MethodPost, Method: http.MethodPost,
OKStatus: http.StatusNoContent, OKStatus: http.StatusNoContent,
@ -79,8 +79,8 @@ func (c *Client) SendShoutout(channel, user string) error {
params.Set("to_broadcaster_id", userID) params.Set("to_broadcaster_id", userID)
return errors.Wrap( return errors.Wrap(
c.request(clientRequestOpts{ c.Request(ClientRequestOpts{
AuthType: authTypeBearerToken, AuthType: AuthTypeBearerToken,
Context: context.Background(), Context: context.Background(),
Method: http.MethodPost, Method: http.MethodPost,
OKStatus: http.StatusNoContent, OKStatus: http.StatusNoContent,

View file

@ -44,8 +44,8 @@ func (c *Client) GetClipByID(ctx context.Context, clipID string) (ClipInfo, erro
Data []ClipInfo Data []ClipInfo
} }
if err := c.request(clientRequestOpts{ if err := c.Request(ClientRequestOpts{
AuthType: authTypeAppAccessToken, AuthType: AuthTypeAppAccessToken,
Context: ctx, Context: ctx,
Method: http.MethodGet, Method: http.MethodGet,
OKStatus: http.StatusOK, OKStatus: http.StatusOK,

View file

@ -212,14 +212,14 @@ func (e EventSubCondition) Hash() (string, error) {
} }
func (c *Client) createEventSubSubscriptionWebhook(ctx context.Context, sub eventSubSubscription) (*eventSubSubscription, error) { func (c *Client) createEventSubSubscriptionWebhook(ctx context.Context, sub eventSubSubscription) (*eventSubSubscription, error) {
return c.createEventSubSubscription(ctx, authTypeAppAccessToken, sub) return c.createEventSubSubscription(ctx, AuthTypeAppAccessToken, sub)
} }
func (c *Client) createEventSubSubscriptionWebsocket(ctx context.Context, sub eventSubSubscription) (*eventSubSubscription, error) { func (c *Client) createEventSubSubscriptionWebsocket(ctx context.Context, sub eventSubSubscription) (*eventSubSubscription, error) {
return c.createEventSubSubscription(ctx, authTypeBearerToken, sub) return c.createEventSubSubscription(ctx, AuthTypeBearerToken, sub)
} }
func (c *Client) createEventSubSubscription(ctx context.Context, auth authType, sub eventSubSubscription) (*eventSubSubscription, error) { func (c *Client) createEventSubSubscription(ctx context.Context, auth AuthType, sub eventSubSubscription) (*eventSubSubscription, error) {
var ( var (
buf = new(bytes.Buffer) buf = new(bytes.Buffer)
resp struct { resp struct {
@ -235,7 +235,7 @@ func (c *Client) createEventSubSubscription(ctx context.Context, auth authType,
return nil, errors.Wrap(err, "assemble subscribe payload") return nil, errors.Wrap(err, "assemble subscribe payload")
} }
if err := c.request(clientRequestOpts{ if err := c.Request(ClientRequestOpts{
AuthType: auth, AuthType: auth,
Body: buf, Body: buf,
Context: ctx, Context: ctx,
@ -251,8 +251,8 @@ func (c *Client) createEventSubSubscription(ctx context.Context, auth authType,
} }
func (c *Client) deleteEventSubSubscription(ctx context.Context, id string) error { func (c *Client) deleteEventSubSubscription(ctx context.Context, id string) error {
return errors.Wrap(c.request(clientRequestOpts{ return errors.Wrap(c.Request(ClientRequestOpts{
AuthType: authTypeAppAccessToken, AuthType: AuthTypeAppAccessToken,
Context: ctx, Context: ctx,
Method: http.MethodDelete, Method: http.MethodDelete,
OKStatus: http.StatusNoContent, OKStatus: http.StatusNoContent,
@ -278,8 +278,8 @@ func (c *Client) getEventSubSubscriptions(ctx context.Context) ([]eventSubSubscr
) )
for { for {
if err := c.request(clientRequestOpts{ if err := c.Request(ClientRequestOpts{
AuthType: authTypeAppAccessToken, AuthType: AuthTypeAppAccessToken,
Context: ctx, Context: ctx,
Method: http.MethodGet, Method: http.MethodGet,
OKStatus: http.StatusOK, OKStatus: http.StatusOK,

View file

@ -55,8 +55,8 @@ func (c *Client) BanUser(channel, username string, duration time.Duration, reaso
} }
return errors.Wrap( return errors.Wrap(
c.request(clientRequestOpts{ c.Request(ClientRequestOpts{
AuthType: authTypeBearerToken, AuthType: AuthTypeBearerToken,
Context: context.Background(), Context: context.Background(),
Method: http.MethodPost, Method: http.MethodPost,
OKStatus: http.StatusOK, OKStatus: http.StatusOK,
@ -93,8 +93,8 @@ func (c *Client) DeleteMessage(channel, messageID string) error {
} }
return errors.Wrap( return errors.Wrap(
c.request(clientRequestOpts{ c.Request(ClientRequestOpts{
AuthType: authTypeBearerToken, AuthType: AuthTypeBearerToken,
Context: context.Background(), Context: context.Background(),
Method: http.MethodDelete, Method: http.MethodDelete,
OKStatus: http.StatusNoContent, OKStatus: http.StatusNoContent,
@ -125,8 +125,8 @@ func (c *Client) UnbanUser(channel, username string) error {
} }
return errors.Wrap( return errors.Wrap(
c.request(clientRequestOpts{ c.Request(ClientRequestOpts{
AuthType: authTypeBearerToken, AuthType: AuthTypeBearerToken,
Context: context.Background(), Context: context.Background(),
Method: http.MethodDelete, Method: http.MethodDelete,
OKStatus: http.StatusNoContent, OKStatus: http.StatusNoContent,
@ -159,8 +159,8 @@ func (c *Client) UpdateShieldMode(ctx context.Context, channel string, enable bo
} }
return errors.Wrap( return errors.Wrap(
c.request(clientRequestOpts{ c.Request(ClientRequestOpts{
AuthType: authTypeBearerToken, AuthType: AuthTypeBearerToken,
Context: ctx, Context: ctx,
Method: http.MethodPut, Method: http.MethodPut,
OKStatus: http.StatusOK, OKStatus: http.StatusOK,

View file

@ -48,8 +48,8 @@ func (c *Client) GetLatestPoll(ctx context.Context, username string) (*PollInfo,
Data []*PollInfo `json:"data"` Data []*PollInfo `json:"data"`
} }
if err := c.request(clientRequestOpts{ if err := c.Request(ClientRequestOpts{
AuthType: authTypeBearerToken, AuthType: AuthTypeBearerToken,
Context: ctx, Context: ctx,
Method: http.MethodGet, Method: http.MethodGet,
OKStatus: http.StatusOK, OKStatus: http.StatusOK,

View file

@ -32,8 +32,8 @@ func (c *Client) SearchCategories(ctx context.Context, name string) ([]Category,
} }
for { for {
if err := c.request(clientRequestOpts{ if err := c.Request(ClientRequestOpts{
AuthType: authTypeBearerToken, AuthType: AuthTypeBearerToken,
Context: ctx, Context: ctx,
Method: http.MethodGet, Method: http.MethodGet,
OKStatus: http.StatusOK, OKStatus: http.StatusOK,

View file

@ -43,8 +43,8 @@ func (c *Client) GetCurrentStreamInfo(username string) (*StreamInfo, error) {
Data []*StreamInfo `json:"data"` Data []*StreamInfo `json:"data"`
} }
if err := c.request(clientRequestOpts{ if err := c.Request(ClientRequestOpts{
AuthType: authTypeBearerToken, AuthType: AuthTypeBearerToken,
Context: context.Background(), Context: context.Background(),
Method: http.MethodGet, Method: http.MethodGet,
OKStatus: http.StatusOK, OKStatus: http.StatusOK,
@ -84,8 +84,8 @@ func (c *Client) GetRecentStreamInfo(username string) (string, string, error) {
} `json:"data"` } `json:"data"`
} }
if err := c.request(clientRequestOpts{ if err := c.Request(ClientRequestOpts{
AuthType: authTypeBearerToken, AuthType: AuthTypeBearerToken,
Context: context.Background(), Context: context.Background(),
Method: http.MethodGet, Method: http.MethodGet,
OKStatus: http.StatusOK, OKStatus: http.StatusOK,
@ -119,8 +119,8 @@ func (c *Client) HasLiveStream(username string) (bool, error) {
} `json:"data"` } `json:"data"`
} }
if err := c.request(clientRequestOpts{ if err := c.Request(ClientRequestOpts{
AuthType: authTypeBearerToken, AuthType: AuthTypeBearerToken,
Context: context.Background(), Context: context.Background(),
Method: http.MethodGet, Method: http.MethodGet,
OKStatus: http.StatusOK, OKStatus: http.StatusOK,

View file

@ -33,8 +33,8 @@ func (c *Client) GetBroadcasterSubscriptionCount(ctx context.Context, broadcaste
var data subInfo var data subInfo
if err = c.request(clientRequestOpts{ if err = c.Request(ClientRequestOpts{
AuthType: authTypeBearerToken, AuthType: AuthTypeBearerToken,
Context: ctx, Context: ctx,
Method: http.MethodGet, Method: http.MethodGet,
OKStatus: http.StatusOK, OKStatus: http.StatusOK,

View file

@ -29,9 +29,9 @@ const (
) )
const ( const (
authTypeUnauthorized authType = iota AuthTypeUnauthorized AuthType = iota
authTypeAppAccessToken AuthTypeAppAccessToken
authTypeBearerToken AuthTypeBearerToken
) )
type ( type (
@ -66,10 +66,10 @@ type (
ExpiresIn int `json:"expires_in"` ExpiresIn int `json:"expires_in"`
} }
authType uint8 AuthType uint8
clientRequestOpts struct { ClientRequestOpts struct {
AuthType authType AuthType AuthType
Body io.Reader Body io.Reader
Context context.Context Context context.Context
Method string Method string
@ -120,8 +120,8 @@ func (c *Client) RefreshToken() error {
var resp OAuthTokenResponse var resp OAuthTokenResponse
err := c.request(clientRequestOpts{ err := c.Request(ClientRequestOpts{
AuthType: authTypeUnauthorized, AuthType: AuthTypeUnauthorized,
Context: context.Background(), Context: context.Background(),
Method: http.MethodPost, Method: http.MethodPost,
OKStatus: http.StatusOK, OKStatus: http.StatusOK,
@ -186,8 +186,8 @@ func (c *Client) ValidateToken(ctx context.Context, force bool) error {
var resp OAuthTokenValidationResponse var resp OAuthTokenValidationResponse
if err := c.request(clientRequestOpts{ if err := c.Request(ClientRequestOpts{
AuthType: authTypeBearerToken, AuthType: AuthTypeBearerToken,
Context: ctx, Context: ctx,
Method: http.MethodGet, Method: http.MethodGet,
NoRetry: true, NoRetry: true,
@ -234,8 +234,8 @@ func (c *Client) getTwitchAppAccessToken() (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), twitchRequestTimeout) ctx, cancel := context.WithTimeout(context.Background(), twitchRequestTimeout)
defer cancel() defer cancel()
if err := c.request(clientRequestOpts{ if err := c.Request(ClientRequestOpts{
AuthType: authTypeUnauthorized, AuthType: AuthTypeUnauthorized,
Context: ctx, Context: ctx,
Method: http.MethodPost, Method: http.MethodPost,
OKStatus: http.StatusOK, OKStatus: http.StatusOK,
@ -250,7 +250,7 @@ func (c *Client) getTwitchAppAccessToken() (string, error) {
} }
//nolint:gocognit,gocyclo // Not gonna split to keep as a logical unit //nolint:gocognit,gocyclo // Not gonna split to keep as a logical unit
func (c *Client) request(opts clientRequestOpts) error { func (c *Client) Request(opts ClientRequestOpts) error {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"method": opts.Method, "method": opts.Method,
"url": c.replaceSecrets(opts.URL), "url": c.replaceSecrets(opts.URL),
@ -273,10 +273,10 @@ func (c *Client) request(opts clientRequestOpts) error {
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
switch opts.AuthType { switch opts.AuthType {
case authTypeUnauthorized: case AuthTypeUnauthorized:
// Nothing to do // Nothing to do
case authTypeAppAccessToken: case AuthTypeAppAccessToken:
accessToken, err := c.getTwitchAppAccessToken() accessToken, err := c.getTwitchAppAccessToken()
if err != nil { if err != nil {
return errors.Wrap(err, "getting app-access-token") return errors.Wrap(err, "getting app-access-token")
@ -285,7 +285,7 @@ func (c *Client) request(opts clientRequestOpts) error {
req.Header.Set("Authorization", "Bearer "+accessToken) req.Header.Set("Authorization", "Bearer "+accessToken)
req.Header.Set("Client-Id", c.clientID) req.Header.Set("Client-Id", c.clientID)
case authTypeBearerToken: case AuthTypeBearerToken:
accessToken := c.accessToken accessToken := c.accessToken
if !opts.NoValidateToken { if !opts.NoValidateToken {
accessToken, err = c.GetToken() accessToken, err = c.GetToken()
@ -307,7 +307,7 @@ func (c *Client) request(opts clientRequestOpts) error {
} }
defer resp.Body.Close() defer resp.Body.Close()
if opts.AuthType == authTypeAppAccessToken && resp.StatusCode == http.StatusUnauthorized { if opts.AuthType == AuthTypeAppAccessToken && resp.StatusCode == http.StatusUnauthorized {
// Seems our token was somehow revoked, clear the token and retry which will get a new token // Seems our token was somehow revoked, clear the token and retry which will get a new token
c.appAccessToken = "" c.appAccessToken = ""
return errors.New("app-access-token is invalid") return errors.New("app-access-token is invalid")

View file

@ -26,8 +26,8 @@ func (c *Client) GetAuthorizedUser() (userID string, userName string, err error)
Data []User `json:"data"` Data []User `json:"data"`
} }
if err := c.request(clientRequestOpts{ if err := c.Request(ClientRequestOpts{
AuthType: authTypeBearerToken, AuthType: AuthTypeBearerToken,
Context: context.Background(), Context: context.Background(),
Method: http.MethodGet, Method: http.MethodGet,
OKStatus: http.StatusOK, OKStatus: http.StatusOK,
@ -54,8 +54,8 @@ func (c *Client) GetDisplayNameForUser(username string) (string, error) {
Data []User `json:"data"` Data []User `json:"data"`
} }
if err := c.request(clientRequestOpts{ if err := c.Request(ClientRequestOpts{
AuthType: authTypeAppAccessToken, AuthType: AuthTypeAppAccessToken,
Context: context.Background(), Context: context.Background(),
Method: http.MethodGet, Method: http.MethodGet,
Out: &payload, Out: &payload,
@ -95,8 +95,8 @@ func (c *Client) GetFollowDate(from, to string) (time.Time, error) {
} `json:"data"` } `json:"data"`
} }
if err := c.request(clientRequestOpts{ if err := c.Request(ClientRequestOpts{
AuthType: authTypeAppAccessToken, AuthType: AuthTypeAppAccessToken,
Context: context.Background(), Context: context.Background(),
Method: http.MethodGet, Method: http.MethodGet,
OKStatus: http.StatusOK, OKStatus: http.StatusOK,
@ -133,8 +133,8 @@ func (c *Client) GetIDForUsername(username string) (string, error) {
Data []User `json:"data"` Data []User `json:"data"`
} }
if err := c.request(clientRequestOpts{ if err := c.Request(ClientRequestOpts{
AuthType: authTypeAppAccessToken, AuthType: AuthTypeAppAccessToken,
Context: context.Background(), Context: context.Background(),
Method: http.MethodGet, Method: http.MethodGet,
OKStatus: http.StatusOK, OKStatus: http.StatusOK,
@ -173,8 +173,8 @@ func (c *Client) GetUserInformation(user string) (*User, error) {
param = "id" param = "id"
} }
if err := c.request(clientRequestOpts{ if err := c.Request(ClientRequestOpts{
AuthType: authTypeAppAccessToken, AuthType: AuthTypeAppAccessToken,
Context: context.Background(), Context: context.Background(),
Method: http.MethodGet, Method: http.MethodGet,
OKStatus: http.StatusOK, OKStatus: http.StatusOK,

View file

@ -37,8 +37,8 @@ func (c *Client) SendWhisper(toUser, message string) error {
} }
return errors.Wrap( return errors.Wrap(
c.request(clientRequestOpts{ c.Request(ClientRequestOpts{
AuthType: authTypeBearerToken, AuthType: AuthTypeBearerToken,
Context: context.Background(), Context: context.Background(),
Method: http.MethodPost, Method: http.MethodPost,
OKStatus: http.StatusNoContent, OKStatus: http.StatusNoContent,