Skip to content

Commit 47765ef

Browse files
committed
Sync validated Python 0.1.0 client sources
1 parent 912c624 commit 47765ef

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

‎src/transcriptdock/__init__.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import re
77
import time
88
from urllib.error import HTTPError, URLError
9-
from urllib.parse import quote, urlencode, urlsplit
9+
from urllib.parse import quote, unquote, urlencode, urlsplit
1010
from urllib.request import HTTPRedirectHandler, Request, build_opener
1111

1212
__version__ = "0.1.0"
@@ -79,9 +79,14 @@ def _redact(self, value):
7979

8080
def request(self, method, path, *, body=None, query=None, idempotency_key=None, text=False, timeout=None):
8181
"""Call a public /v1 endpoint; no redirects or automatic retries."""
82+
if not isinstance(method, str):
83+
raise ValueError("Provide an HTTP method.")
84+
method = method.upper()
85+
if method not in ("GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"):
86+
raise ValueError("Provide a supported HTTP method.")
8287
if not isinstance(path, str) or not path.startswith("/v1/") or any(c in path for c in ("?", "#", "\\")):
8388
raise ValueError("Use a /v1/ path and pass query parameters separately.")
84-
if any(part in (".", "..") for part in path.split("/")):
89+
if any(part in (".", "..") for part in unquote(path).split("/")):
8590
raise ValueError("API paths cannot contain traversal segments.")
8691
needs_key = method == "POST" and (path in ("/v1/jobs", "/v1/batches", "/v1/video-discoveries", "/v1/language-discoveries") or path.endswith("/retry"))
8792
if needs_key and idempotency_key is None:

‎tests/test_client.py‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ def respond(self):
3333
code = 429
3434
payload = {"error": {"code": "RATE_LIMITED", "message": KEY,
3535
"retryable": True, "retry_after_seconds": 4}, "request_id": "fixture-request"}
36+
elif self.path in ("/v1/budget", "/v1/conflict", "/v1/jobs/not-owned"):
37+
code, label = {"/v1/budget": (402, "BUDGET_EXCEEDED"), "/v1/conflict": (409, "IDEMPOTENCY_CONFLICT"), "/v1/jobs/not-owned": (404, "NOT_FOUND")}[self.path]
38+
payload = {"error": {"code": label, "message": "Fixture rejection", "retryable": False}}
3639
elif self.path == "/v1/gateway":
3740
code, payload = 502, b"<html>upstream failed</html>"
3841
elif self.path == "/v1/malformed":
@@ -94,6 +97,16 @@ def test_no_automatic_retry_and_safe_errors(self):
9497
self.client.request("GET", "/v1/malformed")
9598
self.assertEqual(ctx.exception.code, "INVALID_RESPONSE")
9699

100+
def test_budget_conflict_and_inaccessible_job_errors_are_not_retried(self):
101+
for path, status, code in [("/v1/budget", 402, "BUDGET_EXCEEDED"),
102+
("/v1/conflict", 409, "IDEMPOTENCY_CONFLICT"),
103+
("/v1/jobs/not-owned", 404, "NOT_FOUND")]:
104+
with self.assertRaises(ApiError) as ctx:
105+
self.client.request("GET", path)
106+
self.assertEqual((ctx.exception.status, ctx.exception.code), (status, code))
107+
self.assertFalse(ctx.exception.retryable)
108+
self.assertEqual(len(Handler.calls), 3)
109+
97110
def test_redirect_never_followed(self):
98111
with self.assertRaises(ApiError) as ctx:
99112
self.client.request("GET", "/v1/redirect")
@@ -116,9 +129,11 @@ def test_rejects_credential_and_submission_hazards_before_network(self):
116129
for key in (None, "short", "has\nnewline"):
117130
with self.assertRaises(ValueError):
118131
self.client.create_job({}, idempotency_key=key)
119-
for path in ("https://example.com", "/v1/../secret", "/v1/jobs?x=1"):
132+
for path in ("https://example.com", "/v1/../secret", "/v1/%2e%2e/secret", "/v1/jobs?x=1"):
120133
with self.assertRaises(ValueError):
121134
self.client.request("GET", path)
135+
with self.assertRaises(ValueError):
136+
self.client.request("post", "/v1/jobs", body={})
122137
self.assertEqual(Handler.calls, [])
123138

124139
def test_polling_terminal_failure_and_timeout_preserve_state(self):

0 commit comments

Comments
 (0)