Skip to content

Commit

Permalink
🚑 Fix: UserAgentsService.load method raises error trying to convert…
Browse files Browse the repository at this point in the history
… text to json. Public client proxy support, `update_session_cookies` util func. v0.5.1
  • Loading branch information
somespecialone committed Jun 18, 2024
1 parent 6c8f324 commit d71cba1
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 36 deletions.
5 changes: 3 additions & 2 deletions aiosteampy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,11 @@ def __init__(
currency=Currency.USD,
country=DEF_COUNTRY,
session: ClientSession = None,
user_agent: str = "",
proxy: str = None,
user_agent: str = None,
):
self.language = language
self.currency = currency
self.country = country

super().__init__(session=session, user_agent=user_agent)
super().__init__(session=session, user_agent=user_agent, proxy=proxy)
34 changes: 17 additions & 17 deletions aiosteampy/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,20 +231,20 @@ async def logout(self: "SteamCommunityMixin") -> None:
)
self._is_logged = False

async def refresh_access_token(self: "SteamCommunityMixin") -> str:
""""""

# TODO this
r = await self.session.post(
STEAM_URL.API.IAuthService.GenerateAccessTokenForApp,
data={"refresh_token": self._refresh_token, "steamid": self.steam_id},
headers={**API_HEADERS, **REFERER_HEADER},
)
rj = await r.json()

try:
self._access_token = rj["response"]["access_token"]
except KeyError:
raise ApiError("Can't renew access token.", rj)

return self._access_token
# async def refresh_access_token(self: "SteamCommunityMixin") -> str:
# """"""
#
# # TODO this
# r = await self.session.post(
# STEAM_URL.API.IAuthService.GenerateAccessTokenForApp,
# data={"refresh_token": self._refresh_token, "steamid": self.steam_id},
# headers={**API_HEADERS, **REFERER_HEADER},
# )
# rj = await r.json()
#
# try:
# self._access_token = rj["response"]["access_token"]
# except KeyError:
# raise ApiError("Can't renew access token.", rj)
#
# return self._access_token
2 changes: 1 addition & 1 deletion aiosteampy/user_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def agents(self) -> list[str]:
async def load(self):
async with ClientSession(raise_for_status=True) as sess:
r = await sess.get(self._api_url / "all")
agents: list[str] = await r.json()
agents: list[str] = (await r.text()).splitlines()

self.data = agents

Expand Down
40 changes: 25 additions & 15 deletions aiosteampy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"to_int_boolean",
"restore_from_cookies",
"get_jsonable_cookies",
"update_session_cookies",
"buyer_pays_to_receive",
"receive_to_buyer_pays",
"generate_session_id",
Expand Down Expand Up @@ -240,6 +241,23 @@ def to_int_boolean(s):
JSONABLE_COOKIE_JAR: TypeAlias = list[dict[str, dict[str, str, None, bool]]]


def update_session_cookies(cookies: JSONABLE_COOKIE_JAR, session: ClientSession):
"""Update the session cookies from jsonable cookie jar."""

for cookie_data in cookies:
c = SimpleCookie()
for k, v in cookie_data.items():
copied = dict(**v) # copy to avoid modification of the arg
m = Morsel()
m._value = copied.pop("value")
m._key = copied.pop("key")
m._coded_value = copied.pop("coded_value")
m.update(copied)
c[k] = m

session.cookie_jar.update_cookies(c)


async def restore_from_cookies(
cookies: JSONABLE_COOKIE_JAR,
client: "SteamCommunityMixin",
Expand All @@ -253,27 +271,19 @@ async def restore_from_cookies(
Return `True` if cookies are valid and not expired.
"""

prepared = []
update_session_cookies(cookies, client.session)

# find access token
# TODO check this
for cookie_data in cookies:
c = SimpleCookie()
for k, v in cookie_data.items():
m = Morsel()
m._value = v.pop("value")
m._key = v.pop("key")
m._coded_value = v.pop("coded_value")
m.update(v)
c[k] = m

if m.key == "steamLoginSecure":
if v["key"] == "steamLoginSecure":
try:
client._access_token = m.value.split("%7C%7C")[1]
client._access_token = v["value"].split("%7C%7C")[1]
break
except IndexError:
pass

prepared.append(c)

for c in prepared:
client.session.cookie_jar.update_cookies(c)
if not (await client.is_session_alive()):
await client.login(init_data=init_data, **init_kwargs)
return False
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "aiosteampy"
version = "0.5.0"
version = "0.5.1"
description = "Trade and interact with steam market, webapi, guard"
license = "MIT"
authors = ["Dmytro Tkachenko <itsme@somespecial.one>"]
Expand Down

0 comments on commit d71cba1

Please sign in to comment.