go-twitch/api/channelpoints/update_custom_reward.go

120 lines
4.4 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package channelpoints
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/google/go-querystring/query"
"go.fifitido.net/twitch/api/endpoint"
)
type UpdateCustomRewardParams struct {
// The ID of the broadcaster thats updating the reward. This ID must match the user ID found in the OAuth token.
BroadcasterID string `url:"broadcaster_id"`
// The ID of the reward to update.
ID string `url:"id"`
}
type UpdateCustomRewardRequest struct {
// The custom rewards title. The title may contain a maximum of 45 characters and it must be unique amongst all of the broadcasters custom rewards.
Title *string `json:"title"`
// The cost of the reward, in Channel Points.
// The minimum is 1 point.
Cost *int64 `json:"cost"`
// The prompt shown to the viewer when they redeem the reward.
// Specify a prompt if is_user_input_required is true.
// The prompt is limited to a maximum of 200 characters.
Prompt *string `json:"prompt"`
// A Boolean value that determines whether the reward is enabled. Viewers see only enabled rewards.
// The default is true.
IsEnabled *bool `json:"is_enabled"`
// The background color to use for the reward. Specify the color using Hex format (for example, #9147FF).
BackgroundColor *string `json:"background_color"`
// A Boolean value that determines whether the user needs to enter information when redeeming the reward. See the prompt field.
// The default is false.
IsUserInputRequired *bool `json:"is_user_input_required"`
// A Boolean value that determines whether to limit the maximum number of redemptions allowed per live stream (see the max_per_stream field).
// The default is false.
IsMaxPerStreamEnabled *bool `json:"is_max_per_stream_enabled"`
// The maximum number of redemptions allowed per live stream. Applied only if is_max_per_stream_enabled is true.
// The minimum value is 1.
MaxPerStream *int `json:"max_per_stream"`
// A Boolean value that determines whether to limit the maximum number of redemptions allowed per user per stream (see the max_per_user_per_stream field).
// The default is false.
IsMaxPerUserPerStreamEnabled *bool `json:"is_max_per_user_per_stream_enabled"`
// The maximum number of redemptions allowed per user per stream. Applied only if is_max_per_user_per_stream_enabled is true.
// The minimum value is 1.
MaxPerUserPerStream *int `json:"max_per_user_per_stream"`
// A Boolean value that determines whether to apply a cooldown period between redemptions (see the global_cooldown_seconds field for the duration of the cooldown period).
// The default is false.
IsGlobalCooldownEnabled *bool `json:"is_global_cooldown_enabled"`
// The cooldown period, in seconds. Applied only if the is_global_cooldown_enabled field is true.
// The minimum value is 1; however, the minimum value is 60 for it to be shown in the Twitch UX.
GlobalCooldownSeconds *int `json:"global_cooldown_seconds"`
// A Boolean value that determines whether redemptions should be set to FULFILLED status immediately when a reward is redeemed.
// If false, status is set to UNFULFILLED and follows the normal request queue process.
// The default is false.
ShouldRedemptionsSkipRequestQueue *bool `json:"should_redemptions_skip_request_queue"`
}
type UpdateCustomRewardResponse struct {
// The list contains the single reward that you updated.
Data []CustomReward `json:"data"`
}
// Updates a custom reward. The app used to create the reward is the only app that may update the reward.
//
// Requires a user access token that includes the channel:manage:redemptions scope.
func (c *ChannelPoints) UpdateCustomReward(ctx context.Context, params *UpdateCustomRewardParams, body *UpdateCustomRewardRequest) (*UpdateCustomRewardResponse, error) {
v, _ := query.Values(body)
r, w := io.Pipe()
go func() {
if err := json.NewEncoder(w).Encode(body); err != nil {
w.CloseWithError(err)
} else {
w.Close()
}
}()
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, endpoint.Make(c.baseUrl, "channel_points/custom_rewards", v), r)
if err != nil {
return nil, err
}
res, err := c.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 update custom reward (%d)", res.StatusCode)
}
var data UpdateCustomRewardResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}