Skip to content

Commit

Permalink
redemption methods
Browse files Browse the repository at this point in the history
  • Loading branch information
chillymosh committed Apr 27, 2024
1 parent ba4d647 commit 311edbd
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
20 changes: 19 additions & 1 deletion twitchio/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
from typing_extensions import Self, Unpack

from .assets import Asset
from .models_test.channel_points import CustomReward
from .types_.conduits import ShardData
from .types_.requests import APIRequestKwargs, HTTPMethod, ParamMapping
from .types_.responses import (
Expand All @@ -71,6 +72,7 @@
ClipsResponseData,
ConduitPayload,
ContentClassificationLabelsResponse,
CustomRewardRedemptionResponse,
CustomRewardRedemptionResponseData,
CustomRewardsResponse,
DeleteVideosResponse,
Expand All @@ -90,7 +92,6 @@
UserChatColorResponse,
VideosResponseData,
)
from .models_test.channel_points import CustomReward


logger: logging.Logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -953,3 +954,20 @@ async def converter(data: CustomRewardRedemptionResponseData) -> CustomRewardRed

iterator = self.request_paginated(route, converter=converter)
return iterator

async def patch_custom_reward_redemption(
self,
*,
broadcaster_id: str,
token_for: str,
reward_id: str,
id: str,
status: Literal["CANCELED", "FULFILLED"],
) -> CustomRewardRedemptionResponse:
params = {"broadcaster_id": broadcaster_id, "reward_id": reward_id, "id": id}
data = {"status": status}

route: Route = Route(
"PATCH", "channel_points/custom_rewards/redemptions", params=params, json=data, token_for=token_for
)
return await self.request_json(route)
54 changes: 52 additions & 2 deletions twitchio/models_test/channel_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ async def fetch_redemptions(
Parameters
-----------
token_for: str
The ID that uniquely identifies this redemption.
The user's token that has permission manage or read the broadcaster's reward redemptions.
status: Literal["CANCELED", "FULFILLED", "UNFULFILLED"]
The state of the redemption. This can be one of the following: "CANCELED", "FULFILLED", "UNFULFILLED"
ids: list[str] | None
Expand Down Expand Up @@ -407,8 +407,58 @@ def __init__(
self.status: Literal["CANCELED", "FULFILLED", "UNFULFILLED"] = data["status"]
self.redeemed_at: datetime.datetime = parse_timestamp(data["redeemed_at"])
self.reward: CustomReward = parent_reward
self._http: HTTPClient | None = http
self._http: HTTPClient = http
self.user: PartialUser = PartialUser(data["user_id"], data["user_login"], http=self._http)

def __repr__(self) -> str:
return f"<CustomRewardRedemption id={self.id} status={self.status} redeemed_at={self.redeemed_at}>"

async def fulfill(self, *, token_for: str) -> CustomRewardRedemption:
"""
Updates a redemption's status to FULFILLED.
!!! note
Requires a user access token that includes the ``channel:manage:redemptions`` scope.
Parameters
-----------
token_for: str
The user's token that has permission manage the broadcaster's reward redemptions.
Returns
--------
CustomRewardRedemption
"""
data = await self._http.patch_custom_reward_redemption(
broadcaster_id=self.reward.broadcaster.id,
id=self.id,
token_for=token_for,
reward_id=self.reward.id,
status="FULFILLED",
)
return CustomRewardRedemption(data["data"][0], parent_reward=self.reward, http=self._http)

async def refund(self, *, token_for: str) -> CustomRewardRedemption:
"""
Updates a redemption's status to CANCELED.
!!! note
Requires a user access token that includes the ``channel:manage:redemptions`` scope.
Parameters
-----------
token_for: str
The user's token that has permission manage the broadcaster's reward redemptions.
Returns
--------
CustomRewardRedemption
"""
data = await self._http.patch_custom_reward_redemption(
broadcaster_id=self.reward.broadcaster.id,
id=self.id,
token_for=token_for,
reward_id=self.reward.id,
status="CANCELED",
)
return CustomRewardRedemption(data["data"][0], parent_reward=self.reward, http=self._http)

0 comments on commit 311edbd

Please sign in to comment.