[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(
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,

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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")

View File

@ -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,

View File

@ -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,