2024-03-03 15:27:37 -05:00
|
|
|
|
package polls
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
2024-03-07 19:41:05 -05:00
|
|
|
|
"fmt"
|
2024-03-03 15:27:37 -05:00
|
|
|
|
"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.
|
2024-03-05 22:29:23 -05:00
|
|
|
|
ID []string `url:"id,omitempty"`
|
2024-03-03 15:27:37 -05:00
|
|
|
|
|
|
|
|
|
// 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.
|
2024-03-05 22:29:23 -05:00
|
|
|
|
First *int `url:"first,omitempty"`
|
2024-03-03 15:27:37 -05:00
|
|
|
|
|
|
|
|
|
// 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
|
2024-03-05 22:29:23 -05:00
|
|
|
|
After *types.Cursor `url:"after,omitempty"`
|
2024-03-03 15:27:37 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
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()
|
|
|
|
|
|
2024-03-07 19:41:05 -05:00
|
|
|
|
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
|
|
|
|
|
if !statusOK {
|
|
|
|
|
return nil, fmt.Errorf("failed to get polls (%d)", res.StatusCode)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-03 15:27:37 -05:00
|
|
|
|
var data GetPollsResponse
|
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &data, nil
|
|
|
|
|
}
|