justdeploy-sdk 0.2.1__tar.gz → 0.2.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.
- {justdeploy_sdk-0.2.1 → justdeploy_sdk-0.2.3}/PKG-INFO +46 -25
- {justdeploy_sdk-0.2.1 → justdeploy_sdk-0.2.3}/README.md +45 -24
- {justdeploy_sdk-0.2.1 → justdeploy_sdk-0.2.3}/src/justdeploy/_auth.py +25 -21
- justdeploy_sdk-0.2.3/src/justdeploy/_response.py +45 -0
- {justdeploy_sdk-0.2.1 → justdeploy_sdk-0.2.3}/src/justdeploy/_transport.py +4 -14
- justdeploy_sdk-0.2.3/src/justdeploy/_version.py +1 -0
- justdeploy_sdk-0.2.1/src/justdeploy/_version.py +0 -1
- {justdeploy_sdk-0.2.1 → justdeploy_sdk-0.2.3}/.gitignore +0 -0
- {justdeploy_sdk-0.2.1 → justdeploy_sdk-0.2.3}/LICENSE +0 -0
- {justdeploy_sdk-0.2.1 → justdeploy_sdk-0.2.3}/pyproject.toml +0 -0
- {justdeploy_sdk-0.2.1 → justdeploy_sdk-0.2.3}/src/justdeploy/__init__.py +0 -0
- {justdeploy_sdk-0.2.1 → justdeploy_sdk-0.2.3}/src/justdeploy/_validation.py +0 -0
- {justdeploy_sdk-0.2.1 → justdeploy_sdk-0.2.3}/src/justdeploy/client.py +0 -0
- {justdeploy_sdk-0.2.1 → justdeploy_sdk-0.2.3}/src/justdeploy/databases.py +0 -0
- {justdeploy_sdk-0.2.1 → justdeploy_sdk-0.2.3}/src/justdeploy/errors.py +0 -0
- {justdeploy_sdk-0.2.1 → justdeploy_sdk-0.2.3}/src/justdeploy/mail.py +0 -0
- {justdeploy_sdk-0.2.1 → justdeploy_sdk-0.2.3}/src/justdeploy/py.typed +0 -0
- {justdeploy_sdk-0.2.1 → justdeploy_sdk-0.2.3}/src/justdeploy/storages.py +0 -0
- {justdeploy_sdk-0.2.1 → justdeploy_sdk-0.2.3}/src/justdeploy/types.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: justdeploy-sdk
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.3
|
|
4
4
|
Summary: Official server-side Python SDK for JustDeploy
|
|
5
5
|
Project-URL: Homepage, https://github.com/cuppasoft/justdeploy-sdk
|
|
6
6
|
Project-URL: Issues, https://github.com/cuppasoft/justdeploy-sdk/issues
|
|
@@ -25,10 +25,22 @@ The official JustDeploy SDK for server-side CPython 3.12, 3.13, and 3.14. Import
|
|
|
25
25
|
|
|
26
26
|
Production supports local Credential authentication and automatic identity in deployed web, API, and cron applications. Development SDK access remains limited to Playground.
|
|
27
27
|
|
|
28
|
+
## Setup through your AI
|
|
29
|
+
|
|
30
|
+
Use **AI Plugin** in [Console](https://console.justdeploy.ai/console/mcp) to connect
|
|
31
|
+
your desktop AI, then ask it to prepare your app’s resources and table structure.
|
|
32
|
+
The SDK runs in your app; the Plugin runs in your AI. Never reuse personal Plugin
|
|
33
|
+
credentials in application code. See the [shared setup and API boundary](../README.md#external-api-scope)
|
|
34
|
+
and the [application API reference](https://www.justdeploy.ai/docs/api).
|
|
35
|
+
|
|
36
|
+
In Development and Production, resource lists and table-management methods return
|
|
37
|
+
`403`; use your connected AI through MCP for those operations. The runtime examples below use IDs supplied through
|
|
38
|
+
server environment variables and tables already prepared through MCP.
|
|
39
|
+
|
|
28
40
|
## Install
|
|
29
41
|
|
|
30
42
|
```bash
|
|
31
|
-
python -m pip install justdeploy-sdk==0.2.
|
|
43
|
+
python -m pip install justdeploy-sdk==0.2.3
|
|
32
44
|
```
|
|
33
45
|
|
|
34
46
|
## Client
|
|
@@ -55,33 +67,19 @@ async def read_orders() -> None:
|
|
|
55
67
|
|
|
56
68
|
For local development, set both `JUSTDEPLOY_ACCESS_KEY` and `JUSTDEPLOY_SECRET_KEY` in the process environment, using a Production Credential and resource IDs from the same organization. Deployed JustDeploy applications need no SDK configuration. A local client cannot use Development Credentials; test a Development organization in its deployed Playground app. If a deployed application explicitly keeps both variables, the SDK accepts and prioritizes them; build feedback recommends the simpler automatic deployment identity without blocking the build.
|
|
57
69
|
|
|
70
|
+
With a manually configured Credential, set `JUSTDEPLOY_ORGANIZATION_ID` to the owning organization's UID to route token requests to its region. For automatic authentication, the SDK uses the organization UID from the deployment identity when present. Requests without a UID use the compatibility route.
|
|
71
|
+
|
|
58
72
|
## Database
|
|
59
73
|
|
|
60
74
|
The examples below run inside an open `with JustDeploy() as justdeploy:` block, using your existing resource IDs. Prepare schema once before starting the application, not on each startup.
|
|
61
75
|
|
|
62
76
|
```python
|
|
63
|
-
databases = justdeploy.databases.list()
|
|
64
77
|
result = justdeploy.databases.query(database_id, "SELECT * FROM orders WHERE customer = ?", params=[customer])
|
|
65
|
-
|
|
66
|
-
tables = justdeploy.databases.list_tables(database_id)
|
|
67
|
-
table = justdeploy.databases.create_table(
|
|
68
|
-
database_id,
|
|
69
|
-
{
|
|
70
|
-
"name": "orders",
|
|
71
|
-
"columns": [{"name": "customer", "type": "string", "nullable": False}],
|
|
72
|
-
},
|
|
73
|
-
)
|
|
74
|
-
justdeploy.databases.update_table(
|
|
75
|
-
database_id,
|
|
76
|
-
"orders",
|
|
77
|
-
{
|
|
78
|
-
"add": [{"name": "paid", "type": "boolean", "default": False}],
|
|
79
|
-
},
|
|
80
|
-
)
|
|
81
|
-
justdeploy.databases.delete_table(database_id, "orders")
|
|
82
78
|
```
|
|
83
79
|
|
|
84
|
-
`query` accepts only the data statements allowed by the JustDeploy API.
|
|
80
|
+
`query` accepts only the data statements allowed by the JustDeploy API. Ask your
|
|
81
|
+
connected AI to change the schema through MCP; do not create tables on app startup.
|
|
82
|
+
Add `await` to each call when using `AsyncJustDeploy`.
|
|
85
83
|
|
|
86
84
|
A read returns `{"rows": [...]}`; a write returns `{"id": ...}`, not `affectedRows`. Separate calls do not share a transaction.
|
|
87
85
|
|
|
@@ -121,7 +119,6 @@ Every call prepares a **new file** and is never automatically retried. A lost re
|
|
|
121
119
|
### Server files: upload bytes or a stream
|
|
122
120
|
|
|
123
121
|
```python
|
|
124
|
-
storages = justdeploy.storages.list()
|
|
125
122
|
file = justdeploy.storages.upload(
|
|
126
123
|
storage_id,
|
|
127
124
|
name="hello.txt",
|
|
@@ -129,7 +126,6 @@ file = justdeploy.storages.upload(
|
|
|
129
126
|
data=b"hello",
|
|
130
127
|
)
|
|
131
128
|
|
|
132
|
-
page = justdeploy.storages.list_files(storage_id, limit=50)
|
|
133
129
|
info = justdeploy.storages.get_file(storage_id, file["id"])
|
|
134
130
|
|
|
135
131
|
with justdeploy.storages.download(storage_id, file["id"]) as download:
|
|
@@ -156,7 +152,8 @@ If a download races with a still-finishing upload, retry after the SDK's clear p
|
|
|
156
152
|
|
|
157
153
|
Upload results have no signed URL. `file["size"]` is the number of bytes successfully transferred. After PUT succeeds, the file can be read without waiting for `active`; metadata may still be `pending`. Before PUT succeeds, `pending` alone does not prove bytes exist. Use `get_file` again only when final metadata is needed. Save the file ID, not the expiring URL.
|
|
158
154
|
|
|
159
|
-
Returned JSON keys stay camelCase in Python.
|
|
155
|
+
Returned JSON keys stay camelCase in Python. Keep the file ID returned by upload
|
|
156
|
+
for later reads and deletion; do not list files to rediscover it.
|
|
160
157
|
|
|
161
158
|
Transfer failures are safe SDK errors, not unchanged exceptions from your byte source. Failed uploads try to remove the pending record; cleanup failure does not replace the transfer error or cancellation.
|
|
162
159
|
|
|
@@ -173,16 +170,40 @@ mail = justdeploy.mail.send(
|
|
|
173
170
|
idempotency_key="welcome-user-123",
|
|
174
171
|
)
|
|
175
172
|
|
|
176
|
-
page = justdeploy.mail.list(limit=50)
|
|
177
173
|
current = justdeploy.mail.get(mail["id"])
|
|
178
174
|
```
|
|
179
175
|
|
|
180
176
|
Use one `idempotency_key` per operation. A retry keeps its key and content; each new password reset needs a new key. The same key with different content is rejected with 409. SDK input uses `sender`; REST input and returned records use `from`. `delivered` means the receiving server accepted the message, not inbox placement. See the shared [Mail and tracking rules](../README.md#request-behavior).
|
|
181
177
|
|
|
178
|
+
## Existing Production management methods
|
|
179
|
+
|
|
180
|
+
These methods remain available in the published SDK for existing Production apps.
|
|
181
|
+
They return `403` in Development. Use MCP for new resource-discovery and schema workflows.
|
|
182
|
+
Async clients expose the same methods with `await`.
|
|
183
|
+
|
|
184
|
+
```python
|
|
185
|
+
databases = justdeploy.databases.list()
|
|
186
|
+
tables = justdeploy.databases.list_tables(database_id)
|
|
187
|
+
table = justdeploy.databases.create_table(
|
|
188
|
+
database_id,
|
|
189
|
+
{"name": "orders", "columns": [{"name": "customer", "type": "string", "nullable": False}]},
|
|
190
|
+
)
|
|
191
|
+
justdeploy.databases.update_table(database_id, "orders", {"add": [{"name": "paid", "type": "boolean", "default": False}]})
|
|
192
|
+
justdeploy.databases.delete_table(database_id, "orders")
|
|
193
|
+
storages = justdeploy.storages.list()
|
|
194
|
+
files = justdeploy.storages.list_files(storage_id, limit=50)
|
|
195
|
+
mails = justdeploy.mail.list(limit=50)
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
For lists that support pagination, pass `nextCursor` as `cursor` until it is None;
|
|
199
|
+
there is no `next_cursor` response key.
|
|
200
|
+
|
|
182
201
|
## Errors and cancellation
|
|
183
202
|
|
|
184
203
|
Handle SDK failures with `JustDeployError`. API errors expose `status`, `retry_after`, `request_id`, and `details`. For diagnostics, record only `status` and `request_id`, not the whole error, request, SQL, file contents, or credentials. Cancellation does not prove a write was rolled back. Authentication and API time limits are separate; see [request behavior](../README.md#request-behavior).
|
|
185
204
|
|
|
205
|
+
Authentication and API errors prefer valid JSON diagnostics and fall back to `Retry-After` and `X-Request-Id` headers. `retry_after` is a positive integer in seconds within JavaScript's safe integer range, or `None`; HTTP-date values are not used. `request_id` is a validated identifier, or `None`. Non-JSON errors keep valid header diagnostics without exposing the response body. A delay does not trigger an automatic retry.
|
|
206
|
+
|
|
186
207
|
Async calls use normal Python task cancellation. Cancel the task with `task.cancel()`; a canceled upload also tries to remove its pending file record. Authentication exchanges have a 10-second timeout; ordinary JustDeploy API requests have a 30-second timeout.
|
|
187
208
|
|
|
188
209
|
The SDK may refresh authentication and repeat one failed `GET` once. It never automatically repeats a mutation.
|
|
@@ -4,10 +4,22 @@ The official JustDeploy SDK for server-side CPython 3.12, 3.13, and 3.14. Import
|
|
|
4
4
|
|
|
5
5
|
Production supports local Credential authentication and automatic identity in deployed web, API, and cron applications. Development SDK access remains limited to Playground.
|
|
6
6
|
|
|
7
|
+
## Setup through your AI
|
|
8
|
+
|
|
9
|
+
Use **AI Plugin** in [Console](https://console.justdeploy.ai/console/mcp) to connect
|
|
10
|
+
your desktop AI, then ask it to prepare your app’s resources and table structure.
|
|
11
|
+
The SDK runs in your app; the Plugin runs in your AI. Never reuse personal Plugin
|
|
12
|
+
credentials in application code. See the [shared setup and API boundary](../README.md#external-api-scope)
|
|
13
|
+
and the [application API reference](https://www.justdeploy.ai/docs/api).
|
|
14
|
+
|
|
15
|
+
In Development and Production, resource lists and table-management methods return
|
|
16
|
+
`403`; use your connected AI through MCP for those operations. The runtime examples below use IDs supplied through
|
|
17
|
+
server environment variables and tables already prepared through MCP.
|
|
18
|
+
|
|
7
19
|
## Install
|
|
8
20
|
|
|
9
21
|
```bash
|
|
10
|
-
python -m pip install justdeploy-sdk==0.2.
|
|
22
|
+
python -m pip install justdeploy-sdk==0.2.3
|
|
11
23
|
```
|
|
12
24
|
|
|
13
25
|
## Client
|
|
@@ -34,33 +46,19 @@ async def read_orders() -> None:
|
|
|
34
46
|
|
|
35
47
|
For local development, set both `JUSTDEPLOY_ACCESS_KEY` and `JUSTDEPLOY_SECRET_KEY` in the process environment, using a Production Credential and resource IDs from the same organization. Deployed JustDeploy applications need no SDK configuration. A local client cannot use Development Credentials; test a Development organization in its deployed Playground app. If a deployed application explicitly keeps both variables, the SDK accepts and prioritizes them; build feedback recommends the simpler automatic deployment identity without blocking the build.
|
|
36
48
|
|
|
49
|
+
With a manually configured Credential, set `JUSTDEPLOY_ORGANIZATION_ID` to the owning organization's UID to route token requests to its region. For automatic authentication, the SDK uses the organization UID from the deployment identity when present. Requests without a UID use the compatibility route.
|
|
50
|
+
|
|
37
51
|
## Database
|
|
38
52
|
|
|
39
53
|
The examples below run inside an open `with JustDeploy() as justdeploy:` block, using your existing resource IDs. Prepare schema once before starting the application, not on each startup.
|
|
40
54
|
|
|
41
55
|
```python
|
|
42
|
-
databases = justdeploy.databases.list()
|
|
43
56
|
result = justdeploy.databases.query(database_id, "SELECT * FROM orders WHERE customer = ?", params=[customer])
|
|
44
|
-
|
|
45
|
-
tables = justdeploy.databases.list_tables(database_id)
|
|
46
|
-
table = justdeploy.databases.create_table(
|
|
47
|
-
database_id,
|
|
48
|
-
{
|
|
49
|
-
"name": "orders",
|
|
50
|
-
"columns": [{"name": "customer", "type": "string", "nullable": False}],
|
|
51
|
-
},
|
|
52
|
-
)
|
|
53
|
-
justdeploy.databases.update_table(
|
|
54
|
-
database_id,
|
|
55
|
-
"orders",
|
|
56
|
-
{
|
|
57
|
-
"add": [{"name": "paid", "type": "boolean", "default": False}],
|
|
58
|
-
},
|
|
59
|
-
)
|
|
60
|
-
justdeploy.databases.delete_table(database_id, "orders")
|
|
61
57
|
```
|
|
62
58
|
|
|
63
|
-
`query` accepts only the data statements allowed by the JustDeploy API.
|
|
59
|
+
`query` accepts only the data statements allowed by the JustDeploy API. Ask your
|
|
60
|
+
connected AI to change the schema through MCP; do not create tables on app startup.
|
|
61
|
+
Add `await` to each call when using `AsyncJustDeploy`.
|
|
64
62
|
|
|
65
63
|
A read returns `{"rows": [...]}`; a write returns `{"id": ...}`, not `affectedRows`. Separate calls do not share a transaction.
|
|
66
64
|
|
|
@@ -100,7 +98,6 @@ Every call prepares a **new file** and is never automatically retried. A lost re
|
|
|
100
98
|
### Server files: upload bytes or a stream
|
|
101
99
|
|
|
102
100
|
```python
|
|
103
|
-
storages = justdeploy.storages.list()
|
|
104
101
|
file = justdeploy.storages.upload(
|
|
105
102
|
storage_id,
|
|
106
103
|
name="hello.txt",
|
|
@@ -108,7 +105,6 @@ file = justdeploy.storages.upload(
|
|
|
108
105
|
data=b"hello",
|
|
109
106
|
)
|
|
110
107
|
|
|
111
|
-
page = justdeploy.storages.list_files(storage_id, limit=50)
|
|
112
108
|
info = justdeploy.storages.get_file(storage_id, file["id"])
|
|
113
109
|
|
|
114
110
|
with justdeploy.storages.download(storage_id, file["id"]) as download:
|
|
@@ -135,7 +131,8 @@ If a download races with a still-finishing upload, retry after the SDK's clear p
|
|
|
135
131
|
|
|
136
132
|
Upload results have no signed URL. `file["size"]` is the number of bytes successfully transferred. After PUT succeeds, the file can be read without waiting for `active`; metadata may still be `pending`. Before PUT succeeds, `pending` alone does not prove bytes exist. Use `get_file` again only when final metadata is needed. Save the file ID, not the expiring URL.
|
|
137
133
|
|
|
138
|
-
Returned JSON keys stay camelCase in Python.
|
|
134
|
+
Returned JSON keys stay camelCase in Python. Keep the file ID returned by upload
|
|
135
|
+
for later reads and deletion; do not list files to rediscover it.
|
|
139
136
|
|
|
140
137
|
Transfer failures are safe SDK errors, not unchanged exceptions from your byte source. Failed uploads try to remove the pending record; cleanup failure does not replace the transfer error or cancellation.
|
|
141
138
|
|
|
@@ -152,16 +149,40 @@ mail = justdeploy.mail.send(
|
|
|
152
149
|
idempotency_key="welcome-user-123",
|
|
153
150
|
)
|
|
154
151
|
|
|
155
|
-
page = justdeploy.mail.list(limit=50)
|
|
156
152
|
current = justdeploy.mail.get(mail["id"])
|
|
157
153
|
```
|
|
158
154
|
|
|
159
155
|
Use one `idempotency_key` per operation. A retry keeps its key and content; each new password reset needs a new key. The same key with different content is rejected with 409. SDK input uses `sender`; REST input and returned records use `from`. `delivered` means the receiving server accepted the message, not inbox placement. See the shared [Mail and tracking rules](../README.md#request-behavior).
|
|
160
156
|
|
|
157
|
+
## Existing Production management methods
|
|
158
|
+
|
|
159
|
+
These methods remain available in the published SDK for existing Production apps.
|
|
160
|
+
They return `403` in Development. Use MCP for new resource-discovery and schema workflows.
|
|
161
|
+
Async clients expose the same methods with `await`.
|
|
162
|
+
|
|
163
|
+
```python
|
|
164
|
+
databases = justdeploy.databases.list()
|
|
165
|
+
tables = justdeploy.databases.list_tables(database_id)
|
|
166
|
+
table = justdeploy.databases.create_table(
|
|
167
|
+
database_id,
|
|
168
|
+
{"name": "orders", "columns": [{"name": "customer", "type": "string", "nullable": False}]},
|
|
169
|
+
)
|
|
170
|
+
justdeploy.databases.update_table(database_id, "orders", {"add": [{"name": "paid", "type": "boolean", "default": False}]})
|
|
171
|
+
justdeploy.databases.delete_table(database_id, "orders")
|
|
172
|
+
storages = justdeploy.storages.list()
|
|
173
|
+
files = justdeploy.storages.list_files(storage_id, limit=50)
|
|
174
|
+
mails = justdeploy.mail.list(limit=50)
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
For lists that support pagination, pass `nextCursor` as `cursor` until it is None;
|
|
178
|
+
there is no `next_cursor` response key.
|
|
179
|
+
|
|
161
180
|
## Errors and cancellation
|
|
162
181
|
|
|
163
182
|
Handle SDK failures with `JustDeployError`. API errors expose `status`, `retry_after`, `request_id`, and `details`. For diagnostics, record only `status` and `request_id`, not the whole error, request, SQL, file contents, or credentials. Cancellation does not prove a write was rolled back. Authentication and API time limits are separate; see [request behavior](../README.md#request-behavior).
|
|
164
183
|
|
|
184
|
+
Authentication and API errors prefer valid JSON diagnostics and fall back to `Retry-After` and `X-Request-Id` headers. `retry_after` is a positive integer in seconds within JavaScript's safe integer range, or `None`; HTTP-date values are not used. `request_id` is a validated identifier, or `None`. Non-JSON errors keep valid header diagnostics without exposing the response body. A delay does not trigger an automatic retry.
|
|
185
|
+
|
|
165
186
|
Async calls use normal Python task cancellation. Cancel the task with `task.cancel()`; a canceled upload also tries to remove its pending file record. Authentication exchanges have a 10-second timeout; ordinary JustDeploy API requests have a 30-second timeout.
|
|
166
187
|
|
|
167
188
|
The SDK may refresh authentication and repeat one failed `GET` once. It never automatically repeats a mutation.
|
|
@@ -20,6 +20,7 @@ from cryptography.exceptions import UnsupportedAlgorithm
|
|
|
20
20
|
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
|
|
21
21
|
from cryptography.hazmat.primitives.serialization import load_der_private_key
|
|
22
22
|
|
|
23
|
+
from ._response import error_metadata
|
|
23
24
|
from ._version import __version__
|
|
24
25
|
from .errors import JustDeployAuthenticationError, JustDeployConfigurationError
|
|
25
26
|
|
|
@@ -39,6 +40,7 @@ class IdentityDocument:
|
|
|
39
40
|
build_id: str
|
|
40
41
|
private_key: str
|
|
41
42
|
api_origin: str
|
|
43
|
+
organization_id: str | None
|
|
42
44
|
|
|
43
45
|
|
|
44
46
|
@dataclass(frozen=True, slots=True)
|
|
@@ -54,6 +56,7 @@ class ResolvedAuthentication:
|
|
|
54
56
|
api_origin: str
|
|
55
57
|
credentials: tuple[str, str] | None
|
|
56
58
|
identity: IdentityDocument | None
|
|
59
|
+
organization_id: str | None
|
|
57
60
|
|
|
58
61
|
|
|
59
62
|
def _validate_api_origin(value: object) -> str:
|
|
@@ -140,7 +143,12 @@ def _parse_identity(path: Path, *, require_build_key: bool) -> IdentityDocument
|
|
|
140
143
|
not isinstance(build_id, str) or BUILD_UUID.fullmatch(build_id) is None or not isinstance(private_key, str) or not private_key
|
|
141
144
|
):
|
|
142
145
|
raise JustDeployConfigurationError("The JustDeploy deployment identity is missing a valid build key.")
|
|
143
|
-
|
|
146
|
+
organization_id = value.get("organizationId")
|
|
147
|
+
if "organizationId" in value and (not isinstance(organization_id, str) or PLATFORM_ID.fullmatch(organization_id) is None):
|
|
148
|
+
raise JustDeployConfigurationError("The JustDeploy organization ID in the deployment identity is invalid.")
|
|
149
|
+
return IdentityDocument(
|
|
150
|
+
protocol_version, str(build_id), str(private_key), _validate_api_origin(value.get("apiBaseUrl")), organization_id
|
|
151
|
+
)
|
|
144
152
|
|
|
145
153
|
|
|
146
154
|
def _resolve_authentication(env: Mapping[str, str], identity_path: Path) -> ResolvedAuthentication:
|
|
@@ -155,17 +163,22 @@ def _resolve_authentication(env: Mapping[str, str], identity_path: Path) -> Reso
|
|
|
155
163
|
api_origin = identity.api_origin if identity else DEFAULT_API_ORIGIN
|
|
156
164
|
if has_access and has_secret:
|
|
157
165
|
assert access is not None and secret is not None
|
|
158
|
-
|
|
166
|
+
organization_id = env.get("JUSTDEPLOY_ORGANIZATION_ID")
|
|
167
|
+
if organization_id is not None and PLATFORM_ID.fullmatch(organization_id) is None:
|
|
168
|
+
raise JustDeployConfigurationError("Set JUSTDEPLOY_ORGANIZATION_ID to a valid organization UID.")
|
|
169
|
+
return ResolvedAuthentication(api_origin, (access, secret), None, organization_id)
|
|
159
170
|
if identity is None:
|
|
160
171
|
raise JustDeployAuthenticationError(
|
|
161
172
|
"JustDeploy authentication is not configured. Set JUSTDEPLOY_ACCESS_KEY and JUSTDEPLOY_SECRET_KEY for local development; "
|
|
162
173
|
"deployed JustDeploy applications receive an identity automatically."
|
|
163
174
|
)
|
|
164
|
-
return ResolvedAuthentication(api_origin, None, identity)
|
|
175
|
+
return ResolvedAuthentication(api_origin, None, identity, identity.organization_id)
|
|
165
176
|
|
|
166
177
|
|
|
167
178
|
def _build_request(resolved: ResolvedAuthentication, now: float) -> tuple[str, dict[str, str], dict[str, object]]:
|
|
168
179
|
common_headers = {"accept": "application/json", "content-type": "application/json", "x-justdeploy-sdk": SDK_HEADER}
|
|
180
|
+
if resolved.organization_id is not None:
|
|
181
|
+
common_headers["x-justdeploy-organization"] = resolved.organization_id
|
|
169
182
|
if resolved.credentials:
|
|
170
183
|
access, secret = resolved.credentials
|
|
171
184
|
return (
|
|
@@ -194,7 +207,7 @@ def _build_request(resolved: ResolvedAuthentication, now: float) -> tuple[str, d
|
|
|
194
207
|
)
|
|
195
208
|
|
|
196
209
|
|
|
197
|
-
def _session_from_response(response: httpx.Response, api_origin: str, now: float) -> AuthSession:
|
|
210
|
+
def _session_from_response(response: httpx.Response, api_origin: str, now: float, expected_organization_id: str | None) -> AuthSession:
|
|
198
211
|
if not response.is_success:
|
|
199
212
|
message = "JustDeploy authentication was rejected."
|
|
200
213
|
details: dict[str, Any] = {}
|
|
@@ -206,15 +219,7 @@ def _session_from_response(response: httpx.Response, api_origin: str, now: float
|
|
|
206
219
|
message = payload["message"]
|
|
207
220
|
except (ValueError, UnicodeDecodeError):
|
|
208
221
|
pass
|
|
209
|
-
|
|
210
|
-
request_id = details.get("requestId")
|
|
211
|
-
raise JustDeployAuthenticationError(
|
|
212
|
-
message,
|
|
213
|
-
status=response.status_code,
|
|
214
|
-
retry_after=retry_after if isinstance(retry_after, int) and not isinstance(retry_after, bool) else None,
|
|
215
|
-
request_id=request_id if isinstance(request_id, str) else response.headers.get("x-request-id"),
|
|
216
|
-
details=details,
|
|
217
|
-
)
|
|
222
|
+
raise JustDeployAuthenticationError(message, **error_metadata(response, details))
|
|
218
223
|
try:
|
|
219
224
|
payload = response.json()
|
|
220
225
|
token = payload["token"]
|
|
@@ -229,20 +234,19 @@ def _session_from_response(response: httpx.Response, api_origin: str, now: float
|
|
|
229
234
|
except (KeyError, TypeError, ValueError, UnicodeDecodeError):
|
|
230
235
|
raise JustDeployAuthenticationError(
|
|
231
236
|
"JustDeploy returned an invalid authentication response.",
|
|
232
|
-
|
|
233
|
-
request_id=response.headers.get("x-request-id"),
|
|
237
|
+
**error_metadata(response),
|
|
234
238
|
) from None
|
|
235
239
|
if (
|
|
236
240
|
not isinstance(token, str)
|
|
237
241
|
or not token
|
|
238
242
|
or not isinstance(organization_id, str)
|
|
239
243
|
or PLATFORM_ID.fullmatch(organization_id) is None
|
|
244
|
+
or (expected_organization_id is not None and organization_id != expected_organization_id)
|
|
240
245
|
or expires_at <= now
|
|
241
246
|
):
|
|
242
247
|
raise JustDeployAuthenticationError(
|
|
243
248
|
"JustDeploy returned an invalid authentication response.",
|
|
244
|
-
|
|
245
|
-
request_id=response.headers.get("x-request-id"),
|
|
249
|
+
**error_metadata(response),
|
|
246
250
|
)
|
|
247
251
|
return AuthSession(token, organization_id, expires_at, api_origin)
|
|
248
252
|
|
|
@@ -294,14 +298,14 @@ class SyncAuthManager:
|
|
|
294
298
|
raise JustDeployAuthenticationError("JustDeploy authentication timed out.") from None
|
|
295
299
|
except httpx.HTTPError:
|
|
296
300
|
raise JustDeployAuthenticationError("JustDeploy authentication failed before the server returned a response.") from None
|
|
297
|
-
return _session_from_response(response, resolved.api_origin, self._clock())
|
|
301
|
+
return _session_from_response(response, resolved.api_origin, self._clock(), resolved.organization_id)
|
|
298
302
|
|
|
299
303
|
def _resolve(self) -> ResolvedAuthentication:
|
|
300
304
|
if self._credential_authentication:
|
|
301
305
|
return self._credential_authentication
|
|
302
306
|
resolved = _resolve_authentication(self._env, self._identity_path)
|
|
303
307
|
if resolved.credentials:
|
|
304
|
-
self._credential_authentication =
|
|
308
|
+
self._credential_authentication = resolved
|
|
305
309
|
return self._credential_authentication
|
|
306
310
|
return resolved
|
|
307
311
|
|
|
@@ -353,13 +357,13 @@ class AsyncAuthManager:
|
|
|
353
357
|
raise JustDeployAuthenticationError("JustDeploy authentication timed out.") from None
|
|
354
358
|
except httpx.HTTPError:
|
|
355
359
|
raise JustDeployAuthenticationError("JustDeploy authentication failed before the server returned a response.") from None
|
|
356
|
-
return _session_from_response(response, resolved.api_origin, self._clock())
|
|
360
|
+
return _session_from_response(response, resolved.api_origin, self._clock(), resolved.organization_id)
|
|
357
361
|
|
|
358
362
|
def _resolve(self) -> ResolvedAuthentication:
|
|
359
363
|
if self._credential_authentication:
|
|
360
364
|
return self._credential_authentication
|
|
361
365
|
resolved = _resolve_authentication(self._env, self._identity_path)
|
|
362
366
|
if resolved.credentials:
|
|
363
|
-
self._credential_authentication =
|
|
367
|
+
self._credential_authentication = resolved
|
|
364
368
|
return self._credential_authentication
|
|
365
369
|
return resolved
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from collections.abc import Mapping
|
|
5
|
+
from typing import Any, TypedDict
|
|
6
|
+
|
|
7
|
+
import httpx
|
|
8
|
+
|
|
9
|
+
MAX_SAFE_INTEGER = 2**53 - 1
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class ErrorMetadata(TypedDict):
|
|
13
|
+
status: int
|
|
14
|
+
retry_after: int | None
|
|
15
|
+
request_id: str | None
|
|
16
|
+
details: Mapping[str, Any]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _retry_delay(value: object) -> int | None:
|
|
20
|
+
if isinstance(value, bool) or not isinstance(value, (int, float)):
|
|
21
|
+
return None
|
|
22
|
+
if not 0 < value <= MAX_SAFE_INTEGER or int(value) != value:
|
|
23
|
+
return None
|
|
24
|
+
return int(value)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _request_id(value: object) -> str | None:
|
|
28
|
+
return value if isinstance(value, str) and re.fullmatch(r"[A-Za-z0-9_-]{1,128}", value) else None
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def error_metadata(response: httpx.Response, payload: object = None) -> ErrorMetadata:
|
|
32
|
+
"""The body owns diagnostics; headers recover missing or malformed fields."""
|
|
33
|
+
details = payload if isinstance(payload, dict) else {}
|
|
34
|
+
header_delay = response.headers.get("retry-after", "")
|
|
35
|
+
# Bound parsing to JavaScript's integer range and Python's integer-string limit.
|
|
36
|
+
normalized_delay = header_delay.lstrip("0")
|
|
37
|
+
header_seconds = (
|
|
38
|
+
_retry_delay(int(normalized_delay or "0")) if re.fullmatch(r"[0-9]+", header_delay) and len(normalized_delay) <= 16 else None
|
|
39
|
+
)
|
|
40
|
+
return {
|
|
41
|
+
"status": response.status_code,
|
|
42
|
+
"retry_after": _retry_delay(details.get("retryAfter")) or header_seconds,
|
|
43
|
+
"request_id": _request_id(details.get("requestId")) or _request_id(response.headers.get("x-request-id")),
|
|
44
|
+
"details": details,
|
|
45
|
+
}
|
|
@@ -2,12 +2,13 @@ from __future__ import annotations
|
|
|
2
2
|
|
|
3
3
|
import json
|
|
4
4
|
from collections.abc import Mapping
|
|
5
|
-
from typing import
|
|
5
|
+
from typing import Literal
|
|
6
6
|
from urllib.parse import quote, urlsplit
|
|
7
7
|
|
|
8
8
|
import httpx
|
|
9
9
|
|
|
10
10
|
from ._auth import SDK_HEADER, AsyncAuthManager, AuthSession, SyncAuthManager
|
|
11
|
+
from ._response import error_metadata
|
|
11
12
|
from .errors import JustDeployError, JustDeployValidationError
|
|
12
13
|
from .types import AsyncUploadBody, SyncUploadBody
|
|
13
14
|
|
|
@@ -21,17 +22,7 @@ def _api_error(response: httpx.Response, payload: object) -> JustDeployError:
|
|
|
21
22
|
message = details.get("message")
|
|
22
23
|
if not isinstance(message, str) or not message:
|
|
23
24
|
message = f"JustDeploy request failed with status {response.status_code}."
|
|
24
|
-
|
|
25
|
-
request_id = details.get("requestId")
|
|
26
|
-
if not isinstance(request_id, str):
|
|
27
|
-
request_id = response.headers.get("x-request-id")
|
|
28
|
-
return JustDeployError(
|
|
29
|
-
message,
|
|
30
|
-
status=response.status_code,
|
|
31
|
-
retry_after=retry_after if isinstance(retry_after, int) and not isinstance(retry_after, bool) else None,
|
|
32
|
-
request_id=request_id,
|
|
33
|
-
details=cast(Mapping[str, Any], details),
|
|
34
|
-
)
|
|
25
|
+
return JustDeployError(message, **error_metadata(response, details))
|
|
35
26
|
|
|
36
27
|
|
|
37
28
|
def _payload(response: httpx.Response) -> object:
|
|
@@ -42,8 +33,7 @@ def _payload(response: httpx.Response) -> object:
|
|
|
42
33
|
except (ValueError, UnicodeDecodeError):
|
|
43
34
|
raise JustDeployError(
|
|
44
35
|
"JustDeploy returned a response that was not valid JSON.",
|
|
45
|
-
|
|
46
|
-
request_id=response.headers.get("x-request-id"),
|
|
36
|
+
**error_metadata(response),
|
|
47
37
|
) from None
|
|
48
38
|
|
|
49
39
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.2.3"
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "0.2.1"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|