perception-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.
- perception_sdk-0.1.0/.gitignore +5 -0
- perception_sdk-0.1.0/LICENSE +21 -0
- perception_sdk-0.1.0/PKG-INFO +248 -0
- perception_sdk-0.1.0/README.md +229 -0
- perception_sdk-0.1.0/pyproject.toml +43 -0
- perception_sdk-0.1.0/src/perception_sdk/__init__.py +42 -0
- perception_sdk-0.1.0/src/perception_sdk/_http.py +262 -0
- perception_sdk-0.1.0/src/perception_sdk/admin.py +193 -0
- perception_sdk-0.1.0/src/perception_sdk/cli.py +358 -0
- perception_sdk-0.1.0/src/perception_sdk/client.py +816 -0
- perception_sdk-0.1.0/src/perception_sdk/errors.py +92 -0
- perception_sdk-0.1.0/src/perception_sdk/models.py +135 -0
- perception_sdk-0.1.0/src/perception_sdk/py.typed +0 -0
- perception_sdk-0.1.0/tests/test_cli.py +343 -0
- perception_sdk-0.1.0/tests/test_client.py +520 -0
- perception_sdk-0.1.0/tests/test_console.py +215 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Metacog 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,248 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: perception-sdk
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A Python client and command line for the Perception console API
|
|
5
|
+
Author: Metacog Labs
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Classifier: Operating System :: OS Independent
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
15
|
+
Classifier: Typing :: Typed
|
|
16
|
+
Requires-Python: >=3.10
|
|
17
|
+
Requires-Dist: httpx<1,>=0.27
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
|
|
20
|
+
# perception-sdk
|
|
21
|
+
|
|
22
|
+
A Python client and command line for the Perception console API. The engine takes video, audio,
|
|
23
|
+
images and documents, works out what happened in them, and answers questions about it. The
|
|
24
|
+
console organises that work into projects, threads, workflows and jobs.
|
|
25
|
+
|
|
26
|
+
Python 3.10 or newer. The only dependency is `httpx`.
|
|
27
|
+
|
|
28
|
+
## Install
|
|
29
|
+
|
|
30
|
+
```sh
|
|
31
|
+
pip install perception-sdk
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Configure
|
|
35
|
+
|
|
36
|
+
The client needs the API's URL and an API key. Pass them in, or set them in the environment.
|
|
37
|
+
There is no default URL:
|
|
38
|
+
|
|
39
|
+
```sh
|
|
40
|
+
export PERCEPTION_API_URL=https://perception-console.eastus.cloudapp.azure.com
|
|
41
|
+
export PERCEPTION_API_KEY=pk_...
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
| Variable | Used by | What it is |
|
|
45
|
+
|---|---|---|
|
|
46
|
+
| `PERCEPTION_API_URL` | `Client`, `AdminClient` | The API's base URL |
|
|
47
|
+
| `PERCEPTION_API_KEY` | `Client` | One of your workspace's API keys (`pk_...`) |
|
|
48
|
+
| `PERCEPTION_ADMIN_TOKEN` | `AdminClient` | The operator's admin token. Only the engine's operator has one |
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
from perception_sdk import Client
|
|
52
|
+
|
|
53
|
+
with Client() as client: # reads the environment
|
|
54
|
+
print(client.whoami()) # WhoAmI(tenant_id='Acme', key_id='4f1c2a9be07d3e15')
|
|
55
|
+
|
|
56
|
+
client = Client("https://perception-console.eastus.cloudapp.azure.com", "pk_...")
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
The key is sent only to the API. It never appears in `repr()`, in log lines or in exception
|
|
60
|
+
messages, and it is never sent to the storage that uploads go to.
|
|
61
|
+
|
|
62
|
+
## A workspace's first key
|
|
63
|
+
|
|
64
|
+
Your workspace's first key is issued by the operator. From then on the workspace issues and
|
|
65
|
+
revokes its own keys, from the console (Settings, API keys) or with this package.
|
|
66
|
+
|
|
67
|
+
## Manage API keys
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
with Client() as client:
|
|
71
|
+
new = client.keys.create("ci", expires_at="2027-06-30T00:00:00+00:00")
|
|
72
|
+
print(new.api_key) # the secret: shown this once, store it now
|
|
73
|
+
|
|
74
|
+
keys = client.keys.list()
|
|
75
|
+
for key in keys:
|
|
76
|
+
print(key.key_id, key.name, key.expires_at, key.last_used_at, key.revoked_at)
|
|
77
|
+
if keys.truncated:
|
|
78
|
+
print("only the newest 200 keys are listed")
|
|
79
|
+
|
|
80
|
+
replacement = client.keys.rotate(new.key_id) # revokes the old key, same name
|
|
81
|
+
client.keys.revoke(replacement.key_id) # answers the revoked key
|
|
82
|
+
client.keys.delete(new.key_id) # the same, answering nothing
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
`keys.list()` returns a `KeyList`: its keys, newest first, revoked and expired ones included, and
|
|
86
|
+
`truncated`, which is True when the API stopped at its limit of 200. A key's `name` is a string,
|
|
87
|
+
empty when it has none. `expires_at` must be in the future and carry a time zone.
|
|
88
|
+
|
|
89
|
+
You cannot revoke the key you are calling with (`ConflictError`); revoke it with another key. You
|
|
90
|
+
can rotate it: the answer carries its successor, and the old key stops working at once.
|
|
91
|
+
|
|
92
|
+
Rotating without `expires_at` gives the new key no expiry, even if the old key had one. Pass
|
|
93
|
+
`expires_at=` to `keys.rotate()` to keep one.
|
|
94
|
+
|
|
95
|
+
## Projects, threads, workflows and jobs
|
|
96
|
+
|
|
97
|
+
These methods return the API's JSON answer as a dict. A list answers with the list under its name
|
|
98
|
+
and, where the API caps it, `truncated`.
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
with Client() as client:
|
|
102
|
+
project = client.projects.create("Docks") # comes with a workflow named "Default"
|
|
103
|
+
p = project["project_id"]
|
|
104
|
+
[workflow] = client.workflows.list(p)["workflows"]
|
|
105
|
+
|
|
106
|
+
thread = client.threads.create(p, "Monday")
|
|
107
|
+
source = client.sources.upload(thread["subject_id"], "dock-camera.mp4")
|
|
108
|
+
|
|
109
|
+
job = client.jobs.create(
|
|
110
|
+
p,
|
|
111
|
+
workflow["workflow_id"],
|
|
112
|
+
workflow["latest_version"],
|
|
113
|
+
[source.source_id],
|
|
114
|
+
thread_id=thread["thread_id"],
|
|
115
|
+
)
|
|
116
|
+
print(client.jobs.status(p, job["job_id"]))
|
|
117
|
+
|
|
118
|
+
# once it has finished
|
|
119
|
+
print(client.jobs.output(p, job["job_id"])) # a summary and the first elements
|
|
120
|
+
print(client.jobs.download_url(p, job["job_id"])) # a short-lived link to the whole file
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
| Namespace | Methods |
|
|
124
|
+
|---|---|
|
|
125
|
+
| `client.projects` | `create`, `list`, `get`, `update` |
|
|
126
|
+
| `client.threads` | `create`, `list`, `get`, `rename`, `delete`, `reassemble`, `cancel_rebuild`, `take_file` |
|
|
127
|
+
| `client.workflows` | `create`, `list`, `get`, `rename`, `save_version`, `list_versions`, `get_version`, `restore_version`, `run` |
|
|
128
|
+
| `client.jobs` | `create`, `list`, `get`, `status`, `retry_failure`, `output`, `download_url` |
|
|
129
|
+
| `client.connectors` | `create`, `list`, `get`, `update`, `delete`, `check`, `objects` |
|
|
130
|
+
| `client` | `catalogue`, `whoami`, `query`, `perceive_sync` |
|
|
131
|
+
|
|
132
|
+
`jobs.list()` pages newest first: pass its `next_after` back as `after=` for the next page. Pass
|
|
133
|
+
`request_id=` (a UUID) to `jobs.create()` or `workflows.run()` so that sending the same request
|
|
134
|
+
again returns the same job instead of starting another.
|
|
135
|
+
|
|
136
|
+
## Upload media
|
|
137
|
+
|
|
138
|
+
```python
|
|
139
|
+
with Client() as client:
|
|
140
|
+
source = client.sources.upload(
|
|
141
|
+
thread["subject_id"],
|
|
142
|
+
"dock-camera.mp4",
|
|
143
|
+
on_progress=lambda sent, total: print(f"{sent}/{total} bytes"),
|
|
144
|
+
)
|
|
145
|
+
print(source.source_id, source.size_bytes)
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
`upload()` registers the file, sends it straight to the engine's storage in 5 MiB parts (reading
|
|
149
|
+
one part at a time, never the whole file), and completes it. A part that fails for a moment is
|
|
150
|
+
retried three times before `UploadError` is raised. The content type comes from the file name and
|
|
151
|
+
the media type from that; pass `content_type=` or `media_type=` when the guess is wrong.
|
|
152
|
+
|
|
153
|
+
Uploading is idempotent: the same file on the same stream returns the source already there. By
|
|
154
|
+
default the stream is named after the file and a digest of its content. Pass `stream_id=` to put
|
|
155
|
+
several files on one stream, with `stream_offset_us=` saying where each one starts.
|
|
156
|
+
|
|
157
|
+
In the console a thread's files are processed by a job. `sources.process()` starts processing one
|
|
158
|
+
source on its own, outside any job.
|
|
159
|
+
|
|
160
|
+
## Errors
|
|
161
|
+
|
|
162
|
+
Every refusal from the API raises a `PerceptionError` with `status` (the HTTP status), `error` (the
|
|
163
|
+
API's error code) and `detail` (its explanation). Catch the subclass you act on:
|
|
164
|
+
|
|
165
|
+
| Exception | When |
|
|
166
|
+
|---|---|
|
|
167
|
+
| `AuthenticationError` | 401: the key is missing, wrong, expired or revoked |
|
|
168
|
+
| `PermissionDenied` | 403: the key may not touch what was named |
|
|
169
|
+
| `NotFoundError` | 404: no such project, thread, job, key or record |
|
|
170
|
+
| `ConflictError` | 409: the request conflicts with the current state |
|
|
171
|
+
| `PayloadTooLarge` | 413: the media is over a limit |
|
|
172
|
+
| `ValidationError` | 422: a field was wrong; `detail` says which |
|
|
173
|
+
| `ServerError` | 5xx |
|
|
174
|
+
| `APIConnectionError` | No answer: the connection failed or timed out |
|
|
175
|
+
| `UploadError` | A part of an upload could not be stored, or the API's upload answer was malformed |
|
|
176
|
+
| `PerceptionError` | Anything else, such as a success answer that is not JSON |
|
|
177
|
+
| `ConfigurationError` | No URL or credential was given (not a `PerceptionError`) |
|
|
178
|
+
|
|
179
|
+
```python
|
|
180
|
+
from perception_sdk import Client, ConflictError, NotFoundError, PerceptionError
|
|
181
|
+
|
|
182
|
+
with Client() as client:
|
|
183
|
+
try:
|
|
184
|
+
client.keys.revoke("4f1c2a9be07d3e15")
|
|
185
|
+
except ConflictError:
|
|
186
|
+
print("that is the key you are calling with; revoke it with another")
|
|
187
|
+
except NotFoundError:
|
|
188
|
+
print("no such key")
|
|
189
|
+
except PerceptionError as e:
|
|
190
|
+
print(e.status, e.error, e.detail)
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
Reads (`GET`) are tried up to three times (the first attempt and two retries) on a failed
|
|
194
|
+
connection or a 502, 503 or 504. Writes are never retried, because a write that timed out may
|
|
195
|
+
already have happened.
|
|
196
|
+
|
|
197
|
+
## The operator's client
|
|
198
|
+
|
|
199
|
+
`AdminClient` uses the admin token. The admin API is not public; reach it through an SSH tunnel to
|
|
200
|
+
the server and pass `--url http://127.0.0.1:<port>` (or the same URL to `AdminClient`), where
|
|
201
|
+
`<port>` is the local end of the tunnel.
|
|
202
|
+
|
|
203
|
+
```python
|
|
204
|
+
from perception_sdk import AdminClient
|
|
205
|
+
|
|
206
|
+
with AdminClient("http://127.0.0.1:<port>") as admin: # PERCEPTION_ADMIN_TOKEN
|
|
207
|
+
admin.tenants.create("Acme")
|
|
208
|
+
first = admin.keys.create("Acme", name="bootstrap")
|
|
209
|
+
print(first.api_key)
|
|
210
|
+
print(admin.keys.list("Acme").truncated)
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
`admin.tenants` (`create`, `list`), `admin.subjects` (`create`), `admin.keys` (`list`, `create`,
|
|
214
|
+
`revoke`, `rotate`), `admin.dead_letters`, `admin.quarantine` and `admin.subscriptions` cover the
|
|
215
|
+
admin routes.
|
|
216
|
+
|
|
217
|
+
## Command line
|
|
218
|
+
|
|
219
|
+
Installing the package also installs `perception`. It reads the same environment variables:
|
|
220
|
+
`PERCEPTION_API_URL` (or `--url`), `PERCEPTION_API_KEY` for a workspace's commands and
|
|
221
|
+
`PERCEPTION_ADMIN_TOKEN` for `admin`. Credentials are taken from the environment only, never from
|
|
222
|
+
a flag, so they stay out of your shell history.
|
|
223
|
+
|
|
224
|
+
```sh
|
|
225
|
+
perception whoami
|
|
226
|
+
perception keys create --name ci --expires-at 2027-06-30T00:00:00+00:00
|
|
227
|
+
perception keys list --table
|
|
228
|
+
perception keys rotate 4f1c2a9be07d3e15
|
|
229
|
+
perception keys delete 4f1c2a9be07d3e15
|
|
230
|
+
perception jobs list pr_123 --table
|
|
231
|
+
perception upload th_123 dock-camera.mp4 --no-process
|
|
232
|
+
|
|
233
|
+
# with an SSH tunnel to the admin API open on <port>
|
|
234
|
+
perception --url http://127.0.0.1:<port> admin tenants list --table
|
|
235
|
+
perception --url http://127.0.0.1:<port> admin keys issue Acme --name bootstrap
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
Answers are printed to stdout as JSON; `--table` prints a list command's answer as columns, and
|
|
239
|
+
says on stderr when the list was truncated or, for `jobs list`, how to ask for the next page
|
|
240
|
+
(`more results: pass --after <job_id>`). An issued or rotated key's secret is in that JSON,
|
|
241
|
+
printed this once. Errors go to stderr on one line. The exit status is 0 on success, 1 when the
|
|
242
|
+
API refused or could not be reached (or anything unexpected went wrong), and 2 for a usage or
|
|
243
|
+
configuration error such as a missing URL or key. `perception --help` and each command's
|
|
244
|
+
`--help` list the rest.
|
|
245
|
+
|
|
246
|
+
## License
|
|
247
|
+
|
|
248
|
+
MIT
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
# perception-sdk
|
|
2
|
+
|
|
3
|
+
A Python client and command line for the Perception console API. The engine takes video, audio,
|
|
4
|
+
images and documents, works out what happened in them, and answers questions about it. The
|
|
5
|
+
console organises that work into projects, threads, workflows and jobs.
|
|
6
|
+
|
|
7
|
+
Python 3.10 or newer. The only dependency is `httpx`.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
pip install perception-sdk
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Configure
|
|
16
|
+
|
|
17
|
+
The client needs the API's URL and an API key. Pass them in, or set them in the environment.
|
|
18
|
+
There is no default URL:
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
export PERCEPTION_API_URL=https://perception-console.eastus.cloudapp.azure.com
|
|
22
|
+
export PERCEPTION_API_KEY=pk_...
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
| Variable | Used by | What it is |
|
|
26
|
+
|---|---|---|
|
|
27
|
+
| `PERCEPTION_API_URL` | `Client`, `AdminClient` | The API's base URL |
|
|
28
|
+
| `PERCEPTION_API_KEY` | `Client` | One of your workspace's API keys (`pk_...`) |
|
|
29
|
+
| `PERCEPTION_ADMIN_TOKEN` | `AdminClient` | The operator's admin token. Only the engine's operator has one |
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
from perception_sdk import Client
|
|
33
|
+
|
|
34
|
+
with Client() as client: # reads the environment
|
|
35
|
+
print(client.whoami()) # WhoAmI(tenant_id='Acme', key_id='4f1c2a9be07d3e15')
|
|
36
|
+
|
|
37
|
+
client = Client("https://perception-console.eastus.cloudapp.azure.com", "pk_...")
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
The key is sent only to the API. It never appears in `repr()`, in log lines or in exception
|
|
41
|
+
messages, and it is never sent to the storage that uploads go to.
|
|
42
|
+
|
|
43
|
+
## A workspace's first key
|
|
44
|
+
|
|
45
|
+
Your workspace's first key is issued by the operator. From then on the workspace issues and
|
|
46
|
+
revokes its own keys, from the console (Settings, API keys) or with this package.
|
|
47
|
+
|
|
48
|
+
## Manage API keys
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
with Client() as client:
|
|
52
|
+
new = client.keys.create("ci", expires_at="2027-06-30T00:00:00+00:00")
|
|
53
|
+
print(new.api_key) # the secret: shown this once, store it now
|
|
54
|
+
|
|
55
|
+
keys = client.keys.list()
|
|
56
|
+
for key in keys:
|
|
57
|
+
print(key.key_id, key.name, key.expires_at, key.last_used_at, key.revoked_at)
|
|
58
|
+
if keys.truncated:
|
|
59
|
+
print("only the newest 200 keys are listed")
|
|
60
|
+
|
|
61
|
+
replacement = client.keys.rotate(new.key_id) # revokes the old key, same name
|
|
62
|
+
client.keys.revoke(replacement.key_id) # answers the revoked key
|
|
63
|
+
client.keys.delete(new.key_id) # the same, answering nothing
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
`keys.list()` returns a `KeyList`: its keys, newest first, revoked and expired ones included, and
|
|
67
|
+
`truncated`, which is True when the API stopped at its limit of 200. A key's `name` is a string,
|
|
68
|
+
empty when it has none. `expires_at` must be in the future and carry a time zone.
|
|
69
|
+
|
|
70
|
+
You cannot revoke the key you are calling with (`ConflictError`); revoke it with another key. You
|
|
71
|
+
can rotate it: the answer carries its successor, and the old key stops working at once.
|
|
72
|
+
|
|
73
|
+
Rotating without `expires_at` gives the new key no expiry, even if the old key had one. Pass
|
|
74
|
+
`expires_at=` to `keys.rotate()` to keep one.
|
|
75
|
+
|
|
76
|
+
## Projects, threads, workflows and jobs
|
|
77
|
+
|
|
78
|
+
These methods return the API's JSON answer as a dict. A list answers with the list under its name
|
|
79
|
+
and, where the API caps it, `truncated`.
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
with Client() as client:
|
|
83
|
+
project = client.projects.create("Docks") # comes with a workflow named "Default"
|
|
84
|
+
p = project["project_id"]
|
|
85
|
+
[workflow] = client.workflows.list(p)["workflows"]
|
|
86
|
+
|
|
87
|
+
thread = client.threads.create(p, "Monday")
|
|
88
|
+
source = client.sources.upload(thread["subject_id"], "dock-camera.mp4")
|
|
89
|
+
|
|
90
|
+
job = client.jobs.create(
|
|
91
|
+
p,
|
|
92
|
+
workflow["workflow_id"],
|
|
93
|
+
workflow["latest_version"],
|
|
94
|
+
[source.source_id],
|
|
95
|
+
thread_id=thread["thread_id"],
|
|
96
|
+
)
|
|
97
|
+
print(client.jobs.status(p, job["job_id"]))
|
|
98
|
+
|
|
99
|
+
# once it has finished
|
|
100
|
+
print(client.jobs.output(p, job["job_id"])) # a summary and the first elements
|
|
101
|
+
print(client.jobs.download_url(p, job["job_id"])) # a short-lived link to the whole file
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
| Namespace | Methods |
|
|
105
|
+
|---|---|
|
|
106
|
+
| `client.projects` | `create`, `list`, `get`, `update` |
|
|
107
|
+
| `client.threads` | `create`, `list`, `get`, `rename`, `delete`, `reassemble`, `cancel_rebuild`, `take_file` |
|
|
108
|
+
| `client.workflows` | `create`, `list`, `get`, `rename`, `save_version`, `list_versions`, `get_version`, `restore_version`, `run` |
|
|
109
|
+
| `client.jobs` | `create`, `list`, `get`, `status`, `retry_failure`, `output`, `download_url` |
|
|
110
|
+
| `client.connectors` | `create`, `list`, `get`, `update`, `delete`, `check`, `objects` |
|
|
111
|
+
| `client` | `catalogue`, `whoami`, `query`, `perceive_sync` |
|
|
112
|
+
|
|
113
|
+
`jobs.list()` pages newest first: pass its `next_after` back as `after=` for the next page. Pass
|
|
114
|
+
`request_id=` (a UUID) to `jobs.create()` or `workflows.run()` so that sending the same request
|
|
115
|
+
again returns the same job instead of starting another.
|
|
116
|
+
|
|
117
|
+
## Upload media
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
with Client() as client:
|
|
121
|
+
source = client.sources.upload(
|
|
122
|
+
thread["subject_id"],
|
|
123
|
+
"dock-camera.mp4",
|
|
124
|
+
on_progress=lambda sent, total: print(f"{sent}/{total} bytes"),
|
|
125
|
+
)
|
|
126
|
+
print(source.source_id, source.size_bytes)
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
`upload()` registers the file, sends it straight to the engine's storage in 5 MiB parts (reading
|
|
130
|
+
one part at a time, never the whole file), and completes it. A part that fails for a moment is
|
|
131
|
+
retried three times before `UploadError` is raised. The content type comes from the file name and
|
|
132
|
+
the media type from that; pass `content_type=` or `media_type=` when the guess is wrong.
|
|
133
|
+
|
|
134
|
+
Uploading is idempotent: the same file on the same stream returns the source already there. By
|
|
135
|
+
default the stream is named after the file and a digest of its content. Pass `stream_id=` to put
|
|
136
|
+
several files on one stream, with `stream_offset_us=` saying where each one starts.
|
|
137
|
+
|
|
138
|
+
In the console a thread's files are processed by a job. `sources.process()` starts processing one
|
|
139
|
+
source on its own, outside any job.
|
|
140
|
+
|
|
141
|
+
## Errors
|
|
142
|
+
|
|
143
|
+
Every refusal from the API raises a `PerceptionError` with `status` (the HTTP status), `error` (the
|
|
144
|
+
API's error code) and `detail` (its explanation). Catch the subclass you act on:
|
|
145
|
+
|
|
146
|
+
| Exception | When |
|
|
147
|
+
|---|---|
|
|
148
|
+
| `AuthenticationError` | 401: the key is missing, wrong, expired or revoked |
|
|
149
|
+
| `PermissionDenied` | 403: the key may not touch what was named |
|
|
150
|
+
| `NotFoundError` | 404: no such project, thread, job, key or record |
|
|
151
|
+
| `ConflictError` | 409: the request conflicts with the current state |
|
|
152
|
+
| `PayloadTooLarge` | 413: the media is over a limit |
|
|
153
|
+
| `ValidationError` | 422: a field was wrong; `detail` says which |
|
|
154
|
+
| `ServerError` | 5xx |
|
|
155
|
+
| `APIConnectionError` | No answer: the connection failed or timed out |
|
|
156
|
+
| `UploadError` | A part of an upload could not be stored, or the API's upload answer was malformed |
|
|
157
|
+
| `PerceptionError` | Anything else, such as a success answer that is not JSON |
|
|
158
|
+
| `ConfigurationError` | No URL or credential was given (not a `PerceptionError`) |
|
|
159
|
+
|
|
160
|
+
```python
|
|
161
|
+
from perception_sdk import Client, ConflictError, NotFoundError, PerceptionError
|
|
162
|
+
|
|
163
|
+
with Client() as client:
|
|
164
|
+
try:
|
|
165
|
+
client.keys.revoke("4f1c2a9be07d3e15")
|
|
166
|
+
except ConflictError:
|
|
167
|
+
print("that is the key you are calling with; revoke it with another")
|
|
168
|
+
except NotFoundError:
|
|
169
|
+
print("no such key")
|
|
170
|
+
except PerceptionError as e:
|
|
171
|
+
print(e.status, e.error, e.detail)
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
Reads (`GET`) are tried up to three times (the first attempt and two retries) on a failed
|
|
175
|
+
connection or a 502, 503 or 504. Writes are never retried, because a write that timed out may
|
|
176
|
+
already have happened.
|
|
177
|
+
|
|
178
|
+
## The operator's client
|
|
179
|
+
|
|
180
|
+
`AdminClient` uses the admin token. The admin API is not public; reach it through an SSH tunnel to
|
|
181
|
+
the server and pass `--url http://127.0.0.1:<port>` (or the same URL to `AdminClient`), where
|
|
182
|
+
`<port>` is the local end of the tunnel.
|
|
183
|
+
|
|
184
|
+
```python
|
|
185
|
+
from perception_sdk import AdminClient
|
|
186
|
+
|
|
187
|
+
with AdminClient("http://127.0.0.1:<port>") as admin: # PERCEPTION_ADMIN_TOKEN
|
|
188
|
+
admin.tenants.create("Acme")
|
|
189
|
+
first = admin.keys.create("Acme", name="bootstrap")
|
|
190
|
+
print(first.api_key)
|
|
191
|
+
print(admin.keys.list("Acme").truncated)
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
`admin.tenants` (`create`, `list`), `admin.subjects` (`create`), `admin.keys` (`list`, `create`,
|
|
195
|
+
`revoke`, `rotate`), `admin.dead_letters`, `admin.quarantine` and `admin.subscriptions` cover the
|
|
196
|
+
admin routes.
|
|
197
|
+
|
|
198
|
+
## Command line
|
|
199
|
+
|
|
200
|
+
Installing the package also installs `perception`. It reads the same environment variables:
|
|
201
|
+
`PERCEPTION_API_URL` (or `--url`), `PERCEPTION_API_KEY` for a workspace's commands and
|
|
202
|
+
`PERCEPTION_ADMIN_TOKEN` for `admin`. Credentials are taken from the environment only, never from
|
|
203
|
+
a flag, so they stay out of your shell history.
|
|
204
|
+
|
|
205
|
+
```sh
|
|
206
|
+
perception whoami
|
|
207
|
+
perception keys create --name ci --expires-at 2027-06-30T00:00:00+00:00
|
|
208
|
+
perception keys list --table
|
|
209
|
+
perception keys rotate 4f1c2a9be07d3e15
|
|
210
|
+
perception keys delete 4f1c2a9be07d3e15
|
|
211
|
+
perception jobs list pr_123 --table
|
|
212
|
+
perception upload th_123 dock-camera.mp4 --no-process
|
|
213
|
+
|
|
214
|
+
# with an SSH tunnel to the admin API open on <port>
|
|
215
|
+
perception --url http://127.0.0.1:<port> admin tenants list --table
|
|
216
|
+
perception --url http://127.0.0.1:<port> admin keys issue Acme --name bootstrap
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
Answers are printed to stdout as JSON; `--table` prints a list command's answer as columns, and
|
|
220
|
+
says on stderr when the list was truncated or, for `jobs list`, how to ask for the next page
|
|
221
|
+
(`more results: pass --after <job_id>`). An issued or rotated key's secret is in that JSON,
|
|
222
|
+
printed this once. Errors go to stderr on one line. The exit status is 0 on success, 1 when the
|
|
223
|
+
API refused or could not be reached (or anything unexpected went wrong), and 2 for a usage or
|
|
224
|
+
configuration error such as a missing URL or key. `perception --help` and each command's
|
|
225
|
+
`--help` list the rest.
|
|
226
|
+
|
|
227
|
+
## License
|
|
228
|
+
|
|
229
|
+
MIT
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "perception-sdk"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "A Python client and command line for the Perception console API"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
authors = [{ name = "Metacog Labs" }]
|
|
9
|
+
requires-python = ">=3.10"
|
|
10
|
+
dependencies = ["httpx>=0.27,<1"]
|
|
11
|
+
classifiers = [
|
|
12
|
+
"Programming Language :: Python :: 3",
|
|
13
|
+
"Programming Language :: Python :: 3.10",
|
|
14
|
+
"Programming Language :: Python :: 3.11",
|
|
15
|
+
"Programming Language :: Python :: 3.12",
|
|
16
|
+
"Programming Language :: Python :: 3.13",
|
|
17
|
+
"Programming Language :: Python :: 3.14",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
"Typing :: Typed",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
[project.scripts]
|
|
23
|
+
perception = "perception_sdk.cli:main"
|
|
24
|
+
|
|
25
|
+
[build-system]
|
|
26
|
+
requires = ["hatchling>=1.27"]
|
|
27
|
+
build-backend = "hatchling.build"
|
|
28
|
+
|
|
29
|
+
[tool.hatch.build.targets.wheel]
|
|
30
|
+
packages = ["src/perception_sdk"]
|
|
31
|
+
|
|
32
|
+
[tool.hatch.build.targets.sdist]
|
|
33
|
+
only-include = ["src", "tests", "README.md", "LICENSE", "pyproject.toml"]
|
|
34
|
+
|
|
35
|
+
[dependency-groups]
|
|
36
|
+
dev = ["pytest>=8", "respx>=0.22"]
|
|
37
|
+
|
|
38
|
+
[tool.pytest.ini_options]
|
|
39
|
+
testpaths = ["tests"]
|
|
40
|
+
|
|
41
|
+
[tool.ruff]
|
|
42
|
+
line-length = 100
|
|
43
|
+
target-version = "py310"
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""A Python client and command line for the Perception console API."""
|
|
2
|
+
|
|
3
|
+
from ._http import VERSION as __version__
|
|
4
|
+
from .admin import AdminClient
|
|
5
|
+
from .client import PART_SIZE, Client
|
|
6
|
+
from .errors import (
|
|
7
|
+
APIConnectionError,
|
|
8
|
+
AuthenticationError,
|
|
9
|
+
ConfigurationError,
|
|
10
|
+
ConflictError,
|
|
11
|
+
NotFoundError,
|
|
12
|
+
PayloadTooLarge,
|
|
13
|
+
PerceptionError,
|
|
14
|
+
PermissionDenied,
|
|
15
|
+
ServerError,
|
|
16
|
+
UploadError,
|
|
17
|
+
ValidationError,
|
|
18
|
+
)
|
|
19
|
+
from .models import ApiKey, IssuedApiKey, KeyList, Source, WhoAmI
|
|
20
|
+
|
|
21
|
+
__all__ = [
|
|
22
|
+
"PART_SIZE",
|
|
23
|
+
"APIConnectionError",
|
|
24
|
+
"AdminClient",
|
|
25
|
+
"ApiKey",
|
|
26
|
+
"AuthenticationError",
|
|
27
|
+
"Client",
|
|
28
|
+
"ConfigurationError",
|
|
29
|
+
"ConflictError",
|
|
30
|
+
"IssuedApiKey",
|
|
31
|
+
"KeyList",
|
|
32
|
+
"NotFoundError",
|
|
33
|
+
"PayloadTooLarge",
|
|
34
|
+
"PerceptionError",
|
|
35
|
+
"PermissionDenied",
|
|
36
|
+
"ServerError",
|
|
37
|
+
"Source",
|
|
38
|
+
"UploadError",
|
|
39
|
+
"ValidationError",
|
|
40
|
+
"WhoAmI",
|
|
41
|
+
"__version__",
|
|
42
|
+
]
|