Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added arguments to all API calls that accepts auth options. #131

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ads.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ type StartCommercialResponse struct {
// StartCommercial starts a commercial on a specified channel
// OAuth Token required
// Requires channel:edit:commercial scope
func (c *Client) StartCommercial(params *StartCommercialParams) (*StartCommercialResponse, error) {
resp, err := c.post("/channels/commercial", &ManyAdDetails{}, params)
func (c *Client) StartCommercial(params *StartCommercialParams, opts ...Options) (*StartCommercialResponse, error) {
resp, err := c.post("/channels/commercial", &ManyAdDetails{}, params, opts...)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ type ExtensionAnalyticsParams struct {

// GetExtensionAnalytics returns a URL to the downloadable CSV file
// containing analytics data. Valid for 5 minutes.
func (c *Client) GetExtensionAnalytics(params *ExtensionAnalyticsParams) (*ExtensionAnalyticsResponse, error) {
resp, err := c.get("/analytics/extensions", &ManyExtensionAnalytics{}, params)
func (c *Client) GetExtensionAnalytics(params *ExtensionAnalyticsParams, opts ...Options) (*ExtensionAnalyticsResponse, error) {
resp, err := c.get("/analytics/extensions", &ManyExtensionAnalytics{}, params, opts...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -69,9 +69,9 @@ type GameAnalyticsParams struct {

// GetGameAnalytics returns a URL to the downloadable CSV file
// containing analytics data for the specified game. Valid for 5 minutes.
func (c *Client) GetGameAnalytics(params *GameAnalyticsParams) (*GameAnalyticsResponse, error) {
func (c *Client) GetGameAnalytics(params *GameAnalyticsParams, opts ...Options) (*GameAnalyticsResponse, error) {

resp, err := c.get("/analytics/games", &ManyGameAnalytics{}, params)
resp, err := c.get("/analytics/games", &ManyGameAnalytics{}, params, opts...)
if err != nil {
return nil, err
}
Expand Down
76 changes: 52 additions & 24 deletions authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,18 @@ type AuthorizationURLParams struct {
ForceVerify bool // (Optional)
}

func (c *Client) GetAuthorizationURL(params *AuthorizationURLParams) string {
func (c *Client) GetAuthorizationURL(params *AuthorizationURLParams, opts ...Options) string {
var options Options
if len(opts) == 0 {
options = *c.opts
} else {
options = opts[0]
}

url := AuthBaseURL + "/authorize"
url += "?response_type=" + params.ResponseType
url += "&client_id=" + c.opts.ClientID
url += "&redirect_uri=" + c.opts.RedirectURI
url += "&client_id=" + options.ClientID
url += "&redirect_uri=" + options.RedirectURI

if params.State != "" {
url += "&state=" + params.State
Expand Down Expand Up @@ -51,12 +58,18 @@ type AppAccessTokenResponse struct {
Data AccessCredentials
}

func (c *Client) RequestAppAccessToken(scopes []string) (*AppAccessTokenResponse, error) {
opts := c.opts
func (c *Client) RequestAppAccessToken(scopes []string, opts ...Options) (*AppAccessTokenResponse, error) {
var options Options
if len(opts) == 0 {
options = *c.opts
} else {
options = opts[0]
}

data := &accessTokenRequestData{
ClientID: opts.ClientID,
ClientSecret: opts.ClientSecret,
RedirectURI: opts.RedirectURI,
ClientID: options.ClientID,
ClientSecret: options.ClientSecret,
RedirectURI: options.RedirectURI,
GrantType: "client_credentials",
Scopes: strings.Join(scopes, " "),
}
Expand Down Expand Up @@ -90,13 +103,18 @@ type accessTokenRequestData struct {
Scopes string `query:"scope"`
}

func (c *Client) RequestUserAccessToken(code string) (*UserAccessTokenResponse, error) {
opts := c.opts
func (c *Client) RequestUserAccessToken(code string, opts ...Options) (*UserAccessTokenResponse, error) {
var options Options
if len(opts) == 0 {
options = *c.opts
} else {
options = opts[0]
}
data := &accessTokenRequestData{
Code: code,
ClientID: opts.ClientID,
ClientSecret: opts.ClientSecret,
RedirectURI: opts.RedirectURI,
ClientID: options.ClientID,
ClientSecret: options.ClientSecret,
RedirectURI: options.RedirectURI,
GrantType: "authorization_code",
}

Expand Down Expand Up @@ -131,11 +149,16 @@ type refreshTokenRequestData struct {
// access token extended. Twitch OAuth2 access tokens have expirations.
// Token-expiration periods vary in length. You should build your applications
// in such a way that they are resilient to token authentication failures.
func (c *Client) RefreshUserAccessToken(refreshToken string) (*RefreshTokenResponse, error) {
opts := c.opts
func (c *Client) RefreshUserAccessToken(refreshToken string, opts ...Options) (*RefreshTokenResponse, error) {
var options Options
if len(opts) == 0 {
options = *c.opts
} else {
options = opts[0]
}
data := &refreshTokenRequestData{
ClientID: opts.ClientID,
ClientSecret: opts.ClientSecret,
ClientID: options.ClientID,
ClientSecret: options.ClientSecret,
GrantType: "refresh_token",
RefreshToken: refreshToken,
}
Expand Down Expand Up @@ -169,9 +192,15 @@ type revokeAccessTokenRequestData struct {
// Both successful requests and requests with bad tokens return 200 OK with
// no body. Requests with bad tokens return the same response, as there is no
// meaningful action a client can take after sending a bad token.
func (c *Client) RevokeUserAccessToken(accessToken string) (*RevokeAccessTokenResponse, error) {
func (c *Client) RevokeUserAccessToken(accessToken string, opts ...Options) (*RevokeAccessTokenResponse, error) {
var options Options
if len(opts) == 0 {
options = *c.opts
} else {
options = opts[0]
}
data := &revokeAccessTokenRequestData{
ClientID: c.opts.ClientID,
ClientID: options.ClientID,
AccessToken: accessToken,
}

Expand Down Expand Up @@ -200,13 +229,12 @@ type validateTokenDetails struct {

// ValidateToken - Validate access token
func (c *Client) ValidateToken(accessToken string) (bool, *ValidateTokenResponse, error) {
// Reset to original token after request
currentToken := c.opts.UserAccessToken
c.SetUserAccessToken(accessToken)
defer c.SetUserAccessToken(currentToken)
opts := Options{
UserAccessToken: accessToken,
}

var data validateTokenDetails
resp, err := c.get(authPaths["validate"], &data, nil)
resp, err := c.get(authPaths["validate"], &data, nil, opts)
if err != nil {
return false, nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions bits.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ type BitsLeaderboardParams struct {
// information for an authorized broadcaster.
//
// Required Scope: bits:read
func (c *Client) GetBitsLeaderboard(params *BitsLeaderboardParams) (*BitsLeaderboardResponse, error) {
resp, err := c.get("/bits/leaderboard", &ManyUserBitTotals{}, params)
func (c *Client) GetBitsLeaderboard(params *BitsLeaderboardParams, opts ...Options) (*BitsLeaderboardResponse, error) {
resp, err := c.get("/bits/leaderboard", &ManyUserBitTotals{}, params, opts...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -96,8 +96,8 @@ type CheermotesResponse struct {
Data ManyCheermotes
}

func (c *Client) GetCheermotes(params *CheermotesParams) (*CheermotesResponse, error) {
resp, err := c.get("/bits/cheermotes", &ManyCheermotes{}, params)
func (c *Client) GetCheermotes(params *CheermotesParams, opts ...Options) (*CheermotesResponse, error) {
resp, err := c.get("/bits/cheermotes", &ManyCheermotes{}, params, opts...)
if err != nil {
return nil, err
}
Expand Down
12 changes: 6 additions & 6 deletions channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ type SearchChannelsResponse struct {

// SearchChannels searches for Twitch channels based on the given search
// parameters. Unlike GetStreams, this can also return offline channels.
func (c *Client) SearchChannels(params *SearchChannelsParams) (*SearchChannelsResponse, error) {
resp, err := c.get("/search/channels", &ManySearchChannels{}, params)
func (c *Client) SearchChannels(params *SearchChannelsParams, opts ...Options) (*SearchChannelsResponse, error) {
resp, err := c.get("/search/channels", &ManySearchChannels{}, params, opts...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -88,8 +88,8 @@ type ChannelInformation struct {
Delay int `json:"delay"`
}

func (c *Client) GetChannelInformation(params *GetChannelInformationParams) (*GetChannelInformationResponse, error) {
resp, err := c.get("/channels", &ManyChannelInformation{}, params)
func (c *Client) GetChannelInformation(params *GetChannelInformationParams, opts ...Options) (*GetChannelInformationResponse, error) {
resp, err := c.get("/channels", &ManyChannelInformation{}, params, opts...)
if err != nil {
return nil, err
}
Expand All @@ -101,8 +101,8 @@ func (c *Client) GetChannelInformation(params *GetChannelInformationParams) (*Ge
return channels, nil
}

func (c *Client) EditChannelInformation(params *EditChannelInformationParams) (*EditChannelInformationResponse, error) {
resp, err := c.patchAsJSON("/channels", &EditChannelInformationResponse{}, params)
func (c *Client) EditChannelInformation(params *EditChannelInformationParams, opts ...Options) (*EditChannelInformationResponse, error) {
resp, err := c.patchAsJSON("/channels", &EditChannelInformationResponse{}, params, opts...)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions channels_editors.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type ChannelEditorsResponse struct {

// GetChannelEditors Get a list of users who have editor permissions for a specific channel
// Required scope: channel:read:editors
func (c *Client) GetChannelEditors(params *ChannelEditorsParams) (*ChannelEditorsResponse, error) {
resp, err := c.get("/channels/editors", &ManyChannelEditors{}, params)
func (c *Client) GetChannelEditors(params *ChannelEditorsParams, opts ...Options) (*ChannelEditorsResponse, error) {
resp, err := c.get("/channels/editors", &ManyChannelEditors{}, params, opts...)
if err != nil {
return nil, err
}
Expand Down
12 changes: 6 additions & 6 deletions channels_points.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ type DeleteCustomRewardsResponse struct {

// CreateCustomReward : Creates a Custom Reward on a channel.
// Required scope: channel:manage:redemptions
func (c *Client) CreateCustomReward(params *ChannelCustomRewardsParams) (*ChannelCustomRewardResponse, error) {
resp, err := c.postAsJSON("/channel_points/custom_rewards", &ManyChannelCustomRewards{}, params)
func (c *Client) CreateCustomReward(params *ChannelCustomRewardsParams, opts ...Options) (*ChannelCustomRewardResponse, error) {
resp, err := c.postAsJSON("/channel_points/custom_rewards", &ManyChannelCustomRewards{}, params, opts...)
if err != nil {
return nil, err
}
Expand All @@ -102,8 +102,8 @@ func (c *Client) CreateCustomReward(params *ChannelCustomRewardsParams) (*Channe

// DeleteCustomRewards : Deletes a Custom Rewards on a channel
// Required scope: channel:manage:redemptions
func (c *Client) DeleteCustomRewards(params *DeleteCustomRewardsParams) (*DeleteCustomRewardsResponse, error) {
resp, err := c.delete("/channel_points/custom_rewards", nil, params)
func (c *Client) DeleteCustomRewards(params *DeleteCustomRewardsParams, opts ...Options) (*DeleteCustomRewardsResponse, error) {
resp, err := c.delete("/channel_points/custom_rewards", nil, params, opts...)
if err != nil {
return nil, err
}
Expand All @@ -116,8 +116,8 @@ func (c *Client) DeleteCustomRewards(params *DeleteCustomRewardsParams) (*Delete

// GetCustomRewards : Get Custom Rewards on a channel
// Required scope: channel:read:redemptions
func (c *Client) GetCustomRewards(params *GetCustomRewardsParams) (*ChannelCustomRewardResponse, error) {
resp, err := c.get("/channel_points/custom_rewards", &ManyChannelCustomRewards{}, params)
func (c *Client) GetCustomRewards(params *GetCustomRewardsParams, opts ...Options) (*ChannelCustomRewardResponse, error) {
resp, err := c.get("/channel_points/custom_rewards", &ManyChannelCustomRewards{}, params, opts...)
if err != nil {
return nil, err
}
Expand Down
20 changes: 10 additions & 10 deletions chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ type BadgeVersion struct {
ImageUrl4x string `json:"image_url_4x"`
}

func (c *Client) GetChannelChatBadges(params *GetChatBadgeParams) (*GetChatBadgeResponse, error) {
resp, err := c.get("/chat/badges", &ManyChatBadge{}, params)
func (c *Client) GetChannelChatBadges(params *GetChatBadgeParams, opts ...Options) (*GetChatBadgeResponse, error) {
resp, err := c.get("/chat/badges", &ManyChatBadge{}, params, opts...)
if err != nil {
return nil, err
}
Expand All @@ -38,8 +38,8 @@ func (c *Client) GetChannelChatBadges(params *GetChatBadgeParams) (*GetChatBadge
return channels, nil
}

func (c *Client) GetGlobalChatBadges() (*GetChatBadgeResponse, error) {
resp, err := c.get("/chat/badges/global", &ManyChatBadge{}, nil)
func (c *Client) GetGlobalChatBadges(opts ...Options) (*GetChatBadgeResponse, error) {
resp, err := c.get("/chat/badges/global", &ManyChatBadge{}, nil, opts...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -97,8 +97,8 @@ type EmoteImage struct {
Url4x string `json:"url_4x"`
}

func (c *Client) GetChannelEmotes(params *GetChannelEmotesParams) (*GetChannelEmotesResponse, error) {
resp, err := c.get("/chat/emotes", &ManyEmotes{}, params)
func (c *Client) GetChannelEmotes(params *GetChannelEmotesParams, opts ...Options) (*GetChannelEmotesResponse, error) {
resp, err := c.get("/chat/emotes", &ManyEmotes{}, params, opts...)
if err != nil {
return nil, err
}
Expand All @@ -110,8 +110,8 @@ func (c *Client) GetChannelEmotes(params *GetChannelEmotesParams) (*GetChannelEm
return emotes, nil
}

func (c *Client) GetGlobalEmotes() (*GetChannelEmotesResponse, error) {
resp, err := c.get("/chat/emotes/global", &ManyEmotes{}, nil)
func (c *Client) GetGlobalEmotes(opts ...Options) (*GetChannelEmotesResponse, error) {
resp, err := c.get("/chat/emotes/global", &ManyEmotes{}, nil, opts...)
if err != nil {
return nil, err
}
Expand All @@ -124,8 +124,8 @@ func (c *Client) GetGlobalEmotes() (*GetChannelEmotesResponse, error) {
}

// GetEmoteSets
func (c *Client) GetEmoteSets(params *GetEmoteSetsParams) (*GetEmoteSetsResponse, error) {
resp, err := c.get("/chat/emotes/set", &ManyEmotesWithOwner{}, params)
func (c *Client) GetEmoteSets(params *GetEmoteSetsParams, opts ...Options) (*GetEmoteSetsResponse, error) {
resp, err := c.get("/chat/emotes/set", &ManyEmotesWithOwner{}, params, opts...)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions clips.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ type ClipsParams struct {
}

// GetClips returns information about a specified clip.
func (c *Client) GetClips(params *ClipsParams) (*ClipsResponse, error) {
resp, err := c.get("/clips", &ManyClips{}, params)
func (c *Client) GetClips(params *ClipsParams, opts ...Options) (*ClipsResponse, error) {
resp, err := c.get("/clips", &ManyClips{}, params, opts...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -98,8 +98,8 @@ type CreateClipParams struct {
// clip was not created and retry Create Clip.
//
// Required scope: clips:edit
func (c *Client) CreateClip(params *CreateClipParams) (*CreateClipResponse, error) {
resp, err := c.post("/clips", &ManyClipEditURLs{}, params)
func (c *Client) CreateClip(params *CreateClipParams, opts ...Options) (*CreateClipResponse, error) {
resp, err := c.post("/clips", &ManyClipEditURLs{}, params, opts...)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions drops.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ type UpdateDropsEntitlementsResponse struct {
// Filtering by FulfillmentStatus returns all of the entitlements with the specified fulfillment status.
// Entitlements are digital items that users are entitled to use. Twitch entitlements are granted based on viewership
// engagement with a content creator, based on the game developers' campaign.
func (c *Client) GetDropsEntitlements(params *GetDropEntitlementsParams) (*GetDropsEntitlementsResponse, error) {
resp, err := c.get("/entitlements/drops", &ManyEntitlementsWithPagination{}, params)
func (c *Client) GetDropsEntitlements(params *GetDropEntitlementsParams, opts ...Options) (*GetDropsEntitlementsResponse, error) {
resp, err := c.get("/entitlements/drops", &ManyEntitlementsWithPagination{}, params, opts...)
if err != nil {
return nil, err
}
Expand All @@ -81,8 +81,8 @@ func (c *Client) GetDropsEntitlements(params *GetDropEntitlementsParams) (*GetDr
// operation should be retried again later.
// Entitlements are digital items that users are entitled to use. Twitch entitlements are granted based on viewership
// engagement with a content creator, based on the game developers' campaign.
func (c *Client) UpdateDropsEntitlements(params *UpdateDropsEntitlementsParams) (*UpdateDropsEntitlementsResponse, error) {
resp, err := c.patchAsJSON("/entitlements/drops", &ManyUpdatedEntitlementSet{}, params)
func (c *Client) UpdateDropsEntitlements(params *UpdateDropsEntitlementsParams, opts ...Options) (*UpdateDropsEntitlementsResponse, error) {
resp, err := c.patchAsJSON("/entitlements/drops", &ManyUpdatedEntitlementSet{}, params, opts...)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions entitlement_codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ type CodeResponse struct {
// Per https://dev.twitch.tv/docs/api/reference#get-code-status
// Access is controlled via an app access token on the calling service. The client ID associated with the app access token must be approved by Twitch as part of a contracted arrangement.
// Callers with an app access token are authorized to redeem codes on behalf of any Twitch user account.
func (c *Client) GetEntitlementCodeStatus(params *CodesParams) (*CodeResponse, error) {
resp, err := c.get("/entitlements/codes", &ManyCodes{}, params)
func (c *Client) GetEntitlementCodeStatus(params *CodesParams, opts ...Options) (*CodeResponse, error) {
resp, err := c.get("/entitlements/codes", &ManyCodes{}, params, opts...)
if err != nil {
return nil, err
}
Expand All @@ -55,8 +55,8 @@ func (c *Client) GetEntitlementCodeStatus(params *CodesParams) (*CodeResponse, e
// Per https://dev.twitch.tv/docs/api/reference/#redeem-code
// Access is controlled via an app access token on the calling service. The client ID associated with the app access token must be approved by Twitch.
// Callers with an app access token are authorized to redeem codes on behalf of any Twitch user account.
func (c *Client) RedeemEntitlementCode(params *CodesParams) (*CodeResponse, error) {
resp, err := c.post("/entitlements/code", &ManyCodes{}, params)
func (c *Client) RedeemEntitlementCode(params *CodesParams, opts ...Options) (*CodeResponse, error) {
resp, err := c.post("/entitlements/code", &ManyCodes{}, params, opts...)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions entitlement_grants.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ type EntitlementsUploadResponse struct {
// file and notify users that they have an entitlement. Entitlements are digital
// items that users are entitled to use. Twitch entitlements are granted to users
// gratis or as part of a purchase on Twitch.
func (c *Client) CreateEntitlementsUploadURL(manifestID, entitlementType string) (*EntitlementsUploadResponse, error) {
func (c *Client) CreateEntitlementsUploadURL(manifestID, entitlementType string, opts ...Options) (*EntitlementsUploadResponse, error) {
data := &entitlementUploadURLRequest{
ManifestID: manifestID,
Type: entitlementType,
}

resp, err := c.post("/entitlements/upload", &ManyEntitlementsUploadURLs{}, data)
resp, err := c.post("/entitlements/upload", &ManyEntitlementsUploadURLs{}, data, opts...)
if err != nil {
return nil, err
}
Expand Down
Loading