deta / deta-python

deta's official sdk
https://deta.space/docs/en/build/reference/sdk/base
MIT License
153 stars 26 forks source link

External session for _AsyncBase #70

Closed jnsougata closed 2 years ago

jnsougata commented 2 years ago

Previously it was just creating the client session internally. There's no option to use external session. Now as session is an optioal arg user may pass in an external session otherwise it will create it's own session automatically.

abdelhai commented 2 years ago

hi @jnsougata could you explain (with use case code) how this behavior is better?

jnsougata commented 2 years ago

hi @jnsougata could you explain (with use case code) how this behavior is better?

Let's say I am making a discord bot using aiohttp. So I will already be having an aiohttp client session. If I want to integrate async base there, then I will like to use that client session rather than internally created client session. In this case if we let async base create another session internally that will increase the number of sessions which is no good.

For example: The connect method actually assigns the session to be used for API calls. This is how I made my lib. I want it into this lib for more flexibility...

async def setup_hook(self) -> None:
        deta = Deta(os.getenv('DETA_TOKEN'))
        await deta.connect(session=self.http._HTTPClient__session, loop=self.loop)
        self.db = deta.base('01PIXEL')
        self.drive = deta.drive('PixeL_@11223344')
abdelhai commented 2 years ago

@jnsougata, thank you for the additional useful input! seems like we need to rethink the async-base spec. we will start working on it soon and use your PR as a reference.

abdelhai commented 2 years ago

i'm merging this into the next branch which will be the base for the major async release. thanks!