go-twitch/api/polls/get_polls.go

72 lines
2.3 KiB
Go
Raw Normal View History

2024-03-03 15:27:37 -05:00
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
}