-
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
55e8f84
commit b228d2b
Showing
7 changed files
with
133 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import copy | ||
import dataclasses | ||
from typing import Any, Callable, Dict, Optional | ||
|
||
from supabase import __version__ | ||
|
||
|
||
DEFAULT_HEADERS = {"X-Client-Info": f"supabase-py/{__version__}"} | ||
|
||
|
||
@dataclasses.dataclass | ||
class ClientOptions: | ||
|
||
"""The Postgres schema which your tables belong to. Must be on the list of exposed schemas in Supabase. Defaults to 'public'.""" | ||
|
||
schema: str = "public" | ||
|
||
"""Optional headers for initializing the client.""" | ||
headers: Dict[str, str] = dataclasses.field(default_factory=DEFAULT_HEADERS.copy) | ||
|
||
"""Automatically refreshes the token for logged in users.""" | ||
auto_refresh_token: bool = True | ||
|
||
"""Whether to persist a logged in session to storage.""" | ||
persist_session: bool = True | ||
|
||
"""Detect a session from the URL. Used for OAuth login callbacks.""" | ||
detect_session_in_url: bool = True | ||
|
||
"""A storage provider. Used to store the logged in session.""" | ||
local_storage: Dict[str, Any] = dataclasses.field(default_factory=lambda: {}) | ||
|
||
"""Options passed to the realtime-js instance""" | ||
realtime: Optional[Dict[str, Any]] = None | ||
|
||
"""A custom `fetch` implementation.""" | ||
fetch: Optional[Callable] = None | ||
|
||
def replace( | ||
self, | ||
schema: Optional[str] = None, | ||
headers: Optional[Dict[str, str]] = None, | ||
auto_refresh_token: Optional[bool] = None, | ||
persist_session: Optional[bool] = None, | ||
detect_session_in_url: Optional[bool] = None, | ||
local_storage: Optional[Dict[str, Any]] = None, | ||
realtime: Optional[Dict[str, Any]] = None, | ||
fetch: Optional[Callable] = None, | ||
) -> "ClientOptions": | ||
"""Create a new SupabaseClientOptions with changes""" | ||
changes = { | ||
key: value | ||
for key, value in locals().items() | ||
if key != "self" and value is not None | ||
} | ||
client_options = dataclasses.replace(self, **changes) | ||
client_options = copy.deepcopy(client_options) | ||
return client_options |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from supabase import __version__ | ||
from supabase.lib.client_options import ClientOptions | ||
|
||
DEFAULT_HEADERS = {"X-Client-Info": f"supabase-py/{__version__}"} | ||
|
||
DEFAULT_OPTIONS: ClientOptions = ClientOptions() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
powershell -Command { | ||
$env:SUPABASE_TEST_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiYW5vbiIsImlhdCI6MTYzNTAwODQ4NywiZXhwIjoxOTUwNTg0NDg3fQ.l8IgkO7TQokGSc9OJoobXIVXsOXkilXl4Ak6SCX5qI8"; | ||
$env:SUPABASE_TEST_URL = "https://ibrydvrsxoapzgtnhpso.supabase.co"; | ||
poetry run pytest; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from supabase.lib.client_options import ClientOptions | ||
|
||
|
||
def test__client_options__replace__returns_updated_options(): | ||
options = ClientOptions( | ||
schema="schema", | ||
headers={"key": "value"}, | ||
auto_refresh_token=False, | ||
persist_session=False, | ||
detect_session_in_url=False, | ||
local_storage={"key": "value"}, | ||
realtime={"key": "value"} | ||
) | ||
|
||
actual = options.replace(schema="new schema") | ||
expected = ClientOptions( | ||
schema="new schema", | ||
headers={"key": "value"}, | ||
auto_refresh_token=False, | ||
persist_session=False, | ||
detect_session_in_url=False, | ||
local_storage={"key": "value"}, | ||
realtime={"key": "value"} | ||
) | ||
|
||
assert actual == expected | ||
|
||
|
||
def test__client_options__replace__updates_only_new_options(): | ||
# Arrange | ||
options = ClientOptions(local_storage={"key": "value"}) | ||
new_options = options.replace() | ||
|
||
# Act | ||
new_options.local_storage["key"] = "new_value" | ||
|
||
# Assert | ||
assert options.local_storage["key"] == "value" | ||
assert new_options.local_storage["key"] == "new_value" |