Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async WIP #777

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ gotrue = ">=1.3,<3.0"
httpx = ">=0.24,<0.28"
storage3 = ">=0.5.3,<0.8.0"
supafunc = ">=0.3.1,<0.5.0"
httpcore = {extras = ["asyncio"], version = "^1.0.5"}
pytest-asyncio = "^0.23.6"

[tool.poetry.dev-dependencies]
pre-commit = "^3.5.0"
Expand Down
10 changes: 6 additions & 4 deletions supabase/_async/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def _init_postgrest_client(
rest_url: str,
headers: Dict[str, str],
schema: str,
timeout: Union[int, float, Timeout] = DEFAULT_POSTGREST_CLIENT_TIMEOUT,
timeout: Union[int, float, Timeout] = None,
) -> AsyncPostgrestClient:
"""Private helper for creating an instance of the Postgrest client."""
return AsyncPostgrestClient(
Expand Down Expand Up @@ -285,16 +285,18 @@ async def create_client(
Examples
--------
Instantiating the client.
>>> import httpcore
>>> import asyncio
>>> import os
>>> from supabase import create_client, Client
>>> from supabase._async.client import create_client
>>>
>>> url: str = os.environ.get("SUPABASE_TEST_URL")
>>> key: str = os.environ.get("SUPABASE_TEST_KEY")
>>> supabase: Client = create_client(url, key)
>>> supabase: AsyncClient = await asyncio.create_task(create_client(url, key))

Returns
-------
Client
AsyncClient
"""
return await AsyncClient.create(
supabase_url=supabase_url, supabase_key=supabase_key, options=options
Expand Down
30 changes: 30 additions & 0 deletions tests/test_async_client_configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import asyncio
import os

import pytest

from supabase._async.client import create_client

pytest_plugins = ("pytest_asyncio", )


@pytest.mark.asyncio
async def test_async_configuration():
url: str = os.environ.get("SUPABASE_TEST_URL")
key: str = os.environ.get("SUPABASE_TEST_KEY")

"""
Initializing an AsynClient:
Using asyncio.create_task instead of asyncio.run allows you to reuse
the same client across different request, avoiding an event loop error.
"""

client = await asyncio.create_task(create_client(url, key))
data = [{"item": value} for value in range(0, 100000)]

# insert
await client.table("sample").insert(data).execute()
# select
await client.table("sample").select("*").execute()
# delete
await client.table("sample").delete().gt("item", 100).execute()
Loading