fix: label timeouts, render empty-but-valid preview bodies, snapshot override dicts (v0.1.4)
- request_with_retries labels an exhausted total-timeout as 'timeout' instead of the
generic 'unexpected error' catch-all (nit)
- as_curl() renders an empty-but-valid json body ({} / []) via is-not-None instead of
dropping it as falsy (nit)
- _apply_overwrites snapshots the shared override dicts before iterating, so a
concurrent mutation can't raise 'dict changed size during iteration' (nit).
Signed-off-by: disqualifier <dev@disqualifier.me>
This commit is contained in:
parent
382b8aa632
commit
7da06443c8
@ -11,13 +11,13 @@ and swap the HTTP client while inheriting everything else.
|
|||||||
`requirements.txt`:
|
`requirements.txt`:
|
||||||
|
|
||||||
```
|
```
|
||||||
aioweb @ git+ssh://git@git.rethinkstudios.io/rethink-public/aioweb.git@v0.1.3
|
aioweb @ git+ssh://git@git.rethinkstudios.io/rethink-public/aioweb.git@v0.1.4
|
||||||
```
|
```
|
||||||
|
|
||||||
Direct:
|
Direct:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
pip install "aioweb @ git+ssh://git@git.rethinkstudios.io/rethink-public/aioweb.git@v0.1.3"
|
pip install "aioweb @ git+ssh://git@git.rethinkstudios.io/rethink-public/aioweb.git@v0.1.4"
|
||||||
```
|
```
|
||||||
|
|
||||||
Requires `aiohttp` and `yarl` (pulled transitively).
|
Requires `aiohttp` and `yarl` (pulled transitively).
|
||||||
|
|||||||
@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "aioweb"
|
name = "aioweb"
|
||||||
version = "0.1.3"
|
version = "0.1.4"
|
||||||
description = "Async HTTP session wrapper over aiohttp — proxies, header overwrites, retries, previews. Config-free, installable."
|
description = "Async HTTP session wrapper over aiohttp — proxies, header overwrites, retries, previews. Config-free, installable."
|
||||||
requires-python = ">=3.10"
|
requires-python = ">=3.10"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
|||||||
@ -35,9 +35,11 @@ class RequestPreview:
|
|||||||
parts = [f"curl -X {shlex.quote(self.details['method'])}"]
|
parts = [f"curl -X {shlex.quote(self.details['method'])}"]
|
||||||
for header, value in (self.details["headers"] or {}).items():
|
for header, value in (self.details["headers"] or {}).items():
|
||||||
parts.append(f"-H {shlex.quote(f'{header}: {value}')}")
|
parts.append(f"-H {shlex.quote(f'{header}: {value}')}")
|
||||||
if self.details["data"]:
|
if self.details["data"] is not None:
|
||||||
parts.append(f"--data {shlex.quote(str(self.details['data']))}")
|
parts.append(f"--data {shlex.quote(str(self.details['data']))}")
|
||||||
elif self.details["json"]:
|
elif self.details["json"] is not None:
|
||||||
|
# is-not-None, not truthiness: an empty-but-valid body ({} / []) must still
|
||||||
|
# render rather than being dropped as falsy
|
||||||
parts.append(f"--data {shlex.quote(_json.dumps(self.details['json']))}")
|
parts.append(f"--data {shlex.quote(_json.dumps(self.details['json']))}")
|
||||||
parts.append(shlex.quote(str(self.details["url"])))
|
parts.append(shlex.quote(str(self.details["url"])))
|
||||||
if self.details["proxy"]:
|
if self.details["proxy"]:
|
||||||
|
|||||||
@ -124,10 +124,13 @@ class ExtendedSession:
|
|||||||
def _apply_overwrites(self, request_headers):
|
def _apply_overwrites(self, request_headers):
|
||||||
"""apply static overwrites and ephemeral headers to a request's headers"""
|
"""apply static overwrites and ephemeral headers to a request's headers"""
|
||||||
request_headers = dict(request_headers or {})
|
request_headers = dict(request_headers or {})
|
||||||
for header, value in self.header_overwrites.items():
|
# snapshot the shared override dicts so a concurrent mutation (e.g. a command
|
||||||
|
# editing ephemerals on a shared session) can't raise "dict changed size during
|
||||||
|
# iteration" — the loop body is sync, but the snapshot is cheap insurance
|
||||||
|
for header, value in list(self.header_overwrites.items()):
|
||||||
if self.inject or header in request_headers:
|
if self.inject or header in request_headers:
|
||||||
request_headers[header] = value
|
request_headers[header] = value
|
||||||
for header, value_callable in self.ephemeral_headers.items():
|
for header, value_callable in list(self.ephemeral_headers.items()):
|
||||||
if self.inject or header in request_headers:
|
if self.inject or header in request_headers:
|
||||||
value = value_callable()
|
value = value_callable()
|
||||||
if isinstance(value, dict):
|
if isinstance(value, dict):
|
||||||
@ -364,6 +367,11 @@ class ExtendedSession:
|
|||||||
except aiohttp.ClientError as error:
|
except aiohttp.ClientError as error:
|
||||||
log.error("all %d attempts failed for %s (client error: %s)", attempts, url, error)
|
log.error("all %d attempts failed for %s (client error: %s)", attempts, url, error)
|
||||||
return FailureResponse(reason=f"client error: {error}", url=url)
|
return FailureResponse(reason=f"client error: {error}", url=url)
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
# a total ClientTimeout surfaces as a bare asyncio.TimeoutError; label it as
|
||||||
|
# a timeout rather than letting it fall to the generic "unexpected" branch
|
||||||
|
log.error("all %d attempts timed out for %s", attempts, url)
|
||||||
|
return FailureResponse(reason="timeout", url=url)
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
log.error("all %d attempts failed for %s (unexpected: %s)", attempts, url, error)
|
log.error("all %d attempts failed for %s (unexpected: %s)", attempts, url, error)
|
||||||
return FailureResponse(reason=f"unexpected error: {error}", url=url)
|
return FailureResponse(reason=f"unexpected error: {error}", url=url)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user