Function for getting data from one of Scratch's iterative JSON API endpoints (like /users/ /followers, or /users/ /projects)
(url: str, *, limit: int, offset: int, max_req_limit: int = 40, add_params: str = "",
_headers: Optional[dict] = None, cookies: Optional[dict] = None)
| 97 | |
| 98 | |
| 99 | def api_iterative(url: str, *, limit: int, offset: int, max_req_limit: int = 40, add_params: str = "", |
| 100 | _headers: Optional[dict] = None, cookies: Optional[dict] = None): |
| 101 | """ |
| 102 | Function for getting data from one of Scratch's iterative JSON API endpoints (like /users/<user>/followers, or /users/<user>/projects) |
| 103 | """ |
| 104 | if _headers is None: |
| 105 | _headers = headers.copy() |
| 106 | if cookies is None: |
| 107 | cookies = {} |
| 108 | |
| 109 | if offset < 0: |
| 110 | raise exceptions.BadRequest("offset parameter must be >= 0") |
| 111 | if limit < 0: |
| 112 | raise exceptions.BadRequest("limit parameter must be >= 0") |
| 113 | |
| 114 | def fetch(off: int, lim: int): |
| 115 | """ |
| 116 | Performs a single API request |
| 117 | """ |
| 118 | resp = requests.get( |
| 119 | f"{url}?limit={lim}&offset={off}{add_params}", headers=_headers, cookies=cookies, timeout=10 |
| 120 | ).json() |
| 121 | |
| 122 | if not resp: |
| 123 | return None |
| 124 | if resp == {"code": "BadRequest", "message": ""}: |
| 125 | raise exceptions.BadRequest("The passed arguments are invalid") |
| 126 | return resp |
| 127 | |
| 128 | api_data = api_iterative_data( |
| 129 | fetch, limit, offset, max_req_limit=max_req_limit, unpack=True |
| 130 | ) |
| 131 | return api_data |
| 132 | |
| 133 | def _get_object(identificator_name, identificator, __class: type[C], NotFoundException, session=None) -> C: |
| 134 | # Internal function: Generalization of the process ran by get_user, get_studio etc. |
nothing calls this directly
no test coverage detected