From 4803b82eba6aea819052d9c9ed6712a56be50d03 Mon Sep 17 00:00:00 2001 From: Evan Fiordeliso Date: Tue, 5 Mar 2024 22:29:23 -0500 Subject: [PATCH] Fix optional query params by adding ,omitempty and fix create polls to use request body --- api/channels/get_channel_followers.go | 2 +- api/extensions/create_extension_secret.go | 2 +- api/gueststar/delete_gueststar_slot.go | 2 +- .../update_gueststar_slot_settings.go | 8 ++--- api/hypetrain/get_hypetrain_events.go | 4 +-- api/moderation/get_banned_users.go | 8 ++--- api/moderation/get_blocked_terms.go | 4 +-- api/moderation/get_moderated_channels.go | 4 +-- api/moderation/get_moderators.go | 6 ++-- api/moderation/get_vips.go | 6 ++-- api/polls/create_poll.go | 36 +++++++++++-------- api/polls/get_polls.go | 6 ++-- auth/authorize.go | 8 ++--- 13 files changed, 52 insertions(+), 44 deletions(-) diff --git a/api/channels/get_channel_followers.go b/api/channels/get_channel_followers.go index 2d93508..0f8d8f8 100644 --- a/api/channels/get_channel_followers.go +++ b/api/channels/get_channel_followers.go @@ -18,7 +18,7 @@ type GetChannelFollowersParams struct { // // Using this parameter requires both a user access token with the moderator:read:followers scope and the user ID // in the access token match the broadcaster_id or be the user ID for a moderator of the specified broadcaster. - UserID *string `url:"user_id"` + UserID *string `url:"user_id,omitempty"` // The broadcaster’s ID. Returns the list of users that follow this broadcaster. BroadcasterID string `url:"broadcaster_id"` diff --git a/api/extensions/create_extension_secret.go b/api/extensions/create_extension_secret.go index 12e0a96..48d591f 100644 --- a/api/extensions/create_extension_secret.go +++ b/api/extensions/create_extension_secret.go @@ -17,7 +17,7 @@ type CreateExtensionSecretParams struct { // The delay should provide enough time for instances of the extension to gracefully switch over to the new secret. // The minimum delay is 300 seconds (5 minutes). // The default is 300 seconds. - Delay *int `url:"delay"` + Delay *int `url:"delay,omitempty"` } type CreateExtensionSecretResponse struct { diff --git a/api/gueststar/delete_gueststar_slot.go b/api/gueststar/delete_gueststar_slot.go index 3a9daa7..191e836 100644 --- a/api/gueststar/delete_gueststar_slot.go +++ b/api/gueststar/delete_gueststar_slot.go @@ -27,7 +27,7 @@ type DeleteGuestStarSlotParams struct { SlotID int `url:"slot_id"` // Flag signaling that the guest should be reinvited to the session, sending them back to the invite queue. - ShouldReinviteGuest *bool `url:"should_reinvite_guest"` + ShouldReinviteGuest *bool `url:"should_reinvite_guest,omitempty"` } // Allows a caller to remove a slot assignment from a user participating in an active Guest Star session. This revokes their access to the session immediately and disables their access to publish or subscribe to media within the session. diff --git a/api/gueststar/update_gueststar_slot_settings.go b/api/gueststar/update_gueststar_slot_settings.go index eae0e65..a93eafa 100644 --- a/api/gueststar/update_gueststar_slot_settings.go +++ b/api/gueststar/update_gueststar_slot_settings.go @@ -24,18 +24,18 @@ type UpdateGuestStarSlotSettingsParams struct { // Flag indicating whether the slot is allowed to share their audio with the rest of the session. // If false, the slot will be muted in any views containing the slot. - IsAudioEnabled *bool `url:"is_audio_enabled"` + IsAudioEnabled *bool `url:"is_audio_enabled,omitempty"` // Flag indicating whether the slot is allowed to share their video with the rest of the session. // If false, the slot will have no video shared in any views containing the slot. - IsVideoEnabled *bool `url:"is_video_enabled"` + IsVideoEnabled *bool `url:"is_video_enabled,omitempty"` // Flag indicating whether the user assigned to this slot is visible/can be heard from any public subscriptions. // Generally, this determines whether or not the slot is enabled in any broadcasting software integrations. - IsLive *bool `url:"is_live"` + IsLive *bool `url:"is_live,omitempty"` // Value from 0-100 that controls the audio volume for shared views containing the slot. - Volume *int `url:"volume"` + Volume *int `url:"volume,omitempty"` } // Allows a user to update slot settings for a particular guest within a Guest Star session, such as allowing the user to share audio or video within the call as a host. These settings will be broadcasted to all subscribers which control their view of the guest in that slot. One or more of the optional parameters to this API can be specified at any time. diff --git a/api/hypetrain/get_hypetrain_events.go b/api/hypetrain/get_hypetrain_events.go index 929f886..1664787 100644 --- a/api/hypetrain/get_hypetrain_events.go +++ b/api/hypetrain/get_hypetrain_events.go @@ -19,11 +19,11 @@ type GetHypeTrainEventsParams struct { // The maximum number of items to return per page in the response. // The minimum page size is 1 item per page and the maximum is 100 items per page. // The default is 1. - First *int `url:"first"` + First *int `url:"first,omitempty"` // The cursor used to get the next page of results. The Pagination object in the response contains the cursor’s value. // Read More: https://dev.twitch.tv/docs/api/guide#pagination - After *types.Cursor `url:"after"` + After *types.Cursor `url:"after,omitempty"` } type Event struct { diff --git a/api/moderation/get_banned_users.go b/api/moderation/get_banned_users.go index 90475cf..363b726 100644 --- a/api/moderation/get_banned_users.go +++ b/api/moderation/get_banned_users.go @@ -22,20 +22,20 @@ type GetBannedUsersParams struct { // // The returned list includes only those users that were banned or put in a timeout. // The list is returned in the same order that you specified the IDs. - UserID []string `url:"user_id"` + UserID []string `url:"user_id,omitempty"` // The maximum number of items to return per page in the response. // The minimum page size is 1 item per page and the maximum is 100 items per page. // The default is 20. - First *int `url:"first"` + First *int `url:"first,omitempty"` // The cursor used to get the next page of results. The Pagination object in the response contains the cursor’s value. // Read More: https://dev.twitch.tv/docs/api/guide#pagination - After *types.Cursor `url:"after"` + After *types.Cursor `url:"after,omitempty"` // The cursor used to get the previous page of results. The Pagination object in the response contains the cursor’s value. // Read More: https://dev.twitch.tv/docs/api/guide#pagination - Before *types.Cursor `url:"before"` + Before *types.Cursor `url:"before,omitempty"` } type GetBannedUsersResponse struct { diff --git a/api/moderation/get_blocked_terms.go b/api/moderation/get_blocked_terms.go index fbcb0ec..bfdeb22 100644 --- a/api/moderation/get_blocked_terms.go +++ b/api/moderation/get_blocked_terms.go @@ -21,10 +21,10 @@ type GetBlockedTermsParams struct { // The maximum number of items to return per page in the response. // The minimum page size is 1 item per page and the maximum is 100 items per page. // The default is 20. - First *int `url:"first"` + First *int `url:"first,omitempty"` // The cursor used to get the next page of results. The Pagination object in the response contains the cursor’s value. - After *types.Cursor `url:"after"` + After *types.Cursor `url:"after,omitempty"` } type GetBlockedTermsResponse struct { diff --git a/api/moderation/get_moderated_channels.go b/api/moderation/get_moderated_channels.go index 3bf0e97..93c4d67 100644 --- a/api/moderation/get_moderated_channels.go +++ b/api/moderation/get_moderated_channels.go @@ -15,12 +15,12 @@ type GetModeratedChannelsParams struct { UserID string `url:"user_id"` // The cursor used to get the next page of results. The Pagination object in the response contains the cursor’s value. - After *types.Cursor `url:"after"` + After *types.Cursor `url:"after,omitempty"` // The maximum number of items to return per page in the response. // The minimum page size is 1 item per page and the maximum is 100 items per page. // The default is 20. - First *int `url:"first"` + First *int `url:"first,omitempty"` } type GetModeratedChannelsResponse struct { diff --git a/api/moderation/get_moderators.go b/api/moderation/get_moderators.go index 5b24111..fd693bd 100644 --- a/api/moderation/get_moderators.go +++ b/api/moderation/get_moderators.go @@ -21,16 +21,16 @@ type GetModeratorsParams struct { // // The returned list includes only the users from the list who are moderators in the broadcaster’s channel. // The list is returned in the same order as you specified the IDs. - UserID []string `url:"user_id"` + UserID []string `url:"user_id,omitempty"` // The maximum number of items to return per page in the response. // The minimum page size is 1 item per page and the maximum is 100 items per page. // The default is 20. - First *int `url:"first"` + First *int `url:"first,omitempty"` // The cursor used to get the next page of results. The Pagination object in the response contains the cursor’s value. // Read More: https://dev.twitch.tv/docs/api/guide#pagination - After *types.Cursor `url:"after"` + After *types.Cursor `url:"after,omitempty"` } type GetModeratorsResponse struct { diff --git a/api/moderation/get_vips.go b/api/moderation/get_vips.go index 8b72ebe..4537477 100644 --- a/api/moderation/get_vips.go +++ b/api/moderation/get_vips.go @@ -15,7 +15,7 @@ type GetVIPsParams struct { // To specify more than one user, include the user_id parameter for each user to get. For example, &user_id=1234&user_id=5678. // The maximum number of IDs that you may specify is 100. // Ignores the ID of those users in the list that aren’t VIPs. - UserIDs []string `url:"user_id"` + UserIDs []string `url:"user_id,omitempty"` // The ID of the broadcaster whose list of VIPs you want to get. This ID must match the user ID in the access token. BroadcasterID string `url:"broadcaster_id"` @@ -23,12 +23,12 @@ type GetVIPsParams struct { // The maximum number of items to return per page in the response. // The minimum page size is 1 item per page and the maximum is 100 items per page. // The default is 20. - First *int `url:"first"` + First *int `url:"first,omitempty"` // The cursor used to get the next page of results. // The Pagination object in the response contains the cursor’s value. // Read More: https://dev.twitch.tv/docs/api/guide#pagination - After *types.Cursor `url:"after"` + After *types.Cursor `url:"after,omitempty"` } type GetVIPsResponse struct { diff --git a/api/polls/create_poll.go b/api/polls/create_poll.go index 04336ba..00f1337 100644 --- a/api/polls/create_poll.go +++ b/api/polls/create_poll.go @@ -3,39 +3,38 @@ package polls import ( "context" "encoding/json" + "io" "net/http" "net/url" - - "github.com/google/go-querystring/query" ) -type CreatePollParams struct { +type CreatePollRequest struct { // The ID of the broadcaster that’s running the poll. This ID must match the user ID in the user access token. - BroadcasterID string `url:"broadcaster_id"` + BroadcasterID string `json:"broadcaster_id"` // The question that viewers will vote on. For example, What game should I play next? The question may contain a maximum of 60 characters. - Title string `url:"title"` + Title string `json:"title"` // A list of choices that viewers may choose from. The list must contain a minimum of 2 choices and up to a maximum of 5 choices. - Choices []CreateChoice `url:"choices"` + Choices []CreateChoice `json:"choices"` // The length of time (in seconds) that the poll will run for. The minimum is 15 seconds and the maximum is 1800 seconds (30 minutes). - Duration int `url:"duration"` + Duration int `json:"duration"` // A Boolean value that indicates whether viewers may cast additional votes using Channel Points. // If true, the viewer may cast more than one vote but each additional vote costs the number of Channel Points specified in channel_points_per_vote. // The default is false (viewers may cast only one vote). For information about Channel Points, see Channel Points Guide. - ChannelPointsVotingEnabled bool `url:"channel_points_voting_enabled"` + ChannelPointsVotingEnabled bool `json:"channel_points_voting_enabled"` // The number of points that the viewer must spend to cast one additional vote. The minimum is 1 and the maximum is 1000000. // Set only if ChannelPointsVotingEnabled is true. - ChannelPointsPerVote int `url:"channel_points_per_vote"` + ChannelPointsPerVote int `json:"channel_points_per_vote"` } type CreateChoice struct { // One of the choices the viewer may select. The choice may contain a maximum of 25 characters. - Title string `url:"title"` + Title string `json:"title"` } type CreatePollResponse struct { @@ -48,11 +47,20 @@ type CreatePollResponse struct { // The poll begins as soon as it’s created. You may run only one poll at a time. // // Requires a user access token that includes the channel:manage:polls scope. -func (p *Polls) CreatePoll(ctx context.Context, params *CreatePollParams) (*CreatePollResponse, error) { - v, _ := query.Values(params) - endpoint := p.baseUrl.ResolveReference(&url.URL{Path: "polls", RawQuery: v.Encode()}) +func (p *Polls) CreatePoll(ctx context.Context, body *CreatePollRequest) (*CreatePollResponse, error) { + endpoint := p.baseUrl.ResolveReference(&url.URL{Path: "polls"}) - req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), nil) + r, w := io.Pipe() + + go func() { + if err := json.NewEncoder(w).Encode(body); err != nil { + w.CloseWithError(err) + } else { + w.Close() + } + }() + + req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), r) if err != nil { return nil, err } diff --git a/api/polls/get_polls.go b/api/polls/get_polls.go index 2de7ab2..8fbdb60 100644 --- a/api/polls/get_polls.go +++ b/api/polls/get_polls.go @@ -18,16 +18,16 @@ type GetPollsParams struct { // A list of IDs that identify the polls to return. // To specify more than one ID, include this parameter for each poll you want to get. For example, id=1234&id=5678. // You may specify a maximum of 20 IDs. - ID []string `url:"id"` + ID []string `url:"id,omitempty"` // The maximum number of items to return per page in the response. // The minimum page size is 1 item per page and the maximum is 20 items per page. // The default is 20. - First *int `url:"first"` + First *int `url:"first,omitempty"` // The cursor used to get the next page of results. The Pagination object in the response contains the cursor’s value. // Read More: https://dev.twitch.tv/docs/api/guide#pagination - After *types.Cursor `url:"after"` + After *types.Cursor `url:"after,omitempty"` } type GetPollsResponse struct { diff --git a/auth/authorize.go b/auth/authorize.go index 0fd9b60..500616a 100644 --- a/auth/authorize.go +++ b/auth/authorize.go @@ -12,11 +12,11 @@ type AuthorizeParams struct { // For information about claims, see Requesting claims: https://dev.twitch.tv/docs/authentication/getting-tokens-oidc/#requesting-claims // // Only used for OIDC auth flows - Claims *Claims `url:"claims"` + Claims *Claims `url:"claims,omitempty"` // Set to true to force the user to re-authorize your app’s access to their resources. // The default is false. - ForceVerify *bool `url:"force_verify"` + ForceVerify *bool `url:"force_verify,omitempty"` // Although optional, you are strongly encouraged to pass a nonce string to help // prevent Cross-Site Request Forgery (CSRF) attacks. The server returns this string @@ -25,7 +25,7 @@ type AuthorizeParams struct { // generated and unique for each OAuth request. // // Only used for OIDC auth flows - Nonce *string `url:"nonce"` + Nonce *string `url:"nonce,omitempty"` // Must be set to code for Authorization code grant flow. // Recommended for Server-to-Server flows. (with backend) @@ -45,7 +45,7 @@ type AuthorizeParams struct { // the URI). If this string doesn’t match the state string that you passed, ignore // the response. The state string should be randomly generated and unique for each // OAuth request. - State *string `url:"state"` + State *string `url:"state,omitempty"` } const AuthorizeUrl = "https://id.twitch.tv/oauth2/authorize"