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

fix: async client options default values #937

Merged
merged 3 commits into from
Sep 28, 2024
Merged
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
8 changes: 6 additions & 2 deletions supabase/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

# Async Client
from ._async.auth_client import AsyncSupabaseAuthClient as ASupabaseAuthClient
from ._async.client import AsyncClient
from ._async.client import AsyncClient as AClient
from ._async.client import AsyncStorageClient as ASupabaseStorageClient
from ._async.client import ClientOptions as AClientOptions
from ._async.client import create_client as acreate_client
from ._async.client import create_client as create_async_client

Expand All @@ -29,7 +29,9 @@
from ._sync.client import create_client

# Lib
from .lib.client_options import ClientOptions
from .lib.client_options import AsyncClientOptions
from .lib.client_options import AsyncClientOptions as AClientOptions
from .lib.client_options import SyncClientOptions as ClientOptions

# Version
from .version import __version__
Expand All @@ -41,6 +43,8 @@
"ASupabaseAuthClient",
"ASupabaseStorageClient",
"AClientOptions",
"AsyncClient",
"AsyncClientOptions",
"create_client",
"Client",
"SupabaseAuthClient",
Expand Down
2 changes: 1 addition & 1 deletion supabase/_async/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from storage3.constants import DEFAULT_TIMEOUT as DEFAULT_STORAGE_CLIENT_TIMEOUT
from supafunc import AsyncFunctionsClient

from ..lib.client_options import ClientOptions
from ..lib.client_options import AsyncClientOptions as ClientOptions
from .auth_client import AsyncSupabaseAuthClient


Expand Down
2 changes: 1 addition & 1 deletion supabase/_sync/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from storage3.constants import DEFAULT_TIMEOUT as DEFAULT_STORAGE_CLIENT_TIMEOUT
from supafunc import SyncFunctionsClient

from ..lib.client_options import ClientOptions
from ..lib.client_options import SyncClientOptions as ClientOptions
from .auth_client import SyncSupabaseAuthClient


Expand Down
33 changes: 32 additions & 1 deletion supabase/client.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
from gotrue.errors import (
AuthApiError,
AuthError,
AuthImplicitGrantRedirectError,
AuthInvalidCredentialsError,
AuthRetryableError,
AuthSessionMissingError,
AuthUnknownError,
AuthWeakPasswordError,
)
from postgrest import APIError as PostgrestAPIError
from postgrest import APIResponse as PostgrestAPIResponse
from realtime import AuthorizationError, NotConnectedError
from storage3.utils import StorageException
from supafunc.errors import FunctionsError, FunctionsHttpError, FunctionsRelayError

# Async Client
from ._async.auth_client import AsyncSupabaseAuthClient
from ._async.client import AsyncClient
from ._async.client import AsyncStorageClient as AsyncSupabaseStorageClient
from ._async.client import create_client as acreate_client
from ._async.client import create_client as create_async_client

# Sync Client
Expand All @@ -15,15 +28,20 @@
from ._sync.client import create_client

# Lib
from .lib.client_options import ClientOptions
from .lib.client_options import AsyncClientOptions
from .lib.client_options import AsyncClientOptions as AClientOptions
from .lib.client_options import SyncClientOptions as ClientOptions

# Version
from .version import __version__

__all__ = [
"AsyncSupabaseAuthClient",
"acreate_client",
"create_async_client",
"AClientOptions",
"AsyncClient",
"AsyncClientOptions",
"AsyncSupabaseStorageClient",
"SupabaseAuthClient",
"create_client",
Expand All @@ -34,4 +52,17 @@
"PostgrestAPIResponse",
"StorageException",
"__version__",
"AuthApiError",
"AuthError",
"AuthImplicitGrantRedirectError",
"AuthInvalidCredentialsError",
"AuthRetryableError",
"AuthSessionMissingError",
"AuthWeakPasswordError",
"AuthUnknownError",
"FunctionsHttpError",
"FunctionsRelayError",
"FunctionsError",
"AuthorizationError",
"NotConnectedError",
]
86 changes: 85 additions & 1 deletion supabase/lib/client_options.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from dataclasses import dataclass, field
from typing import Any, Dict, Optional, Union

from gotrue import AuthFlowType, SyncMemoryStorage, SyncSupportedStorage
from gotrue import (
AsyncMemoryStorage,
AuthFlowType,
SyncMemoryStorage,
SyncSupportedStorage,
)
from httpx import Timeout
from postgrest.constants import DEFAULT_POSTGREST_CLIENT_TIMEOUT
from storage3.constants import DEFAULT_TIMEOUT as DEFAULT_STORAGE_CLIENT_TIMEOUT
Expand Down Expand Up @@ -85,3 +90,82 @@ def replace(
)
client_options.flow_type = flow_type or self.flow_type
return client_options


@dataclass
class AsyncClientOptions(ClientOptions):
storage: SyncSupportedStorage = field(default_factory=AsyncMemoryStorage)
"""A storage provider. Used to store the logged in session."""

def replace(
self,
schema: Optional[str] = None,
headers: Optional[Dict[str, str]] = None,
auto_refresh_token: Optional[bool] = None,
persist_session: Optional[bool] = None,
storage: Optional[SyncSupportedStorage] = None,
realtime: Optional[Dict[str, Any]] = None,
postgrest_client_timeout: Union[
int, float, Timeout
] = DEFAULT_POSTGREST_CLIENT_TIMEOUT,
storage_client_timeout: Union[
int, float, Timeout
] = DEFAULT_STORAGE_CLIENT_TIMEOUT,
flow_type: Optional[AuthFlowType] = None,
) -> "AsyncClientOptions":
"""Create a new SupabaseClientOptions with changes"""
client_options = AsyncClientOptions()
client_options.schema = schema or self.schema
client_options.headers = headers or self.headers
client_options.auto_refresh_token = (
auto_refresh_token or self.auto_refresh_token
)
client_options.persist_session = persist_session or self.persist_session
client_options.storage = storage or self.storage
client_options.realtime = realtime or self.realtime
client_options.postgrest_client_timeout = (
postgrest_client_timeout or self.postgrest_client_timeout
)
client_options.storage_client_timeout = (
storage_client_timeout or self.storage_client_timeout
)
client_options.flow_type = flow_type or self.flow_type
return client_options


@dataclass
class SyncClientOptions(ClientOptions):
def replace(
self,
schema: Optional[str] = None,
headers: Optional[Dict[str, str]] = None,
auto_refresh_token: Optional[bool] = None,
persist_session: Optional[bool] = None,
storage: Optional[SyncSupportedStorage] = None,
realtime: Optional[Dict[str, Any]] = None,
postgrest_client_timeout: Union[
int, float, Timeout
] = DEFAULT_POSTGREST_CLIENT_TIMEOUT,
storage_client_timeout: Union[
int, float, Timeout
] = DEFAULT_STORAGE_CLIENT_TIMEOUT,
flow_type: Optional[AuthFlowType] = None,
) -> "SyncClientOptions":
"""Create a new SupabaseClientOptions with changes"""
client_options = SyncClientOptions()
client_options.schema = schema or self.schema
client_options.headers = headers or self.headers
client_options.auto_refresh_token = (
auto_refresh_token or self.auto_refresh_token
)
client_options.persist_session = persist_session or self.persist_session
client_options.storage = storage or self.storage
client_options.realtime = realtime or self.realtime
client_options.postgrest_client_timeout = (
postgrest_client_timeout or self.postgrest_client_timeout
)
client_options.storage_client_timeout = (
storage_client_timeout or self.storage_client_timeout
)
client_options.flow_type = flow_type or self.flow_type
return client_options
3 changes: 1 addition & 2 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

import pytest

from supabase import Client, create_client
from supabase.lib.client_options import ClientOptions
from supabase import Client, ClientOptions, create_client


@pytest.mark.xfail(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_client_options.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from gotrue import SyncMemoryStorage

from supabase.lib.client_options import ClientOptions
from supabase import ClientOptions


class TestClientOptions:
Expand Down
Loading