codecraftai-restclient-aws 1.2.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.
- ccai/restclient/aws/__init__.py +11 -0
- ccai/restclient/aws/awserror.py +51 -0
- ccai/restclient/aws/awstransport.py +287 -0
- ccai/restclient/aws/querycodec.py +378 -0
- ccai/restclient/aws/serviceclients.py +632 -0
- codecraftai_restclient_aws-1.2.0.dist-info/METADATA +71 -0
- codecraftai_restclient_aws-1.2.0.dist-info/RECORD +9 -0
- codecraftai_restclient_aws-1.2.0.dist-info/WHEEL +4 -0
- codecraftai_restclient_aws-1.2.0.dist-info/licenses/LICENSE.md +204 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"""
|
|
2
|
+
.. package:: ccai.restclient.aws
|
|
3
|
+
:platform: Darwin, Linux, Unix, Windows
|
|
4
|
+
:synopsis: A pure-Python client for the AWS APIs.
|
|
5
|
+
|
|
6
|
+
.. moduleauthor:: Myron Walker <myron.walker@gmail.com>
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
__author__ = "Myron Walker"
|
|
10
|
+
__copyright__ = "Copyright 2026, Code Craft AI LLC"
|
|
11
|
+
__credits__ = []
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""
|
|
2
|
+
.. module:: awserror
|
|
3
|
+
:platform: Darwin, Linux, Unix, Windows
|
|
4
|
+
:synopsis: Errors raised for non-success AWS responses.
|
|
5
|
+
|
|
6
|
+
:class:`ClientError` keeps the name and the ``response`` shape botocore
|
|
7
|
+
used, because consumer code catches it and reads
|
|
8
|
+
``xcpt.response["Error"]["Code"]`` to decide whether a resource is merely
|
|
9
|
+
absent — a 404 from ``head_bucket`` means "create it", while a 403 means
|
|
10
|
+
"stop". Keeping the shape lets those handlers keep working.
|
|
11
|
+
|
|
12
|
+
.. moduleauthor:: Myron Walker <myron.walker@gmail.com>
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
__author__ = "Myron Walker"
|
|
16
|
+
__copyright__ = "Copyright 2026, Code Craft AI LLC"
|
|
17
|
+
__credits__ = []
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
from typing import Any, Dict, Optional
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class ClientError(Exception):
|
|
24
|
+
"""
|
|
25
|
+
Raised when an AWS service returns a non-success status.
|
|
26
|
+
|
|
27
|
+
:ivar response: The botocore-shaped error envelope, carrying
|
|
28
|
+
``Error.Code``, ``Error.Message`` and
|
|
29
|
+
``ResponseMetadata.HTTPStatusCode``.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
def __init__(self, message: str, *, code: str = "", status: int = 0,
|
|
33
|
+
detail: str = "", body: Optional[str] = None) -> None:
|
|
34
|
+
"""
|
|
35
|
+
Construct a ClientError.
|
|
36
|
+
|
|
37
|
+
:param message: The human-readable error message.
|
|
38
|
+
:param code: The AWS error code, e.g. ``NoSuchBucket``.
|
|
39
|
+
:param status: The HTTP status code.
|
|
40
|
+
:param detail: The AWS error message.
|
|
41
|
+
:param body: The raw response body.
|
|
42
|
+
"""
|
|
43
|
+
super().__init__(message)
|
|
44
|
+
self.response: Dict[str, Any] = {
|
|
45
|
+
"Error": {"Code": code, "Message": detail},
|
|
46
|
+
"ResponseMetadata": {"HTTPStatusCode": status},
|
|
47
|
+
}
|
|
48
|
+
self.code: str = code
|
|
49
|
+
self.status: int = status
|
|
50
|
+
self.body: Optional[str] = body
|
|
51
|
+
return
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
"""
|
|
2
|
+
.. module:: awstransport
|
|
3
|
+
:platform: Darwin, Linux, Unix, Windows
|
|
4
|
+
:synopsis: :class:`AwsTransport` — the SigV4-signed transport.
|
|
5
|
+
|
|
6
|
+
AWS speaks four different protocols across the services this package
|
|
7
|
+
covers, and they differ in more than encoding:
|
|
8
|
+
|
|
9
|
+
* EC2 uses the **Query** protocol — form-encoded requests, XML responses
|
|
10
|
+
whose element names differ from the modelled ones.
|
|
11
|
+
* S3 and Route53 use **REST-XML** — the operation is the HTTP method and
|
|
12
|
+
path, and responses are XML that already carries modelled names.
|
|
13
|
+
* FSx and SSM use **JSON 1.1** — the operation is named in an
|
|
14
|
+
``X-Amz-Target`` header and both directions are JSON.
|
|
15
|
+
|
|
16
|
+
What they share is SigV4, so this transport owns signing and each client
|
|
17
|
+
owns its protocol.
|
|
18
|
+
|
|
19
|
+
.. moduleauthor:: Myron Walker <myron.walker@gmail.com>
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
__author__ = "Myron Walker"
|
|
23
|
+
__copyright__ = "Copyright 2026, Code Craft AI LLC"
|
|
24
|
+
__credits__ = []
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
import os
|
|
28
|
+
|
|
29
|
+
from typing import Any, Dict, Optional
|
|
30
|
+
|
|
31
|
+
import requests
|
|
32
|
+
|
|
33
|
+
from ccai.restclient.base.awssigv4 import SigV4Signer
|
|
34
|
+
|
|
35
|
+
from ccai.restclient.aws.awserror import ClientError
|
|
36
|
+
from ccai.restclient.aws.querycodec import decode_response, strip_namespace
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
DEFAULT_TIMEOUT_SECONDS = 60.0
|
|
40
|
+
|
|
41
|
+
# S3 addresses its single global endpoint by this region name when signing,
|
|
42
|
+
# regardless of where a bucket lives.
|
|
43
|
+
S3_GLOBAL_REGION = "us-east-1"
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def endpoint_for(*, service: str, region: str) -> str:
|
|
47
|
+
"""
|
|
48
|
+
Build a service endpoint.
|
|
49
|
+
|
|
50
|
+
Route53 and IAM are global services with a single endpoint; giving
|
|
51
|
+
them a regional hostname would fail to resolve.
|
|
52
|
+
|
|
53
|
+
:param service: The service name.
|
|
54
|
+
:param region: The region name.
|
|
55
|
+
|
|
56
|
+
:returns: The endpoint URL.
|
|
57
|
+
"""
|
|
58
|
+
if service == "route53":
|
|
59
|
+
rtnval = "https://route53.amazonaws.com"
|
|
60
|
+
return rtnval
|
|
61
|
+
|
|
62
|
+
rtnval = "https://{}.{}.amazonaws.com".format(service, region)
|
|
63
|
+
return rtnval
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def signing_region_for(*, service: str, region: str) -> str:
|
|
67
|
+
"""
|
|
68
|
+
Return the region a service's signature is scoped to.
|
|
69
|
+
|
|
70
|
+
:param service: The service name.
|
|
71
|
+
:param region: The caller's region.
|
|
72
|
+
|
|
73
|
+
:returns: The signing region.
|
|
74
|
+
"""
|
|
75
|
+
if service == "route53":
|
|
76
|
+
# A global endpoint still signs against us-east-1.
|
|
77
|
+
rtnval = S3_GLOBAL_REGION
|
|
78
|
+
return rtnval
|
|
79
|
+
|
|
80
|
+
rtnval = region
|
|
81
|
+
return rtnval
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
class AwsTransport:
|
|
85
|
+
"""
|
|
86
|
+
SigV4-signed transport shared by the service clients.
|
|
87
|
+
"""
|
|
88
|
+
|
|
89
|
+
def __init__(self, *, access_key: str, secret_key: str, region: str,
|
|
90
|
+
session_token: Optional[str] = None,
|
|
91
|
+
session: Optional[Any] = None,
|
|
92
|
+
timeout_seconds: float = DEFAULT_TIMEOUT_SECONDS) -> None:
|
|
93
|
+
"""
|
|
94
|
+
Construct an AwsTransport.
|
|
95
|
+
|
|
96
|
+
:param access_key: The access key id.
|
|
97
|
+
:param secret_key: The secret access key.
|
|
98
|
+
:param region: The default region.
|
|
99
|
+
:param session_token: Optional session token, for temporary
|
|
100
|
+
credentials.
|
|
101
|
+
:param session: Optional pre-built ``requests.Session`` — the test
|
|
102
|
+
seam.
|
|
103
|
+
:param timeout_seconds: The default per-request timeout.
|
|
104
|
+
"""
|
|
105
|
+
self._access_key = access_key
|
|
106
|
+
self._secret_key = secret_key
|
|
107
|
+
self._region = region
|
|
108
|
+
self._session_token = session_token
|
|
109
|
+
self._timeout_seconds = timeout_seconds
|
|
110
|
+
|
|
111
|
+
if session is not None:
|
|
112
|
+
self._session = session
|
|
113
|
+
else:
|
|
114
|
+
self._session = requests.Session()
|
|
115
|
+
return
|
|
116
|
+
|
|
117
|
+
@property
|
|
118
|
+
def region(self) -> str:
|
|
119
|
+
"""
|
|
120
|
+
Return the default region.
|
|
121
|
+
|
|
122
|
+
:returns: The region name.
|
|
123
|
+
"""
|
|
124
|
+
rtnval = self._region
|
|
125
|
+
return rtnval
|
|
126
|
+
|
|
127
|
+
def signer_for(self, *, service: str) -> SigV4Signer:
|
|
128
|
+
"""
|
|
129
|
+
Build a signer scoped to one service.
|
|
130
|
+
|
|
131
|
+
:param service: The service name.
|
|
132
|
+
|
|
133
|
+
:returns: The :class:`SigV4Signer`.
|
|
134
|
+
"""
|
|
135
|
+
rtnval = SigV4Signer(
|
|
136
|
+
access_key=self._access_key, secret_key=self._secret_key,
|
|
137
|
+
region=signing_region_for(service=service, region=self._region),
|
|
138
|
+
service=service, session_token=self._session_token)
|
|
139
|
+
return rtnval
|
|
140
|
+
|
|
141
|
+
def send(self, *, service: str, method: str, url: str,
|
|
142
|
+
params: Optional[Dict[str, Any]] = None,
|
|
143
|
+
body: Optional[bytes] = None,
|
|
144
|
+
headers: Optional[Dict[str, str]] = None,
|
|
145
|
+
timeout_seconds: Optional[float] = None) -> Any:
|
|
146
|
+
"""
|
|
147
|
+
Sign and send one request.
|
|
148
|
+
|
|
149
|
+
:param service: The service name, which scopes the signature.
|
|
150
|
+
:param method: The HTTP method.
|
|
151
|
+
:param url: The absolute URL.
|
|
152
|
+
:param params: Query parameters, which are signed.
|
|
153
|
+
:param body: The request body.
|
|
154
|
+
:param headers: Any caller headers, which are signed.
|
|
155
|
+
:param timeout_seconds: Optional per-request timeout override.
|
|
156
|
+
|
|
157
|
+
:returns: The raw response.
|
|
158
|
+
"""
|
|
159
|
+
signer = self.signer_for(service=service)
|
|
160
|
+
|
|
161
|
+
signed = signer.sign(method=method, url=url, params=params,
|
|
162
|
+
headers=headers, body=body)
|
|
163
|
+
|
|
164
|
+
timeout = timeout_seconds
|
|
165
|
+
if timeout is None:
|
|
166
|
+
timeout = self._timeout_seconds
|
|
167
|
+
|
|
168
|
+
rtnval = self._session.request(method, url, params=params or {},
|
|
169
|
+
data=body, headers=signed,
|
|
170
|
+
timeout=timeout)
|
|
171
|
+
return rtnval
|
|
172
|
+
|
|
173
|
+
def raise_for_status(self, *, response: Any, method: str,
|
|
174
|
+
url: str) -> None:
|
|
175
|
+
"""
|
|
176
|
+
Raise a botocore-shaped error for a non-success response.
|
|
177
|
+
|
|
178
|
+
:param response: The response.
|
|
179
|
+
:param method: The HTTP method, for the message.
|
|
180
|
+
:param url: The URL, for the message.
|
|
181
|
+
|
|
182
|
+
:returns: ``None``
|
|
183
|
+
|
|
184
|
+
:raises: :class:`ClientError` on a non-success status.
|
|
185
|
+
"""
|
|
186
|
+
status = getattr(response, "status_code", 0)
|
|
187
|
+
if 200 <= status < 300:
|
|
188
|
+
return
|
|
189
|
+
|
|
190
|
+
text = getattr(response, "text", "")
|
|
191
|
+
code, detail = self._error_of(response=response, status=status)
|
|
192
|
+
|
|
193
|
+
err_msg_lines = ["The AWS request failed."]
|
|
194
|
+
err_msg_lines.append("METHOD: {}".format(method))
|
|
195
|
+
err_msg_lines.append("URL: {}".format(url))
|
|
196
|
+
err_msg_lines.append("STATUS: {}".format(status))
|
|
197
|
+
err_msg_lines.append("CODE: {}".format(code))
|
|
198
|
+
err_msg_lines.append("MESSAGE: {}".format(detail))
|
|
199
|
+
err_msg = os.linesep.join(err_msg_lines)
|
|
200
|
+
|
|
201
|
+
raise ClientError(err_msg, code=code, status=status, detail=detail,
|
|
202
|
+
body=text)
|
|
203
|
+
|
|
204
|
+
def _error_of(self, *, response: Any, status: int) -> Any:
|
|
205
|
+
"""
|
|
206
|
+
Pull the AWS error code and message out of a failed response.
|
|
207
|
+
|
|
208
|
+
The three protocols report errors differently: XML services nest
|
|
209
|
+
a ``<Error><Code>``, JSON services use ``__type``, and a failed
|
|
210
|
+
``HEAD`` has no body at all — in which case the status stands in,
|
|
211
|
+
because callers key on the code to tell "absent" from "denied".
|
|
212
|
+
|
|
213
|
+
:param response: The failed response.
|
|
214
|
+
:param status: The HTTP status code.
|
|
215
|
+
|
|
216
|
+
:returns: A ``(code, message)`` pair.
|
|
217
|
+
"""
|
|
218
|
+
text = getattr(response, "text", "")
|
|
219
|
+
|
|
220
|
+
if text is not None and len(text.strip()) > 0:
|
|
221
|
+
try:
|
|
222
|
+
decoded = response.json()
|
|
223
|
+
if isinstance(decoded, dict) is True:
|
|
224
|
+
typed = decoded.get("__type", None)
|
|
225
|
+
message = decoded.get("message", None)
|
|
226
|
+
if message is None:
|
|
227
|
+
message = decoded.get("Message", "")
|
|
228
|
+
if typed is not None:
|
|
229
|
+
# JSON services qualify the type with a namespace.
|
|
230
|
+
rtnval = (str(typed).split("#")[-1], str(message))
|
|
231
|
+
return rtnval
|
|
232
|
+
except Exception:
|
|
233
|
+
# Probing for a JSON error body; a body that does not decode as
|
|
234
|
+
# JSON is an XML (or empty) error body and is handled below.
|
|
235
|
+
pass
|
|
236
|
+
|
|
237
|
+
found = self._error_from_xml(text=text)
|
|
238
|
+
if found is not None:
|
|
239
|
+
rtnval = found
|
|
240
|
+
return rtnval
|
|
241
|
+
|
|
242
|
+
rtnval = (str(status), getattr(response, "reason", ""))
|
|
243
|
+
return rtnval
|
|
244
|
+
|
|
245
|
+
def _error_from_xml(self, *, text: str) -> Any:
|
|
246
|
+
"""
|
|
247
|
+
Read an ``<Error><Code>`` pair out of an XML body.
|
|
248
|
+
|
|
249
|
+
:param text: The body text.
|
|
250
|
+
|
|
251
|
+
:returns: A ``(code, message)`` pair, or None.
|
|
252
|
+
"""
|
|
253
|
+
import xml.etree.ElementTree as ElementTree
|
|
254
|
+
|
|
255
|
+
try:
|
|
256
|
+
root = ElementTree.fromstring(text)
|
|
257
|
+
except ElementTree.ParseError:
|
|
258
|
+
rtnval = None
|
|
259
|
+
return rtnval
|
|
260
|
+
|
|
261
|
+
code = ""
|
|
262
|
+
message = ""
|
|
263
|
+
for element in root.iter():
|
|
264
|
+
name = strip_namespace(element.tag)
|
|
265
|
+
if name == "Code" and len(code) == 0:
|
|
266
|
+
code = (element.text or "").strip()
|
|
267
|
+
elif name == "Message" and len(message) == 0:
|
|
268
|
+
message = (element.text or "").strip()
|
|
269
|
+
|
|
270
|
+
if len(code) == 0:
|
|
271
|
+
rtnval = None
|
|
272
|
+
return rtnval
|
|
273
|
+
|
|
274
|
+
rtnval = (code, message)
|
|
275
|
+
return rtnval
|
|
276
|
+
|
|
277
|
+
def decode_xml(self, *, response: Any) -> Dict[str, Any]:
|
|
278
|
+
"""
|
|
279
|
+
Decode an XML response body into modelled data.
|
|
280
|
+
|
|
281
|
+
:param response: The response.
|
|
282
|
+
|
|
283
|
+
:returns: The decoded body.
|
|
284
|
+
"""
|
|
285
|
+
text = getattr(response, "text", "")
|
|
286
|
+
rtnval = decode_response(text)
|
|
287
|
+
return rtnval
|