Skip to content

Commit 9882372

Browse files
authored
Merge pull request #9 from salmon-21/fix-remote-ankiconnect-url
fix: wire up ankiconnect_url and allow_non_localhost from config
2 parents 4457995 + e7fe2a5 commit 9882372

5 files changed

Lines changed: 15 additions & 2 deletions

File tree

anki_cli/backends/ankiconnect.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ def _invoke(self, action: str, **params: JSONValue) -> JSONValue:
9292
}
9393

9494
try:
95-
response = self._client.post(self._url, json=payload)
95+
response = self._client.post(
96+
self._url,
97+
json=payload,
98+
headers={"Connection": "close"},
99+
)
96100
response.raise_for_status()
97101
except httpx.TimeoutException as exc:
98102
raise AnkiConnectUnavailableError(
@@ -102,6 +106,10 @@ def _invoke(self, action: str, **params: JSONValue) -> JSONValue:
102106
raise AnkiConnectUnavailableError(
103107
f"Cannot connect to AnkiConnect at {self._url}. Is Anki running with the add-on?"
104108
) from exc
109+
except httpx.RemoteProtocolError as exc:
110+
raise AnkiConnectUnavailableError(
111+
f"AnkiConnect at {self._url} disconnected unexpectedly: {exc}"
112+
) from exc
105113
except httpx.HTTPError as exc:
106114
raise AnkiConnectUnavailableError(
107115
f"HTTP error contacting AnkiConnect at {self._url}: {exc}"

anki_cli/backends/detect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def detect_backend(
4040
if forced == "ankiconnect":
4141
if not _ankiconnect_reachable(ankiconnect_url):
4242
raise DetectionError(
43-
"AnkiConnect backend forced, but it is not reachable at localhost:8765.",
43+
f"AnkiConnect backend forced, but it is not reachable at {ankiconnect_url}.",
4444
exit_code=7
4545
)
4646
return DetectionResult(

anki_cli/backends/factory.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,18 @@ def create_backend_from_context(obj: dict[str, Any]) -> AnkiBackend:
2525
app_config = obj.get("app_config")
2626

2727
ankiconnect_url = "http://localhost:8765"
28+
allow_non_localhost = False
2829
if isinstance(app_config, AppConfig):
2930
ankiconnect_url = app_config.backend.ankiconnect_url
31+
allow_non_localhost = app_config.backend.allow_non_localhost
3032

3133
if backend_name == "ankiconnect":
3234
try:
3335
return AnkiConnectBackend(
3436
url=ankiconnect_url,
3537
collection_path=collection_path,
3638
verify_version=True,
39+
allow_non_localhost=allow_non_localhost,
3740
)
3841
except AnkiConnectError as exc:
3942
raise BackendFactoryError(str(exc)) from exc

anki_cli/cli/app.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ def main(
133133
detection = detect_backend(
134134
forced_backend=runtime.backend,
135135
col_override=runtime.collection_override,
136+
ankiconnect_url=runtime.app.backend.ankiconnect_url,
136137
)
137138
except DetectionError as exc:
138139
formatter = formatter_from_ctx(ctx)

anki_cli/models/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class CollectionConfig(BaseModel):
1111
class BackendConfig(BaseModel):
1212
prefer: str = Field(default="auto")
1313
ankiconnect_url: str = "http://localhost:8765"
14+
allow_non_localhost: bool = False
1415

1516

1617
class DisplayConfig(BaseModel):

0 commit comments

Comments
 (0)