codecraftai-restclient-base 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/base/__init__.py +0 -0
- ccai/restclient/base/aspectsrest.py +122 -0
- ccai/restclient/base/awssigv4.py +364 -0
- ccai/restclient/base/itemsresult.py +274 -0
- ccai/restclient/base/pagedlist.py +158 -0
- ccai/restclient/base/py.typed +0 -0
- ccai/restclient/base/restapigroup.py +434 -0
- ccai/restclient/base/restclientbase.py +666 -0
- ccai/restclient/base/restclientgenerator.py +1595 -0
- ccai/restclient/base/restclientutils.py +66 -0
- ccai/restclient/base/schemafield.py +11 -0
- ccai/restclient/base/schemaobject.py +12 -0
- ccai/restclient/base/taskhandle.py +267 -0
- codecraftai_restclient_base-1.2.0.dist-info/METADATA +88 -0
- codecraftai_restclient_base-1.2.0.dist-info/RECORD +17 -0
- codecraftai_restclient_base-1.2.0.dist-info/WHEEL +4 -0
- codecraftai_restclient_base-1.2.0.dist-info/licenses/LICENSE.md +204 -0
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
"""
|
|
2
|
+
.. module:: itemsresult
|
|
3
|
+
:platform: Darwin, Linux, Unix, Windows
|
|
4
|
+
:synopsis: itemsresult module.
|
|
5
|
+
|
|
6
|
+
.. moduleauthor:: Myron Walker <myron.walker@gmail.com>
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
__author__ = "Myron Walker"
|
|
10
|
+
__copyright__ = "Copyright 2025, Code Craft AI LLC"
|
|
11
|
+
__credits__ = []
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
from typing import Any, Callable, Dict, Generic, List, Optional, TypeVar, Union
|
|
15
|
+
|
|
16
|
+
from requests import Response
|
|
17
|
+
|
|
18
|
+
from ccai.restclient.base.aspectsrest import AspectsRest
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
CT = TypeVar("CT")
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class PreCannedRequest(Generic[CT]):
|
|
25
|
+
"""
|
|
26
|
+
Container for REST call details used to generate an :class:`ItemsResult` later.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
def __init__(
|
|
30
|
+
self,
|
|
31
|
+
*,
|
|
32
|
+
leaf: str,
|
|
33
|
+
method: str,
|
|
34
|
+
params: Union[Dict[str, str], None],
|
|
35
|
+
body: Union[Dict[str, Any], None],
|
|
36
|
+
exp_status: Union[int, List[int]],
|
|
37
|
+
aspects: Union[AspectsRest, None],
|
|
38
|
+
perform_call: Callable,
|
|
39
|
+
package_result: Callable
|
|
40
|
+
):
|
|
41
|
+
"""
|
|
42
|
+
Stores call details for later execution.
|
|
43
|
+
|
|
44
|
+
:param leaf: API leaf path.
|
|
45
|
+
:param method: HTTP method name.
|
|
46
|
+
:param params: Query parameters for the call.
|
|
47
|
+
:param body: Body payload for the call.
|
|
48
|
+
:param exp_status: Expected HTTP status codes.
|
|
49
|
+
:param aspects: Optional call aspects.
|
|
50
|
+
:param perform_call: Callable used to perform the call.
|
|
51
|
+
:param package_result: Callable used to package the response.
|
|
52
|
+
|
|
53
|
+
:returns: None
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
self._leaf = leaf
|
|
57
|
+
self._method = method
|
|
58
|
+
self._params = params
|
|
59
|
+
self._body = body
|
|
60
|
+
self._exp_status = exp_status
|
|
61
|
+
self._aspects = aspects
|
|
62
|
+
self._perform_call = perform_call
|
|
63
|
+
self._package_result = package_result
|
|
64
|
+
return
|
|
65
|
+
|
|
66
|
+
def perform_call(self) -> "ItemsResult[CT]":
|
|
67
|
+
"""
|
|
68
|
+
Perform the pre-canned REST call stored on this instance and
|
|
69
|
+
package the result into an :class:`ItemsResult` object.
|
|
70
|
+
|
|
71
|
+
:returns: The result object created from the REST call.
|
|
72
|
+
"""
|
|
73
|
+
|
|
74
|
+
resp: Response = self._perform_call(
|
|
75
|
+
leaf=self.leaf,
|
|
76
|
+
method=self._method,
|
|
77
|
+
params=self._params,
|
|
78
|
+
body=self._body,
|
|
79
|
+
exp_status=self._exp_status,
|
|
80
|
+
aspects=self._aspects,
|
|
81
|
+
)
|
|
82
|
+
result = self._package_result(resp)
|
|
83
|
+
|
|
84
|
+
return result
|
|
85
|
+
|
|
86
|
+
@property
|
|
87
|
+
def leaf(self) -> str:
|
|
88
|
+
"""
|
|
89
|
+
API leaf path for the stored call.
|
|
90
|
+
|
|
91
|
+
:returns: Leaf path string.
|
|
92
|
+
"""
|
|
93
|
+
return self._leaf
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class Paging:
|
|
97
|
+
"""
|
|
98
|
+
The :class:`Paging` object combines a the collection of parameters that are necessary to preform paging opertions
|
|
99
|
+
on the results of a REST API call for a collection.
|
|
100
|
+
"""
|
|
101
|
+
|
|
102
|
+
def __init__(
|
|
103
|
+
self,
|
|
104
|
+
*,
|
|
105
|
+
page: int,
|
|
106
|
+
count: int,
|
|
107
|
+
total_count: int,
|
|
108
|
+
per_page: int,
|
|
109
|
+
first_page: int,
|
|
110
|
+
last_page: int,
|
|
111
|
+
request_next: Optional[PreCannedRequest] = None,
|
|
112
|
+
request_prev: Optional[PreCannedRequest] = None
|
|
113
|
+
):
|
|
114
|
+
"""
|
|
115
|
+
Construct a paging descriptor for REST collection results.
|
|
116
|
+
|
|
117
|
+
:param page: Current page number.
|
|
118
|
+
:param count: Number of items in this page.
|
|
119
|
+
:param total_count: Total items across all pages.
|
|
120
|
+
:param per_page: Maximum items per page.
|
|
121
|
+
:param first_page: First page index available.
|
|
122
|
+
:param last_page: Last page index available.
|
|
123
|
+
:param request_next: Callable request for the next page.
|
|
124
|
+
:param request_prev: Callable request for the previous page.
|
|
125
|
+
"""
|
|
126
|
+
self._page = page
|
|
127
|
+
self._count = count
|
|
128
|
+
self._total_count = total_count
|
|
129
|
+
self._per_page = per_page
|
|
130
|
+
self._first_page = first_page
|
|
131
|
+
self._last_page = last_page
|
|
132
|
+
|
|
133
|
+
self._request_next = request_next
|
|
134
|
+
self._request_prev = request_prev
|
|
135
|
+
return
|
|
136
|
+
|
|
137
|
+
@property
|
|
138
|
+
def count(self) -> int:
|
|
139
|
+
"""
|
|
140
|
+
Number of items returned in the current result.
|
|
141
|
+
|
|
142
|
+
:returns: Count of items in this page.
|
|
143
|
+
"""
|
|
144
|
+
return self._count
|
|
145
|
+
|
|
146
|
+
@property
|
|
147
|
+
def first_page(self) -> int:
|
|
148
|
+
"""
|
|
149
|
+
First page number available in the result set.
|
|
150
|
+
|
|
151
|
+
:returns: Integer index of the first page.
|
|
152
|
+
"""
|
|
153
|
+
return self._first_page
|
|
154
|
+
|
|
155
|
+
@property
|
|
156
|
+
def last_page(self) -> int:
|
|
157
|
+
"""
|
|
158
|
+
Last page number available in the result set.
|
|
159
|
+
|
|
160
|
+
:returns: Integer index of the last page.
|
|
161
|
+
"""
|
|
162
|
+
return self._last_page
|
|
163
|
+
|
|
164
|
+
@property
|
|
165
|
+
def page(self) -> int:
|
|
166
|
+
"""
|
|
167
|
+
Current page number of the results.
|
|
168
|
+
|
|
169
|
+
:returns: Page index beginning at ``1``.
|
|
170
|
+
"""
|
|
171
|
+
return self._page
|
|
172
|
+
|
|
173
|
+
@property
|
|
174
|
+
def per_page(self) -> int:
|
|
175
|
+
"""
|
|
176
|
+
Maximum number of items returned per page.
|
|
177
|
+
|
|
178
|
+
:returns: Items per page value.
|
|
179
|
+
"""
|
|
180
|
+
return self._per_page
|
|
181
|
+
|
|
182
|
+
@property
|
|
183
|
+
def request_next(self) -> Union[PreCannedRequest, None]:
|
|
184
|
+
"""
|
|
185
|
+
Stored callable to retrieve the next page of results.
|
|
186
|
+
|
|
187
|
+
:returns: A :class:`PreCannedRequest` for the next page or ``None``.
|
|
188
|
+
"""
|
|
189
|
+
return self._request_next
|
|
190
|
+
|
|
191
|
+
@property
|
|
192
|
+
def request_prev(self) -> Union[PreCannedRequest, None]:
|
|
193
|
+
"""
|
|
194
|
+
Stored callable to retrieve the previous page of results.
|
|
195
|
+
|
|
196
|
+
:returns: A :class:`PreCannedRequest` for the previous page or ``None``.
|
|
197
|
+
"""
|
|
198
|
+
return self._request_prev
|
|
199
|
+
|
|
200
|
+
@property
|
|
201
|
+
def total_count(self) -> int:
|
|
202
|
+
"""
|
|
203
|
+
Total number of items available across all pages.
|
|
204
|
+
|
|
205
|
+
:returns: Overall item count.
|
|
206
|
+
"""
|
|
207
|
+
return self._total_count
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
class ItemsResult(Generic[CT]):
|
|
211
|
+
"""
|
|
212
|
+
Holds a list of returned items and optional paging information.
|
|
213
|
+
"""
|
|
214
|
+
|
|
215
|
+
def __init__(self, items: List[CT], paging: Optional[Paging] = None):
|
|
216
|
+
"""
|
|
217
|
+
Container object for paged or unpaged collections.
|
|
218
|
+
"""
|
|
219
|
+
self._items = items
|
|
220
|
+
self._paging = paging
|
|
221
|
+
return
|
|
222
|
+
|
|
223
|
+
@property
|
|
224
|
+
def is_paged(self):
|
|
225
|
+
"""
|
|
226
|
+
Returns a value indicating if this items result is paged.
|
|
227
|
+
|
|
228
|
+
:returns: ``True`` if paging information is present.
|
|
229
|
+
"""
|
|
230
|
+
rtnval = True if self._paging is not None else False
|
|
231
|
+
return rtnval
|
|
232
|
+
|
|
233
|
+
@property
|
|
234
|
+
def items(self) -> List[CT]:
|
|
235
|
+
"""
|
|
236
|
+
Returns a list of items. This could be a complete list of items or the items from a page depending
|
|
237
|
+
on the results of the API call.
|
|
238
|
+
|
|
239
|
+
:returns: The list of items for this result.
|
|
240
|
+
"""
|
|
241
|
+
return self._items
|
|
242
|
+
|
|
243
|
+
@property
|
|
244
|
+
def paging(self) -> Union[Paging, None]:
|
|
245
|
+
"""
|
|
246
|
+
Returns the paging details for this result.
|
|
247
|
+
|
|
248
|
+
:returns: A :class:`Paging` object or ``None`` if the result is unpaged.
|
|
249
|
+
"""
|
|
250
|
+
return self._paging
|
|
251
|
+
|
|
252
|
+
def next_page_result(self) -> "ItemsResult[CT]":
|
|
253
|
+
"""
|
|
254
|
+
Retrieve the next page of results if there is one.
|
|
255
|
+
"""
|
|
256
|
+
|
|
257
|
+
result = None
|
|
258
|
+
|
|
259
|
+
if self._paging is not None and self._paging.request_next is not None:
|
|
260
|
+
result = self._paging.request_next.perform_call()
|
|
261
|
+
|
|
262
|
+
return result
|
|
263
|
+
|
|
264
|
+
def prev_page_result(self) -> "ItemsResult[CT]":
|
|
265
|
+
"""
|
|
266
|
+
Retrieve the next page of results if there is one.
|
|
267
|
+
"""
|
|
268
|
+
|
|
269
|
+
result = None
|
|
270
|
+
|
|
271
|
+
if self._paging is not None and self._paging.request_prev is not None:
|
|
272
|
+
result = self._paging.request_prev.perform_call()
|
|
273
|
+
|
|
274
|
+
return result
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"""
|
|
2
|
+
.. module:: pagedlist
|
|
3
|
+
:platform: Darwin, Linux, Unix, Windows
|
|
4
|
+
:synopsis: Container for paginated list responses with navigation support.
|
|
5
|
+
|
|
6
|
+
.. moduleauthor:: Myron Walker <myron.walker@gmail.com>
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from typing import Any, Dict, Generic, List, Optional, TypeVar
|
|
12
|
+
from dataclasses import dataclass
|
|
13
|
+
import re
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
T = TypeVar('T')
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass
|
|
20
|
+
class LinkInfo:
|
|
21
|
+
"""
|
|
22
|
+
Parsed pagination link information from Link header.
|
|
23
|
+
"""
|
|
24
|
+
url: str
|
|
25
|
+
rel: str
|
|
26
|
+
page: Optional[int] = None
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class PagedList(Generic[T]):
|
|
30
|
+
"""
|
|
31
|
+
Container for paginated list responses.
|
|
32
|
+
|
|
33
|
+
Provides typed access to list items plus pagination metadata
|
|
34
|
+
from the Link header and response data.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
def __init__(self, items: List[T], headers: Dict[str, Any], response_data: Any = None):
|
|
38
|
+
"""
|
|
39
|
+
Initialize a PagedList.
|
|
40
|
+
|
|
41
|
+
:param items: The list of typed items
|
|
42
|
+
:param headers: HTTP response headers (for Link header)
|
|
43
|
+
:param response_data: Raw response data for extracting pagination hints
|
|
44
|
+
"""
|
|
45
|
+
self._items = items
|
|
46
|
+
self._headers = headers
|
|
47
|
+
self._response_data = response_data
|
|
48
|
+
self._links: Optional[Dict[str, LinkInfo]] = None
|
|
49
|
+
return
|
|
50
|
+
|
|
51
|
+
@property
|
|
52
|
+
def items(self) -> List[T]:
|
|
53
|
+
"""
|
|
54
|
+
Get the list of typed items.
|
|
55
|
+
"""
|
|
56
|
+
return self._items
|
|
57
|
+
|
|
58
|
+
def __iter__(self):
|
|
59
|
+
"""
|
|
60
|
+
Allow iteration over items directly.
|
|
61
|
+
"""
|
|
62
|
+
return iter(self._items)
|
|
63
|
+
|
|
64
|
+
def __len__(self) -> int:
|
|
65
|
+
"""
|
|
66
|
+
Get the count of items in this page.
|
|
67
|
+
"""
|
|
68
|
+
return len(self._items)
|
|
69
|
+
|
|
70
|
+
def __getitem__(self, index):
|
|
71
|
+
"""
|
|
72
|
+
Allow indexing into items.
|
|
73
|
+
"""
|
|
74
|
+
return self._items[index]
|
|
75
|
+
|
|
76
|
+
@property
|
|
77
|
+
def links(self) -> Dict[str, LinkInfo]:
|
|
78
|
+
"""
|
|
79
|
+
Get pagination links from Link header.
|
|
80
|
+
"""
|
|
81
|
+
if self._links is None:
|
|
82
|
+
self._links = self._parse_link_header()
|
|
83
|
+
return self._links
|
|
84
|
+
|
|
85
|
+
@property
|
|
86
|
+
def has_next(self) -> bool:
|
|
87
|
+
"""
|
|
88
|
+
Returns True if there is a next page.
|
|
89
|
+
"""
|
|
90
|
+
return 'next' in self.links
|
|
91
|
+
|
|
92
|
+
@property
|
|
93
|
+
def has_prev(self) -> bool:
|
|
94
|
+
"""
|
|
95
|
+
Returns True if there is a previous page.
|
|
96
|
+
"""
|
|
97
|
+
return 'prev' in self.links
|
|
98
|
+
|
|
99
|
+
@property
|
|
100
|
+
def next_page(self) -> Optional[int]:
|
|
101
|
+
"""
|
|
102
|
+
Get the next page number if available.
|
|
103
|
+
"""
|
|
104
|
+
next_link = self.links.get('next')
|
|
105
|
+
return next_link.page if next_link else None
|
|
106
|
+
|
|
107
|
+
@property
|
|
108
|
+
def prev_page(self) -> Optional[int]:
|
|
109
|
+
"""
|
|
110
|
+
Get the previous page number if available.
|
|
111
|
+
"""
|
|
112
|
+
prev_link = self.links.get('prev')
|
|
113
|
+
return prev_link.page if prev_link else None
|
|
114
|
+
|
|
115
|
+
@property
|
|
116
|
+
def first_page(self) -> Optional[int]:
|
|
117
|
+
"""
|
|
118
|
+
Get the first page number if available.
|
|
119
|
+
"""
|
|
120
|
+
first_link = self.links.get('first')
|
|
121
|
+
return first_link.page if first_link else None
|
|
122
|
+
|
|
123
|
+
@property
|
|
124
|
+
def last_page(self) -> Optional[int]:
|
|
125
|
+
"""
|
|
126
|
+
Get the last page number if available.
|
|
127
|
+
"""
|
|
128
|
+
last_link = self.links.get('last')
|
|
129
|
+
return last_link.page if last_link else None
|
|
130
|
+
|
|
131
|
+
def _parse_link_header(self) -> Dict[str, LinkInfo]:
|
|
132
|
+
"""
|
|
133
|
+
Parse the Link header for pagination information.
|
|
134
|
+
|
|
135
|
+
:returns: Dictionary mapping rel types (next, prev, first, last) to LinkInfo
|
|
136
|
+
"""
|
|
137
|
+
link_header = self._headers.get('Link', self._headers.get('link', ''))
|
|
138
|
+
if not link_header:
|
|
139
|
+
return {}
|
|
140
|
+
|
|
141
|
+
links: Dict[str, LinkInfo] = {}
|
|
142
|
+
|
|
143
|
+
# Link header format: <url>; rel="next", <url>; rel="prev"
|
|
144
|
+
parts = link_header.split(',')
|
|
145
|
+
for part in parts:
|
|
146
|
+
part = part.strip()
|
|
147
|
+
match = re.match(r'<([^>]+)>;\s*rel="([^"]+)"', part)
|
|
148
|
+
if match:
|
|
149
|
+
url = match.group(1)
|
|
150
|
+
rel = match.group(2)
|
|
151
|
+
|
|
152
|
+
# Try to extract page number from URL
|
|
153
|
+
page_match = re.search(r'[?&]page=(\d+)', url)
|
|
154
|
+
page = int(page_match.group(1)) if page_match else None
|
|
155
|
+
|
|
156
|
+
links[rel] = LinkInfo(url=url, rel=rel, page=page)
|
|
157
|
+
|
|
158
|
+
return links
|
|
File without changes
|