From 7f8d70bf40b404596c28f911aa4ab4d1410ccdc4 Mon Sep 17 00:00:00 2001 From: ashlen Date: Mon, 19 Aug 2024 15:08:56 +0200 Subject: [PATCH] Fix mypy typing with aiohttp headers (#220) --- genshin/client/components/base.py | 19 ++++++++++++++----- .../client/components/calculator/client.py | 2 +- genshin/client/components/daily.py | 2 +- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/genshin/client/components/base.py b/genshin/client/components/base.py index cbef2c87..c45beead 100644 --- a/genshin/client/components/base.py +++ b/genshin/client/components/base.py @@ -12,6 +12,7 @@ import warnings import aiohttp.typedefs +import multidict import yarl from genshin import constants, errors, types, utility @@ -30,6 +31,13 @@ AsyncCallableT = typing.TypeVar("AsyncCallableT", bound="typing.Callable[..., typing.Awaitable[object]]") +def parse_loose_headers( + loose_headers: typing.Optional[aiohttp.typedefs.LooseHeaders] = None, +) -> multidict.CIMultiDict[str]: + """Parse loose aiohttp headers.""" + return multidict.CIMultiDict((str(k), str(v)) for k, v in dict(loose_headers or ()).items()) + + class BaseClient(abc.ABC): """Base ABC Client.""" @@ -60,6 +68,7 @@ class BaseClient(abc.ABC): authkeys: typing.Dict[types.Game, str] _hoyolab_id: typing.Optional[int] _accounts: typing.Dict[types.Game, hoyolab_models.GenshinAccount] + custom_headers: multidict.CIMultiDict[str] def __init__( self, @@ -94,7 +103,7 @@ def __init__( self.uid = uid self.hoyolab_id = hoyolab_id - self.custom_headers: typing.Dict[str, str] = dict(headers or {}) + self.custom_headers = parse_loose_headers(headers) self.custom_headers.update({"x-rpc-device_id": device_id} if device_id else {}) self.custom_headers.update({"x-rpc-device_fp": device_fp} if device_fp else {}) @@ -362,7 +371,7 @@ async def request( # actual request - headers = dict(headers or {}) + headers = parse_loose_headers(headers) headers["User-Agent"] = self.USER_AGENT headers.update(self.custom_headers) @@ -409,7 +418,7 @@ async def request_webstatic( url = routes.WEBSTATIC_URL.get_url(region).join(yarl.URL(url)) - headers = dict(headers or {}) + headers = parse_loose_headers(headers) headers["User-Agent"] = self.USER_AGENT headers.update(self.custom_headers) @@ -446,7 +455,7 @@ async def request_bbs( url = routes.BBS_URL.get_url(region).join(yarl.URL(url)) - headers = dict(headers or {}) + headers = parse_loose_headers(headers) headers.update(ds.get_ds_headers(data=data, params=params, region=region, lang=lang or self.lang)) headers["Referer"] = str(routes.BBS_REFERER_URL.get_url(self.region)) @@ -474,7 +483,7 @@ async def request_hoyolab( url = routes.TAKUMI_URL.get_url(region).join(yarl.URL(url)) - headers = dict(headers or {}) + headers = parse_loose_headers(headers) headers.update(ds.get_ds_headers(data=data, params=params, region=region, lang=lang or self.lang)) data = await self.request(url, method=method, params=params, data=data, headers=headers, **kwargs) diff --git a/genshin/client/components/calculator/client.py b/genshin/client/components/calculator/client.py index a629ab84..2ba59a4d 100644 --- a/genshin/client/components/calculator/client.py +++ b/genshin/client/components/calculator/client.py @@ -41,7 +41,7 @@ async def request_calculator( ) -> typing.Mapping[str, typing.Any]: """Make a request towards the calculator endpoint.""" params = dict(params or {}) - headers = dict(headers or {}) + headers = base.parse_loose_headers(headers) base_url = routes.CALCULATOR_URL.get_url(self.region) url = base_url / endpoint diff --git a/genshin/client/components/daily.py b/genshin/client/components/daily.py index 134f5937..9b65605b 100644 --- a/genshin/client/components/daily.py +++ b/genshin/client/components/daily.py @@ -36,7 +36,7 @@ async def request_daily_reward( ) -> typing.Mapping[str, typing.Any]: """Make a request towards the daily reward endpoint.""" params = dict(params or {}) - headers = dict(headers or {}) + headers = base.parse_loose_headers(headers) if game is None: if self.default_game is None: