supabase / supabase-py

Python Client for Supabase. Query Postgres from Flask, Django, FastAPI. Python user authentication, security policies, edge functions, file storage, and realtime data streaming. Good first issue.
https://supabase.com/docs/reference/python
MIT License
1.73k stars 205 forks source link

Unify sync and async clients #697

Open tinvaan opened 8 months ago

tinvaan commented 8 months ago

[This is more of a question at this point and not a feature request as such, so I'm diverging from the issue template.]

I noticed that there is a ton of shared and redundant code between the sync and async client classes. From what I'm seeing, there is only one place where a coroutine gets awaited in the async class (i.e while fetching the session token from AsyncGoTrueClient).

Does it make sense then to attempt to converge the two classes into one? if not, what are some of the challenges in doing so?

I've attempted a converge at https://github.com/tinvaan/supabase-py/pull/2 . It's very early days still and I haven't had a chance to test the changes completely yet. But interestingly we can get rid of a lot of redundant code in this approach and as you can see in the linked PR, the code coverage jumps up significantly as a result.

However, I'd like to hear the community's thoughts on if this was attempted before or if indeed such a change makes sense.

tinvaan commented 8 months ago

On a related note, would it make sense to return the relevant go-true client directly in auth_client.py instead of the wrapped clients ? -- why/why not?

https://github.com/supabase-community/supabase-py/blob/main/supabase/_sync/auth_client.py#L12

/cc @J0 , @olirice , @silentworks

silentworks commented 8 months ago

There are no manual duplication being done in the codebase as we have a make command we run to create the sync code. We have these separate because not all Python framework's support async and I think the libraries were originally written as a sync library. We are always open to ways of improving the codebase and will take a look at your PR.

tinvaan commented 8 months ago

Thanks @silentworks. Yeah, in that case I don't see any reason to check in the generated _sync folder in to version control. The _sync folder can be generated on the fly during the release as it is practically just a build artifact.

That said, I'd feel more confident in releasing code that we actively maintain and track in version control and has sufficient test coverage. Hence, making this unification attempt.

Please correct me if I'm wrong^

tinvaan commented 8 months ago

Just a heads up that there is a WIP changeset over at https://github.com/tinvaan/supabase-py/pull/2

eavolpe commented 2 months ago

Is there a simple working minimal tutorial for the async client? I am writing some features for a FastAPI backend and would like to understand best practices for initializing the client for some helping functions that query different tables.

Attels commented 2 months ago

Is there a simple working minimal tutorial for the async client? I am writing some features for a FastAPI backend and would like to understand best practices for initializing the client for some helping functions that query different tables.

+1, could we have some examples of FastAPI + async supabase client initialization?

J0 commented 2 months ago

cc: @silentworks @grdsdev @juancarlospaco