Skip to content
This repository has been archived by the owner on May 20, 2021. It is now read-only.

Commit

Permalink
async fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemanspiff2007 committed Apr 19, 2020
1 parent 053ea9f commit eaa5214
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
10 changes: 5 additions & 5 deletions darksky/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ async def get_forecast(
self,
latitude: float,
longitude: float,
client_session: aiohttp.ClientSession,
extend: bool = None,
lang=languages.ENGLISH,
values_units=units.AUTO,
exclude: [weather] = None,
timezone: str = None,
client_session: aiohttp.ClientSession = None
) -> Forecast:
url = self.get_url(latitude, longitude)
data = await self.request_manager.make_request(
Expand All @@ -136,7 +136,7 @@ async def get_forecast(
units=values_units,
exclude=exclude,
timezone=timezone,
client_session=client_session,
session=client_session,
)
return Forecast(**data)

Expand All @@ -145,12 +145,12 @@ async def get_time_machine_forecast(
latitude: float,
longitude: float,
time: datetime,
client_session: aiohttp.ClientSession,
extend: bool = False,
lang=languages.ENGLISH,
values_units=units.AUTO,
exclude: [weather] = None,
timezone: str = None,
client_session: aiohttp.ClientSession = None
timezone: str = None
) -> Forecast:
url = self.get_url(latitude, longitude, int(time.timestamp()))
data = await self.request_manager.make_request(
Expand All @@ -160,6 +160,6 @@ async def get_time_machine_forecast(
units=values_units,
exclude=exclude,
timezone=timezone,
client_session=client_session,
session=client_session,
)
return Forecast(**data)
22 changes: 8 additions & 14 deletions darksky/request_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,22 @@ class RequestMangerAsync(BaseRequestManger):
async def make_request(
self,
url: str,
client_session: ClientSession,
session: ClientSession,
**params
):
assert (
isinstance(client_session, ClientSession) or client_session is None
)
client_session = (
ClientSession() if client_session is None else client_session
)
assert isinstance(session, ClientSession)

for key in list(params.keys()):
if params[key] is None:
del params[key]
elif isinstance(params[key], list):
params[key] = ",".join(params[key])

async with client_session as session:
async with session.get(
url, params=params, headers=self.headers
) as resp:
response = await resp.json()
if "error" in response:
raise DarkSkyException(response["code"], response["error"])
async with session.get(
url, params=params, headers=self.headers
) as resp:
response = await resp.json()
if "error" in response:
raise DarkSkyException(response["code"], response["error"])
response["timezone"] = params.get("timezone") or response["timezone"]
return response

0 comments on commit eaa5214

Please sign in to comment.