MCPcopy Create free account
hub / github.com/TimMcCool/scratchattach / api_iterative

Function api_iterative

scratchattach/utils/commons.py:99–131  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

97
98
99def 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&#x27;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
133def _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.

Callers

nothing calls this directly

Calls 2

api_iterative_dataFunction · 0.85
copyMethod · 0.80

Tested by

no test coverage detected