Skip to content

Commit

Permalink
feat: add key to clients
Browse files Browse the repository at this point in the history
  • Loading branch information
Dani Reinón committed Apr 28, 2022
1 parent ab7275f commit 838af7c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
10 changes: 5 additions & 5 deletions storage3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@

@overload
def create_client(
url: str, headers: dict[str, str], *, is_async: Literal[True]
url: str, key: str, headers: dict[str, str], *, is_async: Literal[True]
) -> AsyncStorageClient:
...


@overload
def create_client(
url: str, headers: dict[str, str], *, is_async: Literal[False]
url: str, key: str, headers: dict[str, str], *, is_async: Literal[False]
) -> SyncStorageClient:
...


def create_client(
url: str, headers: dict[str, str], *, is_async: bool
url: str, key: str, headers: dict[str, str] = None, *, is_async: bool
) -> Union[AsyncStorageClient, SyncStorageClient]:
if is_async:
return AsyncStorageClient(url, headers)
return AsyncStorageClient(url, key, headers)
else:
return SyncStorageClient(url, headers)
return SyncStorageClient(url, key, headers)
18 changes: 16 additions & 2 deletions storage3/_async/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import annotations
from ..utils import AsyncClient, __version__
from .bucket import AsyncStorageBucketAPI
from .file_api import AsyncBucketProxy
Expand All @@ -10,10 +11,14 @@
class AsyncStorageClient(AsyncStorageBucketAPI):
"""Manage storage buckets and files."""

def __init__(self, url: str, headers: dict[str, str]) -> None:
def __init__(self, url: str, key: str, headers: dict[str, str] = None) -> None:
super().__init__(
url,
{"User-Agent": f"supabase-py/storage3 v{__version__}", **headers},
{
"User-Agent": f"supabase-py/storage3 v{__version__}",
**self._get_auth_headers(key),
**(headers or {}),
},
AsyncClient(),
)

Expand All @@ -26,3 +31,12 @@ def from_(self, id: str) -> AsyncBucketProxy:
The unique identifier of the bucket
"""
return AsyncBucketProxy(id, self.url, self.headers, self._client)

@staticmethod
def _get_auth_headers(key: str) -> dict[str, str]:
"""Helper method to get auth headers."""
# What's the corresponding method to get the token
return {
"apiKey": key,
"Authorization": f"Bearer {key}",
}
18 changes: 16 additions & 2 deletions storage3/_sync/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import annotations
from ..utils import SyncClient, __version__
from .bucket import SyncStorageBucketAPI
from .file_api import SyncBucketProxy
Expand All @@ -10,10 +11,14 @@
class SyncStorageClient(SyncStorageBucketAPI):
"""Manage storage buckets and files."""

def __init__(self, url: str, headers: dict[str, str]) -> None:
def __init__(self, url: str, key: str, headers: dict[str, str] = None) -> None:
super().__init__(
url,
{"User-Agent": f"supabase-py/storage3 v{__version__}", **headers},
{
"User-Agent": f"supabase-py/storage3 v{__version__}",
**self._get_auth_headers(key),
**(headers or {}),
},
SyncClient(),
)

Expand All @@ -26,3 +31,12 @@ def from_(self, id: str) -> SyncBucketProxy:
The unique identifier of the bucket
"""
return SyncBucketProxy(id, self.url, self.headers, self._client)

@staticmethod
def _get_auth_headers(key: str) -> dict[str, str]:
"""Helper method to get auth headers."""
# What's the corresponding method to get the token
return {
"apiKey": key,
"Authorization": f"Bearer {key}",
}

0 comments on commit 838af7c

Please sign in to comment.