go-twitch/api/channelpoints/get_custom_reward_redemptio...

81 lines
3.0 KiB
Go
Raw Normal View History

2024-02-27 22:13:57 -05:00
package channelpoints
import (
"context"
2024-02-27 22:13:57 -05:00
"encoding/json"
"fmt"
"net/http"
2024-02-27 22:13:57 -05:00
"github.com/google/go-querystring/query"
"go.fifitido.net/twitch/api/endpoint"
2024-02-27 22:13:57 -05:00
"go.fifitido.net/twitch/api/types"
)
type GetCustomRewardRedemptionParams struct {
// The ID of the broadcaster that owns the custom reward. This ID must match the user ID found in the user OAuth token.
BroadcasterID string `url:"broadcaster_id"`
// The ID that identifies the custom reward whose redemptions you want to get.
RewardID string `url:"reward_id"`
// The status of the redemptions to return. The possible case-sensitive values are:
//
// NOTE: This field is required only if you dont specify the id query parameter.
//
// NOTE: Canceled and fulfilled redemptions are returned for only a few days after theyre canceled or fulfilled.
Status *RewardRedemptionStatus `url:"status,omitempty"`
// A list of IDs to filter the redemptions by. To specify more than one ID, include this parameter for each redemption you want to get.
// For example, id=1234&id=5678. You may specify a maximum of 50 IDs.
//
// Duplicate IDs are ignored. The response contains only the IDs that were found. If none of the IDs were found, the response is 404 Not Found.
IDs []string `url:"id,omitempty"`
// The order to sort redemptions by. The default is OLDEST.
Sort *types.SortOrder `url:"sort,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 *string `url:"after,omitempty"`
// The maximum number of redemptions to return per page in the response.
// The minimum page size is 1 redemption per page and the maximum is 50.
// The default is 20.
First *int `url:"first,omitempty"`
}
type GetCustomRewardRedemptionResponse struct {
// The list of redemptions for the specified reward. The list is empty if there are no redemptions that match the redemption criteria.
Data []CustomRewardRedemption `json:"data"`
}
// Gets a list of redemptions for the specified custom reward. The app used to create the reward is the only app that may get the redemptions.
//
// Requires a user access token that includes the channel:read:redemptions or channel:manage:redemptions scope.
func (c *ChannelPoints) GetCustomRewardRedemption(ctx context.Context, params *GetCustomRewardRedemptionParams) (*GetCustomRewardRedemptionResponse, error) {
2024-02-27 22:13:57 -05:00
v, _ := query.Values(params)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.Make(c.baseUrl, "channel_points/custom_rewards/redemptions", v), nil)
2024-02-27 22:13:57 -05:00
if err != nil {
return nil, err
}
res, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
2024-02-27 22:13:57 -05:00
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
if !statusOK {
return nil, fmt.Errorf("failed to get custom reward redemptions (%d)", res.StatusCode)
}
2024-02-27 22:13:57 -05:00
var data GetCustomRewardRedemptionResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
2024-02-27 22:13:57 -05:00
return nil, err
}
return &data, nil
}