justdeploy-sdk 0.2.1__tar.gz → 0.2.2__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: justdeploy-sdk
3
- Version: 0.2.1
3
+ Version: 0.2.2
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
@@ -28,7 +28,7 @@ Production supports local Credential authentication and automatic identity in de
28
28
  ## Install
29
29
 
30
30
  ```bash
31
- python -m pip install justdeploy-sdk==0.2.1
31
+ python -m pip install justdeploy-sdk==0.2.2
32
32
  ```
33
33
 
34
34
  ## Client
@@ -183,6 +183,8 @@ Use one `idempotency_key` per operation. A retry keeps its key and content; each
183
183
 
184
184
  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
185
 
186
+ 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.
187
+
186
188
  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
189
 
188
190
  The SDK may refresh authentication and repeat one failed `GET` once. It never automatically repeats a mutation.
@@ -7,7 +7,7 @@ Production supports local Credential authentication and automatic identity in de
7
7
  ## Install
8
8
 
9
9
  ```bash
10
- python -m pip install justdeploy-sdk==0.2.1
10
+ python -m pip install justdeploy-sdk==0.2.2
11
11
  ```
12
12
 
13
13
  ## Client
@@ -162,6 +162,8 @@ Use one `idempotency_key` per operation. A retry keeps its key and content; each
162
162
 
163
163
  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
164
 
165
+ 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.
166
+
165
167
  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
168
 
167
169
  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
 
@@ -206,15 +207,7 @@ def _session_from_response(response: httpx.Response, api_origin: str, now: float
206
207
  message = payload["message"]
207
208
  except (ValueError, UnicodeDecodeError):
208
209
  pass
209
- retry_after = details.get("retryAfter")
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
- )
210
+ raise JustDeployAuthenticationError(message, **error_metadata(response, details))
218
211
  try:
219
212
  payload = response.json()
220
213
  token = payload["token"]
@@ -229,8 +222,7 @@ def _session_from_response(response: httpx.Response, api_origin: str, now: float
229
222
  except (KeyError, TypeError, ValueError, UnicodeDecodeError):
230
223
  raise JustDeployAuthenticationError(
231
224
  "JustDeploy returned an invalid authentication response.",
232
- status=response.status_code,
233
- request_id=response.headers.get("x-request-id"),
225
+ **error_metadata(response),
234
226
  ) from None
235
227
  if (
236
228
  not isinstance(token, str)
@@ -241,8 +233,7 @@ def _session_from_response(response: httpx.Response, api_origin: str, now: float
241
233
  ):
242
234
  raise JustDeployAuthenticationError(
243
235
  "JustDeploy returned an invalid authentication response.",
244
- status=response.status_code,
245
- request_id=response.headers.get("x-request-id"),
236
+ **error_metadata(response),
246
237
  )
247
238
  return AuthSession(token, organization_id, expires_at, api_origin)
248
239
 
@@ -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 Any, Literal, cast
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
- retry_after = details.get("retryAfter")
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
- status=response.status_code,
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.2"
@@ -1 +0,0 @@
1
- __version__ = "0.2.1"
File without changes