Skip to content

Commit

Permalink
Fix mypy typing with aiohttp headers (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
thesadru authored Aug 19, 2024
1 parent 6ee36e8 commit 7f8d70b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
19 changes: 14 additions & 5 deletions genshin/client/components/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import warnings

import aiohttp.typedefs
import multidict
import yarl

from genshin import constants, errors, types, utility
Expand All @@ -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."""

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {})

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion genshin/client/components/calculator/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion genshin/client/components/daily.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 7f8d70b

Please sign in to comment.