go-twitch/api/channelpoints/get_custom_reward.go

66 lines
2.3 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
"net/url"
"github.com/google/go-querystring/query"
)
type GetCustomRewardParams struct {
// The ID of the broadcaster whose custom rewards you want to get. This ID must match the user ID found in the OAuth token.
BroadcasterID string `url:"broadcaster_id"`
// A list of IDs to filter the rewards by. To specify more than one ID, include this parameter for each reward 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"`
// A Boolean value that determines whether the response contains only the custom rewards that the app may manage
// (the app is identified by the ID in the Client-Id header). Set to true to get only the custom rewards that the app may manage.
// The default is false.
OnlyManageableRewards *bool `url:"only_manageable_rewards,omitempty"`
}
type GetCustomRewardResponse struct {
// A list of custom rewards. The list is in ascending order by id. If the broadcaster hasnt created custom rewards, the list is empty.
Data []CustomReward `json:"data"`
}
// Gets a list of custom rewards that the specified broadcaster created.
//
// NOTE: A channel may offer a maximum of 50 rewards, which includes both enabled and disabled rewards.
//
// Requires a user access token that includes the channel:read:redemptions or channel:manage:redemptions scope.
func (c *ChannelPoints) GetCustomReward(ctx context.Context, params *GetCustomRewardParams) (*GetCustomRewardResponse, error) {
2024-02-27 22:13:57 -05:00
v, _ := query.Values(params)
endpoint := c.baseUrl.ResolveReference(&url.URL{Path: "channel_points/custom_rewards", RawQuery: v.Encode()})
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), 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 rewards (%d)", res.StatusCode)
}
2024-02-27 22:13:57 -05:00
var data GetCustomRewardResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
2024-02-27 22:13:57 -05:00
return nil, err
}
return &data, nil
}