Merge origin/main into docs/agents-pr-only
This commit is contained in:
+27
-11
@@ -59,6 +59,19 @@ def save_response(response: httpx.Response, destination: Path) -> None:
|
||||
destination.write_bytes(response.content)
|
||||
|
||||
|
||||
def format_http_error(response: httpx.Response, endpoint: str) -> str:
|
||||
body = response.text.strip() or "<empty response body>"
|
||||
return f"HTTP {response.status_code} from {endpoint}: {body}"
|
||||
|
||||
|
||||
def format_request_error(exc: httpx.RequestError, endpoint: str) -> str:
|
||||
if isinstance(exc, httpx.TimeoutException):
|
||||
return f"Request to {endpoint} timed out."
|
||||
|
||||
reason = str(exc).strip() or exc.__class__.__name__
|
||||
return f"Request to {endpoint} failed: {reason}"
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = build_parser()
|
||||
args = parser.parse_args()
|
||||
@@ -70,21 +83,24 @@ def main() -> int:
|
||||
server = resolve_server(args)
|
||||
endpoint = f"{server}/transcriptions"
|
||||
|
||||
with input_file.open("rb") as handle, httpx.Client(timeout=300.0) as client:
|
||||
response = client.post(
|
||||
endpoint,
|
||||
data={
|
||||
"model": args.model,
|
||||
"language": args.language or "",
|
||||
"output_format": args.output_format,
|
||||
},
|
||||
files={"file": (input_file.name, handle, "application/octet-stream")},
|
||||
)
|
||||
try:
|
||||
with input_file.open("rb") as handle, httpx.Client(timeout=300.0) as client:
|
||||
response = client.post(
|
||||
endpoint,
|
||||
data={
|
||||
"model": args.model,
|
||||
"language": args.language or "",
|
||||
"output_format": args.output_format,
|
||||
},
|
||||
files={"file": (input_file.name, handle, "application/octet-stream")},
|
||||
)
|
||||
except httpx.RequestError as exc:
|
||||
parser.exit(1, f"{format_request_error(exc, endpoint)}\n")
|
||||
|
||||
try:
|
||||
response.raise_for_status()
|
||||
except httpx.HTTPStatusError as exc:
|
||||
message = exc.response.text.strip() or str(exc)
|
||||
message = format_http_error(exc.response, endpoint)
|
||||
parser.exit(1, f"{message}\n")
|
||||
|
||||
if args.to_file:
|
||||
|
||||
+43
-3
@@ -1,9 +1,10 @@
|
||||
import os
|
||||
from argparse import Namespace
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
import sys
|
||||
from dataclasses import dataclass
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
|
||||
@@ -32,6 +33,37 @@ def test_infer_output_path_for_explicit_file(tmp_path: Path) -> None:
|
||||
assert destination == tmp_path / "custom-name.txt"
|
||||
|
||||
|
||||
def test_format_http_error_with_body() -> None:
|
||||
request = httpx.Request("POST", "http://localhost:8000/transcriptions")
|
||||
response = httpx.Response(500, text="Internal Server Error", request=request)
|
||||
message = main.format_http_error(response, "http://localhost:8000/transcriptions")
|
||||
assert message == "HTTP 500 from http://localhost:8000/transcriptions: Internal Server Error"
|
||||
|
||||
|
||||
def test_format_http_error_with_empty_body() -> None:
|
||||
request = httpx.Request("POST", "http://localhost:8000/transcriptions")
|
||||
response = httpx.Response(500, text="", request=request)
|
||||
message = main.format_http_error(response, "http://localhost:8000/transcriptions")
|
||||
assert message == "HTTP 500 from http://localhost:8000/transcriptions: <empty response body>"
|
||||
|
||||
|
||||
def test_format_request_error_timeout() -> None:
|
||||
request = httpx.Request("POST", "http://localhost:8000/transcriptions")
|
||||
exc = httpx.ReadTimeout("read timed out", request=request)
|
||||
message = main.format_request_error(exc, "http://localhost:8000/transcriptions")
|
||||
assert message == "Request to http://localhost:8000/transcriptions timed out."
|
||||
|
||||
|
||||
def test_format_request_error_network_failure() -> None:
|
||||
request = httpx.Request("POST", "http://localhost:8000/transcriptions")
|
||||
exc = httpx.ConnectError("connection refused", request=request)
|
||||
message = main.format_request_error(exc, "http://localhost:8000/transcriptions")
|
||||
assert (
|
||||
message
|
||||
== "Request to http://localhost:8000/transcriptions failed: connection refused"
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class FakeResponse:
|
||||
content: bytes
|
||||
@@ -61,8 +93,16 @@ def test_main_writes_transcript_to_file(monkeypatch, tmp_path: Path, capsys) ->
|
||||
destination = tmp_path / "saved" / "clip.txt"
|
||||
|
||||
monkeypatch.setenv("WHISPER_REMOTE", "http://localhost:8000")
|
||||
monkeypatch.setattr(main.httpx, "Client", lambda timeout: FakeClient(FakeResponse(b"hello world", "hello world")))
|
||||
monkeypatch.setattr(sys, "argv", ["whisper-remote", str(input_file), "--model", "base", "--to-file", str(destination)])
|
||||
monkeypatch.setattr(
|
||||
main.httpx,
|
||||
"Client",
|
||||
lambda timeout: FakeClient(FakeResponse(b"hello world", "hello world")),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
sys,
|
||||
"argv",
|
||||
["whisper-remote", str(input_file), "--model", "base", "--to-file", str(destination)],
|
||||
)
|
||||
|
||||
exit_code = main.main()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user