# gitea-client — HTTP helpers # # Shared HTTP utilities for Gitea API calls. # All functions take a `conn` dict from connections.get("gitea"). def auth_headers(conn): """Build authorization headers from a connection dict.""" token = conn.get("api_token", "") if not token: return {} return {"Authorization": "token " + token} def _base(conn): """Return base URL with trailing slash stripped.""" url = conn.get("base_url", "") if url.endswith("/"): url = url[:-1] return url def gitea_get(conn, path): """GET request to Gitea API. Returns decoded body or None.""" url = _base(conn) + "/api/v1" + path resp = http.get(url=url, headers=auth_headers(conn)) if int(resp["status"]) >= 400: return None body = resp.get("body", "") if not body: return None return json.decode(body) def gitea_post(conn, path, data): """POST request to Gitea API. Returns decoded body or None.""" url = _base(conn) + "/api/v1" + path hdrs = dict(auth_headers(conn), **{"Content-Type": "application/json"}) resp = http.post(url=url, body=json.encode(data), headers=hdrs) if int(resp["status"]) >= 400: return None body = resp.get("body", "") if not body: return None return json.decode(body) def gitea_patch(conn, path, data): """PATCH request to Gitea API (via X-HTTP-Method-Override). Returns decoded body or None.""" url = _base(conn) + "/api/v1" + path hdrs = dict(auth_headers(conn), **{ "Content-Type": "application/json", "X-HTTP-Method-Override": "PATCH", }) resp = http.post(url=url, body=json.encode(data), headers=hdrs) if int(resp["status"]) >= 400: return None body = resp.get("body", "") if not body: return None return json.decode(body) def resp(status, data): """Build a standard JSON response dict.""" return {"status": status, "body": json.encode(data), "headers": {"Content-Type": "application/json"}}