77 lines
2.5 KiB
Go
77 lines
2.5 KiB
Go
package polls
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"fmt"
|
||
"net/http"
|
||
|
||
"github.com/google/go-querystring/query"
|
||
"go.fifitido.net/twitch/api/endpoint"
|
||
"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,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,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,omitempty"`
|
||
}
|
||
|
||
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 they’re returned in the same order as you passed them in the request.
|
||
// The list is empty if the broadcaster hasn’t 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 they’re 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)
|
||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.Make(p.baseUrl, "polls", v), nil)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
res, err := p.client.Do(req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
defer res.Body.Close()
|
||
|
||
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
|
||
if !statusOK {
|
||
return nil, fmt.Errorf("failed to get polls (%d)", res.StatusCode)
|
||
}
|
||
|
||
var data GetPollsResponse
|
||
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return &data, nil
|
||
}
|