interlude-sdk 0.3.0__tar.gz → 0.5.0__tar.gz

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.
Files changed (24) hide show
  1. {interlude_sdk-0.3.0 → interlude_sdk-0.5.0}/PKG-INFO +29 -1
  2. {interlude_sdk-0.3.0 → interlude_sdk-0.5.0}/README.md +28 -0
  3. {interlude_sdk-0.3.0 → interlude_sdk-0.5.0}/pyproject.toml +1 -1
  4. {interlude_sdk-0.3.0 → interlude_sdk-0.5.0}/src/interlude/__init__.py +6 -3
  5. {interlude_sdk-0.3.0 → interlude_sdk-0.5.0}/src/interlude/table.py +82 -2
  6. {interlude_sdk-0.3.0 → interlude_sdk-0.5.0}/src/interlude_sdk.egg-info/PKG-INFO +29 -1
  7. {interlude_sdk-0.3.0 → interlude_sdk-0.5.0}/tests/test_table.py +73 -0
  8. {interlude_sdk-0.3.0 → interlude_sdk-0.5.0}/setup.cfg +0 -0
  9. {interlude_sdk-0.3.0 → interlude_sdk-0.5.0}/src/interlude/_http.py +0 -0
  10. {interlude_sdk-0.3.0 → interlude_sdk-0.5.0}/src/interlude/_parse.py +0 -0
  11. {interlude_sdk-0.3.0 → interlude_sdk-0.5.0}/src/interlude/account.py +0 -0
  12. {interlude_sdk-0.3.0 → interlude_sdk-0.5.0}/src/interlude/client.py +0 -0
  13. {interlude_sdk-0.3.0 → interlude_sdk-0.5.0}/src/interlude/errors.py +0 -0
  14. {interlude_sdk-0.3.0 → interlude_sdk-0.5.0}/src/interlude/models.py +0 -0
  15. {interlude_sdk-0.3.0 → interlude_sdk-0.5.0}/src/interlude/storage.py +0 -0
  16. {interlude_sdk-0.3.0 → interlude_sdk-0.5.0}/src/interlude_sdk.egg-info/SOURCES.txt +0 -0
  17. {interlude_sdk-0.3.0 → interlude_sdk-0.5.0}/src/interlude_sdk.egg-info/dependency_links.txt +0 -0
  18. {interlude_sdk-0.3.0 → interlude_sdk-0.5.0}/src/interlude_sdk.egg-info/requires.txt +0 -0
  19. {interlude_sdk-0.3.0 → interlude_sdk-0.5.0}/src/interlude_sdk.egg-info/top_level.txt +0 -0
  20. {interlude_sdk-0.3.0 → interlude_sdk-0.5.0}/tests/test_account_session.py +0 -0
  21. {interlude_sdk-0.3.0 → interlude_sdk-0.5.0}/tests/test_client_auth.py +0 -0
  22. {interlude_sdk-0.3.0 → interlude_sdk-0.5.0}/tests/test_end_user.py +0 -0
  23. {interlude_sdk-0.3.0 → interlude_sdk-0.5.0}/tests/test_integration.py +0 -0
  24. {interlude_sdk-0.3.0 → interlude_sdk-0.5.0}/tests/test_storage.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: interlude-sdk
3
- Version: 0.3.0
3
+ Version: 0.5.0
4
4
  Summary: Official Python SDK for Interlude: tables, storage, end users and projects
5
5
  License: MIT
6
6
  Project-URL: Homepage, https://useinterlude.com
@@ -28,6 +28,34 @@ pip install interlude-sdk
28
28
 
29
29
  Package name `interlude-sdk`; import name `interlude`. Python 3.10+.
30
30
 
31
+ ## Tables
32
+
33
+ Once, from a setup script. Safe to run again: it adds what is missing and
34
+ leaves the rest alone.
35
+
36
+ ```python
37
+ client.table("todos").create({
38
+ "owner_id": {"type": "uuid", "nullable": False},
39
+ "title": {"type": "text", "nullable": False},
40
+ "done": {"type": "boolean", "nullable": False, "default": False},
41
+ "note": "text",
42
+ })
43
+
44
+ # Which rows your app's users may touch. Needs the owner's account session.
45
+ owner = Client(project_id="proj_123")
46
+ owner.account.sign_in(email, password)
47
+ owner.table("todos").set_access(
48
+ owner_column="owner_id", select="owner", insert="owner", update="owner", delete="owner"
49
+ )
50
+
51
+ # A claim belongs to whoever made it; the list's owner may also see and clear
52
+ # it. "owner_or_manager" is for select and delete only.
53
+ owner.table("claims").set_access(
54
+ owner_column="claimer_id", manager_column="list_owner",
55
+ select="owner_or_manager", insert="owner", update="none", delete="owner_or_manager",
56
+ )
57
+ ```
58
+
31
59
  ## Rows, as your server
32
60
 
33
61
  ```python
@@ -8,6 +8,34 @@ pip install interlude-sdk
8
8
 
9
9
  Package name `interlude-sdk`; import name `interlude`. Python 3.10+.
10
10
 
11
+ ## Tables
12
+
13
+ Once, from a setup script. Safe to run again: it adds what is missing and
14
+ leaves the rest alone.
15
+
16
+ ```python
17
+ client.table("todos").create({
18
+ "owner_id": {"type": "uuid", "nullable": False},
19
+ "title": {"type": "text", "nullable": False},
20
+ "done": {"type": "boolean", "nullable": False, "default": False},
21
+ "note": "text",
22
+ })
23
+
24
+ # Which rows your app's users may touch. Needs the owner's account session.
25
+ owner = Client(project_id="proj_123")
26
+ owner.account.sign_in(email, password)
27
+ owner.table("todos").set_access(
28
+ owner_column="owner_id", select="owner", insert="owner", update="owner", delete="owner"
29
+ )
30
+
31
+ # A claim belongs to whoever made it; the list's owner may also see and clear
32
+ # it. "owner_or_manager" is for select and delete only.
33
+ owner.table("claims").set_access(
34
+ owner_column="claimer_id", manager_column="list_owner",
35
+ select="owner_or_manager", insert="owner", update="none", delete="owner_or_manager",
36
+ )
37
+ ```
38
+
11
39
  ## Rows, as your server
12
40
 
13
41
  ```python
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "interlude-sdk"
7
- version = "0.3.0"
7
+ version = "0.5.0"
8
8
  description = "Official Python SDK for Interlude: tables, storage, end users and projects"
9
9
  readme = "README.md"
10
10
  license = {text = "MIT"}
@@ -7,7 +7,7 @@ Official Python client library for the Interlude BaaS platform.
7
7
  from __future__ import annotations
8
8
 
9
9
  from .account import AccountClient
10
- from .client import Client, EndUserAuth, UserScope
10
+ from .client import DEFAULT_BASE_URL, Client, EndUserAuth, UserScope
11
11
  from .errors import ClientError, InterludeError
12
12
  from .models import (
13
13
  Account,
@@ -20,15 +20,18 @@ from .models import (
20
20
  SummaryGroup,
21
21
  )
22
22
  from .storage import StorageClient
23
- from .table import TableClient
23
+ from .table import Access, ColumnSpec, TableClient
24
24
 
25
- __version__ = "0.3.0"
25
+ __version__ = "0.5.0"
26
26
 
27
27
  __all__ = [
28
+ "Access",
28
29
  "Account",
29
30
  "AccountClient",
30
31
  "Client",
31
32
  "ClientError",
33
+ "ColumnSpec",
34
+ "DEFAULT_BASE_URL",
32
35
  "EndUser",
33
36
  "EndUserAuth",
34
37
  "Filter",
@@ -2,12 +2,21 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
- from typing import Any, Callable, Iterable, Literal, Optional
5
+ from typing import Any, Callable, Iterable, Literal, Mapping, Optional, Union
6
6
 
7
7
  from ._http import Credentials, Transport, segment
8
+ from .errors import InterludeError
8
9
  from .models import Filter, FilterLike, Measure, Page, Row, Summary, SummaryGroup
9
10
 
10
11
 
12
+ Access = Literal["owner", "owner_or_manager", "public", "none"]
13
+ """Who an access rule lets through. See :meth:`TableClient.set_access`."""
14
+
15
+ ColumnSpec = Union[str, Mapping[str, Any]]
16
+ """A column for :meth:`TableClient.create`: its type (``"text"``), or a dict
17
+ with ``type`` and any of ``nullable``, ``unique`` and ``default``."""
18
+
19
+
11
20
  class TableClient:
12
21
  """Reads and writes one table's rows.
13
22
 
@@ -25,7 +34,78 @@ class TableClient:
25
34
  ) -> None:
26
35
  self._transport = transport
27
36
  self._credentials = credentials
28
- self._path = f"/projects/{segment(project_id)}/tables/{segment(name)}/rows"
37
+ self._name = name
38
+ self._tables = f"/projects/{segment(project_id)}/tables"
39
+ self._path = f"{self._tables}/{segment(name)}/rows"
40
+
41
+ def create(self, columns: Optional[Mapping[str, ColumnSpec]] = None) -> list[str]:
42
+ """Creates this table and any of ``columns`` it lacks. What already
43
+ exists is left as it is, so a setup script can run any number of
44
+ times. Needs the API key. Returns the columns added this time.
45
+
46
+ .. code-block:: python
47
+
48
+ client.table("events").create({
49
+ "title": {"type": "text", "nullable": False},
50
+ "slug": {"type": "text", "nullable": False, "unique": True},
51
+ "venue": "text",
52
+ })
53
+
54
+ A column is its type, or a dict with ``type`` and any of
55
+ ``nullable`` (default True), ``unique`` and ``default``.
56
+ """
57
+ self._ignoring("table_already_exists", self._tables, {"name": self._name})
58
+ added = []
59
+ for name, spec in (columns or {}).items():
60
+ column = {"type": spec} if isinstance(spec, str) else dict(spec)
61
+ if "default" in column and column["default"] is not None:
62
+ column["default"] = _format_value(column["default"])
63
+ body = {"name": name, **{k: v for k, v in column.items() if v is not None}}
64
+ if self._ignoring("column_already_exists", f"{self._tables}/{segment(self._name)}/columns", body):
65
+ added.append(name)
66
+ return added
67
+
68
+ def set_access(
69
+ self,
70
+ *,
71
+ select: Access,
72
+ insert: Access,
73
+ update: Access,
74
+ delete: Access,
75
+ owner_column: Optional[str] = None,
76
+ manager_column: Optional[str] = None,
77
+ ) -> None:
78
+ """Sets which rows your app's users may read and write. A new table
79
+ lets them do nothing until this is set. Needs the project owner's
80
+ account session (``client.account.sign_in``), not the API key.
81
+
82
+ Each rule is ``"owner"`` (rows whose ``owner_column`` is the user's
83
+ id), ``"owner_or_manager"`` (``select`` and ``delete`` only: also rows
84
+ whose ``manager_column`` is the user's id), ``"public"`` (``select``
85
+ only: anyone) or ``"none"``.
86
+ """
87
+ self._transport.send_as(
88
+ self._credentials(),
89
+ "PUT",
90
+ f"{self._tables}/{segment(self._name)}/access",
91
+ body={
92
+ "owner_column": owner_column or "",
93
+ "manager_column": manager_column or "",
94
+ "select": select,
95
+ "insert": insert,
96
+ "update": update,
97
+ "delete": delete,
98
+ },
99
+ )
100
+
101
+ def _ignoring(self, code: str, path: str, body: Any) -> bool:
102
+ try:
103
+ self._transport.send_as(self._credentials(), "POST", path, body=body)
104
+ return True
105
+ except InterludeError as err:
106
+ if err.code == code:
107
+ return False
108
+ raise
29
109
 
30
110
  def list(
31
111
  self,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: interlude-sdk
3
- Version: 0.3.0
3
+ Version: 0.5.0
4
4
  Summary: Official Python SDK for Interlude: tables, storage, end users and projects
5
5
  License: MIT
6
6
  Project-URL: Homepage, https://useinterlude.com
@@ -28,6 +28,34 @@ pip install interlude-sdk
28
28
 
29
29
  Package name `interlude-sdk`; import name `interlude`. Python 3.10+.
30
30
 
31
+ ## Tables
32
+
33
+ Once, from a setup script. Safe to run again: it adds what is missing and
34
+ leaves the rest alone.
35
+
36
+ ```python
37
+ client.table("todos").create({
38
+ "owner_id": {"type": "uuid", "nullable": False},
39
+ "title": {"type": "text", "nullable": False},
40
+ "done": {"type": "boolean", "nullable": False, "default": False},
41
+ "note": "text",
42
+ })
43
+
44
+ # Which rows your app's users may touch. Needs the owner's account session.
45
+ owner = Client(project_id="proj_123")
46
+ owner.account.sign_in(email, password)
47
+ owner.table("todos").set_access(
48
+ owner_column="owner_id", select="owner", insert="owner", update="owner", delete="owner"
49
+ )
50
+
51
+ # A claim belongs to whoever made it; the list's owner may also see and clear
52
+ # it. "owner_or_manager" is for select and delete only.
53
+ owner.table("claims").set_access(
54
+ owner_column="claimer_id", manager_column="list_owner",
55
+ select="owner_or_manager", insert="owner", update="none", delete="owner_or_manager",
56
+ )
57
+ ```
58
+
31
59
  ## Rows, as your server
32
60
 
33
61
  ```python
@@ -192,3 +192,76 @@ def httpx_no_content():
192
192
  import httpx
193
193
 
194
194
  return httpx.Response(204)
195
+
196
+
197
+ TABLES = "/projects/proj_1/tables"
198
+ COLUMNS = "/projects/proj_1/tables/events/columns"
199
+
200
+
201
+ def test_create_makes_the_table_then_each_column():
202
+ rec = Recorder(ok({}, status=201))
203
+
204
+ added = server(rec).table("events").create({
205
+ "title": {"type": "text", "nullable": False},
206
+ "capacity": {"type": "integer", "default": 0},
207
+ "open": {"type": "boolean", "default": True},
208
+ "venue": "text",
209
+ })
210
+
211
+ assert added == ["title", "capacity", "open", "venue"]
212
+ assert [r.url.path for r in rec.requests] == [TABLES] + [COLUMNS] * 4
213
+ assert all(r.headers["X-API-Key"] == API_KEY for r in rec.requests)
214
+ bodies = [json.loads(r.content) for r in rec.requests]
215
+ assert bodies[0] == {"name": "events"}
216
+ assert bodies[1] == {"name": "title", "type": "text", "nullable": False}
217
+ assert bodies[2] == {"name": "capacity", "type": "integer", "default": "0"}
218
+ assert bodies[3] == {"name": "open", "type": "boolean", "default": "true"}
219
+ assert bodies[4] == {"name": "venue", "type": "text"}
220
+
221
+
222
+ def test_create_leaves_what_exists_alone():
223
+ rec = Recorder(
224
+ fail(409, "table_already_exists"),
225
+ fail(409, "column_already_exists"),
226
+ ok({}, status=201),
227
+ )
228
+
229
+ assert server(rec).table("events").create({"title": "text", "venue": "text"}) == ["venue"]
230
+
231
+
232
+ def test_create_surfaces_any_other_failure():
233
+ rec = Recorder(ok({}, status=201), fail(400, "column_type_unsupported"))
234
+
235
+ with pytest.raises(InterludeError) as err:
236
+ server(rec).table("events").create({"bad": "nope"})
237
+
238
+ assert err.value.code == "column_type_unsupported"
239
+
240
+
241
+ def test_set_access_puts_all_four_rules():
242
+ rec = Recorder(ok({}))
243
+
244
+ server(rec).table("todos").set_access(
245
+ owner_column="owner_id", select="owner", insert="owner", update="owner", delete="none"
246
+ )
247
+
248
+ assert rec.last.method == "PUT"
249
+ assert rec.last.url.path == "/projects/proj_1/tables/todos/access"
250
+ assert json.loads(rec.last.content) == {
251
+ "owner_column": "owner_id", "manager_column": "",
252
+ "select": "owner", "insert": "owner", "update": "owner", "delete": "none",
253
+ }
254
+
255
+
256
+ def test_set_access_sends_the_manager_column():
257
+ rec = Recorder(ok({}))
258
+
259
+ server(rec).table("claims").set_access(
260
+ owner_column="claimer_id", manager_column="list_owner",
261
+ select="owner_or_manager", insert="owner", update="none", delete="owner_or_manager",
262
+ )
263
+
264
+ assert json.loads(rec.last.content) == {
265
+ "owner_column": "claimer_id", "manager_column": "list_owner",
266
+ "select": "owner_or_manager", "insert": "owner", "update": "none", "delete": "owner_or_manager",
267
+ }
File without changes