skopik-sdk 0.1.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.
- skopik_sdk-0.1.0/.gitignore +8 -0
- skopik_sdk-0.1.0/LICENSE +21 -0
- skopik_sdk-0.1.0/PKG-INFO +218 -0
- skopik_sdk-0.1.0/README.md +190 -0
- skopik_sdk-0.1.0/pyproject.toml +70 -0
- skopik_sdk-0.1.0/src/skopik/__init__.py +99 -0
- skopik_sdk-0.1.0/src/skopik/_async/__init__.py +0 -0
- skopik_sdk-0.1.0/src/skopik/_async/_pages.py +43 -0
- skopik_sdk-0.1.0/src/skopik/_async/_transport.py +202 -0
- skopik_sdk-0.1.0/src/skopik/_async/buckets.py +188 -0
- skopik_sdk-0.1.0/src/skopik/_async/client.py +130 -0
- skopik_sdk-0.1.0/src/skopik/_async/databases.py +308 -0
- skopik_sdk-0.1.0/src/skopik/_async/email.py +37 -0
- skopik_sdk-0.1.0/src/skopik/_async/mcp.py +101 -0
- skopik_sdk-0.1.0/src/skopik/_async/queues.py +169 -0
- skopik_sdk-0.1.0/src/skopik/_async/sandboxes.py +186 -0
- skopik_sdk-0.1.0/src/skopik/_async/topics.py +241 -0
- skopik_sdk-0.1.0/src/skopik/_async/workers.py +314 -0
- skopik_sdk-0.1.0/src/skopik/_bundle.py +91 -0
- skopik_sdk-0.1.0/src/skopik/_common.py +388 -0
- skopik_sdk-0.1.0/src/skopik/_config.py +99 -0
- skopik_sdk-0.1.0/src/skopik/_contract.py +154 -0
- skopik_sdk-0.1.0/src/skopik/_delivery.py +100 -0
- skopik_sdk-0.1.0/src/skopik/_errors.py +102 -0
- skopik_sdk-0.1.0/src/skopik/_sync/__init__.py +0 -0
- skopik_sdk-0.1.0/src/skopik/_sync/_pages.py +44 -0
- skopik_sdk-0.1.0/src/skopik/_sync/_transport.py +203 -0
- skopik_sdk-0.1.0/src/skopik/_sync/buckets.py +189 -0
- skopik_sdk-0.1.0/src/skopik/_sync/client.py +131 -0
- skopik_sdk-0.1.0/src/skopik/_sync/databases.py +309 -0
- skopik_sdk-0.1.0/src/skopik/_sync/email.py +38 -0
- skopik_sdk-0.1.0/src/skopik/_sync/mcp.py +102 -0
- skopik_sdk-0.1.0/src/skopik/_sync/queues.py +170 -0
- skopik_sdk-0.1.0/src/skopik/_sync/sandboxes.py +187 -0
- skopik_sdk-0.1.0/src/skopik/_sync/topics.py +242 -0
- skopik_sdk-0.1.0/src/skopik/_sync/workers.py +315 -0
- skopik_sdk-0.1.0/src/skopik/_types.py +359 -0
- skopik_sdk-0.1.0/src/skopik/_version.py +1 -0
- skopik_sdk-0.1.0/src/skopik/py.typed +0 -0
skopik_sdk-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Skaler Labs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: skopik-sdk
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: The Python SDK for Skopik Platform: Databases, Buckets, Queues, Topics, Sandboxes, Workers and Email.
|
|
5
|
+
Project-URL: Homepage, https://platform.skopik.com
|
|
6
|
+
Project-URL: Documentation, https://platform.skopik.com/docs/sdk
|
|
7
|
+
Project-URL: Source, https://github.com/skalerlabs/skopik-platform/tree/main/sdk-python
|
|
8
|
+
Author: Skaler Labs
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: buckets,database,queues,sandbox,sdk,skopik,skopik-platform,topics,workers
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Framework :: AsyncIO
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Requires-Python: >=3.10
|
|
25
|
+
Requires-Dist: httpx<1,>=0.27
|
|
26
|
+
Requires-Dist: websockets>=13.1
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# skopik-sdk
|
|
30
|
+
|
|
31
|
+
The Python SDK for [Skopik Platform](https://platform.skopik.com): Databases, Buckets, Queues, Topics, Sandboxes, Workers and Email.
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pip install skopik-sdk
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
It runs on Python 3.10 or later, in servers, scripts and CI, and inside Skopik Workers and Sandboxes. `Skopik` is the client for ordinary code and `AsyncSkopik` the one for `asyncio`: they have the same methods, and the async one's are awaited.
|
|
38
|
+
|
|
39
|
+
## Connecting
|
|
40
|
+
|
|
41
|
+
From a server, a script or CI, the client uses an org's key: `api_key`, or `SKOPIK_API_KEY`, or the key `skopik login` stored. A `read` key can read but not change anything.
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from skopik import Skopik
|
|
45
|
+
|
|
46
|
+
skopik = Skopik() # SKOPIK_API_KEY, or `skopik login`
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
from skopik import AsyncSkopik
|
|
51
|
+
|
|
52
|
+
async with AsyncSkopik() as skopik:
|
|
53
|
+
rows = (await skopik.databases.sql("shop").query("SELECT * FROM orders")).objects
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Inside a Worker or Sandbox, `Skopik.from_bindings()` uses its [bindings](https://platform.skopik.com/docs/bindings/): no key, and the binding's name from `skopik.json` in place of the resource's name. Bindings reach data operations only: creating, deleting or rewinding a resource needs a key.
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
skopik = Skopik.from_bindings()
|
|
60
|
+
db = skopik.databases.sql("DB") # the binding DB
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
A Python Worker carries its packages in its bundle: `pip install --target .packages skopik-sdk` before you deploy. The SDK and what it needs are pure Python.
|
|
64
|
+
|
|
65
|
+
`SKOPIK_API_URL` (or `base_url=`) points the client at another API, such as `https://api.platform-staging.skopik.com`.
|
|
66
|
+
|
|
67
|
+
## Databases
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
from datetime import datetime, timedelta, timezone
|
|
71
|
+
|
|
72
|
+
shop = skopik.databases.sql("shop")
|
|
73
|
+
shop.execute("CREATE TABLE IF NOT EXISTS orders (id TEXT PRIMARY KEY, total REAL)")
|
|
74
|
+
shop.execute("INSERT INTO orders VALUES (?, ?)", ["o_1", 42])
|
|
75
|
+
for order in shop.query("SELECT * FROM orders"): # each row a dict
|
|
76
|
+
print(order["id"], order["total"])
|
|
77
|
+
|
|
78
|
+
shop.batch([
|
|
79
|
+
("UPDATE orders SET total = ? WHERE id = ?", [40, "o_1"]),
|
|
80
|
+
"DELETE FROM orders WHERE total = 0",
|
|
81
|
+
]) # one transaction
|
|
82
|
+
|
|
83
|
+
notes = skopik.databases.documents("notes")
|
|
84
|
+
notes.put({"id": "n1", "title": "Hello", "tags": ["inbox"]})
|
|
85
|
+
for note in notes.query({"field": "tags", "op": "contains", "value": "inbox"}):
|
|
86
|
+
print(note["title"])
|
|
87
|
+
|
|
88
|
+
# History: read, compare or rewind to any moment of the last 30 days
|
|
89
|
+
an_hour_ago = datetime.now(timezone.utc) - timedelta(hours=1)
|
|
90
|
+
shop.query_at("SELECT count(*) AS n FROM orders", at=an_hour_ago)
|
|
91
|
+
shop.rewind(at="2026-11-30T12:00:00Z")
|
|
92
|
+
shop.undo_rewind()
|
|
93
|
+
|
|
94
|
+
skopik.databases.create("analytics", type="sql")
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
A result has the engine's `columns` and `rows`, and `objects`, each row as a dict. `bytes` parameters are sent as blobs, and blobs come back as `bytes`.
|
|
98
|
+
|
|
99
|
+
## Buckets
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
from pathlib import Path
|
|
103
|
+
|
|
104
|
+
uploads = skopik.buckets.bucket("uploads")
|
|
105
|
+
uploads.put("avatars/1.png", Path("1.png")) # a file is sent as it is read
|
|
106
|
+
uploads.put("notes.txt", "hello")
|
|
107
|
+
print(uploads.get("notes.txt").text())
|
|
108
|
+
for o in uploads.list(prefix="avatars/"):
|
|
109
|
+
print(o["key"], o["size"])
|
|
110
|
+
|
|
111
|
+
with uploads.open("videos/big.mp4") as obj: # a stream, for a large object
|
|
112
|
+
for chunk in obj:
|
|
113
|
+
...
|
|
114
|
+
uploads.download("videos/big.mp4", "big.mp4")
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## Queues
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
jobs = skopik.queues.queue("jobs")
|
|
121
|
+
jobs.send({"task": "resize", "key": "avatars/1.png"}) # sent as JSON
|
|
122
|
+
jobs.send_batch(["a", "b", {"body": b"\x00\x01", "content_type": "application/octet-stream"}])
|
|
123
|
+
jobs.schedules.create("nightly", "0 3 * * *", {"task": "cleanup"})
|
|
124
|
+
|
|
125
|
+
for dead in jobs.dead_letters():
|
|
126
|
+
print(dead["id"], dead.payload.json())
|
|
127
|
+
jobs.send_back() # every dead letter back to the Queue
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
In the consumer Worker, `queue_delivery(request)` reads a delivery's headers, from an `http.server` handler, a WSGI environ, an ASGI scope or a framework's request:
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
from skopik import queue_delivery
|
|
134
|
+
|
|
135
|
+
delivery = queue_delivery(self) # None for any other request
|
|
136
|
+
if delivery:
|
|
137
|
+
print(delivery.queue, delivery.message_id, delivery.attempt)
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## Topics
|
|
141
|
+
|
|
142
|
+
```python
|
|
143
|
+
skopik.topics.publish("chat/lobby", {"text": "hi"})
|
|
144
|
+
credential = skopik.topics.create_credential(["chat/**"], ttl_seconds=3600) # for a browser
|
|
145
|
+
|
|
146
|
+
with skopik.topics.subscribe(["chat/lobby"], on_reconnect=refetch_state) as sub:
|
|
147
|
+
for message in sub:
|
|
148
|
+
print(message.topic, message.json())
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
A subscription connects again after a lost connection and calls `on_reconnect`, since nothing published meanwhile is kept. Inside a Worker, publish and make credentials through a Topic binding by naming it: `topics.publish(topic, body, binding="EVENTS")`.
|
|
152
|
+
|
|
153
|
+
## Sandboxes
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
box = skopik.sandboxes.create(name="builds/42", memory_mib=1024)
|
|
157
|
+
box.write_file("/work/requirements.txt", "requests\n")
|
|
158
|
+
result = box.exec(["pip", "install", "-r", "requirements.txt"], cwd="/work", timeout_secs=600)
|
|
159
|
+
print(result["exit_code"], result["stdout"])
|
|
160
|
+
|
|
161
|
+
for event in box.exec_stream("pytest", cwd="/work"):
|
|
162
|
+
if event.type == "exit":
|
|
163
|
+
print("exit", event.exit_code)
|
|
164
|
+
else:
|
|
165
|
+
print(event.text, end="")
|
|
166
|
+
box.stop() # the disk is kept; the next command boots it again
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## Workers
|
|
170
|
+
|
|
171
|
+
```python
|
|
172
|
+
deployed = skopik.workers.deploy("api", directory="./app", runtime="python")
|
|
173
|
+
print(deployed["url"])
|
|
174
|
+
|
|
175
|
+
api = skopik.workers.worker("api")
|
|
176
|
+
api.secrets.set("STRIPE_KEY", "sk_live_…")
|
|
177
|
+
deployments = api.deployments()
|
|
178
|
+
if len(deployments) > 1:
|
|
179
|
+
api.set_alias("production", deployments[1]["id"]) # roll back
|
|
180
|
+
for line in api.logs.tail():
|
|
181
|
+
print(line["line"])
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
`deploy` packs the directory (or a mapping of `files`), makes the Worker if it does not exist, uploads a new deployment and points `production` (or `alias=`) at it, as `skopik deploy` does.
|
|
185
|
+
|
|
186
|
+
## Email
|
|
187
|
+
|
|
188
|
+
```python
|
|
189
|
+
skopik.email.send(to="ada@example.com", subject="Welcome", text="Your workspace is ready.")
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
## Errors, retries and lists
|
|
193
|
+
|
|
194
|
+
Every failure is a `SkopikError` with the API's stable `code`, the HTTP `status` and a `message`:
|
|
195
|
+
|
|
196
|
+
```python
|
|
197
|
+
from skopik import SkopikError
|
|
198
|
+
|
|
199
|
+
try:
|
|
200
|
+
skopik.databases.create("shop", type="sql")
|
|
201
|
+
except SkopikError as e:
|
|
202
|
+
if e.code != "database_exists":
|
|
203
|
+
raise
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
Reads, puts and deletes are retried twice after a network error, `429`, `502`, `503` or `504`, waiting `Retry-After` when the API sends it (`max_retries=` changes it). Anything else that changes data, such as a publish, a send or a command, is never sent again by the SDK, since it may already have happened.
|
|
207
|
+
|
|
208
|
+
A list goes through every page when iterated; `.page()` reads one page (`items`, `prefixes`, `next`) and `.all()` reads every item into a list.
|
|
209
|
+
|
|
210
|
+
API records are plain dicts with the API's own field names, typed for editors with `TypedDict`.
|
|
211
|
+
|
|
212
|
+
## Anything else
|
|
213
|
+
|
|
214
|
+
`skopik.call(tool, **arguments)` calls any [MCP tool](https://platform.skopik.com/docs/mcp/) by name, as `skopik <tool>` does, for an operation the SDK does not wrap, such as members, invites or the Git integration.
|
|
215
|
+
|
|
216
|
+
## License
|
|
217
|
+
|
|
218
|
+
MIT
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
# skopik-sdk
|
|
2
|
+
|
|
3
|
+
The Python SDK for [Skopik Platform](https://platform.skopik.com): Databases, Buckets, Queues, Topics, Sandboxes, Workers and Email.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
pip install skopik-sdk
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
It runs on Python 3.10 or later, in servers, scripts and CI, and inside Skopik Workers and Sandboxes. `Skopik` is the client for ordinary code and `AsyncSkopik` the one for `asyncio`: they have the same methods, and the async one's are awaited.
|
|
10
|
+
|
|
11
|
+
## Connecting
|
|
12
|
+
|
|
13
|
+
From a server, a script or CI, the client uses an org's key: `api_key`, or `SKOPIK_API_KEY`, or the key `skopik login` stored. A `read` key can read but not change anything.
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
from skopik import Skopik
|
|
17
|
+
|
|
18
|
+
skopik = Skopik() # SKOPIK_API_KEY, or `skopik login`
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```python
|
|
22
|
+
from skopik import AsyncSkopik
|
|
23
|
+
|
|
24
|
+
async with AsyncSkopik() as skopik:
|
|
25
|
+
rows = (await skopik.databases.sql("shop").query("SELECT * FROM orders")).objects
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Inside a Worker or Sandbox, `Skopik.from_bindings()` uses its [bindings](https://platform.skopik.com/docs/bindings/): no key, and the binding's name from `skopik.json` in place of the resource's name. Bindings reach data operations only: creating, deleting or rewinding a resource needs a key.
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
skopik = Skopik.from_bindings()
|
|
32
|
+
db = skopik.databases.sql("DB") # the binding DB
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
A Python Worker carries its packages in its bundle: `pip install --target .packages skopik-sdk` before you deploy. The SDK and what it needs are pure Python.
|
|
36
|
+
|
|
37
|
+
`SKOPIK_API_URL` (or `base_url=`) points the client at another API, such as `https://api.platform-staging.skopik.com`.
|
|
38
|
+
|
|
39
|
+
## Databases
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
from datetime import datetime, timedelta, timezone
|
|
43
|
+
|
|
44
|
+
shop = skopik.databases.sql("shop")
|
|
45
|
+
shop.execute("CREATE TABLE IF NOT EXISTS orders (id TEXT PRIMARY KEY, total REAL)")
|
|
46
|
+
shop.execute("INSERT INTO orders VALUES (?, ?)", ["o_1", 42])
|
|
47
|
+
for order in shop.query("SELECT * FROM orders"): # each row a dict
|
|
48
|
+
print(order["id"], order["total"])
|
|
49
|
+
|
|
50
|
+
shop.batch([
|
|
51
|
+
("UPDATE orders SET total = ? WHERE id = ?", [40, "o_1"]),
|
|
52
|
+
"DELETE FROM orders WHERE total = 0",
|
|
53
|
+
]) # one transaction
|
|
54
|
+
|
|
55
|
+
notes = skopik.databases.documents("notes")
|
|
56
|
+
notes.put({"id": "n1", "title": "Hello", "tags": ["inbox"]})
|
|
57
|
+
for note in notes.query({"field": "tags", "op": "contains", "value": "inbox"}):
|
|
58
|
+
print(note["title"])
|
|
59
|
+
|
|
60
|
+
# History: read, compare or rewind to any moment of the last 30 days
|
|
61
|
+
an_hour_ago = datetime.now(timezone.utc) - timedelta(hours=1)
|
|
62
|
+
shop.query_at("SELECT count(*) AS n FROM orders", at=an_hour_ago)
|
|
63
|
+
shop.rewind(at="2026-11-30T12:00:00Z")
|
|
64
|
+
shop.undo_rewind()
|
|
65
|
+
|
|
66
|
+
skopik.databases.create("analytics", type="sql")
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
A result has the engine's `columns` and `rows`, and `objects`, each row as a dict. `bytes` parameters are sent as blobs, and blobs come back as `bytes`.
|
|
70
|
+
|
|
71
|
+
## Buckets
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
from pathlib import Path
|
|
75
|
+
|
|
76
|
+
uploads = skopik.buckets.bucket("uploads")
|
|
77
|
+
uploads.put("avatars/1.png", Path("1.png")) # a file is sent as it is read
|
|
78
|
+
uploads.put("notes.txt", "hello")
|
|
79
|
+
print(uploads.get("notes.txt").text())
|
|
80
|
+
for o in uploads.list(prefix="avatars/"):
|
|
81
|
+
print(o["key"], o["size"])
|
|
82
|
+
|
|
83
|
+
with uploads.open("videos/big.mp4") as obj: # a stream, for a large object
|
|
84
|
+
for chunk in obj:
|
|
85
|
+
...
|
|
86
|
+
uploads.download("videos/big.mp4", "big.mp4")
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Queues
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
jobs = skopik.queues.queue("jobs")
|
|
93
|
+
jobs.send({"task": "resize", "key": "avatars/1.png"}) # sent as JSON
|
|
94
|
+
jobs.send_batch(["a", "b", {"body": b"\x00\x01", "content_type": "application/octet-stream"}])
|
|
95
|
+
jobs.schedules.create("nightly", "0 3 * * *", {"task": "cleanup"})
|
|
96
|
+
|
|
97
|
+
for dead in jobs.dead_letters():
|
|
98
|
+
print(dead["id"], dead.payload.json())
|
|
99
|
+
jobs.send_back() # every dead letter back to the Queue
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
In the consumer Worker, `queue_delivery(request)` reads a delivery's headers, from an `http.server` handler, a WSGI environ, an ASGI scope or a framework's request:
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
from skopik import queue_delivery
|
|
106
|
+
|
|
107
|
+
delivery = queue_delivery(self) # None for any other request
|
|
108
|
+
if delivery:
|
|
109
|
+
print(delivery.queue, delivery.message_id, delivery.attempt)
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Topics
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
skopik.topics.publish("chat/lobby", {"text": "hi"})
|
|
116
|
+
credential = skopik.topics.create_credential(["chat/**"], ttl_seconds=3600) # for a browser
|
|
117
|
+
|
|
118
|
+
with skopik.topics.subscribe(["chat/lobby"], on_reconnect=refetch_state) as sub:
|
|
119
|
+
for message in sub:
|
|
120
|
+
print(message.topic, message.json())
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
A subscription connects again after a lost connection and calls `on_reconnect`, since nothing published meanwhile is kept. Inside a Worker, publish and make credentials through a Topic binding by naming it: `topics.publish(topic, body, binding="EVENTS")`.
|
|
124
|
+
|
|
125
|
+
## Sandboxes
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
box = skopik.sandboxes.create(name="builds/42", memory_mib=1024)
|
|
129
|
+
box.write_file("/work/requirements.txt", "requests\n")
|
|
130
|
+
result = box.exec(["pip", "install", "-r", "requirements.txt"], cwd="/work", timeout_secs=600)
|
|
131
|
+
print(result["exit_code"], result["stdout"])
|
|
132
|
+
|
|
133
|
+
for event in box.exec_stream("pytest", cwd="/work"):
|
|
134
|
+
if event.type == "exit":
|
|
135
|
+
print("exit", event.exit_code)
|
|
136
|
+
else:
|
|
137
|
+
print(event.text, end="")
|
|
138
|
+
box.stop() # the disk is kept; the next command boots it again
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## Workers
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
deployed = skopik.workers.deploy("api", directory="./app", runtime="python")
|
|
145
|
+
print(deployed["url"])
|
|
146
|
+
|
|
147
|
+
api = skopik.workers.worker("api")
|
|
148
|
+
api.secrets.set("STRIPE_KEY", "sk_live_…")
|
|
149
|
+
deployments = api.deployments()
|
|
150
|
+
if len(deployments) > 1:
|
|
151
|
+
api.set_alias("production", deployments[1]["id"]) # roll back
|
|
152
|
+
for line in api.logs.tail():
|
|
153
|
+
print(line["line"])
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
`deploy` packs the directory (or a mapping of `files`), makes the Worker if it does not exist, uploads a new deployment and points `production` (or `alias=`) at it, as `skopik deploy` does.
|
|
157
|
+
|
|
158
|
+
## Email
|
|
159
|
+
|
|
160
|
+
```python
|
|
161
|
+
skopik.email.send(to="ada@example.com", subject="Welcome", text="Your workspace is ready.")
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## Errors, retries and lists
|
|
165
|
+
|
|
166
|
+
Every failure is a `SkopikError` with the API's stable `code`, the HTTP `status` and a `message`:
|
|
167
|
+
|
|
168
|
+
```python
|
|
169
|
+
from skopik import SkopikError
|
|
170
|
+
|
|
171
|
+
try:
|
|
172
|
+
skopik.databases.create("shop", type="sql")
|
|
173
|
+
except SkopikError as e:
|
|
174
|
+
if e.code != "database_exists":
|
|
175
|
+
raise
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
Reads, puts and deletes are retried twice after a network error, `429`, `502`, `503` or `504`, waiting `Retry-After` when the API sends it (`max_retries=` changes it). Anything else that changes data, such as a publish, a send or a command, is never sent again by the SDK, since it may already have happened.
|
|
179
|
+
|
|
180
|
+
A list goes through every page when iterated; `.page()` reads one page (`items`, `prefixes`, `next`) and `.all()` reads every item into a list.
|
|
181
|
+
|
|
182
|
+
API records are plain dicts with the API's own field names, typed for editors with `TypedDict`.
|
|
183
|
+
|
|
184
|
+
## Anything else
|
|
185
|
+
|
|
186
|
+
`skopik.call(tool, **arguments)` calls any [MCP tool](https://platform.skopik.com/docs/mcp/) by name, as `skopik <tool>` does, for an operation the SDK does not wrap, such as members, invites or the Git integration.
|
|
187
|
+
|
|
188
|
+
## License
|
|
189
|
+
|
|
190
|
+
MIT
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.27"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "skopik-sdk"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "The Python SDK for Skopik Platform: Databases, Buckets, Queues, Topics, Sandboxes, Workers and Email."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
requires-python = ">=3.10"
|
|
13
|
+
authors = [{ name = "Skaler Labs" }]
|
|
14
|
+
keywords = ["skopik", "skopik-platform", "sdk", "database", "sandbox", "queues", "topics", "buckets", "workers"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 4 - Beta",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
21
|
+
"Programming Language :: Python :: 3.10",
|
|
22
|
+
"Programming Language :: Python :: 3.11",
|
|
23
|
+
"Programming Language :: Python :: 3.12",
|
|
24
|
+
"Programming Language :: Python :: 3.13",
|
|
25
|
+
"Programming Language :: Python :: 3.14",
|
|
26
|
+
"Framework :: AsyncIO",
|
|
27
|
+
"Typing :: Typed",
|
|
28
|
+
]
|
|
29
|
+
dependencies = [
|
|
30
|
+
"httpx>=0.27,<1",
|
|
31
|
+
"websockets>=13.1",
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
[project.urls]
|
|
35
|
+
Homepage = "https://platform.skopik.com"
|
|
36
|
+
Documentation = "https://platform.skopik.com/docs/sdk"
|
|
37
|
+
Source = "https://github.com/skalerlabs/skopik-platform/tree/main/sdk-python"
|
|
38
|
+
|
|
39
|
+
[dependency-groups]
|
|
40
|
+
dev = ["pytest>=8", "pytest-asyncio>=0.24", "mypy>=1.11", "ruff>=0.6"]
|
|
41
|
+
|
|
42
|
+
[tool.hatch.version]
|
|
43
|
+
path = "src/skopik/_version.py"
|
|
44
|
+
|
|
45
|
+
[tool.hatch.build.targets.wheel]
|
|
46
|
+
packages = ["src/skopik"]
|
|
47
|
+
|
|
48
|
+
[tool.hatch.build.targets.sdist]
|
|
49
|
+
include = ["src/skopik", "README.md", "LICENSE", "pyproject.toml"]
|
|
50
|
+
|
|
51
|
+
[tool.pytest.ini_options]
|
|
52
|
+
testpaths = ["tests"]
|
|
53
|
+
asyncio_mode = "auto"
|
|
54
|
+
asyncio_default_fixture_loop_scope = "function"
|
|
55
|
+
|
|
56
|
+
[tool.ruff]
|
|
57
|
+
line-length = 160
|
|
58
|
+
target-version = "py310"
|
|
59
|
+
extend-exclude = ["src/skopik/_sync"]
|
|
60
|
+
|
|
61
|
+
[tool.ruff.lint]
|
|
62
|
+
select = ["E", "F", "I", "B", "UP"]
|
|
63
|
+
ignore = ["UP006", "UP007", "UP035", "UP045", "B008", "B905", "E501"]
|
|
64
|
+
|
|
65
|
+
[tool.mypy]
|
|
66
|
+
python_version = "3.10"
|
|
67
|
+
strict = true
|
|
68
|
+
warn_return_any = false
|
|
69
|
+
warn_unused_ignores = false
|
|
70
|
+
files = ["src/skopik"]
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"""The Python SDK for Skopik Platform: Databases, Buckets, Queues, Topics,
|
|
2
|
+
Sandboxes, Workers and Email.
|
|
3
|
+
|
|
4
|
+
from skopik import Skopik
|
|
5
|
+
|
|
6
|
+
skopik = Skopik() # SKOPIK_API_KEY, or `skopik login`
|
|
7
|
+
shop = skopik.databases.sql("shop")
|
|
8
|
+
rows = shop.query("SELECT * FROM orders").objects
|
|
9
|
+
|
|
10
|
+
``AsyncSkopik`` has the same methods for ``asyncio``. Inside a Worker or a
|
|
11
|
+
Sandbox, ``Skopik.from_bindings()`` uses its bindings, with no key.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from ._async._pages import AsyncPager
|
|
15
|
+
from ._async.buckets import AsyncBucket, AsyncBuckets, AsyncObjectStream
|
|
16
|
+
from ._async.client import AsyncSkopik
|
|
17
|
+
from ._async.databases import AsyncDatabases, AsyncDocumentsDatabase, AsyncSavedQueries, AsyncSqlDatabase
|
|
18
|
+
from ._async.email import AsyncEmail
|
|
19
|
+
from ._async.queues import AsyncQueue, AsyncQueues, AsyncSchedules
|
|
20
|
+
from ._async.sandboxes import AsyncSandbox, AsyncSandboxes
|
|
21
|
+
from ._async.topics import AsyncSubscription, AsyncTopics
|
|
22
|
+
from ._async.workers import AsyncWorker, AsyncWorkerDomains, AsyncWorkerLogs, AsyncWorkers, AsyncWorkerSecrets
|
|
23
|
+
from ._bundle import pack_bundle, read_directory
|
|
24
|
+
from ._common import BatchResult, BucketObject, DeadLetter, ExecEvent, ObjectInfo, Page, Payload, SqlResult, TopicMessage
|
|
25
|
+
from ._delivery import QueueDelivery, queue_delivery
|
|
26
|
+
from ._errors import SkopikError, is_skopik_error
|
|
27
|
+
from ._sync._pages import Pager
|
|
28
|
+
from ._sync.buckets import Bucket, Buckets, ObjectStream
|
|
29
|
+
from ._sync.client import Skopik
|
|
30
|
+
from ._sync.databases import Databases, DocumentsDatabase, SavedQueries, SqlDatabase
|
|
31
|
+
from ._sync.email import Email
|
|
32
|
+
from ._sync.queues import Queue, Queues, Schedules
|
|
33
|
+
from ._sync.sandboxes import Sandbox, Sandboxes
|
|
34
|
+
from ._sync.topics import Subscription, Topics
|
|
35
|
+
from ._sync.workers import Worker, WorkerDomains, WorkerLogs, Workers, WorkerSecrets
|
|
36
|
+
from ._version import __version__
|
|
37
|
+
|
|
38
|
+
__all__ = [
|
|
39
|
+
"Skopik",
|
|
40
|
+
"AsyncSkopik",
|
|
41
|
+
"SkopikError",
|
|
42
|
+
"is_skopik_error",
|
|
43
|
+
"queue_delivery",
|
|
44
|
+
"QueueDelivery",
|
|
45
|
+
"pack_bundle",
|
|
46
|
+
"read_directory",
|
|
47
|
+
"Page",
|
|
48
|
+
"Pager",
|
|
49
|
+
"AsyncPager",
|
|
50
|
+
"Payload",
|
|
51
|
+
"TopicMessage",
|
|
52
|
+
"DeadLetter",
|
|
53
|
+
"SqlResult",
|
|
54
|
+
"BatchResult",
|
|
55
|
+
"BucketObject",
|
|
56
|
+
"ObjectInfo",
|
|
57
|
+
"ExecEvent",
|
|
58
|
+
"Databases",
|
|
59
|
+
"SqlDatabase",
|
|
60
|
+
"DocumentsDatabase",
|
|
61
|
+
"SavedQueries",
|
|
62
|
+
"Buckets",
|
|
63
|
+
"Bucket",
|
|
64
|
+
"ObjectStream",
|
|
65
|
+
"Queues",
|
|
66
|
+
"Queue",
|
|
67
|
+
"Schedules",
|
|
68
|
+
"Topics",
|
|
69
|
+
"Subscription",
|
|
70
|
+
"Sandboxes",
|
|
71
|
+
"Sandbox",
|
|
72
|
+
"Workers",
|
|
73
|
+
"Worker",
|
|
74
|
+
"WorkerSecrets",
|
|
75
|
+
"WorkerDomains",
|
|
76
|
+
"WorkerLogs",
|
|
77
|
+
"Email",
|
|
78
|
+
"AsyncDatabases",
|
|
79
|
+
"AsyncSqlDatabase",
|
|
80
|
+
"AsyncDocumentsDatabase",
|
|
81
|
+
"AsyncSavedQueries",
|
|
82
|
+
"AsyncBuckets",
|
|
83
|
+
"AsyncBucket",
|
|
84
|
+
"AsyncObjectStream",
|
|
85
|
+
"AsyncQueues",
|
|
86
|
+
"AsyncQueue",
|
|
87
|
+
"AsyncSchedules",
|
|
88
|
+
"AsyncTopics",
|
|
89
|
+
"AsyncSubscription",
|
|
90
|
+
"AsyncSandboxes",
|
|
91
|
+
"AsyncSandbox",
|
|
92
|
+
"AsyncWorkers",
|
|
93
|
+
"AsyncWorker",
|
|
94
|
+
"AsyncWorkerSecrets",
|
|
95
|
+
"AsyncWorkerDomains",
|
|
96
|
+
"AsyncWorkerLogs",
|
|
97
|
+
"AsyncEmail",
|
|
98
|
+
"__version__",
|
|
99
|
+
]
|
|
File without changes
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"""Lists answer a page at a time. A pager is both: ``await pager.page()``
|
|
2
|
+
for one page, or ``async for`` over it for every item, following
|
|
3
|
+
``next``."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from typing import AsyncIterator, Awaitable, Callable, Generic, List, Optional, TypeVar
|
|
8
|
+
|
|
9
|
+
from .._common import Page
|
|
10
|
+
|
|
11
|
+
T = TypeVar("T")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class AsyncPager(Generic[T]):
|
|
15
|
+
def __init__(self, fetch: Callable[[Optional[str]], Awaitable[Page[T]]], after: Optional[str] = None) -> None:
|
|
16
|
+
self._fetch = fetch
|
|
17
|
+
self._after = after
|
|
18
|
+
|
|
19
|
+
async def page(self) -> Page[T]:
|
|
20
|
+
"""The first page (or the one after ``after``)."""
|
|
21
|
+
return await self._fetch(self._after)
|
|
22
|
+
|
|
23
|
+
async def pages(self) -> AsyncIterator[Page[T]]:
|
|
24
|
+
"""Every page, from this one to the last."""
|
|
25
|
+
after = self._after
|
|
26
|
+
while True:
|
|
27
|
+
page = await self._fetch(after)
|
|
28
|
+
yield page
|
|
29
|
+
if not page.next:
|
|
30
|
+
return
|
|
31
|
+
after = page.next
|
|
32
|
+
|
|
33
|
+
async def _items(self) -> AsyncIterator[T]:
|
|
34
|
+
async for page in self.pages():
|
|
35
|
+
for item in page.items:
|
|
36
|
+
yield item
|
|
37
|
+
|
|
38
|
+
def __aiter__(self) -> AsyncIterator[T]:
|
|
39
|
+
return self._items()
|
|
40
|
+
|
|
41
|
+
async def all(self) -> List[T]:
|
|
42
|
+
"""Every item, read into one list."""
|
|
43
|
+
return [item async for item in self]
|