diff --git a/twitch_hdt_ebs/twitch.py b/twitch_hdt_ebs/twitch.py index 8b1e81d..64dfe61 100644 --- a/twitch_hdt_ebs/twitch.py +++ b/twitch_hdt_ebs/twitch.py @@ -18,6 +18,9 @@ class TwitchClient: API_EXTENSION_REQUIRED_CONFIGURATION = ( API_ROOT + "/extensions/required_configuration" ) + API_EXTENSION_CONFIGURATIONS = ( + API_ROOT + "/extensions/configurations" + ) API_GET_STREAMS = API_ROOT + "/streams" API_GET_USERS = API_ROOT + "/users" API_GET_VIDEOS = API_ROOT + "/videos" @@ -81,6 +84,23 @@ def set_extension_required_configuration( return self.put(endpoint, data=data, params=params, authorization=authorization) + def set_extension_configuration_segment( + self, channel_id: str, segment: str, version: str, + ) -> requests.Response: + endpoint = self.API_EXTENSION_CONFIGURATIONS + data = { + "broadcaster_id": channel_id, + "extension_id": self.client_id, + "segment": segment, + "version": str(version), + } + authorization = self.get_ebs_authorization(channel_id) + + return self.put( + endpoint, data=data, authorization=authorization, + content_type="application/json", + ) + def get_user_stream(self, user_id: str): endpoint = self.API_GET_STREAMS authorization = f"Bearer {get_twitch_app_access_token()}" @@ -148,7 +168,10 @@ def post( def put( self, url: str, data: dict, params: dict = None, - authorization: str = "", timeout: int = DEFAULT_TIMEOUT + authorization: str = "", timeout: int = DEFAULT_TIMEOUT, + content_type=None, ) -> requests.Response: headers = self.get_headers(authorization) + if content_type is not None: + headers["Content-Type"] = content_type return requests.put(url, params=params, headers=headers, json=data, timeout=timeout) diff --git a/twitch_hdt_ebs/views.py b/twitch_hdt_ebs/views.py index 9e386a1..878cd9f 100644 --- a/twitch_hdt_ebs/views.py +++ b/twitch_hdt_ebs/views.py @@ -254,14 +254,32 @@ def post(self, request, format=None) -> Response: if not authorized_apps.count(): raise PermissionDenied({"error": "upstream_client_not_found"}) - value = "COMPLETE" + use_legacy_configuration = version == "1.3.0" + if use_legacy_configuration: + try: + resp = twitch_client.set_extension_required_configuration( + version=version, value="COMPLETE", channel_id=self.request.twitch_user_id + ) + except Timeout: + raise TwitchAPITimeout() try: - resp = twitch_client.set_extension_required_configuration( - version=version, value=value, channel_id=self.request.twitch_user_id + new_resp = twitch_client.set_extension_configuration_segment( + channel_id=self.request.twitch_user_id, + segment="developer", + version=1, ) + write_point( + "new_extension_configuration", + {"count": 1, "user_id": self.request.twitch_user_id}, + version=version, + status_code=new_resp.status_code, + ) + if not use_legacy_configuration: + resp = new_resp except Timeout: - raise TwitchAPITimeout() + if not use_legacy_configuration: + raise TwitchAPITimeout() if resp.status_code > 299: try: @@ -279,7 +297,7 @@ def post(self, request, format=None) -> Response: } ) - return Response({"required_configuration": value}) + return Response() class SetConfigView(BaseTwitchAPIView):