go-twitch/api/channelpoints/update_redemption_status.go

80 lines
2.3 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 UpdateRedemptionStatusParams struct {
// A list of IDs that identify the redemptions to update. To specify more than one ID, include this parameter for each redemption you want to update.
// For example, id=1234&id=5678. You may specify a maximum of 50 IDs.
IDs []string `url:"id"`
// The ID of the broadcaster thats updating the redemption. This ID must match the user ID in the user access token.
BroadcasterID string `url:"broadcaster_id"`
// The ID that identifies the reward thats been redeemed.
RewardID string `url:"reward_id"`
}
type UpdateRedemptionStatusRequest struct {
// The status to set the redemption to. Possible values are:
//
// CANCELED, FULFILLED
//
// Setting the status to CANCELED refunds the users channel points.
Status RewardRedemptionStatus `json:"status"`
}
type UpdateRedemptionStatusResponse struct {
// The list contains the single redemption that you updated.
Data []CustomRewardRedemption `json:"data"`
}
// Updates a redemptions status. You may update a redemption only if its status is UNFULFILLED.
// The app used to create the reward is the only app that may update the redemption.
//
// Requires a user access token that includes the channel:manage:redemptions scope.
func (c *ChannelPoints) UpdateRedemptionStatus(ctx context.Context, params *UpdateRedemptionStatusParams, body *UpdateRedemptionStatusRequest) (*UpdateRedemptionStatusResponse, 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/redemptions", 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 redemption status (%d)", res.StatusCode)
}
var data UpdateRedemptionStatusResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}