Skip to content

Commit

Permalink
feat: start migrating to extension configuration service
Browse files Browse the repository at this point in the history
  • Loading branch information
beheh committed Aug 22, 2024
1 parent 1e124bb commit 1b434f3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
25 changes: 24 additions & 1 deletion twitch_hdt_ebs/twitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()}"
Expand Down Expand Up @@ -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)
26 changes: 23 additions & 3 deletions twitch_hdt_ebs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 1b434f3

Please sign in to comment.