-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathresourceset.py
More file actions
527 lines (426 loc) · 14.1 KB
/
Copy pathresourceset.py
File metadata and controls
527 lines (426 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#
# This file is part of the Ingram Micro CloudBlue Connect Python OpenAPI Client.
#
# Copyright (c) 2025 CloudBlue. All Rights Reserved.
#
import copy
from connect.client.models.exceptions import NotYetEvaluatedError
from connect.client.models.iterators import (
AsyncResourceIterator,
AsyncValuesListIterator,
ResourceIterator,
ValuesListIterator,
aiter,
)
from connect.client.rql import R
from connect.client.utils import parse_content_range, resolve_attribute
class _ResourceSetBase:
def __init__(
self,
client,
path,
query=None,
):
self._client = client
self._path = path
self._query = query or R()
self._results = None
self._limit = self._client.default_limit or 100
self._offset = 0
self._slice = None
self._content_range = None
self._fields = None
self._search = None
self._select = []
self._ordering = []
self._config = {}
@property
def path(self):
return self._path
@property
def query(self):
return self._query
@property
def content_range(self):
return self._content_range
def configure(self, **kwargs):
"""
Set the default keyword arguments that must be provided to the
underlying GET call on each page fetch.
"""
copy = self._copy()
copy._config = kwargs or {}
return copy
def limit(self, limit: int):
"""
Set the number of results that must be fetched on each
HTTP call.
Args:
limit (int): Number of results to fetch in each HTTP call.
Returns:
(ResourceSet): Returns a copy of the current ResourceSet with the limit applied.
"""
if not isinstance(limit, int):
raise TypeError('`limit` must be an integer.')
if limit <= 0:
raise ValueError('`limit` must be a positive, non-zero integer.')
copy = self._copy()
copy._limit = limit
return copy
def order_by(self, *fields):
"""
Add fields for ordering.
Usage:
```py3
purchases = client.requests.all().order_by(
'asset.tiers.customer.name',
'-created'
)
```
!!! note
To sort results in descending order the name of the field must
be prefixed with a `-` (minus) sign.
Returns:
(ResourceSet): Returns a copy of the current ResourceSet with the order applied.
"""
copy = self._copy()
copy._ordering.extend(fields)
return copy
def select(self, *fields):
"""
Apply the RQL ``select`` operator to
this ResourceSet object.
Usage:
```py3
purchases = client.requests.all().select(
'-asset.items',
'-asset.params',
'activation_key',
)
```
!!! note
To unselect a field it must
be prefixed with a `-` (minus) sign.
Returns:
(ResourceSet): Returns a copy of the current ResourceSet with the select applied.
"""
copy = self._copy()
copy._select.extend(fields)
return copy
def filter(self, *args, **kwargs):
"""
Applies filters to this ResourceSet object.
Arguments can be RQL filter expressions as strings
or R objects.
Usage:
```py3
rs = rs.filter('eq(field,value)', 'eq(another.field,value2)')
rs = rs.filter(R().field.eq('value'), R().another.field.eq('value2'))
```
All the arguments will be combined with logical `and`.
Filters can be also specified as keyword argument using the `__` (double underscore)
notation.
```py3
rs = rs.filter(
field=value,
another__field=value,
field2__in=('a', 'b'),
field3__null=True,
)
```
Also keyword arguments will be combined with logical `and`.
Returns:
(ResourceSet): Returns a copy of the current ResourceSet with the filter applied.
"""
copy = self._copy()
for arg in args:
if isinstance(arg, str):
copy._query &= R(_expr=arg)
continue
if isinstance(arg, R):
copy._query &= arg
continue
raise TypeError(f'arguments must be string or R not {type(arg)}')
if kwargs:
copy._query &= R(**kwargs)
return copy
def all(self):
"""
Returns a copy of the current ResourceSet.
Returns:
(ResourceSet): Returns a copy of the current ResourceSet.
"""
return self._copy()
def search(self, term: str):
"""
Create a copy of the current ResourceSet applying the `search` RQL
operator equal to `term`.
Args:
term (str): The term to search for.
Returns:
(ResourceSet): Create a copy of the current ResourceSet applying the `search` RQL
operator equal to `term`.
"""
copy = self._copy()
copy._search = term
return copy
def values_list(self, *fields):
"""
Returns a flat dictionary containing only the fields passed as arguments
for each resource that belongs to this ResourceSet.
Nested field can be specified using dot notation.
Usage:
```py3
values = rs.values_list('field', 'nested.field')
```
"""
if self._results:
self._fields = fields
return [self._get_values(item) for item in self._results]
copy = self._copy()
copy._fields = fields
return copy
def _get_values(self, item):
return {field: resolve_attribute(field, item) for field in self._fields}
def _build_qs(self):
qs = ''
if self._select:
qs += f'&select({",".join(self._select)})'
if self._query:
qs += f'&{str(self._query)}'
if self._ordering:
qs += f'&ordering({",".join(self._ordering)})'
return qs[1:] if qs else ''
def _get_request_url(self):
url = f'{self._path}'
qs = self._build_qs()
if qs:
url = f'{url}?{qs}'
return url
def _get_request_kwargs(self):
config = copy.deepcopy(self._config)
config.setdefault('params', {})
config['params'].update(
{
'limit': self._limit,
'offset': self._offset,
},
)
if self._search:
config['params']['search'] = self._search
return config
def _copy(self):
rs = self.__class__(self._client, self._path, self._query)
rs._limit = self._limit
rs._offset = self._offset
rs._slice = self._slice
rs._fields = self._fields
rs._search = self._search
rs._select = copy.copy(self._select)
rs._ordering = copy.copy(self._ordering)
rs._config = copy.deepcopy(self._config)
return rs
def _validate_key(self, key):
if not isinstance(key, (int, slice)):
raise TypeError('ResourceSet indices must be integers or slices.')
if isinstance(key, slice) and (key.start is None or key.stop is None):
raise ValueError('Both start and stop indexes must be specified.')
if (not isinstance(key, slice) and (key < 0)) or (
isinstance(key, slice) and (key.start < 0 or key.stop < 0)
):
raise ValueError('Negative indexing is not supported.')
if isinstance(key, slice) and not (key.step is None or key.step == 0):
raise ValueError('Indexing with step is not supported.')
def help(self):
self._client.print_help(self)
return self
class ResourceSet(_ResourceSetBase):
"""
Represent a set of resources.
Usage:
```py3
for product in client.products.all().filter(
R().status.eq('published')
).order_by('created'):
...
```
"""
def __iter__(self):
if self._results is None:
return self._iterator()
return iter(self._results)
def __bool__(self):
if self._results is not None:
return bool(self._results)
copy = self._copy()
copy._fetch_all()
return bool(copy._results)
def __getitem__(self, key): # noqa: CCR001
self._validate_key(key)
if self._results is not None:
return self._results[key]
if isinstance(key, int):
copy = self._copy()
copy._limit = 1
copy._offset = key
copy._fetch_all()
return copy._results[0] if copy._results else None
copy = self._copy()
copy._offset = key.start
copy._slice = key
if copy._slice.stop - copy._slice.start < copy._limit:
copy._limit = copy._slice.stop - copy._slice.start
return copy
def count(self) -> int:
"""
Returns the total number of resources within this ResourceSet object.
Usage:
```py3
no_of_products = client.products.all().count()
```
Returns:
(int): Returns the total number of resources within this ResourceSet object.
"""
if not self._content_range:
copy = self._copy()
url = copy._get_request_url()
kwargs = copy._get_request_kwargs()
kwargs['params']['limit'] = 0
copy._execute_request(url, kwargs)
return copy._content_range.count
return self._content_range.count
def first(self):
"""
Returns the first resource that belongs to this ResourceSet object
or None if the ResourceSet doesn't contains resources.
Usage:
```py3
latest_news = client.news.all().order_by('-updated').first()
```
Returns:
(Resource): Returns the first resource that belongs to this ResourceSet object
or None.
"""
copy = self._copy()
copy._limit = 1
copy._offset = 0
copy._fetch_all()
return copy._results[0] if copy._results else None
def _iterator(self):
args = (
self,
self._client,
self._path,
self._build_qs(),
self._get_request_kwargs(),
)
iterator = (
ValuesListIterator(*args, fields=self._fields)
if self._fields
else ResourceIterator(*args)
)
return iterator
def _execute_request(self, url, kwargs):
results = self._client.get(url, **kwargs)
self._content_range = parse_content_range(
self._client.response.headers.get('Content-Range'),
)
return results
def _fetch_all(self):
if self._results is None:
self._results = self._execute_request(
self._get_request_url(),
self._get_request_kwargs(),
)
class AsyncResourceSet(_ResourceSetBase):
"""
Represent a set of resources.
Usage:
```py3
async for product in (
client.products.all().filter(
R().status.eq('published')
).order_by('created')
):
...
```
"""
def __aiter__(self):
if self._results is None:
return self._iterator()
return aiter(self._results)
def __bool__(self):
if self._results is None:
raise NotYetEvaluatedError()
return bool(self._results)
def __getitem__(self, key): # noqa: CCR001
self._validate_key(key)
if self._results is not None:
return self._results[key]
if isinstance(key, int):
raise NotYetEvaluatedError()
copy = self._copy()
copy._offset = key.start
copy._slice = key
if copy._slice.stop - copy._slice.start < copy._limit:
copy._limit = copy._slice.stop - copy._slice.start
return copy
async def count(self) -> int:
"""
Returns the total number of resources within this ResourceSet object.
Usage:
```py3
no_of_products = await client.products.all().count()
```
Returns:
(int): Returns the total number of resources within this ResourceSet object.
"""
if not self._content_range:
url = self._get_request_url()
kwargs = self._get_request_kwargs()
kwargs['params']['limit'] = 0
await self._execute_request(url, kwargs)
return self._content_range.count
async def first(self):
"""
Returns the first resource that belongs to this ResourceSet object
or None if the ResourceSet doesn't contains resources.
Usage:
```py3
latest_news = await client.news.all().order_by('-updated').first()
```
Returns:
(Resource): Returns the first resource that belongs to this ResourceSet object
or None.
"""
copy = self._copy()
copy._limit = 1
copy._offset = 0
await copy._fetch_all()
return copy._results[0] if copy._results else None
def _iterator(self):
args = (
self,
self._client,
self._path,
self._build_qs(),
self._get_request_kwargs(),
)
iterator = (
AsyncValuesListIterator(*args, fields=self._fields)
if self._fields
else AsyncResourceIterator(*args)
)
return iterator
async def _execute_request(self, url, kwargs):
results = await self._client.get(url, **kwargs)
self._content_range = parse_content_range(
self._client.response.headers.get('Content-Range'),
)
return results
async def _fetch_all(self):
if self._results is None: # pragma: no branch
self._results = await self._execute_request(
self._get_request_url(),
self._get_request_kwargs(),
)