(
method: str,
url: str,
data: dict | None = None,
headers: dict | None = None,
timeout: int = 30,
exit_on_error: bool = True,
)
| 75 | |
| 76 | |
| 77 | def _request( |
| 78 | method: str, |
| 79 | url: str, |
| 80 | data: dict | None = None, |
| 81 | headers: dict | None = None, |
| 82 | timeout: int = 30, |
| 83 | exit_on_error: bool = True, |
| 84 | ) -> dict: |
| 85 | hdrs = headers or {} |
| 86 | body = None |
| 87 | if data is not None: |
| 88 | body = json.dumps(data).encode("utf-8") |
| 89 | hdrs["Content-Type"] = "application/json" |
| 90 | |
| 91 | req = urllib.request.Request(url, data=body, headers=hdrs, method=method) |
| 92 | try: |
| 93 | with urllib.request.urlopen(req, timeout=timeout) as resp: |
| 94 | raw = resp.read().decode("utf-8") |
| 95 | return json.loads(raw) if raw.strip() else {} |
| 96 | except urllib.error.HTTPError as e: |
| 97 | body_text = e.read().decode("utf-8", errors="replace") |
| 98 | try: |
| 99 | err = json.loads(body_text) |
| 100 | except json.JSONDecodeError: |
| 101 | err = {"error": body_text, "status": e.code} |
| 102 | |
| 103 | if e.code == 410: |
| 104 | _clear_state() |
| 105 | if exit_on_error: |
| 106 | print("ERROR: Project was evicted from server. Run: coderlm_cli.py init", |
| 107 | file=sys.stderr) |
| 108 | sys.exit(1) |
| 109 | return err |
| 110 | |
| 111 | if exit_on_error: |
| 112 | print(json.dumps(err, indent=2)) |
| 113 | sys.exit(1) |
| 114 | return err |
| 115 | except urllib.error.URLError as e: |
| 116 | err = {"error": f"Cannot connect to coderlm-server: {e.reason}"} |
| 117 | if exit_on_error: |
| 118 | print( |
| 119 | f"ERROR: Cannot connect to coderlm-server: {e.reason}\n" |
| 120 | f"Make sure the server is running: coderlm-server serve", |
| 121 | file=sys.stderr, |
| 122 | ) |
| 123 | sys.exit(1) |
| 124 | return err |
| 125 | |
| 126 | |
| 127 | def _get(state: dict, path: str, params: dict | None = None) -> dict: |
no test coverage detected