|
| 1 | +from typing import Any, Callable, Dict, List |
| 2 | + |
| 3 | +from .rest import Result |
| 4 | + |
| 5 | + |
1 | 6 | class baseapi: |
2 | 7 | def __init__(self, client): |
3 | 8 | self.client = client |
4 | 9 | super().__init__() |
5 | 10 |
|
6 | 11 |
|
7 | 12 | class CreateableAPIResource: |
8 | | - def create(self, *args, **kwargs): |
| 13 | + def create(self, *args, **kwargs) -> Result: |
9 | 14 | return self.client.post(self.path, json=args[0] if args else kwargs) |
10 | 15 |
|
11 | 16 |
|
12 | 17 | class DeletableAPIResource: |
13 | | - def delete(self, obj): |
14 | | - if obj and isinstance(obj, list): |
| 18 | + def delete(self, obj: List[Any] | str | int) -> Result: |
| 19 | + if isinstance(obj, list): |
15 | 20 | return self.client.delete(self.path, json=obj) |
| 21 | + |
16 | 22 | return self.client.delete(f"{self.path}{obj}/") |
17 | 23 |
|
18 | 24 |
|
19 | 25 | class ListableAPIResource: |
20 | | - def list(self, **kwargs): |
| 26 | + def paginate(self, result: Result) -> Result: |
| 27 | + next_token = result.pagination["next"] |
| 28 | + yield result |
| 29 | + while next_token: |
| 30 | + result = self.client.get( |
| 31 | + self.path, url_override=next_token, params=result.params |
| 32 | + ) |
| 33 | + yield result |
| 34 | + next_token = result.pagination["next"] |
| 35 | + |
| 36 | + def list(self, **kwargs) -> Result: |
21 | 37 | return self.client.get(self.path, params=kwargs) |
22 | 38 |
|
23 | 39 | def all(self, **kwargs): |
24 | | - ret = self.client.get(self.path, params=kwargs) |
25 | | - pagination = ret.pagination |
26 | | - while pagination["next"]: |
27 | | - partial = self.client.get( |
28 | | - self.path, url_override=pagination["next"], params=kwargs |
29 | | - ) |
30 | | - ret.data.append(partial.data) |
31 | | - pagination = partial.pagination |
| 40 | + result = None |
| 41 | + for page in self.paginate(self.client.get(self.path, params=kwargs)): |
| 42 | + if not result: |
| 43 | + result = page |
| 44 | + else: |
| 45 | + result.data.append(page.data) |
32 | 46 |
|
33 | | - ret.pagination["next"] = None |
34 | | - ret.pagination["previous"] = None |
35 | | - return ret |
| 47 | + result.pagination["next"] = None |
| 48 | + result.pagination["previous"] = None |
| 49 | + return result |
36 | 50 |
|
37 | 51 |
|
38 | 52 | class RetrievableAPIResource: |
39 | | - def get(self, id): |
| 53 | + def get(self, id: str | int) -> Result: |
40 | 54 | return self.client.get(f"{self.path}{id}/") |
41 | 55 |
|
42 | 56 |
|
43 | 57 | class RetrievableRootAPIResource: |
44 | | - def get(self, **kwargs): |
| 58 | + def get(self, **kwargs) -> Result: |
45 | 59 | return self.client.get(self.path, params=kwargs) |
46 | 60 |
|
47 | 61 |
|
48 | 62 | class UpdateableAPIResource: |
49 | | - def update(self, obj, **kwargs): |
50 | | - if obj and isinstance(obj, list): |
| 63 | + def update(self, obj: List[Any] | str | int, **kwargs) -> Result: |
| 64 | + if isinstance(obj, list): |
51 | 65 | return self.client.patch(self.path, json=obj) |
| 66 | + |
52 | 67 | return self.client.patch(f"{self.path}{obj}/", json=kwargs) |
53 | 68 |
|
54 | 69 |
|
|
0 commit comments