MCPcopy Create free account
hub / github.com/Coooolfan/onlyboxes / api_request

Function api_request

scripts/install.py:219–239  ·  view source on GitHub ↗
(
    url: str,
    method: str = "GET",
    data: Optional[dict] = None,
    api_key: Optional[str] = None,
)

Source from the content-addressed store, hash-verified

217
218
219def api_request(
220 url: str,
221 method: str = "GET",
222 data: Optional[dict] = None,
223 api_key: Optional[str] = None,
224) -> dict:
225 body = json.dumps(data).encode() if data is not None else None
226 req = urllib.request.Request(url, data=body, method=method)
227 if body is not None:
228 req.add_header("Content-Type", "application/json")
229 if api_key:
230 req.add_header("Authorization", f"Bearer {api_key}")
231 try:
232 with urllib.request.urlopen(req) as resp:
233 return json.loads(resp.read().decode())
234 except urllib.error.HTTPError as exc:
235 detail = exc.read().decode(errors="replace") if exc.fp else ""
236 fatal("api", f"{method} {url} returned {exc.code}: {detail}")
237 except urllib.error.URLError as exc:
238 fatal("api", f"{method} {url} failed: {exc.reason}")
239 return {} # unreachable
240
241
242# ---------------------------------------------------------------------------

Callers 2

create_workerFunction · 0.85
wait_worker_onlineFunction · 0.85

Calls 1

fatalFunction · 0.70

Tested by

no test coverage detected