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

Commit

Permalink
Merge pull request #54 from spacemanspiff2007/develop
Browse files Browse the repository at this point in the history
async fixes
  • Loading branch information
Detrous committed May 2, 2020
2 parents 053ea9f + eaa5214 commit 4ff1c76
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 21 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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from setuptools import find_packages, setup

__version__ = "1.8.0"
__version__ = "1.9.0"


with open(os.path.join(
Expand Down
4 changes: 3 additions & 1 deletion tests/test_forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys
from datetime import datetime

import aiohttp
import aioresponses
import mock
import pytest
Expand Down Expand Up @@ -42,7 +43,8 @@ async def get_async_data():

result = await darksky.get_forecast(
DATA["latitude"],
DATA["longitude"]
DATA["longitude"],
client_session=aiohttp.ClientSession()
)

return result
Expand Down

0 comments on commit 4ff1c76

Please sign in to comment.