Giter VIP home page Giter VIP logo

Comments (5)

spacemanspiff2007 avatar spacemanspiff2007 commented on June 1, 2024

#42 is what caused the change

from darksky.

allenlawrence94 avatar allenlawrence94 commented on June 1, 2024

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.

spacemanspiff2007 avatar spacemanspiff2007 commented on June 1, 2024

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.

allenlawrence94 avatar allenlawrence94 commented on June 1, 2024

That looks awesome to me. Seems like you should go ahead and open a PR!

from darksky.

Detrous avatar Detrous commented on June 1, 2024

Thank you!
Release 1.9.0 now available.

from darksky.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.