Fix optional query params by adding ,omitempty and fix create polls to use request body

This commit is contained in:
Evan Fiordeliso 2024-03-05 22:29:23 -05:00
parent 39f46618ab
commit 4803b82eba
13 changed files with 52 additions and 44 deletions

View File

@ -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 broadcasters ID. Returns the list of users that follow this broadcaster.
BroadcasterID string `url:"broadcaster_id"`

View File

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

View File

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

View File

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

View File

@ -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 cursors 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 {

View File

@ -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 cursors 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 cursors 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 {

View File

@ -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 cursors value.
After *types.Cursor `url:"after"`
After *types.Cursor `url:"after,omitempty"`
}
type GetBlockedTermsResponse struct {

View File

@ -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 cursors 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 {

View File

@ -21,16 +21,16 @@ type GetModeratorsParams struct {
//
// The returned list includes only the users from the list who are moderators in the broadcasters 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 cursors 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 {

View File

@ -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 arent 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 cursors 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 {

View File

@ -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 thats 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 its 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
}

View File

@ -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 cursors 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 {

View File

@ -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 apps 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 doesnt 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"