Comments (5)
#42 is what caused the change
from darksky.
The bug is that the client_session
passed into RequestMangerAsync.make_request
is used as a context manager, when it should just be used directly. I think an ideal usage of DarkSkyAsync
would look like this:
from aiohttp import ClientSession
import asyncio
import os
from darksky.api import DarkSkyAsync
API_KEY = os.getenv('DARKSKY_SECRET_KEY')
async def test():
client = DarkSkyAsync(API_KEY)
async with ClientSession() as session:
forecast1 = await client.get_forecast(
latitude=40,
longitude=-80,
lang='de',
client_session=session
)
print(forecast1.currently)
async with session.get('https://google.com') as google_response:
print(await google_response.text())
forecast2 = await client.get_forecast(
latitude=40,
longitude=-80,
lang='de',
client_session=session
)
print(forecast2.currently)
if __name__ == '__main__':
asyncio.get_event_loop().run_until_complete(test())
I tossed in a request to google to show what I mean by using the same session for non-darksky requests.
This code still fails for the same reason as yours. Fixing this bug would require little more than removing line 49 of request_manager.py. All I'm saying is that DarkSkyAsync
shouldn't directly manage an aiohttp.ClientSession
- it should use a ClientSession instance that is being managed at a higher level.
from darksky.
This code still fails for the same reason as yours. Fixing this bug would require little more than removing line 49 of request_manager.py. All I'm saying is that DarkSkyAsync shouldn't directly manage an aiohttp.ClientSession - it should use a ClientSession instance that is being managed at a higher level.
That would make sense. Imho it should be enough if you remove line 49 of the request_manager.py and remove the ClientSession creation. Of course client_session then has to be mandatory in get_forecast
.
class RequestMangerAsync(BaseRequestManger):
async def make_request(
self,
url: str,
client_session: ClientSession,
**params
):
assert isinstance(client_session, ClientSession), type(client_session)
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.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
from darksky.
That looks awesome to me. Seems like you should go ahead and open a PR!
from darksky.
Thank you!
Release 1.9.0 now available.
from darksky.
Related Issues (15)
- DailyForecast HOT 1
- Make RequestManger instance non private HOT 6
- HourlyForecast and MinutelyForecast HOT 2
- Currently section does not include nearestStormBearing HOT 1
- Unable to specify timezone from forecast request HOT 3
- Time Machine support HOT 2
- Single ClientSession instance is used for lifetime of DarkSkyAsync instance HOT 6
- unexpected keyword argument 'units' HOT 2
- Mac OSX having issue importing HOT 1
- Syntax error in api.py on Linux Raspbian? HOT 3
- Add a to_frame() method to forecast class HOT 2
- [BUG] HOT 1
- Issue trying to import, no module found HOT 4
- Why aiohttp==3.5.4 in requirements.txt?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from darksky.