From 1b434f3cdf0298dd73ca484c501d4038d7321f0c Mon Sep 17 00:00:00 2001 From: Benedict Etzel Date: Thu, 22 Aug 2024 13:16:45 +0200 Subject: [PATCH] feat: start migrating to extension configuration service --- twitch_hdt_ebs/twitch.py | 25 ++++++++++++++++++++++++- twitch_hdt_ebs/views.py | 26 +++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/twitch_hdt_ebs/twitch.py b/twitch_hdt_ebs/twitch.py index 8b1e81d..aebf202 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..80b0b83 100644 --- a/twitch_hdt_ebs/views.py +++ b/twitch_hdt_ebs/views.py @@ -256,12 +256,32 @@ def post(self, request, format=None) -> Response: value = "COMPLETE" + use_legacy_configuration = version == "1.3.0" + if use_legacy_configuration: + try: + resp = twitch_client.set_extension_required_configuration( + version=version, value=value, 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: