vertracloud-api 0.4.2__tar.gz → 0.4.3__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.
- {vertracloud_api-0.4.2 → vertracloud_api-0.4.3}/CHANGELOG.md +10 -0
- {vertracloud_api-0.4.2 → vertracloud_api-0.4.3}/PKG-INFO +90 -9
- {vertracloud_api-0.4.2 → vertracloud_api-0.4.3}/README.md +89 -8
- {vertracloud_api-0.4.2 → vertracloud_api-0.4.3}/pyproject.toml +1 -1
- {vertracloud_api-0.4.2 → vertracloud_api-0.4.3}/src/vertracloud/__init__.py +10 -0
- {vertracloud_api-0.4.2 → vertracloud_api-0.4.3}/src/vertracloud/client.py +162 -7
- {vertracloud_api-0.4.2 → vertracloud_api-0.4.3}/src/vertracloud/errcodes.py +26 -1
- {vertracloud_api-0.4.2 → vertracloud_api-0.4.3}/src/vertracloud/errors.py +71 -1
- {vertracloud_api-0.4.2 → vertracloud_api-0.4.3}/src/vertracloud/resources/apps.py +113 -13
- {vertracloud_api-0.4.2 → vertracloud_api-0.4.3}/src/vertracloud/resources/databases.py +35 -8
- vertracloud_api-0.4.3/src/vertracloud/resources/snapshot_jobs.py +107 -0
- vertracloud_api-0.4.3/src/vertracloud/resources/snapshots.py +209 -0
- {vertracloud_api-0.4.2 → vertracloud_api-0.4.3}/src/vertracloud/resources/stacks.py +172 -16
- {vertracloud_api-0.4.2 → vertracloud_api-0.4.3}/src/vertracloud/transport.py +36 -2
- {vertracloud_api-0.4.2 → vertracloud_api-0.4.3}/src/vertracloud/types.py +207 -26
- vertracloud_api-0.4.2/src/vertracloud/resources/snapshots.py +0 -80
- {vertracloud_api-0.4.2 → vertracloud_api-0.4.3}/.gitignore +0 -0
- {vertracloud_api-0.4.2 → vertracloud_api-0.4.3}/LICENSE +0 -0
- {vertracloud_api-0.4.2 → vertracloud_api-0.4.3}/src/vertracloud/py.typed +0 -0
- {vertracloud_api-0.4.2 → vertracloud_api-0.4.3}/src/vertracloud/resources/__init__.py +0 -0
- {vertracloud_api-0.4.2 → vertracloud_api-0.4.3}/src/vertracloud/resources/account.py +0 -0
- {vertracloud_api-0.4.2 → vertracloud_api-0.4.3}/src/vertracloud/resources/billing.py +0 -0
- {vertracloud_api-0.4.2 → vertracloud_api-0.4.3}/src/vertracloud/resources/blob.py +0 -0
- {vertracloud_api-0.4.2 → vertracloud_api-0.4.3}/src/vertracloud/resources/images.py +0 -0
- {vertracloud_api-0.4.2 → vertracloud_api-0.4.3}/src/vertracloud/resources/plans.py +0 -0
- {vertracloud_api-0.4.2 → vertracloud_api-0.4.3}/src/vertracloud/resources/templates.py +0 -0
- {vertracloud_api-0.4.2 → vertracloud_api-0.4.3}/src/vertracloud/resources/workspaces.py +0 -0
- {vertracloud_api-0.4.2 → vertracloud_api-0.4.3}/src/vertracloud/sse.py +0 -0
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.4.3](https://github.com/vertracloud/sdk-api-py/compare/v0.4.2...v0.4.3) (2026-11-20)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* **apps:** download an app as a snapshot and stream it to disk ([fe517aa](https://github.com/vertracloud/sdk-api-py/commit/fe517aa66340fdd8012a029863f2300589018c1a))
|
|
9
|
+
* **databases:** reset and delete run right away ([25a1635](https://github.com/vertracloud/sdk-api-py/commit/25a163521993b41f8c90f1f5165a1caa11477afe))
|
|
10
|
+
* **snapshots:** follow snapshot jobs and stream downloads to a file ([3818624](https://github.com/vertracloud/sdk-api-py/commit/381862465befbe645ada8f818fb670441e42d54f))
|
|
11
|
+
* **transport:** read response bodies in chunks ([22d4251](https://github.com/vertracloud/sdk-api-py/commit/22d4251f3761830b14826e7d4d0ceda72f6578eb))
|
|
12
|
+
|
|
3
13
|
## [0.4.2](https://github.com/vertracloud/sdk-api-py/compare/v0.4.1...v0.4.2) (2026-11-11)
|
|
4
14
|
|
|
5
15
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: vertracloud-api
|
|
3
|
-
Version: 0.4.
|
|
3
|
+
Version: 0.4.3
|
|
4
4
|
Summary: Official Python SDK for the public API of Vertra Cloud, a Brazilian application hosting platform (PaaS).
|
|
5
5
|
Project-URL: Homepage, https://vertracloud.app
|
|
6
6
|
Project-URL: Documentation, https://docs.vertracloud.app/sdks
|
|
@@ -76,14 +76,14 @@ client = VertraClient(
|
|
|
76
76
|
|
|
77
77
|
### Per-call options
|
|
78
78
|
|
|
79
|
-
Methods on apps, databases, images, templates and stacks take a keyword-only `workspace_id`; downloads, uploads and streams also take `timeout
|
|
79
|
+
Methods on apps, databases, images, templates and stacks take a keyword-only `workspace_id`; downloads, uploads and streams also take `timeout` (the limit of each request, never a total wait; the `wait_timeout` of the `*_and_wait` and `wait_for_*` helpers is the total):
|
|
80
80
|
|
|
81
81
|
```python
|
|
82
82
|
# Act on a resource that belongs to a workspace
|
|
83
83
|
app = client.apps.get(app_id, workspace_id=workspace_id)
|
|
84
84
|
|
|
85
85
|
# Give one slow call more time
|
|
86
|
-
zip_bytes = client.apps.download(app_id, timeout=300)
|
|
86
|
+
zip_bytes = client.apps.download(app_id, timeout=300) # per request; the wait for the snapshot is `wait_timeout`
|
|
87
87
|
```
|
|
88
88
|
|
|
89
89
|
Nested resources are attributes: `client.apps.deploys`, `client.apps.releases`, `client.apps.envs`, `client.workspaces.members`, `client.billing.orders`, …
|
|
@@ -174,17 +174,26 @@ An app with no site published answers `web_published: False` and empty data. `PL
|
|
|
174
174
|
|
|
175
175
|
### Uploading and downloading
|
|
176
176
|
|
|
177
|
-
Uploads take `bytes
|
|
177
|
+
Uploads take `bytes`. To download, use `download_to_file`, which writes to disk as the bytes arrive and keeps memory constant whatever the size; `download` returns `bytes` and holds the whole file in memory, so keep it for small files.
|
|
178
178
|
|
|
179
179
|
```python
|
|
180
180
|
with open("app.zip", "rb") as f:
|
|
181
181
|
app = client.apps.create({"name": "my-app", "memory": 512}, file=f.read())
|
|
182
182
|
|
|
183
|
-
|
|
184
|
-
f.write(client.apps.download(app_id))
|
|
183
|
+
written = client.apps.download_to_file(app_id, "backup.zip") # bytes written
|
|
185
184
|
```
|
|
186
185
|
|
|
187
|
-
`client.snapshots.download`
|
|
186
|
+
`client.snapshots.download(resource_id, snapshot_id, scope)` downloads one snapshot of an app or a database as `bytes`. It has no total deadline: `timeout` (30 s by default) is the wait of each socket operation, so a large file that keeps arriving completes however long it takes, and one that stops sending bytes fails after `timeout`.
|
|
187
|
+
|
|
188
|
+
`apps.download` downloads the whole app as a zip: it takes a manual snapshot (`snapshots.create_and_wait`, which waits for the job when the API answers 202) and downloads it. The snapshot stays in `snapshots.list` and counts on the manual snapshot quota. An API key needs the scopes `snapshots:write` (to take the snapshot) and `snapshots:read` (to download it); `apps:files` alone does not download. `wait_timeout` is the total wait for the snapshot (30 minutes by default), `interval` the poll interval and `timeout` the limit of each request; it raises what `snapshots.create_and_wait` raises. An account without a plan cannot take the snapshot, but can list and download the ones it has.
|
|
189
|
+
|
|
190
|
+
```python
|
|
191
|
+
written = client.apps.download_to_file(
|
|
192
|
+
app_id, "backup.zip", wait_timeout=900, on_progress=lambda job: print(job["status"])
|
|
193
|
+
)
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
`download_to_file` is the recommended form for anything that can be large (a snapshot of several GB is fine): the zip goes to a temporary file next to the destination, is renamed to the final name only when the download ends, and a failure, a stall (`timeout` without bytes), a connection cut before the announced `Content-Length` (`DownloadIncompleteError`, an `OSError`) or a `KeyboardInterrupt` removes the temporary file, so the destination never holds a partial file. The snapshot and app downloads take `workspace_id` like the rest, and send it on every call of the flow (take the snapshot, follow the job, download). `client.snapshots.download_to_file(resource_id, snapshot_id, scope, path)` does the same for an existing snapshot. `apps.download_stream` and `snapshots.download_stream` return an iterator of byte chunks (1 MiB at most) when you want to send the bytes somewhere other than a file; `apps.download` and `snapshots.download` return the whole zip as `bytes`.
|
|
188
197
|
|
|
189
198
|
### Waiting for the first build
|
|
190
199
|
|
|
@@ -332,7 +341,7 @@ if change["restart_required"]:
|
|
|
332
341
|
print("restart:", change["apps_to_restart"])
|
|
333
342
|
```
|
|
334
343
|
|
|
335
|
-
`delete` needs `confirm_name` equal to the stack name; mode `dissolve` removes only the stack
|
|
344
|
+
`delete` needs `confirm_name` equal to the stack name; mode `dissolve` removes only the stack (200). Mode `delete_all` deletes the members too. Both answer 200 right away with `{"deleted_apps", "deleted_databases", "deleted_blob"}` and are permanent: take a snapshot first (`stacks.snapshots.create`) if you may need the data again. `members`, `bindings` and `snapshots` group the routes of members, connections and whole-stack snapshots; `connection` returns code to connect an app to what it is linked to. Move a stack or a template to a workspace with `workspaces.stacks` and `workspaces.templates`. The error codes of both areas are constants in `vertracloud.errcodes`.
|
|
336
345
|
|
|
337
346
|
### Database versions
|
|
338
347
|
|
|
@@ -344,6 +353,8 @@ print(db["version"]) # major line, e.g. "18"
|
|
|
344
353
|
|
|
345
354
|
Omit `version` to get the recommended one; with `snapshot_id`, omit it to keep the snapshot's line (an older one fails with `SNAPSHOT_VERSION_NEWER`, an unknown one with `INVALID_DATABASE_VERSION`). A database's version cannot be changed after creation; security updates are applied when it is stopped and started.
|
|
346
355
|
|
|
356
|
+
With `snapshot_id` the database is created first (201) and the data comes back in the background: the answer has `restore_job_id`, to follow with `snapshot_jobs.wait(db["id"], db["restore_job_id"], "databases")`. If the restore fails, **the database is kept** (empty, or with what you wrote to it) and the job's `reason` says why. `apps.create` with `snapshot_id` also answers with `restore_job_id`; there an app of code whose restore fails is deleted by the API, while an app on a custom image is kept as it is.
|
|
357
|
+
|
|
347
358
|
### Plans and quotes
|
|
348
359
|
|
|
349
360
|
```python
|
|
@@ -358,6 +369,74 @@ print(quote["total"], quote["period_discount_percent"], quote["legacy_price"])
|
|
|
358
369
|
|
|
359
370
|
`client.billing.orders.create` takes the same `plan` and `months`. `plan` is a `PlanName`: `starter-1`, `starter-2`, `pro-4`, `pro-6`, `pro-8`, `business-12`, `business-16`, `business-24` or `enterprise-32` up to `enterprise-512`.
|
|
360
371
|
|
|
372
|
+
### Snapshots
|
|
373
|
+
|
|
374
|
+
Deleting an app (`apps.delete`) or a database (`databases.delete`), resetting a database (`databases.reset`) and restoring a snapshot (`snapshots.restore`) run right away and are permanent: they replace or remove the current data. If you may need it again, take a snapshot first. `apps.delete` returns `None`, `databases.delete` returns the database id, `databases.reset` returns `{"status": "success"}` and `snapshots.restore` returns `{"job_id": ..., "warning"?: ...}`.
|
|
375
|
+
|
|
376
|
+
```python
|
|
377
|
+
client.snapshots.create_and_wait(db_id, "databases")
|
|
378
|
+
client.databases.reset(db_id)
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
`snapshots.create` can take up to 90 seconds, so its timeout is 120 s (the client's, if larger) and `timeout=` overrides it per call. Creating snapshots is limited: a quota over a rolling 24 hours that depends on the plan's RAM, and a wait between snapshots of the same resource (and a 5 s pause per user, answered with a plain `RATE_LIMIT_EXCEEDED` 429). Over a snapshot limit the call fails with `SNAPSHOT_RATE_LIMITED` (429) and `RateLimitError.retry_after` is the number of seconds to wait; `err.details` has the `scope` (`resource` or `account`) and, for `account`, `resets_at`. A manual snapshot can also answer 202 (`OPERATION_IN_PROGRESS` carries `details.job_id` when one of the same resource is already running) with `{"job_id": ...}` instead of a snapshot: it is still being taken. `snapshots.create` makes one request and returns either; `snapshots.create_and_wait` follows the job (below) and returns the snapshot. Other codes are constants in `vertracloud.errcodes` (`SNAPSHOT_TOO_LARGE` carries `details.max_bytes`).
|
|
382
|
+
|
|
383
|
+
#### Restore as a job
|
|
384
|
+
|
|
385
|
+
`snapshots.restore` answers 202: the restore runs in the background, so it returns `{"job_id": ..., "warning"?: ...}` and the resource is not restored yet. Follow the job with `client.snapshot_jobs`:
|
|
386
|
+
|
|
387
|
+
```python
|
|
388
|
+
from vertracloud import SnapshotJobFailedError, SnapshotJobSkippedError, SnapshotJobTimeoutError
|
|
389
|
+
|
|
390
|
+
accepted = client.snapshots.restore(app_id, snapshot_id, "applications", restore_image_version=True)
|
|
391
|
+
if accepted.get("warning"):
|
|
392
|
+
print(accepted["warning"]) # IMAGE_VERSION_NOT_FOUND: only the data is restored
|
|
393
|
+
|
|
394
|
+
job = client.snapshot_jobs.get(app_id, accepted["job_id"]) # one look; `scope` is optional here
|
|
395
|
+
print(job["status"]) # queued, running, ready, skipped or failed
|
|
396
|
+
|
|
397
|
+
# on_progress runs on every poll while the job has not ended; job["progress"] exists only while it is running
|
|
398
|
+
# (phase, percent?, done_bytes?, total_bytes?, eta_seconds?)
|
|
399
|
+
show = lambda job: print(job["status"], job.get("progress")) # noqa: E731
|
|
400
|
+
|
|
401
|
+
try:
|
|
402
|
+
job = client.snapshot_jobs.wait(
|
|
403
|
+
app_id, accepted["job_id"], "applications", wait_timeout=900, interval=2, on_progress=show
|
|
404
|
+
)
|
|
405
|
+
except (SnapshotJobFailedError, SnapshotJobSkippedError) as err:
|
|
406
|
+
print(err.code, err.details) # the job's public reason, e.g. SNAPSHOT_CORRUPTED; err.job is the last payload
|
|
407
|
+
except SnapshotJobTimeoutError:
|
|
408
|
+
... # still running; the job is not cancelled
|
|
409
|
+
|
|
410
|
+
recent = client.snapshot_jobs.list(app_id, "applications", kind="restore", limit=5) # limit 1 to 50
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
`stacks.snapshots.create` also answers 202, with `{"id": ..., "jobs": [{"resource_id", "resource_type", "job_id"}, ...]}`: one snapshot job per member (databases first), each a regular manual snapshot. The stack snapshot is listed as `pending` and becomes `ready` when the last job ends. Follow the jobs with `snapshot_jobs`, let `wait_for_create` do it under one `wait_timeout` (same outcomes as `wait_for_restore`) or call `stacks.snapshots.create_and_wait(stack_id)`, which resolves with the accepted answer plus `finished`, the final job of each member.
|
|
414
|
+
|
|
415
|
+
`stacks.snapshots.restore` also answers 202, with `{"jobs": [{"resource_id", "resource_type", "job_id", "warning"?}, ...]}`: one job per member (`warning` is `IMAGE_VERSION_NOT_FOUND` for an application whose recorded image version is gone), restored in series by the server (databases first). The stack is back when every job is `ready`; when a member fails, the jobs after it end `failed` with `STACK_RESTORE_MEMBER_FAILED`. A refusal before the queue keeps the code and `details` of the member. Follow each job with `snapshot_jobs` (scope `"databases"` or `"applications"` by `resource_type`) let `wait_for_restore` do it under one `wait_timeout`, or call `stacks.snapshots.restore_and_wait(stack_id, snapshot_id)`:
|
|
416
|
+
|
|
417
|
+
```python
|
|
418
|
+
accepted = client.stacks.snapshots.restore(stack_id, snapshot_id)
|
|
419
|
+
try:
|
|
420
|
+
finished = client.stacks.snapshots.wait_for_restore(accepted, wait_timeout=1800, interval=2)
|
|
421
|
+
except SnapshotJobFailedError as err:
|
|
422
|
+
print(err.code, err.job) # the first failed member (STACK_RESTORE_MEMBER_FAILED for the ones behind it)
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
`wait` is optional: it polls `get` until the job ends and returns the job when it is `ready`. It raises `SnapshotJobFailedError` or `SnapshotJobSkippedError` (`code` and `reason` = the job's `reason`, `details`, `job` = the last payload) when it ends any other way, and `SnapshotJobTimeoutError` (with the last `job`) after `wait_timeout`, the total wait (30 minutes by default); `timeout` is the limit of each request. `create_and_wait`, `wait_for_create`/`wait_for_restore`, `restore_and_wait` and `apps.download` behave the same. The `:id` of both routes is the resource the job acts on; `scope` is required to list and optional to get or wait for one job. While a restore runs every write to the resource fails with `RESTORE_IN_PROGRESS` (409); reads and deleting it still work. Before queueing, the API can refuse with `SNAPSHOT_EXCEEDS_STORAGE` (400; `err.snapshot_exceeds_storage` has `required_bytes`, `available_bytes` and, for databases, `required_ram_mb`), `APP_BUILDING` (409), `RESTORE_IN_PROGRESS` or `RESTORE_COOLDOWN` (429: the wait between restores or rollbacks, `RateLimitError.retry_after`, also in the `Retry-After` header). While a snapshot of an app is being taken, deploys, rollbacks, restarts, stops, environment changes and writes of the file manager fail with `OPERATION_IN_PROGRESS` (409): try again shortly.
|
|
426
|
+
|
|
427
|
+
#### Wait helpers
|
|
428
|
+
|
|
429
|
+
One pattern: `wait_for_<x>` follows something already started, `<verb>_and_wait` starts it and follows it. In all of them `wait_timeout` is the total wait (30 minutes by default), `timeout` the limit of each request and `interval` the poll interval; the work keeps running on the server after a timeout.
|
|
430
|
+
|
|
431
|
+
| Start | Follow | Both |
|
|
432
|
+
|---|---|---|
|
|
433
|
+
| `snapshots.create` | `snapshot_jobs.wait(resource_id, job_id)` | `snapshots.create_and_wait`, `apps.download` (also downloads the zip) |
|
|
434
|
+
| `snapshots.restore` | `snapshot_jobs.wait(resource_id, job_id)` | none |
|
|
435
|
+
| `stacks.snapshots.create` | `stacks.snapshots.wait_for_create(accepted)` | `stacks.snapshots.create_and_wait` |
|
|
436
|
+
| `stacks.snapshots.restore` | `stacks.snapshots.wait_for_restore(accepted)` | `stacks.snapshots.restore_and_wait` |
|
|
437
|
+
|
|
438
|
+
`apps.wait_until_ready(app_id, timeout=...)` keeps its `timeout` as the total wait for the build.
|
|
439
|
+
|
|
361
440
|
### Errors
|
|
362
441
|
|
|
363
442
|
Every non-2xx response raises a `VertraAPIError` carrying the HTTP `status`, the API `code` (e.g. `APP_NOT_FOUND`) and `details`; `str(err)` includes the message. The API key is never part of the error, even when it is printed:
|
|
@@ -377,6 +456,8 @@ except VertraAPIError as err:
|
|
|
377
456
|
|
|
378
457
|
A few errors carry a structured `details`; tolerant properties read them and give an empty value for any other error: `err.issues` (`DOCKERFILE_REFUSED`), `err.manifest_issues` (`TEMPLATE_MANIFEST_INVALID`), `err.secret_files` (`TEMPLATE_SECRET_IN_SOURCE`), `err.update_blocked` (`TEMPLATE_UPDATE_BLOCKED`) and `err.detach_action` (`TEMPLATE_DETACH_CONFIRMATION_REQUIRED`).
|
|
379
458
|
|
|
459
|
+
`apps.files.read` and `apps.files.write` handle files up to 10 MiB; above it the call fails with `FILE_TOO_LARGE` (413) and `details.max_bytes` is the limit.
|
|
460
|
+
|
|
380
461
|
Other subclasses: `AuthenticationError` (401), `ScopeDeniedError` (403 — including a missing key scope) and `ValidationError` (400/422). The error codes are listed in the [API reference](https://docs.vertracloud.app/api-reference/introduction).
|
|
381
462
|
|
|
382
463
|
### Testing your code
|
|
@@ -396,7 +477,7 @@ client = VertraClient(api_key="test", transport=my_fake_transport)
|
|
|
396
477
|
| Custom images | `client.images` | 13 |
|
|
397
478
|
| Templates | `client.templates` (+ `.deploys`, `.share_links`, `.share`, `.updates`) | 32 |
|
|
398
479
|
| Stacks | `client.stacks` (+ `.members`, `.bindings`, `.snapshots`) | 20 |
|
|
399
|
-
| Snapshots | `client.snapshots` | 5 |
|
|
480
|
+
| Snapshots | `client.snapshots`, `client.snapshot_jobs` | 5 + 2 |
|
|
400
481
|
| Account | `client.account` (+ `.sessions`, `.folders`, `.favorites`) | 10 |
|
|
401
482
|
| Workspaces | `client.workspaces` (+ `.members`, `.roles`, `.invites`, `.action_requests`, `.apps`, `.databases`, `.templates`, `.stacks`, `.folders`, `.favorites`) | 34 |
|
|
402
483
|
| Billing | `client.billing` (+ `.orders`, with `orders.quote`) | 8 |
|
|
@@ -57,14 +57,14 @@ client = VertraClient(
|
|
|
57
57
|
|
|
58
58
|
### Per-call options
|
|
59
59
|
|
|
60
|
-
Methods on apps, databases, images, templates and stacks take a keyword-only `workspace_id`; downloads, uploads and streams also take `timeout
|
|
60
|
+
Methods on apps, databases, images, templates and stacks take a keyword-only `workspace_id`; downloads, uploads and streams also take `timeout` (the limit of each request, never a total wait; the `wait_timeout` of the `*_and_wait` and `wait_for_*` helpers is the total):
|
|
61
61
|
|
|
62
62
|
```python
|
|
63
63
|
# Act on a resource that belongs to a workspace
|
|
64
64
|
app = client.apps.get(app_id, workspace_id=workspace_id)
|
|
65
65
|
|
|
66
66
|
# Give one slow call more time
|
|
67
|
-
zip_bytes = client.apps.download(app_id, timeout=300)
|
|
67
|
+
zip_bytes = client.apps.download(app_id, timeout=300) # per request; the wait for the snapshot is `wait_timeout`
|
|
68
68
|
```
|
|
69
69
|
|
|
70
70
|
Nested resources are attributes: `client.apps.deploys`, `client.apps.releases`, `client.apps.envs`, `client.workspaces.members`, `client.billing.orders`, …
|
|
@@ -155,17 +155,26 @@ An app with no site published answers `web_published: False` and empty data. `PL
|
|
|
155
155
|
|
|
156
156
|
### Uploading and downloading
|
|
157
157
|
|
|
158
|
-
Uploads take `bytes
|
|
158
|
+
Uploads take `bytes`. To download, use `download_to_file`, which writes to disk as the bytes arrive and keeps memory constant whatever the size; `download` returns `bytes` and holds the whole file in memory, so keep it for small files.
|
|
159
159
|
|
|
160
160
|
```python
|
|
161
161
|
with open("app.zip", "rb") as f:
|
|
162
162
|
app = client.apps.create({"name": "my-app", "memory": 512}, file=f.read())
|
|
163
163
|
|
|
164
|
-
|
|
165
|
-
f.write(client.apps.download(app_id))
|
|
164
|
+
written = client.apps.download_to_file(app_id, "backup.zip") # bytes written
|
|
166
165
|
```
|
|
167
166
|
|
|
168
|
-
`client.snapshots.download`
|
|
167
|
+
`client.snapshots.download(resource_id, snapshot_id, scope)` downloads one snapshot of an app or a database as `bytes`. It has no total deadline: `timeout` (30 s by default) is the wait of each socket operation, so a large file that keeps arriving completes however long it takes, and one that stops sending bytes fails after `timeout`.
|
|
168
|
+
|
|
169
|
+
`apps.download` downloads the whole app as a zip: it takes a manual snapshot (`snapshots.create_and_wait`, which waits for the job when the API answers 202) and downloads it. The snapshot stays in `snapshots.list` and counts on the manual snapshot quota. An API key needs the scopes `snapshots:write` (to take the snapshot) and `snapshots:read` (to download it); `apps:files` alone does not download. `wait_timeout` is the total wait for the snapshot (30 minutes by default), `interval` the poll interval and `timeout` the limit of each request; it raises what `snapshots.create_and_wait` raises. An account without a plan cannot take the snapshot, but can list and download the ones it has.
|
|
170
|
+
|
|
171
|
+
```python
|
|
172
|
+
written = client.apps.download_to_file(
|
|
173
|
+
app_id, "backup.zip", wait_timeout=900, on_progress=lambda job: print(job["status"])
|
|
174
|
+
)
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
`download_to_file` is the recommended form for anything that can be large (a snapshot of several GB is fine): the zip goes to a temporary file next to the destination, is renamed to the final name only when the download ends, and a failure, a stall (`timeout` without bytes), a connection cut before the announced `Content-Length` (`DownloadIncompleteError`, an `OSError`) or a `KeyboardInterrupt` removes the temporary file, so the destination never holds a partial file. The snapshot and app downloads take `workspace_id` like the rest, and send it on every call of the flow (take the snapshot, follow the job, download). `client.snapshots.download_to_file(resource_id, snapshot_id, scope, path)` does the same for an existing snapshot. `apps.download_stream` and `snapshots.download_stream` return an iterator of byte chunks (1 MiB at most) when you want to send the bytes somewhere other than a file; `apps.download` and `snapshots.download` return the whole zip as `bytes`.
|
|
169
178
|
|
|
170
179
|
### Waiting for the first build
|
|
171
180
|
|
|
@@ -313,7 +322,7 @@ if change["restart_required"]:
|
|
|
313
322
|
print("restart:", change["apps_to_restart"])
|
|
314
323
|
```
|
|
315
324
|
|
|
316
|
-
`delete` needs `confirm_name` equal to the stack name; mode `dissolve` removes only the stack
|
|
325
|
+
`delete` needs `confirm_name` equal to the stack name; mode `dissolve` removes only the stack (200). Mode `delete_all` deletes the members too. Both answer 200 right away with `{"deleted_apps", "deleted_databases", "deleted_blob"}` and are permanent: take a snapshot first (`stacks.snapshots.create`) if you may need the data again. `members`, `bindings` and `snapshots` group the routes of members, connections and whole-stack snapshots; `connection` returns code to connect an app to what it is linked to. Move a stack or a template to a workspace with `workspaces.stacks` and `workspaces.templates`. The error codes of both areas are constants in `vertracloud.errcodes`.
|
|
317
326
|
|
|
318
327
|
### Database versions
|
|
319
328
|
|
|
@@ -325,6 +334,8 @@ print(db["version"]) # major line, e.g. "18"
|
|
|
325
334
|
|
|
326
335
|
Omit `version` to get the recommended one; with `snapshot_id`, omit it to keep the snapshot's line (an older one fails with `SNAPSHOT_VERSION_NEWER`, an unknown one with `INVALID_DATABASE_VERSION`). A database's version cannot be changed after creation; security updates are applied when it is stopped and started.
|
|
327
336
|
|
|
337
|
+
With `snapshot_id` the database is created first (201) and the data comes back in the background: the answer has `restore_job_id`, to follow with `snapshot_jobs.wait(db["id"], db["restore_job_id"], "databases")`. If the restore fails, **the database is kept** (empty, or with what you wrote to it) and the job's `reason` says why. `apps.create` with `snapshot_id` also answers with `restore_job_id`; there an app of code whose restore fails is deleted by the API, while an app on a custom image is kept as it is.
|
|
338
|
+
|
|
328
339
|
### Plans and quotes
|
|
329
340
|
|
|
330
341
|
```python
|
|
@@ -339,6 +350,74 @@ print(quote["total"], quote["period_discount_percent"], quote["legacy_price"])
|
|
|
339
350
|
|
|
340
351
|
`client.billing.orders.create` takes the same `plan` and `months`. `plan` is a `PlanName`: `starter-1`, `starter-2`, `pro-4`, `pro-6`, `pro-8`, `business-12`, `business-16`, `business-24` or `enterprise-32` up to `enterprise-512`.
|
|
341
352
|
|
|
353
|
+
### Snapshots
|
|
354
|
+
|
|
355
|
+
Deleting an app (`apps.delete`) or a database (`databases.delete`), resetting a database (`databases.reset`) and restoring a snapshot (`snapshots.restore`) run right away and are permanent: they replace or remove the current data. If you may need it again, take a snapshot first. `apps.delete` returns `None`, `databases.delete` returns the database id, `databases.reset` returns `{"status": "success"}` and `snapshots.restore` returns `{"job_id": ..., "warning"?: ...}`.
|
|
356
|
+
|
|
357
|
+
```python
|
|
358
|
+
client.snapshots.create_and_wait(db_id, "databases")
|
|
359
|
+
client.databases.reset(db_id)
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
`snapshots.create` can take up to 90 seconds, so its timeout is 120 s (the client's, if larger) and `timeout=` overrides it per call. Creating snapshots is limited: a quota over a rolling 24 hours that depends on the plan's RAM, and a wait between snapshots of the same resource (and a 5 s pause per user, answered with a plain `RATE_LIMIT_EXCEEDED` 429). Over a snapshot limit the call fails with `SNAPSHOT_RATE_LIMITED` (429) and `RateLimitError.retry_after` is the number of seconds to wait; `err.details` has the `scope` (`resource` or `account`) and, for `account`, `resets_at`. A manual snapshot can also answer 202 (`OPERATION_IN_PROGRESS` carries `details.job_id` when one of the same resource is already running) with `{"job_id": ...}` instead of a snapshot: it is still being taken. `snapshots.create` makes one request and returns either; `snapshots.create_and_wait` follows the job (below) and returns the snapshot. Other codes are constants in `vertracloud.errcodes` (`SNAPSHOT_TOO_LARGE` carries `details.max_bytes`).
|
|
363
|
+
|
|
364
|
+
#### Restore as a job
|
|
365
|
+
|
|
366
|
+
`snapshots.restore` answers 202: the restore runs in the background, so it returns `{"job_id": ..., "warning"?: ...}` and the resource is not restored yet. Follow the job with `client.snapshot_jobs`:
|
|
367
|
+
|
|
368
|
+
```python
|
|
369
|
+
from vertracloud import SnapshotJobFailedError, SnapshotJobSkippedError, SnapshotJobTimeoutError
|
|
370
|
+
|
|
371
|
+
accepted = client.snapshots.restore(app_id, snapshot_id, "applications", restore_image_version=True)
|
|
372
|
+
if accepted.get("warning"):
|
|
373
|
+
print(accepted["warning"]) # IMAGE_VERSION_NOT_FOUND: only the data is restored
|
|
374
|
+
|
|
375
|
+
job = client.snapshot_jobs.get(app_id, accepted["job_id"]) # one look; `scope` is optional here
|
|
376
|
+
print(job["status"]) # queued, running, ready, skipped or failed
|
|
377
|
+
|
|
378
|
+
# on_progress runs on every poll while the job has not ended; job["progress"] exists only while it is running
|
|
379
|
+
# (phase, percent?, done_bytes?, total_bytes?, eta_seconds?)
|
|
380
|
+
show = lambda job: print(job["status"], job.get("progress")) # noqa: E731
|
|
381
|
+
|
|
382
|
+
try:
|
|
383
|
+
job = client.snapshot_jobs.wait(
|
|
384
|
+
app_id, accepted["job_id"], "applications", wait_timeout=900, interval=2, on_progress=show
|
|
385
|
+
)
|
|
386
|
+
except (SnapshotJobFailedError, SnapshotJobSkippedError) as err:
|
|
387
|
+
print(err.code, err.details) # the job's public reason, e.g. SNAPSHOT_CORRUPTED; err.job is the last payload
|
|
388
|
+
except SnapshotJobTimeoutError:
|
|
389
|
+
... # still running; the job is not cancelled
|
|
390
|
+
|
|
391
|
+
recent = client.snapshot_jobs.list(app_id, "applications", kind="restore", limit=5) # limit 1 to 50
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
`stacks.snapshots.create` also answers 202, with `{"id": ..., "jobs": [{"resource_id", "resource_type", "job_id"}, ...]}`: one snapshot job per member (databases first), each a regular manual snapshot. The stack snapshot is listed as `pending` and becomes `ready` when the last job ends. Follow the jobs with `snapshot_jobs`, let `wait_for_create` do it under one `wait_timeout` (same outcomes as `wait_for_restore`) or call `stacks.snapshots.create_and_wait(stack_id)`, which resolves with the accepted answer plus `finished`, the final job of each member.
|
|
395
|
+
|
|
396
|
+
`stacks.snapshots.restore` also answers 202, with `{"jobs": [{"resource_id", "resource_type", "job_id", "warning"?}, ...]}`: one job per member (`warning` is `IMAGE_VERSION_NOT_FOUND` for an application whose recorded image version is gone), restored in series by the server (databases first). The stack is back when every job is `ready`; when a member fails, the jobs after it end `failed` with `STACK_RESTORE_MEMBER_FAILED`. A refusal before the queue keeps the code and `details` of the member. Follow each job with `snapshot_jobs` (scope `"databases"` or `"applications"` by `resource_type`) let `wait_for_restore` do it under one `wait_timeout`, or call `stacks.snapshots.restore_and_wait(stack_id, snapshot_id)`:
|
|
397
|
+
|
|
398
|
+
```python
|
|
399
|
+
accepted = client.stacks.snapshots.restore(stack_id, snapshot_id)
|
|
400
|
+
try:
|
|
401
|
+
finished = client.stacks.snapshots.wait_for_restore(accepted, wait_timeout=1800, interval=2)
|
|
402
|
+
except SnapshotJobFailedError as err:
|
|
403
|
+
print(err.code, err.job) # the first failed member (STACK_RESTORE_MEMBER_FAILED for the ones behind it)
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
`wait` is optional: it polls `get` until the job ends and returns the job when it is `ready`. It raises `SnapshotJobFailedError` or `SnapshotJobSkippedError` (`code` and `reason` = the job's `reason`, `details`, `job` = the last payload) when it ends any other way, and `SnapshotJobTimeoutError` (with the last `job`) after `wait_timeout`, the total wait (30 minutes by default); `timeout` is the limit of each request. `create_and_wait`, `wait_for_create`/`wait_for_restore`, `restore_and_wait` and `apps.download` behave the same. The `:id` of both routes is the resource the job acts on; `scope` is required to list and optional to get or wait for one job. While a restore runs every write to the resource fails with `RESTORE_IN_PROGRESS` (409); reads and deleting it still work. Before queueing, the API can refuse with `SNAPSHOT_EXCEEDS_STORAGE` (400; `err.snapshot_exceeds_storage` has `required_bytes`, `available_bytes` and, for databases, `required_ram_mb`), `APP_BUILDING` (409), `RESTORE_IN_PROGRESS` or `RESTORE_COOLDOWN` (429: the wait between restores or rollbacks, `RateLimitError.retry_after`, also in the `Retry-After` header). While a snapshot of an app is being taken, deploys, rollbacks, restarts, stops, environment changes and writes of the file manager fail with `OPERATION_IN_PROGRESS` (409): try again shortly.
|
|
407
|
+
|
|
408
|
+
#### Wait helpers
|
|
409
|
+
|
|
410
|
+
One pattern: `wait_for_<x>` follows something already started, `<verb>_and_wait` starts it and follows it. In all of them `wait_timeout` is the total wait (30 minutes by default), `timeout` the limit of each request and `interval` the poll interval; the work keeps running on the server after a timeout.
|
|
411
|
+
|
|
412
|
+
| Start | Follow | Both |
|
|
413
|
+
|---|---|---|
|
|
414
|
+
| `snapshots.create` | `snapshot_jobs.wait(resource_id, job_id)` | `snapshots.create_and_wait`, `apps.download` (also downloads the zip) |
|
|
415
|
+
| `snapshots.restore` | `snapshot_jobs.wait(resource_id, job_id)` | none |
|
|
416
|
+
| `stacks.snapshots.create` | `stacks.snapshots.wait_for_create(accepted)` | `stacks.snapshots.create_and_wait` |
|
|
417
|
+
| `stacks.snapshots.restore` | `stacks.snapshots.wait_for_restore(accepted)` | `stacks.snapshots.restore_and_wait` |
|
|
418
|
+
|
|
419
|
+
`apps.wait_until_ready(app_id, timeout=...)` keeps its `timeout` as the total wait for the build.
|
|
420
|
+
|
|
342
421
|
### Errors
|
|
343
422
|
|
|
344
423
|
Every non-2xx response raises a `VertraAPIError` carrying the HTTP `status`, the API `code` (e.g. `APP_NOT_FOUND`) and `details`; `str(err)` includes the message. The API key is never part of the error, even when it is printed:
|
|
@@ -358,6 +437,8 @@ except VertraAPIError as err:
|
|
|
358
437
|
|
|
359
438
|
A few errors carry a structured `details`; tolerant properties read them and give an empty value for any other error: `err.issues` (`DOCKERFILE_REFUSED`), `err.manifest_issues` (`TEMPLATE_MANIFEST_INVALID`), `err.secret_files` (`TEMPLATE_SECRET_IN_SOURCE`), `err.update_blocked` (`TEMPLATE_UPDATE_BLOCKED`) and `err.detach_action` (`TEMPLATE_DETACH_CONFIRMATION_REQUIRED`).
|
|
360
439
|
|
|
440
|
+
`apps.files.read` and `apps.files.write` handle files up to 10 MiB; above it the call fails with `FILE_TOO_LARGE` (413) and `details.max_bytes` is the limit.
|
|
441
|
+
|
|
361
442
|
Other subclasses: `AuthenticationError` (401), `ScopeDeniedError` (403 — including a missing key scope) and `ValidationError` (400/422). The error codes are listed in the [API reference](https://docs.vertracloud.app/api-reference/introduction).
|
|
362
443
|
|
|
363
444
|
### Testing your code
|
|
@@ -377,7 +458,7 @@ client = VertraClient(api_key="test", transport=my_fake_transport)
|
|
|
377
458
|
| Custom images | `client.images` | 13 |
|
|
378
459
|
| Templates | `client.templates` (+ `.deploys`, `.share_links`, `.share`, `.updates`) | 32 |
|
|
379
460
|
| Stacks | `client.stacks` (+ `.members`, `.bindings`, `.snapshots`) | 20 |
|
|
380
|
-
| Snapshots | `client.snapshots` | 5 |
|
|
461
|
+
| Snapshots | `client.snapshots`, `client.snapshot_jobs` | 5 + 2 |
|
|
381
462
|
| Account | `client.account` (+ `.sessions`, `.folders`, `.favorites`) | 10 |
|
|
382
463
|
| Workspaces | `client.workspaces` (+ `.members`, `.roles`, `.invites`, `.action_requests`, `.apps`, `.databases`, `.templates`, `.stacks`, `.folders`, `.favorites`) | 34 |
|
|
383
464
|
| Billing | `client.billing` (+ `.orders`, with `orders.quote`) | 8 |
|
|
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "vertracloud-api"
|
|
7
|
-
version = "0.4.
|
|
7
|
+
version = "0.4.3"
|
|
8
8
|
description = "Official Python SDK for the public API of Vertra Cloud, a Brazilian application hosting platform (PaaS)."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = "MIT"
|
|
@@ -6,9 +6,14 @@ from .errors import (
|
|
|
6
6
|
AppBuildFailedError,
|
|
7
7
|
AppNotReadyError,
|
|
8
8
|
AuthenticationError,
|
|
9
|
+
DownloadIncompleteError,
|
|
9
10
|
NotFoundError,
|
|
10
11
|
RateLimitError,
|
|
11
12
|
ScopeDeniedError,
|
|
13
|
+
SnapshotJobError,
|
|
14
|
+
SnapshotJobFailedError,
|
|
15
|
+
SnapshotJobSkippedError,
|
|
16
|
+
SnapshotJobTimeoutError,
|
|
12
17
|
ValidationError,
|
|
13
18
|
VertraAPIError,
|
|
14
19
|
WaitTimeoutError,
|
|
@@ -24,7 +29,12 @@ __all__ = [
|
|
|
24
29
|
"AppNotReadyError",
|
|
25
30
|
"AppBuildFailedError",
|
|
26
31
|
"WaitTimeoutError",
|
|
32
|
+
"SnapshotJobError",
|
|
33
|
+
"SnapshotJobFailedError",
|
|
34
|
+
"SnapshotJobSkippedError",
|
|
35
|
+
"SnapshotJobTimeoutError",
|
|
27
36
|
"AuthenticationError",
|
|
37
|
+
"DownloadIncompleteError",
|
|
28
38
|
"ScopeDeniedError",
|
|
29
39
|
"NotFoundError",
|
|
30
40
|
"ValidationError",
|
|
@@ -6,23 +6,101 @@ from __future__ import annotations
|
|
|
6
6
|
|
|
7
7
|
import json
|
|
8
8
|
import mimetypes
|
|
9
|
+
import os
|
|
10
|
+
import secrets
|
|
9
11
|
import uuid
|
|
10
|
-
from collections.abc import Iterable
|
|
12
|
+
from collections.abc import Iterable, Iterator
|
|
11
13
|
from typing import Any
|
|
12
14
|
from urllib.parse import quote, unquote, urlencode
|
|
13
15
|
|
|
14
|
-
from .errors import error_from_response
|
|
16
|
+
from .errors import DownloadIncompleteError, error_from_response
|
|
15
17
|
from .sse import SseStream
|
|
16
18
|
from .transport import Response, Transport, UrllibTransport
|
|
17
19
|
from .types import JsonValue
|
|
18
20
|
|
|
19
|
-
__version__ = "0.4.
|
|
21
|
+
__version__ = "0.4.3" # x-release-please-version
|
|
20
22
|
|
|
21
23
|
DEFAULT_BASE_URL = "https://api.vertracloud.app"
|
|
22
24
|
DEFAULT_BLOB_BASE_URL = "https://blob.vertracloud.app/v1"
|
|
23
25
|
DEFAULT_TIMEOUT = 30.0
|
|
24
|
-
#
|
|
26
|
+
# Upload/create for an app may involve a large zip file, so the timeout is longer.
|
|
25
27
|
LARGE_PAYLOAD_TIMEOUT = 120.0
|
|
28
|
+
# Creating a snapshot can run for about 90 s.
|
|
29
|
+
SNAPSHOT_ACTION_TIMEOUT = 120.0
|
|
30
|
+
# Default `wait_timeout` of the helpers that follow a job (30 minutes): the total wait, not a per-request limit;
|
|
31
|
+
# the work keeps going on the server after it.
|
|
32
|
+
DEFAULT_WAIT_TIMEOUT = 1800.0
|
|
33
|
+
# Bytes read at a time by the streaming downloads.
|
|
34
|
+
DOWNLOAD_CHUNK_SIZE = 1024 * 1024
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _close(stream: Iterator[bytes]) -> None:
|
|
38
|
+
close = getattr(stream, "close", None)
|
|
39
|
+
if callable(close):
|
|
40
|
+
close()
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class _LengthCheckedStream:
|
|
44
|
+
"""Passes the chunks of `stream` through and, at the end, raises `DownloadIncompleteError` when the bytes received
|
|
45
|
+
are fewer than `expected` (the `Content-Length`): the HTTP layer reports a body cut by the connection as a plain
|
|
46
|
+
end of data."""
|
|
47
|
+
|
|
48
|
+
def __init__(self, stream: Iterator[bytes], expected: int) -> None:
|
|
49
|
+
self._stream = stream
|
|
50
|
+
self._expected = expected
|
|
51
|
+
self._received = 0
|
|
52
|
+
|
|
53
|
+
def __iter__(self) -> _LengthCheckedStream:
|
|
54
|
+
return self
|
|
55
|
+
|
|
56
|
+
def __next__(self) -> bytes:
|
|
57
|
+
try:
|
|
58
|
+
chunk = next(self._stream)
|
|
59
|
+
except StopIteration:
|
|
60
|
+
if self._received != self._expected:
|
|
61
|
+
_close(self._stream)
|
|
62
|
+
raise DownloadIncompleteError(expected=self._expected, received=self._received) from None
|
|
63
|
+
raise
|
|
64
|
+
self._received += len(chunk)
|
|
65
|
+
return chunk
|
|
66
|
+
|
|
67
|
+
def close(self) -> None:
|
|
68
|
+
_close(self._stream)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _content_length(headers: dict[str, str]) -> int | None:
|
|
72
|
+
raw = headers.get("content-length")
|
|
73
|
+
if raw is None or not raw.strip().isdigit():
|
|
74
|
+
return None
|
|
75
|
+
return int(raw)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def write_stream_to_file(chunks: Iterator[bytes], destination: str | os.PathLike[str]) -> int:
|
|
79
|
+
"""Writes `chunks` to `destination` through a temporary file next to it (`<name>.<random>.part`, mode 0600: a
|
|
80
|
+
snapshot can hold secrets), renamed to `destination` only when the iterator ends. A failure removes the temporary
|
|
81
|
+
file and closes `chunks`."""
|
|
82
|
+
final = os.fspath(destination)
|
|
83
|
+
temp = f"{final}.{secrets.token_hex(6)}.part"
|
|
84
|
+
written = 0
|
|
85
|
+
try:
|
|
86
|
+
fd = os.open(temp, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
|
|
87
|
+
except BaseException:
|
|
88
|
+
_close(chunks)
|
|
89
|
+
raise
|
|
90
|
+
try:
|
|
91
|
+
with os.fdopen(fd, "wb") as out:
|
|
92
|
+
for chunk in chunks:
|
|
93
|
+
out.write(chunk)
|
|
94
|
+
written += len(chunk)
|
|
95
|
+
os.replace(temp, final)
|
|
96
|
+
except BaseException:
|
|
97
|
+
_close(chunks)
|
|
98
|
+
try:
|
|
99
|
+
os.unlink(temp)
|
|
100
|
+
except FileNotFoundError:
|
|
101
|
+
pass
|
|
102
|
+
raise
|
|
103
|
+
return written
|
|
26
104
|
|
|
27
105
|
|
|
28
106
|
def _is_dot_segment(value: str) -> bool:
|
|
@@ -138,6 +216,7 @@ class VertraClient:
|
|
|
138
216
|
from .resources.databases import DatabasesResource
|
|
139
217
|
from .resources.images import ImagesResource
|
|
140
218
|
from .resources.plans import PlansResource
|
|
219
|
+
from .resources.snapshot_jobs import SnapshotJobsResource
|
|
141
220
|
from .resources.snapshots import SnapshotsResource
|
|
142
221
|
from .resources.stacks import StacksResource
|
|
143
222
|
from .resources.templates import TemplatesResource
|
|
@@ -149,6 +228,7 @@ class VertraClient:
|
|
|
149
228
|
self.templates = TemplatesResource(self)
|
|
150
229
|
self.stacks = StacksResource(self)
|
|
151
230
|
self.snapshots = SnapshotsResource(self)
|
|
231
|
+
self.snapshot_jobs = SnapshotJobsResource(self)
|
|
152
232
|
self.account = AccountResource(self)
|
|
153
233
|
self.workspaces = WorkspacesResource(self)
|
|
154
234
|
self.billing = BillingResource(self)
|
|
@@ -194,13 +274,41 @@ class VertraClient:
|
|
|
194
274
|
auth: bool = True,
|
|
195
275
|
) -> JsonValue:
|
|
196
276
|
"""Standard JSON call: sends `json_body` (if given), unwraps `{response}`. `auth=False` for public routes."""
|
|
277
|
+
return self._unwrap(
|
|
278
|
+
self._send_json(
|
|
279
|
+
method,
|
|
280
|
+
path,
|
|
281
|
+
query=query,
|
|
282
|
+
json_body=json_body,
|
|
283
|
+
timeout=timeout,
|
|
284
|
+
base_url=base_url,
|
|
285
|
+
extra_headers=extra_headers,
|
|
286
|
+
auth=auth,
|
|
287
|
+
)
|
|
288
|
+
)
|
|
289
|
+
|
|
290
|
+
def snapshot_action_timeout(self, timeout: float | None) -> float:
|
|
291
|
+
"""Timeout of a call that takes a snapshot: the caller's, else the larger of the client's and 120 s."""
|
|
292
|
+
return timeout if timeout is not None else max(self.timeout, SNAPSHOT_ACTION_TIMEOUT)
|
|
293
|
+
|
|
294
|
+
def _send_json(
|
|
295
|
+
self,
|
|
296
|
+
method: str,
|
|
297
|
+
path: str,
|
|
298
|
+
*,
|
|
299
|
+
query: dict[str, Any] | None = None,
|
|
300
|
+
json_body: Any = None,
|
|
301
|
+
timeout: float | None = None,
|
|
302
|
+
base_url: str | None = None,
|
|
303
|
+
extra_headers: dict[str, str] | None = None,
|
|
304
|
+
auth: bool = True,
|
|
305
|
+
) -> Response:
|
|
197
306
|
extra = dict(extra_headers or {})
|
|
198
307
|
if json_body is not None:
|
|
199
308
|
extra["Content-Type"] = "application/json"
|
|
200
309
|
headers = self._headers(extra, auth=auth)
|
|
201
310
|
body = json.dumps(json_body).encode("utf-8") if json_body is not None else None
|
|
202
|
-
|
|
203
|
-
return self._unwrap(resp)
|
|
311
|
+
return self.transport.request(method, self._url(path, query, base_url), headers, body, timeout or self.timeout)
|
|
204
312
|
|
|
205
313
|
def request_multipart(
|
|
206
314
|
self,
|
|
@@ -234,13 +342,60 @@ class VertraClient:
|
|
|
234
342
|
query: dict[str, Any] | None = None,
|
|
235
343
|
timeout: float | None = None,
|
|
236
344
|
) -> bytes:
|
|
237
|
-
"""Binary downloads (
|
|
345
|
+
"""Binary downloads (snapshot zip), the whole body in memory. No JSON envelope. `timeout` is the wait of each
|
|
346
|
+
socket operation (connect, each read), not a total: a file that keeps arriving completes however long it takes.
|
|
347
|
+
For a file that can be large use `request_binary_stream` or `download_to_file`."""
|
|
238
348
|
headers = self._headers({"Accept": "*/*"})
|
|
239
349
|
resp = self.transport.request(method, self._url(path, query), headers, None, timeout or self.timeout)
|
|
240
350
|
if resp.status >= 400:
|
|
241
351
|
self._raise_for_error(resp)
|
|
242
352
|
return resp.body
|
|
243
353
|
|
|
354
|
+
def request_binary_stream(
|
|
355
|
+
self,
|
|
356
|
+
method: str,
|
|
357
|
+
path: str,
|
|
358
|
+
*,
|
|
359
|
+
query: dict[str, Any] | None = None,
|
|
360
|
+
timeout: float | None = None,
|
|
361
|
+
chunk_size: int = DOWNLOAD_CHUNK_SIZE,
|
|
362
|
+
) -> Iterator[bytes]:
|
|
363
|
+
"""Same request as `request_binary`, but returns the body as an iterator of byte chunks read as you ask for
|
|
364
|
+
them: memory does not grow with the file. The request is made (and an API error raised) before this returns.
|
|
365
|
+
`timeout` is the wait of each read, so a body that stops sending raises (`TimeoutError`/`OSError`). Call
|
|
366
|
+
`close()` on the iterator, or iterate to the end, to release the connection."""
|
|
367
|
+
headers = self._headers({"Accept": "*/*"})
|
|
368
|
+
resp = self.transport.request(
|
|
369
|
+
method, self._url(path, query), headers, None, timeout or self.timeout, stream=True, chunk_size=chunk_size
|
|
370
|
+
)
|
|
371
|
+
if resp.status >= 400:
|
|
372
|
+
if resp.stream is None:
|
|
373
|
+
self._raise_for_error(resp)
|
|
374
|
+
else:
|
|
375
|
+
try:
|
|
376
|
+
self._raise_for_error(Response(resp.status, resp.headers, b"".join(resp.stream)))
|
|
377
|
+
finally:
|
|
378
|
+
_close(resp.stream)
|
|
379
|
+
if resp.stream is None:
|
|
380
|
+
return iter([resp.body] if resp.body else [])
|
|
381
|
+
expected = _content_length({k.lower(): v for k, v in resp.headers.items()})
|
|
382
|
+
return resp.stream if expected is None else _LengthCheckedStream(resp.stream, expected)
|
|
383
|
+
|
|
384
|
+
def download_to_file(
|
|
385
|
+
self,
|
|
386
|
+
method: str,
|
|
387
|
+
path: str,
|
|
388
|
+
destination: str | os.PathLike[str],
|
|
389
|
+
*,
|
|
390
|
+
query: dict[str, Any] | None = None,
|
|
391
|
+
timeout: float | None = None,
|
|
392
|
+
) -> int:
|
|
393
|
+
"""`request_binary_stream` written to `destination` as the bytes arrive; returns the bytes written. The bytes go
|
|
394
|
+
to a temporary file next to `destination`, renamed to it only when the body ends; on any failure (including
|
|
395
|
+
`KeyboardInterrupt`) the temporary file is removed and `destination` never holds a partial file."""
|
|
396
|
+
stream = self.request_binary_stream(method, path, query=query, timeout=timeout)
|
|
397
|
+
return write_stream_to_file(stream, destination)
|
|
398
|
+
|
|
244
399
|
def request_sse(
|
|
245
400
|
self,
|
|
246
401
|
path: str,
|