Add Poll endpoints to API

This commit is contained in:
Evan Fiordeliso 2024-03-03 15:27:37 -05:00
parent e941e414e2
commit 11de434482
6 changed files with 297 additions and 0 deletions

View File

@ -21,6 +21,7 @@ import (
"go.fifitido.net/twitch/api/gueststar" "go.fifitido.net/twitch/api/gueststar"
"go.fifitido.net/twitch/api/hypetrain" "go.fifitido.net/twitch/api/hypetrain"
"go.fifitido.net/twitch/api/moderation" "go.fifitido.net/twitch/api/moderation"
"go.fifitido.net/twitch/api/polls"
) )
const HelixBaseUrl = "https://api.twitch.tv/helix" const HelixBaseUrl = "https://api.twitch.tv/helix"
@ -46,6 +47,7 @@ type API struct {
GuestStar *gueststar.GuestStar GuestStar *gueststar.GuestStar
Hypetrain *hypetrain.Hypetrain Hypetrain *hypetrain.Hypetrain
Moderation *moderation.Moderation Moderation *moderation.Moderation
Polls *polls.Polls
} }
func New() *API { func New() *API {
@ -73,5 +75,6 @@ func New() *API {
GuestStar: gueststar.New(client, baseUrl), GuestStar: gueststar.New(client, baseUrl),
Hypetrain: hypetrain.New(client, baseUrl), Hypetrain: hypetrain.New(client, baseUrl),
Moderation: moderation.New(client, baseUrl), Moderation: moderation.New(client, baseUrl),
Polls: polls.New(client, baseUrl),
} }
} }

72
api/polls/create_poll.go Normal file
View File

@ -0,0 +1,72 @@
package polls
import (
"context"
"encoding/json"
"net/http"
"net/url"
"github.com/google/go-querystring/query"
)
type CreatePollParams 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"`
// 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"`
// 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"`
// 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"`
// 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"`
// 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"`
}
type CreateChoice struct {
// One of the choices the viewer may select. The choice may contain a maximum of 25 characters.
Title string `url:"title"`
}
type CreatePollResponse struct {
// A list that contains the single poll that you created.
Data []Poll `json:"data"`
}
// Creates a poll that viewers in the broadcasters channel can vote on.
//
// 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()})
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), nil)
if err != nil {
return nil, err
}
res, err := p.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
var data CreatePollResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}

46
api/polls/end_poll.go Normal file
View File

@ -0,0 +1,46 @@
package polls
import (
"context"
"net/http"
"net/url"
"github.com/google/go-querystring/query"
)
type EndPollParams 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"`
// The ID of the poll to update.
ID string `url:"id"`
// The status to set the poll to. Possible case-sensitive values are:
//
// TERMINATED — Ends the poll before the poll is scheduled to end. The poll remains publicly visible.
//
// ARCHIVED — Ends the poll before the poll is scheduled to end, and then archives it so it's no longer publicly visible.
Status Status `url:"status"`
}
// Ends an active poll. You have the option to end it or end it and archive it.
//
// Requires a user access token that includes the channel:manage:polls scope.
func (p *Polls) EndPoll(ctx context.Context, params *EndPollParams) error {
v, _ := query.Values(params)
endpoint := p.baseUrl.ResolveReference(&url.URL{Path: "polls", RawQuery: v.Encode()})
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, endpoint.String(), nil)
if err != nil {
return err
}
res, err := p.client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
return nil
}

71
api/polls/get_polls.go Normal file
View File

@ -0,0 +1,71 @@
package polls
import (
"context"
"encoding/json"
"net/http"
"net/url"
"github.com/google/go-querystring/query"
"go.fifitido.net/twitch/api/types"
)
type GetPollsParams struct {
// The ID of the broadcaster that created the polls. This ID must match the user ID in the user access token.
BroadcasterID string `url:"broadcaster_id"`
// 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"`
// 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"`
// 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"`
}
type GetPollsResponse struct {
// A list of polls. The polls are returned in descending order of start time unless you specify IDs in the request,
// in which case theyre returned in the same order as you passed them in the request.
// The list is empty if the broadcaster hasnt created polls.
Data []Poll `json:"data"`
// Contains information about the pagination in the response.
// The object is empty if there are no more pages of results.
// Read More: https://dev.twitch.tv/docs/api/guide#pagination
Pagination types.Pagination `json:"pagination"`
}
// Gets a list of polls that the broadcaster created.
//
// Polls are available for 90 days after theyre created.
//
// Requires a user access token that includes the channel:read:polls or channel:manage:polls scope.
func (p *Polls) GetPolls(ctx context.Context, params *GetPollsParams) (*GetPollsResponse, error) {
v, _ := query.Values(params)
endpoint := p.baseUrl.ResolveReference(&url.URL{Path: "polls", RawQuery: v.Encode()})
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), nil)
if err != nil {
return nil, err
}
res, err := p.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
var data GetPollsResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}

87
api/polls/models.go Normal file
View File

@ -0,0 +1,87 @@
package polls
import "time"
type Poll struct {
// An ID that identifies the poll.
ID string `json:"id"`
// An ID that identifies the broadcaster that created the poll.
BroadcasterID string `json:"broadcaster_id"`
// The broadcasters display name.
BroadcasterName string `json:"broadcaster_name"`
// The broadcasters login name.
BroadcasterLogin string `json:"broadcaster_login"`
// The question that viewers are voting on. For example, What game should I play next? The title may contain a maximum of 60 characters.
Title string `json:"title"`
// A list of choices that viewers can choose from. The list will contain a minimum of two choices and up to a maximum of five choices.
Choices []Choice `json:"choices"`
// Not used; will be set to false.
BitsVotingEnabled bool `json:"bits_voting_enabled"`
// Not used; will be set to 0.
BitsPerVote int `json:"bits_per_vote"`
// A Boolean value that indicates whether viewers may cast additional votes using Channel Points.
// For information about Channel Points, see Channel Points Guide: https://help.twitch.tv/s/article/channel-points-guide
ChannelPointsVotingEnabled bool `json:"channel_points_voting_enabled"`
// The number of points the viewer must spend to cast one additional vote.
ChannelPointsPerVote int `json:"channel_points_per_vote"`
// The polls status.
Status Status `json:"status"`
// The length of time (in seconds) that the poll will run for.
Duration int `json:"duration"`
// The UTC date and time (in RFC3339 format) of when the poll began.
StartedAt time.Time `json:"started_at"`
// The UTC date and time (in RFC3339 format) of when the poll ended. If status is ACTIVE, this field is set to null.
EndedAt time.Time `json:"ended_at"`
}
type Choice struct {
// An ID that identifies this choice.
ID string `json:"id"`
// The choices title. The title may contain a maximum of 25 characters.
Title string `json:"title"`
// The total number of votes cast for this choice.
Votes int `json:"votes"`
// The number of votes cast using Channel Points.
ChannelPointsVotes int `json:"channel_points_votes"`
// Not used; will be set to 0.
BitsVotes int `json:"bits_votes"`
}
type Status string
const (
// The poll is running.
Active Status = "ACTIVE"
// The poll ended on schedule (see the duration field).
Completed Status = "COMPLETED"
// The poll was terminated before its scheduled end.
Terminated Status = "TERMINATED"
// The poll has been archived and is no longer visible on the channel.
Archived Status = "ARCHIVED"
// The poll was deleted.
Moderated Status = "MODERATED"
// Something went wrong while determining the state.
Invalid Status = "INVALID"
)

18
api/polls/polls.go Normal file
View File

@ -0,0 +1,18 @@
package polls
import (
"net/http"
"net/url"
)
type Polls struct {
baseUrl *url.URL
client *http.Client
}
func New(client *http.Client, baseUrl *url.URL) *Polls {
return &Polls{
client: client,
baseUrl: baseUrl,
}
}