From c969a9a1ce222d377cb119b44a2ad34e4c17ee01 Mon Sep 17 00:00:00 2001 From: Roy Olav Purser Date: Sat, 12 Feb 2022 13:51:17 +0100 Subject: [PATCH] dataclass for link --- backend/config.py | 49 ++++++++++++++++++++++++----------------------- backend/stream.py | 5 ++++- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/backend/config.py b/backend/config.py index 0d2340a..41f1149 100644 --- a/backend/config.py +++ b/backend/config.py @@ -4,7 +4,8 @@ import os import pprint import re import sys -from typing import Literal, Optional, cast +import dataclasses +from typing import Literal, cast import pydantic @@ -41,20 +42,26 @@ stream_server = os.environ.get("STREAM_SERVER") proxy_server = os.environ.get("PROXY_SERVER") +@dataclasses.dataclass +class LinkWithType: + upstream: str + ctype: str + + class ProxyCreateLink(pydantic.BaseModel): - upstream: Optional[pydantic.HttpUrl] - ctype: Optional[pydantic.StrictStr] - region: Optional[pydantic.StrictStr] + upstream: None | pydantic.HttpUrl + ctype: None | pydantic.StrictStr + region: None | pydantic.StrictStr class ProxyRequest(pydantic.BaseModel): action: Literal["create-urls", "read-config"] - urls: Optional[list[ProxyCreateLink]] + urls: None | list[ProxyCreateLink] class ProxyResponse(pydantic.BaseModel): action: Literal["create-urls", "read-config"] - urls: Optional[list[pydantic.HttpUrl]] + urls: None | list[pydantic.HttpUrl] class ProxyElem: @@ -69,36 +76,30 @@ class ProxyElem: def __repr__(self): return f" proxy=<{str(self.proxy)}>>" - async def proxy_url(self, urls): + async def proxy_url(self, urls: list[LinkWithType]): clean_urls = [] link_requests: list[ProxyCreateLink] = [] for url in urls: - if isinstance(url, tuple): - clean_urls.append(url[0]) - else: - clean_urls.append(url) + clean_urls.append(url.upstream) if not isinstance(proxy_server, str): return clean_urls try: for url in urls: - if isinstance(url, tuple): - link_requests.append( - ProxyCreateLink.parse_obj( - {"upstream": url[0], "ctype": url[1], "region": self.region} - ) - ) - else: - link_requests.append( - ProxyCreateLink.parse_obj( - {"upstream": url, "region": self.region} - ) + link_requests.append( + ProxyCreateLink.parse_obj( + { + "upstream": url.upstream, + "ctype": url.ctype, + "region": self.region, + } ) + ) except pydantic.ValidationError as e: logger.info(e) return clean_urls - response_data: ProxyRequest + request_data: ProxyRequest response_data: ProxyResponse try: @@ -118,7 +119,7 @@ class ProxyElem: except (aiohttp.ClientError, pydantic.ValidationError) as e: logger.info(e) else: - ret_data = [] + ret_data: list[None | str] = [] if response_data.urls is not None: for src, dst in zip(clean_urls, response_data.urls): if isinstance(src, str): diff --git a/backend/stream.py b/backend/stream.py index 4ed150a..0197c3b 100755 --- a/backend/stream.py +++ b/backend/stream.py @@ -134,7 +134,10 @@ class MainHandler(tornado.web.RequestHandler): else: proxied = await handler.proxy.proxy_url( [ - (provider_data.upstream(), provider_data.proxy_ctype()), + config.LinkWithType( + upstream=provider_data.upstream(), + ctype=provider_data.proxy_ctype(), + ), provider_data.thumbnail(), ] )