From fc93c6e63b000484e0b9dbf3f96d1145c489836c Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sat, 1 Jul 2023 16:48:21 +0200 Subject: [PATCH] [core] Export Twitch client Request Signed-off-by: Knut Ahlers --- pkg/twitch/channels.go | 16 ++++++++-------- pkg/twitch/chat.go | 8 ++++---- pkg/twitch/clips.go | 4 ++-- pkg/twitch/eventsub.go | 16 ++++++++-------- pkg/twitch/moderation.go | 16 ++++++++-------- pkg/twitch/polls.go | 4 ++-- pkg/twitch/search.go | 4 ++-- pkg/twitch/streams.go | 12 ++++++------ pkg/twitch/subscriptions.go | 4 ++-- pkg/twitch/twitch.go | 34 +++++++++++++++++----------------- pkg/twitch/users.go | 20 ++++++++++---------- pkg/twitch/whispers.go | 4 ++-- 12 files changed, 71 insertions(+), 71 deletions(-) diff --git a/pkg/twitch/channels.go b/pkg/twitch/channels.go index 4569f2a..f3a0755 100644 --- a/pkg/twitch/channels.go +++ b/pkg/twitch/channels.go @@ -23,8 +23,8 @@ func (c *Client) AddChannelVIP(ctx context.Context, broadcasterName, userName st } return errors.Wrap( - c.request(clientRequestOpts{ - AuthType: authTypeBearerToken, + c.Request(ClientRequestOpts{ + AuthType: AuthTypeBearerToken, Context: ctx, Method: http.MethodPost, OKStatus: http.StatusNoContent, @@ -96,8 +96,8 @@ func (c *Client) ModifyChannelInformation(ctx context.Context, broadcasterName s } return errors.Wrap( - c.request(clientRequestOpts{ - AuthType: authTypeBearerToken, + c.Request(ClientRequestOpts{ + AuthType: AuthTypeBearerToken, Body: body, Context: ctx, Method: http.MethodPatch, @@ -120,8 +120,8 @@ func (c *Client) RemoveChannelVIP(ctx context.Context, broadcasterName, userName } return errors.Wrap( - c.request(clientRequestOpts{ - AuthType: authTypeBearerToken, + c.Request(ClientRequestOpts{ + AuthType: AuthTypeBearerToken, Context: ctx, Method: http.MethodDelete, OKStatus: http.StatusNoContent, @@ -152,8 +152,8 @@ func (c *Client) RunCommercial(ctx context.Context, channel string, duration int } return errors.Wrap( - c.request(clientRequestOpts{ - AuthType: authTypeBearerToken, + c.Request(ClientRequestOpts{ + AuthType: AuthTypeBearerToken, Body: body, Context: ctx, Method: http.MethodPost, diff --git a/pkg/twitch/chat.go b/pkg/twitch/chat.go index 55930eb..91319b7 100644 --- a/pkg/twitch/chat.go +++ b/pkg/twitch/chat.go @@ -40,8 +40,8 @@ func (c *Client) SendChatAnnouncement(channel, color, message string) error { } return errors.Wrap( - c.request(clientRequestOpts{ - AuthType: authTypeBearerToken, + c.Request(ClientRequestOpts{ + AuthType: AuthTypeBearerToken, Context: context.Background(), Method: http.MethodPost, OKStatus: http.StatusNoContent, @@ -79,8 +79,8 @@ func (c *Client) SendShoutout(channel, user string) error { params.Set("to_broadcaster_id", userID) return errors.Wrap( - c.request(clientRequestOpts{ - AuthType: authTypeBearerToken, + c.Request(ClientRequestOpts{ + AuthType: AuthTypeBearerToken, Context: context.Background(), Method: http.MethodPost, OKStatus: http.StatusNoContent, diff --git a/pkg/twitch/clips.go b/pkg/twitch/clips.go index 263f476..987c526 100644 --- a/pkg/twitch/clips.go +++ b/pkg/twitch/clips.go @@ -44,8 +44,8 @@ func (c *Client) GetClipByID(ctx context.Context, clipID string) (ClipInfo, erro Data []ClipInfo } - if err := c.request(clientRequestOpts{ - AuthType: authTypeAppAccessToken, + if err := c.Request(ClientRequestOpts{ + AuthType: AuthTypeAppAccessToken, Context: ctx, Method: http.MethodGet, OKStatus: http.StatusOK, diff --git a/pkg/twitch/eventsub.go b/pkg/twitch/eventsub.go index 8e8bd84..0c7a5ea 100644 --- a/pkg/twitch/eventsub.go +++ b/pkg/twitch/eventsub.go @@ -212,14 +212,14 @@ func (e EventSubCondition) Hash() (string, 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) { - 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 ( buf = new(bytes.Buffer) resp struct { @@ -235,7 +235,7 @@ func (c *Client) createEventSubSubscription(ctx context.Context, auth authType, return nil, errors.Wrap(err, "assemble subscribe payload") } - if err := c.request(clientRequestOpts{ + if err := c.Request(ClientRequestOpts{ AuthType: auth, Body: buf, 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 { - return errors.Wrap(c.request(clientRequestOpts{ - AuthType: authTypeAppAccessToken, + return errors.Wrap(c.Request(ClientRequestOpts{ + AuthType: AuthTypeAppAccessToken, Context: ctx, Method: http.MethodDelete, OKStatus: http.StatusNoContent, @@ -278,8 +278,8 @@ func (c *Client) getEventSubSubscriptions(ctx context.Context) ([]eventSubSubscr ) for { - if err := c.request(clientRequestOpts{ - AuthType: authTypeAppAccessToken, + if err := c.Request(ClientRequestOpts{ + AuthType: AuthTypeAppAccessToken, Context: ctx, Method: http.MethodGet, OKStatus: http.StatusOK, diff --git a/pkg/twitch/moderation.go b/pkg/twitch/moderation.go index 301c656..cd4b877 100644 --- a/pkg/twitch/moderation.go +++ b/pkg/twitch/moderation.go @@ -55,8 +55,8 @@ func (c *Client) BanUser(channel, username string, duration time.Duration, reaso } return errors.Wrap( - c.request(clientRequestOpts{ - AuthType: authTypeBearerToken, + c.Request(ClientRequestOpts{ + AuthType: AuthTypeBearerToken, Context: context.Background(), Method: http.MethodPost, OKStatus: http.StatusOK, @@ -93,8 +93,8 @@ func (c *Client) DeleteMessage(channel, messageID string) error { } return errors.Wrap( - c.request(clientRequestOpts{ - AuthType: authTypeBearerToken, + c.Request(ClientRequestOpts{ + AuthType: AuthTypeBearerToken, Context: context.Background(), Method: http.MethodDelete, OKStatus: http.StatusNoContent, @@ -125,8 +125,8 @@ func (c *Client) UnbanUser(channel, username string) error { } return errors.Wrap( - c.request(clientRequestOpts{ - AuthType: authTypeBearerToken, + c.Request(ClientRequestOpts{ + AuthType: AuthTypeBearerToken, Context: context.Background(), Method: http.MethodDelete, OKStatus: http.StatusNoContent, @@ -159,8 +159,8 @@ func (c *Client) UpdateShieldMode(ctx context.Context, channel string, enable bo } return errors.Wrap( - c.request(clientRequestOpts{ - AuthType: authTypeBearerToken, + c.Request(ClientRequestOpts{ + AuthType: AuthTypeBearerToken, Context: ctx, Method: http.MethodPut, OKStatus: http.StatusOK, diff --git a/pkg/twitch/polls.go b/pkg/twitch/polls.go index 90e0a27..d85123b 100644 --- a/pkg/twitch/polls.go +++ b/pkg/twitch/polls.go @@ -48,8 +48,8 @@ func (c *Client) GetLatestPoll(ctx context.Context, username string) (*PollInfo, Data []*PollInfo `json:"data"` } - if err := c.request(clientRequestOpts{ - AuthType: authTypeBearerToken, + if err := c.Request(ClientRequestOpts{ + AuthType: AuthTypeBearerToken, Context: ctx, Method: http.MethodGet, OKStatus: http.StatusOK, diff --git a/pkg/twitch/search.go b/pkg/twitch/search.go index cc9d771..4e8f4f3 100644 --- a/pkg/twitch/search.go +++ b/pkg/twitch/search.go @@ -32,8 +32,8 @@ func (c *Client) SearchCategories(ctx context.Context, name string) ([]Category, } for { - if err := c.request(clientRequestOpts{ - AuthType: authTypeBearerToken, + if err := c.Request(ClientRequestOpts{ + AuthType: AuthTypeBearerToken, Context: ctx, Method: http.MethodGet, OKStatus: http.StatusOK, diff --git a/pkg/twitch/streams.go b/pkg/twitch/streams.go index 6e41d24..bdd6b26 100644 --- a/pkg/twitch/streams.go +++ b/pkg/twitch/streams.go @@ -43,8 +43,8 @@ func (c *Client) GetCurrentStreamInfo(username string) (*StreamInfo, error) { Data []*StreamInfo `json:"data"` } - if err := c.request(clientRequestOpts{ - AuthType: authTypeBearerToken, + if err := c.Request(ClientRequestOpts{ + AuthType: AuthTypeBearerToken, Context: context.Background(), Method: http.MethodGet, OKStatus: http.StatusOK, @@ -84,8 +84,8 @@ func (c *Client) GetRecentStreamInfo(username string) (string, string, error) { } `json:"data"` } - if err := c.request(clientRequestOpts{ - AuthType: authTypeBearerToken, + if err := c.Request(ClientRequestOpts{ + AuthType: AuthTypeBearerToken, Context: context.Background(), Method: http.MethodGet, OKStatus: http.StatusOK, @@ -119,8 +119,8 @@ func (c *Client) HasLiveStream(username string) (bool, error) { } `json:"data"` } - if err := c.request(clientRequestOpts{ - AuthType: authTypeBearerToken, + if err := c.Request(ClientRequestOpts{ + AuthType: AuthTypeBearerToken, Context: context.Background(), Method: http.MethodGet, OKStatus: http.StatusOK, diff --git a/pkg/twitch/subscriptions.go b/pkg/twitch/subscriptions.go index a80aec5..2f5daa1 100644 --- a/pkg/twitch/subscriptions.go +++ b/pkg/twitch/subscriptions.go @@ -33,8 +33,8 @@ func (c *Client) GetBroadcasterSubscriptionCount(ctx context.Context, broadcaste var data subInfo - if err = c.request(clientRequestOpts{ - AuthType: authTypeBearerToken, + if err = c.Request(ClientRequestOpts{ + AuthType: AuthTypeBearerToken, Context: ctx, Method: http.MethodGet, OKStatus: http.StatusOK, diff --git a/pkg/twitch/twitch.go b/pkg/twitch/twitch.go index 4c51223..ed0bbc7 100644 --- a/pkg/twitch/twitch.go +++ b/pkg/twitch/twitch.go @@ -29,9 +29,9 @@ const ( ) const ( - authTypeUnauthorized authType = iota - authTypeAppAccessToken - authTypeBearerToken + AuthTypeUnauthorized AuthType = iota + AuthTypeAppAccessToken + AuthTypeBearerToken ) type ( @@ -66,10 +66,10 @@ type ( ExpiresIn int `json:"expires_in"` } - authType uint8 + AuthType uint8 - clientRequestOpts struct { - AuthType authType + ClientRequestOpts struct { + AuthType AuthType Body io.Reader Context context.Context Method string @@ -120,8 +120,8 @@ func (c *Client) RefreshToken() error { var resp OAuthTokenResponse - err := c.request(clientRequestOpts{ - AuthType: authTypeUnauthorized, + err := c.Request(ClientRequestOpts{ + AuthType: AuthTypeUnauthorized, Context: context.Background(), Method: http.MethodPost, OKStatus: http.StatusOK, @@ -186,8 +186,8 @@ func (c *Client) ValidateToken(ctx context.Context, force bool) error { var resp OAuthTokenValidationResponse - if err := c.request(clientRequestOpts{ - AuthType: authTypeBearerToken, + if err := c.Request(ClientRequestOpts{ + AuthType: AuthTypeBearerToken, Context: ctx, Method: http.MethodGet, NoRetry: true, @@ -234,8 +234,8 @@ func (c *Client) getTwitchAppAccessToken() (string, error) { ctx, cancel := context.WithTimeout(context.Background(), twitchRequestTimeout) defer cancel() - if err := c.request(clientRequestOpts{ - AuthType: authTypeUnauthorized, + if err := c.Request(ClientRequestOpts{ + AuthType: AuthTypeUnauthorized, Context: ctx, Method: http.MethodPost, 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 -func (c *Client) request(opts clientRequestOpts) error { +func (c *Client) Request(opts ClientRequestOpts) error { log.WithFields(log.Fields{ "method": opts.Method, "url": c.replaceSecrets(opts.URL), @@ -273,10 +273,10 @@ func (c *Client) request(opts clientRequestOpts) error { req.Header.Set("Content-Type", "application/json") switch opts.AuthType { - case authTypeUnauthorized: + case AuthTypeUnauthorized: // Nothing to do - case authTypeAppAccessToken: + case AuthTypeAppAccessToken: accessToken, err := c.getTwitchAppAccessToken() if err != nil { 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("Client-Id", c.clientID) - case authTypeBearerToken: + case AuthTypeBearerToken: accessToken := c.accessToken if !opts.NoValidateToken { accessToken, err = c.GetToken() @@ -307,7 +307,7 @@ func (c *Client) request(opts clientRequestOpts) error { } 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 c.appAccessToken = "" return errors.New("app-access-token is invalid") diff --git a/pkg/twitch/users.go b/pkg/twitch/users.go index 6592a2e..0544469 100644 --- a/pkg/twitch/users.go +++ b/pkg/twitch/users.go @@ -26,8 +26,8 @@ func (c *Client) GetAuthorizedUser() (userID string, userName string, err error) Data []User `json:"data"` } - if err := c.request(clientRequestOpts{ - AuthType: authTypeBearerToken, + if err := c.Request(ClientRequestOpts{ + AuthType: AuthTypeBearerToken, Context: context.Background(), Method: http.MethodGet, OKStatus: http.StatusOK, @@ -54,8 +54,8 @@ func (c *Client) GetDisplayNameForUser(username string) (string, error) { Data []User `json:"data"` } - if err := c.request(clientRequestOpts{ - AuthType: authTypeAppAccessToken, + if err := c.Request(ClientRequestOpts{ + AuthType: AuthTypeAppAccessToken, Context: context.Background(), Method: http.MethodGet, Out: &payload, @@ -95,8 +95,8 @@ func (c *Client) GetFollowDate(from, to string) (time.Time, error) { } `json:"data"` } - if err := c.request(clientRequestOpts{ - AuthType: authTypeAppAccessToken, + if err := c.Request(ClientRequestOpts{ + AuthType: AuthTypeAppAccessToken, Context: context.Background(), Method: http.MethodGet, OKStatus: http.StatusOK, @@ -133,8 +133,8 @@ func (c *Client) GetIDForUsername(username string) (string, error) { Data []User `json:"data"` } - if err := c.request(clientRequestOpts{ - AuthType: authTypeAppAccessToken, + if err := c.Request(ClientRequestOpts{ + AuthType: AuthTypeAppAccessToken, Context: context.Background(), Method: http.MethodGet, OKStatus: http.StatusOK, @@ -173,8 +173,8 @@ func (c *Client) GetUserInformation(user string) (*User, error) { param = "id" } - if err := c.request(clientRequestOpts{ - AuthType: authTypeAppAccessToken, + if err := c.Request(ClientRequestOpts{ + AuthType: AuthTypeAppAccessToken, Context: context.Background(), Method: http.MethodGet, OKStatus: http.StatusOK, diff --git a/pkg/twitch/whispers.go b/pkg/twitch/whispers.go index e4c6d2f..2acf560 100644 --- a/pkg/twitch/whispers.go +++ b/pkg/twitch/whispers.go @@ -37,8 +37,8 @@ func (c *Client) SendWhisper(toUser, message string) error { } return errors.Wrap( - c.request(clientRequestOpts{ - AuthType: authTypeBearerToken, + c.Request(ClientRequestOpts{ + AuthType: AuthTypeBearerToken, Context: context.Background(), Method: http.MethodPost, OKStatus: http.StatusNoContent,