Skip to content

Commit

Permalink
Merge pull request #186 from SaxyPandaBear/update-redemption-status
Browse files Browse the repository at this point in the history
feat: Support for update custom reward redemption status
  • Loading branch information
nicklaw5 authored Mar 2, 2023
2 parents 6b05ae1 + 9a12578 commit 4cf5042
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 1 deletion.
2 changes: 1 addition & 1 deletion SUPPORTED_ENDPOINTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
- [x] Get Custom Reward
- [ ] Get Custom Reward Redemption
- [x] Update Custom Reward
- [ ] Update Redemption Status
- [x] Update Redemption Status
- [x] Get Channel Information
- [x] Modify Channel Information
- [x] Get Channel Editors
Expand Down
45 changes: 45 additions & 0 deletions channels_points.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,36 @@ type DeleteCustomRewardsResponse struct {
ResponseCommon
}

type UpdateChannelCustomRewardsRedemptionStatusParams struct {
ID string `query:"id"`
BroadcasterID string `query:"broadcaster_id"`
RewardID string `query:"reward_id"`
Status string `json:"status"`
}

type ChannelCustomRewardsRedemptionResponse struct {
ResponseCommon
Data ManyChannelCustomRewardsRedemptions
}

type ManyChannelCustomRewardsRedemptions struct {
Redemptions []ChannelCustomRewardsRedemption `json:"data"`
}

type ChannelCustomRewardsRedemption struct {
ID string `json:"id"`
BroadcasterID string `json:"broadcaster_id"`
BroadcasterLogin string `json:"broadcaster_login"`
BroadcasterName string `json:"broadcaster_name"`
UserID string `json:"user_id"`
UserName string `json:"user_name"`
UserLogin string `json:"user_login"`
UserInput string `json:"user_input"`
Status string `json:"status"`
RedeemedAt Time `json:"redeemed_at"`
Reward ChannelCustomReward `json:"reward"`
}

// CreateCustomReward : Creates a Custom Reward on a channel.
// Required scope: channel:manage:redemptions
func (c *Client) CreateCustomReward(params *ChannelCustomRewardsParams) (*ChannelCustomRewardResponse, error) {
Expand Down Expand Up @@ -162,3 +192,18 @@ func (c *Client) GetCustomRewards(params *GetCustomRewardsParams) (*ChannelCusto

return rewards, nil
}

// UpdateChannelCustomRewardsRedemptionStatus : Update a Custom Reward Redemption status on a channel.
// Required scope: channel:manage:redemptions
func (c *Client) UpdateChannelCustomRewardsRedemptionStatus(params *UpdateChannelCustomRewardsRedemptionStatusParams) (*ChannelCustomRewardsRedemptionResponse, error) {
resp, err := c.patchAsJSON("/channel_points/custom_rewards/redemptions", &ManyChannelCustomRewardsRedemptions{}, params)
if err != nil {
return nil, err
}

redemptions := &ChannelCustomRewardsRedemptionResponse{}
resp.HydrateResponseCommon(&redemptions.ResponseCommon)
redemptions.Data.Redemptions = resp.Data.(*ManyChannelCustomRewardsRedemptions).Redemptions

return redemptions, nil
}
64 changes: 64 additions & 0 deletions channels_points_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,67 @@ func TestGetCustomRewards(t *testing.T) {
t.Error("expected error does match return error")
}
}

func TestUpdateCustomRewardRedemptionStatus(t *testing.T) {
t.Parallel()

testCases := []struct {
statusCode int
options *Options
params *UpdateChannelCustomRewardsRedemptionStatusParams
respBody string
}{
{
http.StatusOK,
&Options{ClientID: "my-client-id"},
&UpdateChannelCustomRewardsRedemptionStatusParams{
ID: "17fa2df1-ad76-4804-bfa5-a40ef63efe63",
BroadcasterID: "274637212",
RewardID: "92af127c-7326-4483-a52b-b0da0be61c01",
Status: "CANCELLED",
},
`{"data": [{"broadcaster_name": "torpedo09", "broadcaster_login": "torpedo09", "broadcaster_id": "274637212", "id": "17fa2df1-ad76-4804-bfa5-a40ef63efe63", "user_id": "274637212", "user_name": "torpedo09", "user_login": "torpedo09", "user_input": "", "status": "CANCELED", "redeemed_at": "2020-07-01T18:37:32Z", "reward": { "id": "92af127c-7326-4483-a52b-b0da0be61c01", "title": "game analysis", "prompt": "", "cost": 50000}}]}`,
},
{
http.StatusBadRequest,
&Options{ClientID: "my-client-id"},
&UpdateChannelCustomRewardsRedemptionStatusParams{},
`{"error":"Bad Request","status":400,"message":"Missing required parameter \"id\""}`,
},
}

for _, testCase := range testCases {
c := newMockClient(testCase.options, newMockHandler(testCase.statusCode, testCase.respBody, nil))

resp, err := c.UpdateChannelCustomRewardsRedemptionStatus(testCase.params)
if err != nil {
t.Error(err)
}

// Test Bad Request Responses
if resp.StatusCode == http.StatusBadRequest {
firstErrStr := "Missing required parameter \"id\""
if resp.ErrorMessage != firstErrStr {
t.Errorf("expected error message to be \"%s\", got \"%s\"", firstErrStr, resp.ErrorMessage)
}
continue
}

if resp.StatusCode != testCase.statusCode {
t.Errorf("expected status code to be \"%d\", got \"%d\"", testCase.statusCode, resp.StatusCode)
}

if testCase.statusCode == http.StatusOK {
numRedemptions := len(resp.Data.Redemptions)
if numRedemptions != 1 {
t.Errorf("expected 1 redemption, got %d", numRedemptions)
continue
}

title := resp.Data.Redemptions[0].Reward.Title
if title != "game analysis" {
t.Errorf("expected reward title to be \"game analysis\", got \"%s\"", title)
}
}
}
}
47 changes: 47 additions & 0 deletions docs/channels_points_docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,50 @@ if err != nil {

fmt.Printf("%+v\n", resp)
```

## Get Custom Reward Redemption Status

This is an example of how to get the status of a custom reward redemption.

```go
client, err := helix.NewClient(&helix.Options{
ClientID: "your-client-id",
})
if err != nil {
// handle error
}

resp, err := client.GetCustomRewards(&helix.GetCustomRewardsParams{
BroadcasterID : "145328278",
})
if err != nil {
// handle error
}

fmt.Printf("%+v\n", resp)
```

## Update Custom Reward Redemption Status

This is an example of how to update the status of a custom reward redemption.

```go
client, err := helix.NewClient(&helix.Options{
ClientID: "your-client-id",
})
if err != nil {
// handle error
}

resp, err := client.UpdateChannelCustomRewardsRedemptionStatus(&helix.UpdateChannelCustomRewardsRedemptionStatusParams{
ID : "17fa2df1-ad76-4804-bfa5-a40ef63efe63",
BroadcasterID : "274637212",
RewardID : "92af127c-7326-4483-a52b-b0da0be61c01",
Status : "FULFILLED", // The only acceptable values are "CANCELED" and "FULFILLED"
})
if err != nil {
// handle error
}

fmt.Printf("%+v\n", resp)
```

0 comments on commit 4cf5042

Please sign in to comment.