corebase 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- corebase/__init__.py +108 -0
- corebase/_async/__init__.py +1 -0
- corebase/_async/auth_client.py +55 -0
- corebase/_async/client.py +398 -0
- corebase/_sync/__init__.py +1 -0
- corebase/_sync/auth_client.py +55 -0
- corebase/_sync/client.py +393 -0
- corebase/auth/__init__.py +20 -0
- corebase/auth/_async/__init__.py +1 -0
- corebase/auth/_async/gotrue_admin_api.py +356 -0
- corebase/auth/_async/gotrue_admin_mfa_api.py +32 -0
- corebase/auth/_async/gotrue_admin_oauth_api.py +93 -0
- corebase/auth/_async/gotrue_base_api.py +78 -0
- corebase/auth/_async/gotrue_client.py +1404 -0
- corebase/auth/_async/gotrue_mfa_api.py +94 -0
- corebase/auth/_async/storage.py +31 -0
- corebase/auth/_sync/__init__.py +1 -0
- corebase/auth/_sync/gotrue_admin_api.py +356 -0
- corebase/auth/_sync/gotrue_admin_mfa_api.py +32 -0
- corebase/auth/_sync/gotrue_admin_oauth_api.py +93 -0
- corebase/auth/_sync/gotrue_base_api.py +78 -0
- corebase/auth/_sync/gotrue_client.py +1394 -0
- corebase/auth/_sync/gotrue_mfa_api.py +94 -0
- corebase/auth/_sync/storage.py +31 -0
- corebase/auth/constants.py +15 -0
- corebase/auth/errors.py +259 -0
- corebase/auth/helpers.py +288 -0
- corebase/auth/py.typed +0 -0
- corebase/auth/timer.py +53 -0
- corebase/auth/types.py +1021 -0
- corebase/auth/version.py +1 -0
- corebase/client.py +12 -0
- corebase/functions/__init__.py +47 -0
- corebase/functions/_async/__init__.py +1 -0
- corebase/functions/_async/functions_client.py +218 -0
- corebase/functions/_sync/__init__.py +1 -0
- corebase/functions/_sync/functions_client.py +218 -0
- corebase/functions/errors.py +62 -0
- corebase/functions/py.typed +0 -0
- corebase/functions/utils.py +33 -0
- corebase/functions/version.py +1 -0
- corebase/lib/__init__.py +3 -0
- corebase/lib/client_options.py +189 -0
- corebase/migrate/__init__.py +374 -0
- corebase/migrate/__main__.py +7 -0
- corebase/postgrest/__init__.py +62 -0
- corebase/postgrest/_async/__init__.py +1 -0
- corebase/postgrest/_async/client.py +206 -0
- corebase/postgrest/_async/request_builder.py +503 -0
- corebase/postgrest/_sync/__init__.py +1 -0
- corebase/postgrest/_sync/client.py +206 -0
- corebase/postgrest/_sync/request_builder.py +503 -0
- corebase/postgrest/base_client.py +61 -0
- corebase/postgrest/base_request_builder.py +787 -0
- corebase/postgrest/constants.py +6 -0
- corebase/postgrest/exceptions.py +115 -0
- corebase/postgrest/py.typed +0 -0
- corebase/postgrest/types.py +72 -0
- corebase/postgrest/utils.py +91 -0
- corebase/postgrest/version.py +1 -0
- corebase/py.typed +0 -0
- corebase/realtime/__init__.py +17 -0
- corebase/realtime/_async/__init__.py +0 -0
- corebase/realtime/_async/channel.py +766 -0
- corebase/realtime/_async/client.py +498 -0
- corebase/realtime/_async/presence.py +185 -0
- corebase/realtime/_async/push.py +162 -0
- corebase/realtime/_async/timer.py +41 -0
- corebase/realtime/_sync/__init__.py +0 -0
- corebase/realtime/_sync/channel.py +30 -0
- corebase/realtime/_sync/client.py +71 -0
- corebase/realtime/_sync/presence.py +10 -0
- corebase/realtime/exceptions.py +38 -0
- corebase/realtime/message.py +176 -0
- corebase/realtime/py.typed +0 -0
- corebase/realtime/transformers.py +9 -0
- corebase/realtime/types.py +246 -0
- corebase/realtime/utils.py +5 -0
- corebase/realtime/version.py +1 -0
- corebase/storage/__init__.py +56 -0
- corebase/storage/_async/__init__.py +1 -0
- corebase/storage/_async/analytics.py +82 -0
- corebase/storage/_async/bucket.py +142 -0
- corebase/storage/_async/client.py +106 -0
- corebase/storage/_async/file_api.py +630 -0
- corebase/storage/_async/request.py +32 -0
- corebase/storage/_async/vectors.py +213 -0
- corebase/storage/_sync/__init__.py +1 -0
- corebase/storage/_sync/analytics.py +80 -0
- corebase/storage/_sync/bucket.py +142 -0
- corebase/storage/_sync/client.py +106 -0
- corebase/storage/_sync/file_api.py +630 -0
- corebase/storage/_sync/request.py +32 -0
- corebase/storage/_sync/vectors.py +199 -0
- corebase/storage/constants.py +16 -0
- corebase/storage/exceptions.py +84 -0
- corebase/storage/py.typed +0 -0
- corebase/storage/types.py +313 -0
- corebase/storage/utils.py +26 -0
- corebase/storage/version.py +1 -0
- corebase/types.py +8 -0
- corebase/version.py +1 -0
- corebase-1.0.0.dist-info/METADATA +78 -0
- corebase-1.0.0.dist-info/RECORD +106 -0
- corebase-1.0.0.dist-info/WHEEL +4 -0
- corebase-1.0.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections.abc import Callable
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from enum import Enum
|
|
6
|
+
from typing import Any, Literal, NotRequired, TypeAlias, TypeVar
|
|
7
|
+
|
|
8
|
+
from pydantic import ConfigDict, with_config
|
|
9
|
+
from typing_extensions import (
|
|
10
|
+
ParamSpec,
|
|
11
|
+
TypedDict,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
# Constants
|
|
15
|
+
DEFAULT_TIMEOUT = 10
|
|
16
|
+
PHOENIX_CHANNEL = "phoenix"
|
|
17
|
+
VSN = "1.0.0"
|
|
18
|
+
DEFAULT_HEARTBEAT_INTERVAL = 25
|
|
19
|
+
|
|
20
|
+
# Type variables and custom types
|
|
21
|
+
T_ParamSpec = ParamSpec("T_ParamSpec")
|
|
22
|
+
T_Retval = TypeVar("T_Retval")
|
|
23
|
+
Callback: TypeAlias = Callable[T_ParamSpec, T_Retval]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
# Enums
|
|
27
|
+
class ChannelEvents(str, Enum):
|
|
28
|
+
"""
|
|
29
|
+
ChannelEvents are a bunch of constant strings that are defined according to
|
|
30
|
+
what the Phoenix realtime server expects.
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
close = "phx_close"
|
|
34
|
+
error = "phx_error"
|
|
35
|
+
join = "phx_join" # type: ignore[assignment]
|
|
36
|
+
reply = "phx_reply"
|
|
37
|
+
leave = "phx_leave"
|
|
38
|
+
heartbeat = "heartbeat"
|
|
39
|
+
access_token = "access_token"
|
|
40
|
+
broadcast = "broadcast"
|
|
41
|
+
presence = "presence"
|
|
42
|
+
presence_state = "presence_state"
|
|
43
|
+
presence_diff = "presence_diff"
|
|
44
|
+
system = "system"
|
|
45
|
+
postgres_changes = "postgres_changes"
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class ChannelStates(str, Enum):
|
|
49
|
+
JOINED = "joined"
|
|
50
|
+
CLOSED = "closed"
|
|
51
|
+
ERRORED = "errored"
|
|
52
|
+
JOINING = "joining"
|
|
53
|
+
LEAVING = "leaving"
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class RealtimeSubscribeStates(str, Enum):
|
|
57
|
+
SUBSCRIBED = "SUBSCRIBED"
|
|
58
|
+
TIMED_OUT = "TIMED_OUT"
|
|
59
|
+
CLOSED = "CLOSED"
|
|
60
|
+
CHANNEL_ERROR = "CHANNEL_ERROR"
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class RealtimePresenceListenEvents(str, Enum):
|
|
64
|
+
SYNC = "SYNC"
|
|
65
|
+
JOIN = "JOIN"
|
|
66
|
+
LEAVE = "LEAVE"
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class RealtimeAcknowledgementStatus(str, Enum):
|
|
70
|
+
Ok = "ok"
|
|
71
|
+
Error = "error"
|
|
72
|
+
Timeout = "timeout"
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
# Literals
|
|
76
|
+
class RealtimePostgresChangesListenEvent(str, Enum):
|
|
77
|
+
All = "*"
|
|
78
|
+
Insert = "INSERT"
|
|
79
|
+
Update = "UPDATE"
|
|
80
|
+
Delete = "DELETE"
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
Payload = TypeVar("Payload")
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class PostgresChangesColumn(TypedDict):
|
|
87
|
+
name: str
|
|
88
|
+
type: str
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class PostgresChangesData(TypedDict):
|
|
92
|
+
schema: str
|
|
93
|
+
table: str
|
|
94
|
+
commit_timestamp: str
|
|
95
|
+
type: RealtimePostgresChangesListenEvent
|
|
96
|
+
errors: str | list[str] | None
|
|
97
|
+
columns: list[PostgresChangesColumn]
|
|
98
|
+
record: NotRequired[dict[str, Any] | None]
|
|
99
|
+
old_record: NotRequired[dict[str, Any]] # todo: improve this
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
class PostgresChangesPayload(TypedDict):
|
|
103
|
+
data: PostgresChangesData
|
|
104
|
+
ids: list[int]
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
class BroadcastMeta(TypedDict, total=False):
|
|
108
|
+
replayed: bool
|
|
109
|
+
id: str
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
class BroadcastPayload(TypedDict):
|
|
113
|
+
event: str
|
|
114
|
+
payload: dict[str, Any]
|
|
115
|
+
meta: NotRequired[BroadcastMeta]
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
@dataclass(frozen=True)
|
|
119
|
+
class BroadcastCallback:
|
|
120
|
+
callback: Callable[[BroadcastPayload], None]
|
|
121
|
+
event: str
|
|
122
|
+
|
|
123
|
+
def __call__(self, payload: BroadcastPayload) -> None:
|
|
124
|
+
event = self.event.lower()
|
|
125
|
+
if event == "*" or event == payload["event"].lower():
|
|
126
|
+
return self.callback(payload)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
@dataclass
|
|
130
|
+
class PostgresChangesCallback:
|
|
131
|
+
callback: Callable[[PostgresChangesPayload], None]
|
|
132
|
+
event: RealtimePostgresChangesListenEvent
|
|
133
|
+
table: str | None
|
|
134
|
+
schema: str | None
|
|
135
|
+
filter: str | None
|
|
136
|
+
select: list[str] | None = None
|
|
137
|
+
id: int | None = None
|
|
138
|
+
|
|
139
|
+
def __post_init__(self) -> None:
|
|
140
|
+
if not self.schema:
|
|
141
|
+
self.schema = "public"
|
|
142
|
+
|
|
143
|
+
def __call__(self, payload: PostgresChangesPayload) -> None:
|
|
144
|
+
event_matches = (
|
|
145
|
+
self.event == payload["data"]["type"]
|
|
146
|
+
or self.event == RealtimePostgresChangesListenEvent.All
|
|
147
|
+
)
|
|
148
|
+
if self.id and self.id in payload["ids"] and event_matches:
|
|
149
|
+
return self.callback(payload)
|
|
150
|
+
|
|
151
|
+
@property
|
|
152
|
+
def binding_filter(self) -> dict[str, Any]:
|
|
153
|
+
binding: dict[str, Any] = {"event": self.event, "schema": self.schema}
|
|
154
|
+
if self.table:
|
|
155
|
+
binding["table"] = self.table
|
|
156
|
+
if self.filter:
|
|
157
|
+
binding["filter"] = self.filter
|
|
158
|
+
if self.select:
|
|
159
|
+
binding["select"] = self.select
|
|
160
|
+
return binding
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
@with_config(ConfigDict(extra="allow"))
|
|
164
|
+
class Presence(TypedDict):
|
|
165
|
+
presence_ref: str
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
class PresenceEvents:
|
|
169
|
+
def __init__(self, state: str, diff: str) -> None:
|
|
170
|
+
self.state = state
|
|
171
|
+
self.diff = diff
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
class PresenceOpts:
|
|
175
|
+
def __init__(self, events: PresenceEvents) -> None:
|
|
176
|
+
self.events = events
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
# TypedDicts
|
|
180
|
+
class ReplayOption(TypedDict, total=False):
|
|
181
|
+
since: int
|
|
182
|
+
limit: int
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
class RealtimeChannelBroadcastConfig(TypedDict, total=False):
|
|
186
|
+
ack: bool
|
|
187
|
+
self: bool
|
|
188
|
+
replay: ReplayOption
|
|
189
|
+
replication_ready: bool
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
class RealtimeChannelPresenceConfig(TypedDict):
|
|
193
|
+
key: str
|
|
194
|
+
enabled: bool
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
class RealtimeChannelConfig(TypedDict):
|
|
198
|
+
broadcast: RealtimeChannelBroadcastConfig | None
|
|
199
|
+
presence: RealtimeChannelPresenceConfig | None
|
|
200
|
+
private: bool
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
class RealtimeChannelOptions(TypedDict):
|
|
204
|
+
config: NotRequired[RealtimeChannelConfig]
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
@with_config(ConfigDict(extra="allow"))
|
|
208
|
+
class PresenceMeta(TypedDict):
|
|
209
|
+
phx_ref: NotRequired[str]
|
|
210
|
+
phx_ref_prev: NotRequired[str]
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
class RawPresenceStateEntry(TypedDict):
|
|
214
|
+
metas: list[PresenceMeta]
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
# Custom types
|
|
218
|
+
PresenceOnJoinCallback = Callable[[str, list[Any], list[Presence]], None]
|
|
219
|
+
PresenceOnLeaveCallback = Callable[[str, list[Any], list[Presence]], None]
|
|
220
|
+
RealtimePresenceState = dict[str, list[Presence]]
|
|
221
|
+
RawPresenceState = dict[str, RawPresenceStateEntry]
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
class RawPresenceDiff(TypedDict):
|
|
225
|
+
joins: RawPresenceState
|
|
226
|
+
leaves: RawPresenceState
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
class PresenceDiff(TypedDict):
|
|
230
|
+
joins: RealtimePresenceState
|
|
231
|
+
leaves: RealtimePresenceState
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
# Specific payload types
|
|
235
|
+
class RealtimePresenceJoinPayload(TypedDict):
|
|
236
|
+
event: Literal[RealtimePresenceListenEvents.JOIN]
|
|
237
|
+
key: str
|
|
238
|
+
current_presences: list[Presence]
|
|
239
|
+
new_presences: list[Presence]
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
class RealtimePresenceLeavePayload(TypedDict):
|
|
243
|
+
event: Literal[RealtimePresenceListenEvents.LEAVE]
|
|
244
|
+
key: str
|
|
245
|
+
current_presences: list[Presence]
|
|
246
|
+
left_presences: list[Presence]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "1.0.0" # {x-release-please-version}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Literal, overload
|
|
4
|
+
|
|
5
|
+
from corebase.storage._async import AsyncStorageClient
|
|
6
|
+
from corebase.storage._async.bucket import AsyncStorageBucketAPI
|
|
7
|
+
from corebase.storage._async.file_api import AsyncBucket
|
|
8
|
+
from corebase.storage._sync import SyncStorageClient
|
|
9
|
+
from corebase.storage._sync.bucket import SyncStorageBucketAPI
|
|
10
|
+
from corebase.storage._sync.file_api import SyncBucket
|
|
11
|
+
from corebase.storage.constants import DEFAULT_TIMEOUT as DEFAULT_TIMEOUT
|
|
12
|
+
from corebase.storage.version import __version__
|
|
13
|
+
|
|
14
|
+
__all__ = [
|
|
15
|
+
"create_client",
|
|
16
|
+
"__version__",
|
|
17
|
+
"AsyncStorageClient",
|
|
18
|
+
"AsyncBucket",
|
|
19
|
+
"AsyncStorageBucketAPI",
|
|
20
|
+
"SyncStorageClient",
|
|
21
|
+
"SyncBucket",
|
|
22
|
+
"SyncStorageBucketAPI",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@overload
|
|
27
|
+
def create_client(
|
|
28
|
+
url: str,
|
|
29
|
+
headers: dict[str, str],
|
|
30
|
+
*,
|
|
31
|
+
is_async: Literal[True],
|
|
32
|
+
timeout: int | None = None,
|
|
33
|
+
) -> AsyncStorageClient: ...
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@overload
|
|
37
|
+
def create_client(
|
|
38
|
+
url: str,
|
|
39
|
+
headers: dict[str, str],
|
|
40
|
+
*,
|
|
41
|
+
is_async: Literal[False],
|
|
42
|
+
timeout: int | None = None,
|
|
43
|
+
) -> SyncStorageClient: ...
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def create_client(
|
|
47
|
+
url: str,
|
|
48
|
+
headers: dict[str, str],
|
|
49
|
+
*,
|
|
50
|
+
is_async: bool,
|
|
51
|
+
timeout: int | None = None,
|
|
52
|
+
) -> AsyncStorageClient | SyncStorageClient:
|
|
53
|
+
client_class = AsyncStorageClient if is_async else SyncStorageClient
|
|
54
|
+
if timeout is None:
|
|
55
|
+
return client_class(url, headers)
|
|
56
|
+
return client_class(url, headers, timeout)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .client import AsyncStorageClient as AsyncStorageClient
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
from typing import TYPE_CHECKING
|
|
2
|
+
|
|
3
|
+
from httpx import QueryParams
|
|
4
|
+
|
|
5
|
+
from ..types import (
|
|
6
|
+
AnalyticsBucket,
|
|
7
|
+
AnalyticsBucketDeleteResponse,
|
|
8
|
+
AnalyticsBucketsParser,
|
|
9
|
+
SortColumn,
|
|
10
|
+
SortOrder,
|
|
11
|
+
)
|
|
12
|
+
from .request import AsyncRequestBuilder
|
|
13
|
+
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from pyiceberg.catalog.rest import RestCatalog
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class AsyncStorageAnalyticsClient:
|
|
19
|
+
def __init__(self, request: AsyncRequestBuilder) -> None:
|
|
20
|
+
self._request = request
|
|
21
|
+
|
|
22
|
+
async def create(self, bucket_name: str) -> AnalyticsBucket:
|
|
23
|
+
body = {"name": bucket_name}
|
|
24
|
+
data = await self._request.send(http_method="POST", path=["bucket"], body=body)
|
|
25
|
+
return AnalyticsBucket.model_validate_json(data.content)
|
|
26
|
+
|
|
27
|
+
async def list(
|
|
28
|
+
self,
|
|
29
|
+
limit: int | None = None,
|
|
30
|
+
offset: int | None = None,
|
|
31
|
+
sort_column: SortColumn | None = None,
|
|
32
|
+
sort_order: SortOrder | None = None,
|
|
33
|
+
search: str | None = None,
|
|
34
|
+
) -> list[AnalyticsBucket]:
|
|
35
|
+
params = dict(
|
|
36
|
+
limit=limit,
|
|
37
|
+
offset=offset,
|
|
38
|
+
sortColumn=sort_column,
|
|
39
|
+
sortOrder=sort_order,
|
|
40
|
+
search=search,
|
|
41
|
+
)
|
|
42
|
+
filtered_params = QueryParams(
|
|
43
|
+
**{k: v for k, v in params.items() if v is not None}
|
|
44
|
+
)
|
|
45
|
+
data = await self._request.send(
|
|
46
|
+
http_method="GET", path=["bucket"], query_params=filtered_params
|
|
47
|
+
)
|
|
48
|
+
return AnalyticsBucketsParser.validate_json(data.content)
|
|
49
|
+
|
|
50
|
+
async def delete(self, bucket_name: str) -> AnalyticsBucketDeleteResponse:
|
|
51
|
+
data = await self._request.send(
|
|
52
|
+
http_method="DELETE", path=["bucket", bucket_name]
|
|
53
|
+
)
|
|
54
|
+
return AnalyticsBucketDeleteResponse.model_validate_json(data.content)
|
|
55
|
+
|
|
56
|
+
def catalog(
|
|
57
|
+
self, catalog_name: str, access_key_id: str, secret_access_key: str
|
|
58
|
+
) -> "RestCatalog":
|
|
59
|
+
try:
|
|
60
|
+
from pyiceberg.catalog.rest import RestCatalog
|
|
61
|
+
except ImportError as err:
|
|
62
|
+
raise Exception(
|
|
63
|
+
"pyiceberg is required for storage analytics catalog support"
|
|
64
|
+
) from err
|
|
65
|
+
|
|
66
|
+
catalog_uri = self._request._base_url
|
|
67
|
+
s3_endpoint = self._request._base_url.parent.joinpath("s3")
|
|
68
|
+
service_key = self._request.headers.get("apiKey")
|
|
69
|
+
assert service_key, "apiKey must be passed in the headers."
|
|
70
|
+
return RestCatalog(
|
|
71
|
+
catalog_name,
|
|
72
|
+
warehouse=catalog_name,
|
|
73
|
+
uri=str(catalog_uri),
|
|
74
|
+
token=service_key,
|
|
75
|
+
**{
|
|
76
|
+
"py-io-impl": "pyiceberg.io.pyarrow.PyArrowFileIO",
|
|
77
|
+
"s3.endpoint": str(s3_endpoint),
|
|
78
|
+
"s3.access-key-id": access_key_id,
|
|
79
|
+
"s3.secret-access-key": secret_access_key,
|
|
80
|
+
"s3.force-virtual-addressing": "False",
|
|
81
|
+
},
|
|
82
|
+
)
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import warnings
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
from httpx import AsyncClient, Headers, HTTPStatusError, Response
|
|
7
|
+
from yarl import URL
|
|
8
|
+
|
|
9
|
+
from ..exceptions import StorageApiError
|
|
10
|
+
from ..types import CreateOrUpdateBucketOptions, RequestMethod
|
|
11
|
+
from .file_api import AsyncBucket
|
|
12
|
+
|
|
13
|
+
__all__ = ["AsyncStorageBucketAPI"]
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class AsyncStorageBucketAPI:
|
|
17
|
+
"""This class abstracts access to the endpoint to the Get, List, Empty, and Delete operations on a bucket"""
|
|
18
|
+
|
|
19
|
+
def __init__(self, session: AsyncClient, url: str, headers: Headers) -> None:
|
|
20
|
+
if url and url[-1] != "/":
|
|
21
|
+
warnings.warn(
|
|
22
|
+
"Storage endpoint URL should have a trailing slash. "
|
|
23
|
+
"The URL has been automatically corrected.",
|
|
24
|
+
UserWarning,
|
|
25
|
+
stacklevel=2,
|
|
26
|
+
)
|
|
27
|
+
url += "/"
|
|
28
|
+
self._base_url = URL(url)
|
|
29
|
+
self._client = session
|
|
30
|
+
self._headers = headers
|
|
31
|
+
|
|
32
|
+
async def _request(
|
|
33
|
+
self,
|
|
34
|
+
method: RequestMethod,
|
|
35
|
+
path: list[str],
|
|
36
|
+
json: dict[Any, Any] | None = None,
|
|
37
|
+
) -> Response:
|
|
38
|
+
try:
|
|
39
|
+
url_path = self._base_url.joinpath(*path)
|
|
40
|
+
response = await self._client.request(
|
|
41
|
+
method, str(url_path), json=json, headers=self._headers
|
|
42
|
+
)
|
|
43
|
+
response.raise_for_status()
|
|
44
|
+
except HTTPStatusError as exc:
|
|
45
|
+
try:
|
|
46
|
+
resp = exc.response.json()
|
|
47
|
+
raise StorageApiError(
|
|
48
|
+
resp["message"], resp["error"], resp["statusCode"]
|
|
49
|
+
) from exc
|
|
50
|
+
except (KeyError, TypeError, ValueError) as err:
|
|
51
|
+
message = f"Unable to parse error message: {exc.response.text}"
|
|
52
|
+
raise StorageApiError(
|
|
53
|
+
message, "InternalError", exc.response.status_code
|
|
54
|
+
) from err
|
|
55
|
+
|
|
56
|
+
return response
|
|
57
|
+
|
|
58
|
+
async def list_buckets(self) -> list[AsyncBucket]:
|
|
59
|
+
"""Retrieves the details of all storage buckets within an existing product."""
|
|
60
|
+
# if the request doesn't error, it is assured to return a list
|
|
61
|
+
res = await self._request("GET", ["bucket"])
|
|
62
|
+
return [AsyncBucket(**bucket) for bucket in res.json()]
|
|
63
|
+
|
|
64
|
+
async def get_bucket(self, id: str) -> AsyncBucket:
|
|
65
|
+
"""Retrieves the details of an existing storage bucket.
|
|
66
|
+
|
|
67
|
+
Parameters
|
|
68
|
+
----------
|
|
69
|
+
id
|
|
70
|
+
The unique identifier of the bucket you would like to retrieve.
|
|
71
|
+
"""
|
|
72
|
+
res = await self._request("GET", ["bucket", id])
|
|
73
|
+
json = res.json()
|
|
74
|
+
return AsyncBucket(**json)
|
|
75
|
+
|
|
76
|
+
async def create_bucket(
|
|
77
|
+
self,
|
|
78
|
+
id: str,
|
|
79
|
+
name: str | None = None,
|
|
80
|
+
options: CreateOrUpdateBucketOptions | None = None,
|
|
81
|
+
) -> dict[str, str]:
|
|
82
|
+
"""Creates a new storage bucket.
|
|
83
|
+
|
|
84
|
+
Parameters
|
|
85
|
+
----------
|
|
86
|
+
id
|
|
87
|
+
A unique identifier for the bucket you are creating.
|
|
88
|
+
name
|
|
89
|
+
A name for the bucket you are creating. If not passed, the id is used as the name as well.
|
|
90
|
+
options
|
|
91
|
+
Extra options to send while creating the bucket. Valid options are `public`, `file_size_limit` and
|
|
92
|
+
`allowed_mime_types`.
|
|
93
|
+
"""
|
|
94
|
+
json: dict[str, Any] = {"id": id, "name": name or id}
|
|
95
|
+
if options:
|
|
96
|
+
json.update(**options)
|
|
97
|
+
res = await self._request(
|
|
98
|
+
"POST",
|
|
99
|
+
["bucket"],
|
|
100
|
+
json=json,
|
|
101
|
+
)
|
|
102
|
+
return res.json()
|
|
103
|
+
|
|
104
|
+
async def update_bucket(
|
|
105
|
+
self, id: str, options: CreateOrUpdateBucketOptions
|
|
106
|
+
) -> dict[str, str]:
|
|
107
|
+
"""Update a storage bucket.
|
|
108
|
+
|
|
109
|
+
Parameters
|
|
110
|
+
----------
|
|
111
|
+
id
|
|
112
|
+
The unique identifier of the bucket you would like to update.
|
|
113
|
+
options
|
|
114
|
+
The properties you want to update. Valid options are `public`, `file_size_limit` and
|
|
115
|
+
`allowed_mime_types`.
|
|
116
|
+
"""
|
|
117
|
+
json = {"id": id, "name": id, **options}
|
|
118
|
+
res = await self._request("PUT", ["bucket", id], json=json)
|
|
119
|
+
return res.json()
|
|
120
|
+
|
|
121
|
+
async def empty_bucket(self, id: str) -> dict[str, str]:
|
|
122
|
+
"""Removes all objects inside a single bucket.
|
|
123
|
+
|
|
124
|
+
Parameters
|
|
125
|
+
----------
|
|
126
|
+
id
|
|
127
|
+
The unique identifier of the bucket you would like to empty.
|
|
128
|
+
"""
|
|
129
|
+
res = await self._request("POST", ["bucket", id, "empty"], json={})
|
|
130
|
+
return res.json()
|
|
131
|
+
|
|
132
|
+
async def delete_bucket(self, id: str) -> dict[str, str]:
|
|
133
|
+
"""Deletes an existing bucket. Note that you cannot delete buckets with existing objects inside. You must first
|
|
134
|
+
`empty()` the bucket.
|
|
135
|
+
|
|
136
|
+
Parameters
|
|
137
|
+
----------
|
|
138
|
+
id
|
|
139
|
+
The unique identifier of the bucket you would like to delete.
|
|
140
|
+
"""
|
|
141
|
+
res = await self._request("DELETE", ["bucket", id], json={})
|
|
142
|
+
return res.json()
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import platform
|
|
4
|
+
from warnings import warn
|
|
5
|
+
|
|
6
|
+
from httpx import AsyncClient, Headers
|
|
7
|
+
|
|
8
|
+
from corebase.storage.constants import DEFAULT_TIMEOUT
|
|
9
|
+
|
|
10
|
+
from ..version import __version__
|
|
11
|
+
from .analytics import AsyncStorageAnalyticsClient
|
|
12
|
+
from .bucket import AsyncStorageBucketAPI
|
|
13
|
+
from .file_api import AsyncBucketProxy
|
|
14
|
+
from .request import AsyncRequestBuilder
|
|
15
|
+
from .vectors import AsyncStorageVectorsClient
|
|
16
|
+
|
|
17
|
+
__all__ = [
|
|
18
|
+
"AsyncStorageClient",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class AsyncStorageClient(AsyncStorageBucketAPI):
|
|
23
|
+
"""Manage storage buckets and files."""
|
|
24
|
+
|
|
25
|
+
def __init__(
|
|
26
|
+
self,
|
|
27
|
+
url: str,
|
|
28
|
+
headers: dict[str, str],
|
|
29
|
+
timeout: int | None = None,
|
|
30
|
+
verify: bool | None = None,
|
|
31
|
+
proxy: str | None = None,
|
|
32
|
+
http_client: AsyncClient | None = None,
|
|
33
|
+
) -> None:
|
|
34
|
+
headers = {
|
|
35
|
+
"X-Client-Info": (
|
|
36
|
+
f"corebase-py/storage3 v{__version__}"
|
|
37
|
+
f"; platform={platform.system()}"
|
|
38
|
+
f"; platform-version={platform.release()}"
|
|
39
|
+
f"; runtime=python"
|
|
40
|
+
f"; runtime-version={platform.python_version()}"
|
|
41
|
+
),
|
|
42
|
+
**headers,
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if timeout is not None:
|
|
46
|
+
warn(
|
|
47
|
+
"The 'timeout' parameter is deprecated. Please configure it in the http client instead.",
|
|
48
|
+
DeprecationWarning,
|
|
49
|
+
stacklevel=2,
|
|
50
|
+
)
|
|
51
|
+
if verify is not None:
|
|
52
|
+
warn(
|
|
53
|
+
"The 'verify' parameter is deprecated. Please configure it in the http client instead.",
|
|
54
|
+
DeprecationWarning,
|
|
55
|
+
stacklevel=2,
|
|
56
|
+
)
|
|
57
|
+
if proxy is not None:
|
|
58
|
+
warn(
|
|
59
|
+
"The 'proxy' parameter is deprecated. Please configure it in the http client instead.",
|
|
60
|
+
DeprecationWarning,
|
|
61
|
+
stacklevel=2,
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
self.verify = bool(verify) if verify is not None else True
|
|
65
|
+
self.timeout = int(abs(timeout)) if timeout is not None else DEFAULT_TIMEOUT
|
|
66
|
+
|
|
67
|
+
self.session = http_client or AsyncClient(
|
|
68
|
+
headers=headers,
|
|
69
|
+
timeout=self.timeout,
|
|
70
|
+
proxy=proxy,
|
|
71
|
+
verify=self.verify,
|
|
72
|
+
follow_redirects=True,
|
|
73
|
+
http2=True,
|
|
74
|
+
)
|
|
75
|
+
super().__init__(self.session, url, Headers(headers))
|
|
76
|
+
|
|
77
|
+
async def __aenter__(self) -> AsyncStorageClient:
|
|
78
|
+
return self
|
|
79
|
+
|
|
80
|
+
async def __aexit__(self, exc_type, exc, tb) -> None:
|
|
81
|
+
await self.session.aclose()
|
|
82
|
+
|
|
83
|
+
def from_(self, id: str) -> AsyncBucketProxy:
|
|
84
|
+
"""Run a storage file operation.
|
|
85
|
+
|
|
86
|
+
Parameters
|
|
87
|
+
----------
|
|
88
|
+
id
|
|
89
|
+
The unique identifier of the bucket
|
|
90
|
+
"""
|
|
91
|
+
return AsyncBucketProxy(id, self._base_url, self._headers, self._client)
|
|
92
|
+
|
|
93
|
+
def vectors(self) -> AsyncStorageVectorsClient:
|
|
94
|
+
return AsyncStorageVectorsClient(
|
|
95
|
+
url=self._base_url.joinpath("vector"),
|
|
96
|
+
headers=self._headers,
|
|
97
|
+
session=self.session,
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
def analytics(self) -> AsyncStorageAnalyticsClient:
|
|
101
|
+
request = AsyncRequestBuilder(
|
|
102
|
+
session=self.session,
|
|
103
|
+
headers=self._headers,
|
|
104
|
+
base_url=self._base_url.joinpath("iceberg"),
|
|
105
|
+
)
|
|
106
|
+
return AsyncStorageAnalyticsClient(request=request)
|