capawesome-cloud 0.1.0__py3-none-any.whl
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.
- capawesome_cloud/__init__.py +58 -0
- capawesome_cloud/_http.py +198 -0
- capawesome_cloud/_types.py +23 -0
- capawesome_cloud/_version.py +1 -0
- capawesome_cloud/client.py +88 -0
- capawesome_cloud/exceptions.py +136 -0
- capawesome_cloud/models.py +190 -0
- capawesome_cloud/pagination.py +72 -0
- capawesome_cloud/py.typed +0 -0
- capawesome_cloud/resources/__init__.py +36 -0
- capawesome_cloud/resources/_base.py +100 -0
- capawesome_cloud/resources/apps.py +135 -0
- capawesome_cloud/resources/automations.py +109 -0
- capawesome_cloud/resources/build_sources.py +29 -0
- capawesome_cloud/resources/builds.py +195 -0
- capawesome_cloud/resources/certificates.py +153 -0
- capawesome_cloud/resources/channels.py +104 -0
- capawesome_cloud/resources/deployments.py +112 -0
- capawesome_cloud/resources/destinations.py +117 -0
- capawesome_cloud/resources/devices.py +68 -0
- capawesome_cloud/resources/environments.py +153 -0
- capawesome_cloud/resources/jobs.py +95 -0
- capawesome_cloud/resources/webhooks.py +95 -0
- capawesome_cloud-0.1.0.dist-info/METADATA +321 -0
- capawesome_cloud-0.1.0.dist-info/RECORD +27 -0
- capawesome_cloud-0.1.0.dist-info/WHEEL +4 -0
- capawesome_cloud-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"""Capawesome Cloud Python SDK.
|
|
2
|
+
|
|
3
|
+
A typed, synchronous client for the Capawesome Cloud API.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from ._version import __version__
|
|
7
|
+
from .client import CapawesomeCloud
|
|
8
|
+
from .exceptions import (
|
|
9
|
+
APIConnectionError,
|
|
10
|
+
APITimeoutError,
|
|
11
|
+
CapawesomeCloudError,
|
|
12
|
+
)
|
|
13
|
+
from .models import (
|
|
14
|
+
App,
|
|
15
|
+
AppAutomation,
|
|
16
|
+
AppBuild,
|
|
17
|
+
AppBuildArtifact,
|
|
18
|
+
AppBuildSource,
|
|
19
|
+
AppCertificate,
|
|
20
|
+
AppChannel,
|
|
21
|
+
AppDeployment,
|
|
22
|
+
AppDestination,
|
|
23
|
+
AppDevice,
|
|
24
|
+
AppEnvironment,
|
|
25
|
+
AppEnvironmentSecret,
|
|
26
|
+
AppEnvironmentVariable,
|
|
27
|
+
AppWebhook,
|
|
28
|
+
CapawesomeModel,
|
|
29
|
+
Job,
|
|
30
|
+
)
|
|
31
|
+
from .resources.jobs import JobTimeoutError
|
|
32
|
+
|
|
33
|
+
__all__ = [
|
|
34
|
+
"__version__",
|
|
35
|
+
"CapawesomeCloud",
|
|
36
|
+
# Exceptions
|
|
37
|
+
"CapawesomeCloudError",
|
|
38
|
+
"APIConnectionError",
|
|
39
|
+
"APITimeoutError",
|
|
40
|
+
"JobTimeoutError",
|
|
41
|
+
# Models
|
|
42
|
+
"CapawesomeModel",
|
|
43
|
+
"App",
|
|
44
|
+
"AppAutomation",
|
|
45
|
+
"AppBuild",
|
|
46
|
+
"AppBuildArtifact",
|
|
47
|
+
"AppBuildSource",
|
|
48
|
+
"AppCertificate",
|
|
49
|
+
"AppChannel",
|
|
50
|
+
"AppDeployment",
|
|
51
|
+
"AppDestination",
|
|
52
|
+
"AppDevice",
|
|
53
|
+
"AppEnvironment",
|
|
54
|
+
"AppEnvironmentSecret",
|
|
55
|
+
"AppEnvironmentVariable",
|
|
56
|
+
"AppWebhook",
|
|
57
|
+
"Job",
|
|
58
|
+
]
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
"""Low-level HTTP transport used by every resource.
|
|
2
|
+
|
|
3
|
+
Wraps an :class:`httpx.Client` and adds:
|
|
4
|
+
|
|
5
|
+
* bearer-token authentication,
|
|
6
|
+
* automatic retries with exponential backoff on ``429`` and ``5xx`` responses,
|
|
7
|
+
* mapping of error responses to the SDK exception hierarchy.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import time
|
|
13
|
+
from datetime import datetime, timezone
|
|
14
|
+
from email.utils import parsedate_to_datetime
|
|
15
|
+
from typing import Any, Mapping, Optional
|
|
16
|
+
|
|
17
|
+
import httpx
|
|
18
|
+
|
|
19
|
+
from ._version import __version__
|
|
20
|
+
from .exceptions import (
|
|
21
|
+
APIConnectionError,
|
|
22
|
+
APITimeoutError,
|
|
23
|
+
exception_from_response,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
DEFAULT_BASE_URL = "https://api.cloud.capawesome.io"
|
|
27
|
+
DEFAULT_TIMEOUT = 30.0
|
|
28
|
+
DEFAULT_MAX_RETRIES = 2
|
|
29
|
+
DEFAULT_BACKOFF_FACTOR = 0.5
|
|
30
|
+
|
|
31
|
+
# Statuses retried for any request method (the server rejected the request
|
|
32
|
+
# before processing it, so a retry cannot duplicate side effects).
|
|
33
|
+
_RETRY_STATUS_ALWAYS = frozenset({429})
|
|
34
|
+
# Statuses retried only for idempotent methods, since a 5xx may mean the request
|
|
35
|
+
# was already (partially) processed -- retrying a POST could duplicate it.
|
|
36
|
+
_RETRY_STATUS_IDEMPOTENT = frozenset({500, 502, 503, 504})
|
|
37
|
+
# Methods that are safe to retry on network/timeout/5xx failures.
|
|
38
|
+
_IDEMPOTENT_METHODS = frozenset({"GET", "HEAD", "OPTIONS", "PUT", "DELETE"})
|
|
39
|
+
|
|
40
|
+
# A small JSON-ish type. Responses are intentionally loosely typed because the
|
|
41
|
+
# API does not publish response schemas.
|
|
42
|
+
JSON = Any
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class HttpClient:
|
|
46
|
+
"""Thin, retrying wrapper around :class:`httpx.Client`."""
|
|
47
|
+
|
|
48
|
+
def __init__(
|
|
49
|
+
self,
|
|
50
|
+
*,
|
|
51
|
+
token: str,
|
|
52
|
+
base_url: str = DEFAULT_BASE_URL,
|
|
53
|
+
timeout: float = DEFAULT_TIMEOUT,
|
|
54
|
+
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
55
|
+
backoff_factor: float = DEFAULT_BACKOFF_FACTOR,
|
|
56
|
+
http_client: Optional[httpx.Client] = None,
|
|
57
|
+
) -> None:
|
|
58
|
+
self._max_retries = max_retries
|
|
59
|
+
self._backoff_factor = backoff_factor
|
|
60
|
+
self._owns_client = http_client is None
|
|
61
|
+
self._client = http_client or httpx.Client(
|
|
62
|
+
base_url=base_url.rstrip("/"),
|
|
63
|
+
timeout=timeout,
|
|
64
|
+
)
|
|
65
|
+
# The token must always apply; identify ourselves on clients we create
|
|
66
|
+
# without clobbering a caller-supplied client's User-Agent.
|
|
67
|
+
self._client.headers["Authorization"] = f"Bearer {token}"
|
|
68
|
+
self._client.headers.setdefault("Accept", "application/json")
|
|
69
|
+
if self._owns_client:
|
|
70
|
+
self._client.headers["User-Agent"] = f"capawesome-cloud-python/{__version__}"
|
|
71
|
+
|
|
72
|
+
# -- public API ---------------------------------------------------------
|
|
73
|
+
|
|
74
|
+
def request(
|
|
75
|
+
self,
|
|
76
|
+
method: str,
|
|
77
|
+
path: str,
|
|
78
|
+
*,
|
|
79
|
+
params: Optional[Mapping[str, Any]] = None,
|
|
80
|
+
json: Optional[Any] = None,
|
|
81
|
+
files: Optional[Mapping[str, Any]] = None,
|
|
82
|
+
data: Optional[Mapping[str, Any]] = None,
|
|
83
|
+
) -> httpx.Response:
|
|
84
|
+
"""Send a request, retrying transient failures, and validate the status.
|
|
85
|
+
|
|
86
|
+
Network/timeout failures and ``5xx`` responses are only retried for
|
|
87
|
+
idempotent methods (a ``POST`` may have already been processed, so
|
|
88
|
+
retrying it could duplicate side effects). ``429`` is retried for any
|
|
89
|
+
method, since the request was rejected before processing.
|
|
90
|
+
"""
|
|
91
|
+
clean_params = _drop_none(params) if params else None
|
|
92
|
+
idempotent = method.upper() in _IDEMPOTENT_METHODS
|
|
93
|
+
attempt = 0
|
|
94
|
+
while True:
|
|
95
|
+
try:
|
|
96
|
+
response = self._client.request(
|
|
97
|
+
method,
|
|
98
|
+
path,
|
|
99
|
+
params=clean_params,
|
|
100
|
+
json=json,
|
|
101
|
+
files=files,
|
|
102
|
+
data=data,
|
|
103
|
+
)
|
|
104
|
+
except httpx.TimeoutException as exc:
|
|
105
|
+
if idempotent and attempt < self._max_retries:
|
|
106
|
+
self._sleep(attempt, None)
|
|
107
|
+
attempt += 1
|
|
108
|
+
continue
|
|
109
|
+
raise APITimeoutError(request=exc.request) from exc
|
|
110
|
+
except httpx.TransportError as exc:
|
|
111
|
+
if idempotent and attempt < self._max_retries:
|
|
112
|
+
self._sleep(attempt, None)
|
|
113
|
+
attempt += 1
|
|
114
|
+
continue
|
|
115
|
+
raise APIConnectionError(str(exc), request=exc.request) from exc
|
|
116
|
+
|
|
117
|
+
if attempt < self._max_retries and self._should_retry_status(
|
|
118
|
+
response.status_code, idempotent
|
|
119
|
+
):
|
|
120
|
+
self._sleep(attempt, response)
|
|
121
|
+
attempt += 1
|
|
122
|
+
continue
|
|
123
|
+
|
|
124
|
+
if response.is_success:
|
|
125
|
+
return response
|
|
126
|
+
|
|
127
|
+
raise exception_from_response(response)
|
|
128
|
+
|
|
129
|
+
def request_json(
|
|
130
|
+
self,
|
|
131
|
+
method: str,
|
|
132
|
+
path: str,
|
|
133
|
+
*,
|
|
134
|
+
params: Optional[Mapping[str, Any]] = None,
|
|
135
|
+
json: Optional[Any] = None,
|
|
136
|
+
files: Optional[Mapping[str, Any]] = None,
|
|
137
|
+
data: Optional[Mapping[str, Any]] = None,
|
|
138
|
+
) -> JSON:
|
|
139
|
+
"""Send a request and return the parsed JSON body (``None`` if empty)."""
|
|
140
|
+
response = self.request(method, path, params=params, json=json, files=files, data=data)
|
|
141
|
+
if not response.content:
|
|
142
|
+
return None
|
|
143
|
+
return response.json()
|
|
144
|
+
|
|
145
|
+
def close(self) -> None:
|
|
146
|
+
if self._owns_client:
|
|
147
|
+
self._client.close()
|
|
148
|
+
|
|
149
|
+
def __enter__(self) -> HttpClient:
|
|
150
|
+
return self
|
|
151
|
+
|
|
152
|
+
def __exit__(self, *exc: object) -> None:
|
|
153
|
+
self.close()
|
|
154
|
+
|
|
155
|
+
# -- internals ----------------------------------------------------------
|
|
156
|
+
|
|
157
|
+
@staticmethod
|
|
158
|
+
def _should_retry_status(status_code: int, idempotent: bool) -> bool:
|
|
159
|
+
if status_code in _RETRY_STATUS_ALWAYS:
|
|
160
|
+
return True
|
|
161
|
+
return idempotent and status_code in _RETRY_STATUS_IDEMPOTENT
|
|
162
|
+
|
|
163
|
+
def _sleep(self, attempt: int, response: Optional[httpx.Response]) -> None:
|
|
164
|
+
delay = self._backoff_factor * (2**attempt)
|
|
165
|
+
if response is not None:
|
|
166
|
+
retry_after = _parse_retry_after(response.headers.get("Retry-After"))
|
|
167
|
+
if retry_after is not None:
|
|
168
|
+
delay = max(delay, retry_after)
|
|
169
|
+
time.sleep(delay)
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def _drop_none(mapping: Mapping[str, Any]) -> dict[str, Any]:
|
|
173
|
+
"""Return a copy of ``mapping`` without ``None`` values."""
|
|
174
|
+
return {key: value for key, value in mapping.items() if value is not None}
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def _parse_retry_after(value: Optional[str]) -> Optional[float]:
|
|
178
|
+
"""Parse a ``Retry-After`` header value into a delay in seconds.
|
|
179
|
+
|
|
180
|
+
Per RFC 9110 the value is either a number of seconds or an HTTP-date.
|
|
181
|
+
"""
|
|
182
|
+
if not value:
|
|
183
|
+
return None
|
|
184
|
+
value = value.strip()
|
|
185
|
+
try:
|
|
186
|
+
return max(0.0, float(value))
|
|
187
|
+
except ValueError:
|
|
188
|
+
pass
|
|
189
|
+
try:
|
|
190
|
+
retry_at = parsedate_to_datetime(value)
|
|
191
|
+
except (TypeError, ValueError):
|
|
192
|
+
return None
|
|
193
|
+
if retry_at is None:
|
|
194
|
+
return None
|
|
195
|
+
if retry_at.tzinfo is None:
|
|
196
|
+
retry_at = retry_at.replace(tzinfo=timezone.utc)
|
|
197
|
+
delay = (retry_at - datetime.now(timezone.utc)).total_seconds()
|
|
198
|
+
return max(0.0, delay)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""Shared sentinel types."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Literal
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class NotGiven:
|
|
9
|
+
"""Sentinel marking an omitted argument.
|
|
10
|
+
|
|
11
|
+
Distinguishes "argument not provided" from an explicit ``None``. This lets a
|
|
12
|
+
caller send ``null`` to clear a field (pass ``None``) versus leaving it
|
|
13
|
+
untouched (pass nothing).
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
def __bool__(self) -> Literal[False]:
|
|
17
|
+
return False
|
|
18
|
+
|
|
19
|
+
def __repr__(self) -> str:
|
|
20
|
+
return "NOT_GIVEN"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
NOT_GIVEN = NotGiven()
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.1.0"
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"""The main Capawesome Cloud client."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from typing import Optional
|
|
7
|
+
|
|
8
|
+
import httpx
|
|
9
|
+
|
|
10
|
+
from ._http import (
|
|
11
|
+
DEFAULT_BACKOFF_FACTOR,
|
|
12
|
+
DEFAULT_BASE_URL,
|
|
13
|
+
DEFAULT_MAX_RETRIES,
|
|
14
|
+
DEFAULT_TIMEOUT,
|
|
15
|
+
HttpClient,
|
|
16
|
+
)
|
|
17
|
+
from .exceptions import CapawesomeCloudError
|
|
18
|
+
from .resources import AppsResource, JobsResource
|
|
19
|
+
|
|
20
|
+
# Token environment variables, in order of precedence. CAPAWESOME_TOKEN is
|
|
21
|
+
# accepted for consistency with the Capawesome CLI.
|
|
22
|
+
ENV_TOKENS = ("CAPAWESOME_CLOUD_TOKEN", "CAPAWESOME_TOKEN")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class CapawesomeCloud:
|
|
26
|
+
"""Synchronous client for the Capawesome Cloud API.
|
|
27
|
+
|
|
28
|
+
Create an API token in the Capawesome Cloud Console
|
|
29
|
+
(https://console.cloud.capawesome.io/settings/tokens) and pass it as
|
|
30
|
+
``token`` or via the ``CAPAWESOME_CLOUD_TOKEN`` (or ``CAPAWESOME_TOKEN``)
|
|
31
|
+
environment variable.
|
|
32
|
+
|
|
33
|
+
Example::
|
|
34
|
+
|
|
35
|
+
from capawesome_cloud import CapawesomeCloud
|
|
36
|
+
|
|
37
|
+
client = CapawesomeCloud(token="...")
|
|
38
|
+
for app in client.apps.list():
|
|
39
|
+
print(app.id, app.name)
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
def __init__(
|
|
43
|
+
self,
|
|
44
|
+
token: Optional[str] = None,
|
|
45
|
+
*,
|
|
46
|
+
base_url: str = DEFAULT_BASE_URL,
|
|
47
|
+
timeout: float = DEFAULT_TIMEOUT,
|
|
48
|
+
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
49
|
+
backoff_factor: float = DEFAULT_BACKOFF_FACTOR,
|
|
50
|
+
http_client: Optional[httpx.Client] = None,
|
|
51
|
+
) -> None:
|
|
52
|
+
resolved_token = token
|
|
53
|
+
for env_name in ENV_TOKENS:
|
|
54
|
+
if resolved_token:
|
|
55
|
+
break
|
|
56
|
+
resolved_token = os.environ.get(env_name)
|
|
57
|
+
# Trim surrounding whitespace/newlines (common with secrets piped from
|
|
58
|
+
# files or CI) so they don't break the Authorization header.
|
|
59
|
+
resolved_token = (resolved_token or "").strip()
|
|
60
|
+
if not resolved_token:
|
|
61
|
+
raise CapawesomeCloudError(
|
|
62
|
+
"No API token provided. Pass token=... or set the "
|
|
63
|
+
f"{ENV_TOKENS[0]} environment variable."
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
self._http = HttpClient(
|
|
67
|
+
token=resolved_token,
|
|
68
|
+
base_url=base_url,
|
|
69
|
+
timeout=timeout,
|
|
70
|
+
max_retries=max_retries,
|
|
71
|
+
backoff_factor=backoff_factor,
|
|
72
|
+
http_client=http_client,
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
# App-scoped resources are nested under ``apps`` (e.g.
|
|
76
|
+
# ``client.apps.channels``); ``jobs`` is organization-scoped.
|
|
77
|
+
self.apps = AppsResource(self._http)
|
|
78
|
+
self.jobs = JobsResource(self._http)
|
|
79
|
+
|
|
80
|
+
def close(self) -> None:
|
|
81
|
+
"""Close the underlying HTTP connection pool."""
|
|
82
|
+
self._http.close()
|
|
83
|
+
|
|
84
|
+
def __enter__(self) -> CapawesomeCloud:
|
|
85
|
+
return self
|
|
86
|
+
|
|
87
|
+
def __exit__(self, *exc: object) -> None:
|
|
88
|
+
self.close()
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"""Errors raised by the SDK.
|
|
2
|
+
|
|
3
|
+
Every error derives from :class:`CapawesomeCloudError`, so a single
|
|
4
|
+
``except CapawesomeCloudError`` catches everything the SDK can raise. HTTP
|
|
5
|
+
errors (non-2xx responses) are raised as ``CapawesomeCloudError`` directly and
|
|
6
|
+
carry ``status`` / ``status_text`` / ``message`` / ``body``. Network failures
|
|
7
|
+
raise :class:`APIConnectionError` / :class:`APITimeoutError`.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from typing import Any, Optional
|
|
13
|
+
|
|
14
|
+
import httpx
|
|
15
|
+
|
|
16
|
+
__all__ = [
|
|
17
|
+
"CapawesomeCloudError",
|
|
18
|
+
"APIConnectionError",
|
|
19
|
+
"APITimeoutError",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class CapawesomeCloudError(Exception):
|
|
24
|
+
"""Base error, and the error raised for non-2xx API responses.
|
|
25
|
+
|
|
26
|
+
For HTTP errors, ``status``, ``status_text``, ``response`` and ``body`` are
|
|
27
|
+
populated. For non-HTTP errors (e.g. connection failures or configuration
|
|
28
|
+
problems) ``status`` is ``None``.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
def __init__(
|
|
32
|
+
self,
|
|
33
|
+
message: str,
|
|
34
|
+
*,
|
|
35
|
+
status: Optional[int] = None,
|
|
36
|
+
status_text: Optional[str] = None,
|
|
37
|
+
response: Optional[httpx.Response] = None,
|
|
38
|
+
body: Optional[Any] = None,
|
|
39
|
+
) -> None:
|
|
40
|
+
super().__init__(message)
|
|
41
|
+
self.message = message
|
|
42
|
+
self.status = status
|
|
43
|
+
self.status_text = status_text
|
|
44
|
+
self.response = response
|
|
45
|
+
self.body = body
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class APIConnectionError(CapawesomeCloudError):
|
|
49
|
+
"""Raised when the request could not reach the API (network failure)."""
|
|
50
|
+
|
|
51
|
+
def __init__(
|
|
52
|
+
self,
|
|
53
|
+
message: str = "Could not connect to the Capawesome Cloud API.",
|
|
54
|
+
*,
|
|
55
|
+
request: Optional[httpx.Request] = None,
|
|
56
|
+
) -> None:
|
|
57
|
+
super().__init__(message)
|
|
58
|
+
self.request = request
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class APITimeoutError(APIConnectionError):
|
|
62
|
+
"""Raised when the request timed out."""
|
|
63
|
+
|
|
64
|
+
def __init__(self, *, request: Optional[httpx.Request] = None) -> None:
|
|
65
|
+
super().__init__("Request to the Capawesome Cloud API timed out.", request=request)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def exception_from_response(response: httpx.Response) -> CapawesomeCloudError:
|
|
69
|
+
"""Build a :class:`CapawesomeCloudError` for a non-2xx HTTP response."""
|
|
70
|
+
status = response.status_code
|
|
71
|
+
body: Optional[Any] = None
|
|
72
|
+
message: Optional[str] = None
|
|
73
|
+
try:
|
|
74
|
+
body = response.json()
|
|
75
|
+
message = _message_from_body(body)
|
|
76
|
+
except ValueError:
|
|
77
|
+
text = (response.text or "").strip()
|
|
78
|
+
body = text or None
|
|
79
|
+
# Surface a plain-text body as the message, but not HTML/XML error
|
|
80
|
+
# pages (e.g. from a gateway), which would be noise.
|
|
81
|
+
if text and not text.startswith("<"):
|
|
82
|
+
message = text
|
|
83
|
+
|
|
84
|
+
if not message:
|
|
85
|
+
message = f"HTTP {status} {response.reason_phrase}".strip()
|
|
86
|
+
|
|
87
|
+
return CapawesomeCloudError(
|
|
88
|
+
message,
|
|
89
|
+
status=status,
|
|
90
|
+
status_text=response.reason_phrase or None,
|
|
91
|
+
response=response,
|
|
92
|
+
body=body,
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _message_from_body(body: Any) -> Optional[str]:
|
|
97
|
+
"""Extract a human-readable message from an error response body.
|
|
98
|
+
|
|
99
|
+
Handles the shapes the API (and its validation layer) can return:
|
|
100
|
+
|
|
101
|
+
* a plain string body,
|
|
102
|
+
* ``{"message": "..."}``,
|
|
103
|
+
* ``{"error": "..."}``,
|
|
104
|
+
* ``{"error": [{"path": [...], "message": "..."}]}``,
|
|
105
|
+
* ``{"error": {"issues": [{"path": [...], "message": "..."}]}}``.
|
|
106
|
+
"""
|
|
107
|
+
if isinstance(body, str):
|
|
108
|
+
return body or None
|
|
109
|
+
if not isinstance(body, dict):
|
|
110
|
+
return None
|
|
111
|
+
|
|
112
|
+
raw = body.get("message")
|
|
113
|
+
if isinstance(raw, str) and raw:
|
|
114
|
+
return raw
|
|
115
|
+
|
|
116
|
+
errors = body.get("error")
|
|
117
|
+
if isinstance(errors, str) and errors:
|
|
118
|
+
return errors
|
|
119
|
+
if isinstance(errors, dict):
|
|
120
|
+
errors = errors.get("issues")
|
|
121
|
+
if isinstance(errors, list):
|
|
122
|
+
parts = []
|
|
123
|
+
for item in errors:
|
|
124
|
+
if not isinstance(item, dict):
|
|
125
|
+
continue
|
|
126
|
+
text = item.get("message")
|
|
127
|
+
if not isinstance(text, str) or not text:
|
|
128
|
+
continue
|
|
129
|
+
path = item.get("path")
|
|
130
|
+
if isinstance(path, list) and path:
|
|
131
|
+
text = f"{'.'.join(str(segment) for segment in path)}: {text}"
|
|
132
|
+
parts.append(text)
|
|
133
|
+
if parts:
|
|
134
|
+
return "; ".join(parts)
|
|
135
|
+
|
|
136
|
+
return None
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
"""Response models.
|
|
2
|
+
|
|
3
|
+
The Capawesome Cloud API does not publish response schemas and is still evolving,
|
|
4
|
+
so these models intentionally declare only the **important, stable** fields. Every
|
|
5
|
+
model is configured with ``extra="allow"``: any additional (including internal)
|
|
6
|
+
fields the API returns are preserved and accessible as attributes, but they are
|
|
7
|
+
**not part of the SDK's public contract** and may change or disappear without
|
|
8
|
+
notice -- do not rely on them.
|
|
9
|
+
|
|
10
|
+
App-scoped resources are prefixed with ``App`` (``AppChannel``, ``AppWebhook``,
|
|
11
|
+
...) to mirror the API's entity names and avoid future clashes with
|
|
12
|
+
organization-scoped resources. Field names are exposed in Pythonic
|
|
13
|
+
``snake_case`` while the API's ``camelCase`` keys are accepted transparently.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
from datetime import datetime
|
|
19
|
+
from typing import Optional
|
|
20
|
+
|
|
21
|
+
from pydantic import BaseModel, ConfigDict
|
|
22
|
+
from pydantic.alias_generators import to_camel
|
|
23
|
+
|
|
24
|
+
__all__ = [
|
|
25
|
+
"CapawesomeModel",
|
|
26
|
+
"App",
|
|
27
|
+
"AppChannel",
|
|
28
|
+
"AppBuild",
|
|
29
|
+
"AppBuildArtifact",
|
|
30
|
+
"AppBuildSource",
|
|
31
|
+
"AppDeployment",
|
|
32
|
+
"AppDestination",
|
|
33
|
+
"AppDevice",
|
|
34
|
+
"AppEnvironment",
|
|
35
|
+
"AppEnvironmentVariable",
|
|
36
|
+
"AppEnvironmentSecret",
|
|
37
|
+
"AppCertificate",
|
|
38
|
+
"AppWebhook",
|
|
39
|
+
"AppAutomation",
|
|
40
|
+
"Job",
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class CapawesomeModel(BaseModel):
|
|
45
|
+
"""Base model: maps ``camelCase`` API keys and keeps unknown fields."""
|
|
46
|
+
|
|
47
|
+
model_config = ConfigDict(
|
|
48
|
+
alias_generator=to_camel,
|
|
49
|
+
populate_by_name=True,
|
|
50
|
+
extra="allow",
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
id: Optional[str] = None
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class App(CapawesomeModel):
|
|
57
|
+
name: Optional[str] = None
|
|
58
|
+
type: Optional[str] = None
|
|
59
|
+
created_at: Optional[datetime] = None
|
|
60
|
+
updated_at: Optional[datetime] = None
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class AppChannel(CapawesomeModel):
|
|
64
|
+
name: Optional[str] = None
|
|
65
|
+
app_id: Optional[str] = None
|
|
66
|
+
paused_at: Optional[datetime] = None
|
|
67
|
+
protected_at: Optional[datetime] = None
|
|
68
|
+
created_at: Optional[datetime] = None
|
|
69
|
+
updated_at: Optional[datetime] = None
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class AppBuild(CapawesomeModel):
|
|
73
|
+
number_as_string: Optional[str] = None
|
|
74
|
+
platform: Optional[str] = None
|
|
75
|
+
type: Optional[str] = None
|
|
76
|
+
display_name: Optional[str] = None
|
|
77
|
+
app_id: Optional[str] = None
|
|
78
|
+
job_id: Optional[str] = None
|
|
79
|
+
created_at: Optional[datetime] = None
|
|
80
|
+
updated_at: Optional[datetime] = None
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class AppBuildArtifact(CapawesomeModel):
|
|
84
|
+
type: Optional[str] = None
|
|
85
|
+
status: Optional[str] = None
|
|
86
|
+
app_build_id: Optional[str] = None
|
|
87
|
+
total_size_in_bytes: Optional[int] = None
|
|
88
|
+
download_url: Optional[str] = None
|
|
89
|
+
created_at: Optional[datetime] = None
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class AppBuildSource(CapawesomeModel):
|
|
93
|
+
type: Optional[str] = None
|
|
94
|
+
status: Optional[str] = None
|
|
95
|
+
file_url: Optional[str] = None
|
|
96
|
+
file_size_in_bytes: Optional[int] = None
|
|
97
|
+
created_at: Optional[datetime] = None
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
class AppDeployment(CapawesomeModel):
|
|
101
|
+
app_build_id: Optional[str] = None
|
|
102
|
+
app_channel_id: Optional[str] = None
|
|
103
|
+
app_destination_id: Optional[str] = None
|
|
104
|
+
app_id: Optional[str] = None
|
|
105
|
+
job_id: Optional[str] = None
|
|
106
|
+
rollout_percentage: Optional[float] = None
|
|
107
|
+
created_at: Optional[datetime] = None
|
|
108
|
+
updated_at: Optional[datetime] = None
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
class AppDestination(CapawesomeModel):
|
|
112
|
+
name: Optional[str] = None
|
|
113
|
+
platform: Optional[str] = None
|
|
114
|
+
app_id: Optional[str] = None
|
|
115
|
+
created_at: Optional[datetime] = None
|
|
116
|
+
updated_at: Optional[datetime] = None
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
class AppDevice(CapawesomeModel):
|
|
120
|
+
app_id: Optional[str] = None
|
|
121
|
+
app_version_code: Optional[str] = None
|
|
122
|
+
app_version_name: Optional[str] = None
|
|
123
|
+
os_version: Optional[str] = None
|
|
124
|
+
plugin_version: Optional[str] = None
|
|
125
|
+
custom_id: Optional[str] = None
|
|
126
|
+
last_seen_at: Optional[datetime] = None
|
|
127
|
+
created_at: Optional[datetime] = None
|
|
128
|
+
updated_at: Optional[datetime] = None
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
class AppEnvironment(CapawesomeModel):
|
|
132
|
+
name: Optional[str] = None
|
|
133
|
+
app_id: Optional[str] = None
|
|
134
|
+
created_at: Optional[datetime] = None
|
|
135
|
+
updated_at: Optional[datetime] = None
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
class AppEnvironmentVariable(CapawesomeModel):
|
|
139
|
+
key: Optional[str] = None
|
|
140
|
+
value: Optional[str] = None
|
|
141
|
+
app_environment_id: Optional[str] = None
|
|
142
|
+
created_at: Optional[datetime] = None
|
|
143
|
+
updated_at: Optional[datetime] = None
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
class AppEnvironmentSecret(CapawesomeModel):
|
|
147
|
+
# The secret ``value`` is encrypted and not returned by the API.
|
|
148
|
+
key: Optional[str] = None
|
|
149
|
+
app_environment_id: Optional[str] = None
|
|
150
|
+
created_at: Optional[datetime] = None
|
|
151
|
+
updated_at: Optional[datetime] = None
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
class AppCertificate(CapawesomeModel):
|
|
155
|
+
name: Optional[str] = None
|
|
156
|
+
platform: Optional[str] = None
|
|
157
|
+
type: Optional[str] = None
|
|
158
|
+
app_id: Optional[str] = None
|
|
159
|
+
created_at: Optional[datetime] = None
|
|
160
|
+
updated_at: Optional[datetime] = None
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
class AppWebhook(CapawesomeModel):
|
|
164
|
+
name: Optional[str] = None
|
|
165
|
+
url: Optional[str] = None
|
|
166
|
+
events: Optional[list[str]] = None
|
|
167
|
+
format: Optional[str] = None
|
|
168
|
+
enabled: Optional[bool] = None
|
|
169
|
+
app_id: Optional[str] = None
|
|
170
|
+
created_at: Optional[datetime] = None
|
|
171
|
+
updated_at: Optional[datetime] = None
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
class AppAutomation(CapawesomeModel):
|
|
175
|
+
name: Optional[str] = None
|
|
176
|
+
platform: Optional[str] = None
|
|
177
|
+
trigger_type: Optional[str] = None
|
|
178
|
+
enabled: Optional[bool] = None
|
|
179
|
+
app_id: Optional[str] = None
|
|
180
|
+
created_at: Optional[datetime] = None
|
|
181
|
+
updated_at: Optional[datetime] = None
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
class Job(CapawesomeModel):
|
|
185
|
+
status: Optional[str] = None
|
|
186
|
+
app_id: Optional[str] = None
|
|
187
|
+
app_build_id: Optional[str] = None
|
|
188
|
+
app_deployment_id: Optional[str] = None
|
|
189
|
+
finished_at: Optional[datetime] = None
|
|
190
|
+
created_at: Optional[datetime] = None
|