Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d527174a2b | ||
|
|
bad3ea2677 | ||
|
|
dc3fb70a1e | ||
|
|
7a2f24be9e | ||
|
|
7779d0b050 |
@@ -11,13 +11,13 @@ and swap the HTTP client while inheriting everything else.
|
||||
`requirements.txt`:
|
||||
|
||||
```
|
||||
aioweb @ git+ssh://git@git.rethinkstudios.io/rethink-public/aioweb.git@v0.1.0
|
||||
aioweb @ git+ssh://git@git.rethinkstudios.io/rethink-public/aioweb.git@v0.1.2
|
||||
```
|
||||
|
||||
Direct:
|
||||
|
||||
```bash
|
||||
pip install "aioweb @ git+ssh://git@git.rethinkstudios.io/rethink-public/aioweb.git@v0.1.0"
|
||||
pip install "aioweb @ git+ssh://git@git.rethinkstudios.io/rethink-public/aioweb.git@v0.1.2"
|
||||
```
|
||||
|
||||
Requires `aiohttp` and `yarl` (pulled transitively).
|
||||
@@ -128,6 +128,23 @@ Two changes can't be shimmed without re-introducing the bugs they fix:
|
||||
`await s.close()`; a leaked session emits a `ResourceWarning`. The old finalizer-based
|
||||
auto-close was unsafe and was removed.
|
||||
|
||||
## Changelog
|
||||
|
||||
### v0.1.2
|
||||
|
||||
- Pinned `commons` to v0.2.1 (retry `attempts` floor fix).
|
||||
|
||||
### v0.1.1
|
||||
|
||||
- **JSON list bodies** now route to `json=` (were wrongly form-encoded via `data=` —
|
||||
only dicts went to `json=` before).
|
||||
- **Exhausted retries return the real last response.** When every attempt hit a
|
||||
retryable status (429/5xx), the loop discarded it and returned a synthetic
|
||||
`FailureResponse` (status 0); now the real last 4xx/5xx `Response` is returned (only a
|
||||
pure-exception failure yields `FailureResponse`).
|
||||
- Retry/backoff moved onto `commons.aretry` (shared engine); backoff schedule unchanged.
|
||||
Adds a `commons` dependency.
|
||||
|
||||
## Versioning
|
||||
|
||||
Tagged `vX.Y.Z`. Pin the tag in `requirements.txt`.
|
||||
|
||||
+5
-1
@@ -4,13 +4,17 @@ build-backend = "hatchling.build"
|
||||
|
||||
[project]
|
||||
name = "aioweb"
|
||||
version = "0.1.0"
|
||||
version = "0.1.2"
|
||||
description = "Async HTTP session wrapper over aiohttp — proxies, header overwrites, retries, previews. Config-free, installable."
|
||||
requires-python = ">=3.10"
|
||||
dependencies = [
|
||||
"aiohttp>=3.9",
|
||||
"yarl>=1.9",
|
||||
"commons @ git+ssh://git@git.rethinkstudios.io/rethink-public/commons.git@v0.2.1",
|
||||
]
|
||||
|
||||
[tool.hatch.metadata]
|
||||
allow-direct-references = true
|
||||
|
||||
[tool.hatch.build.targets.wheel]
|
||||
packages = ["src/aioweb"]
|
||||
|
||||
+13
-7
@@ -3,6 +3,7 @@ request preview for aioweb — format or export a request without sending it
|
||||
"""
|
||||
|
||||
import json as _json
|
||||
import shlex
|
||||
|
||||
|
||||
class RequestPreview:
|
||||
@@ -25,15 +26,20 @@ class RequestPreview:
|
||||
return "\n".join(f"{key}: {value}" for key, value in self.details.items())
|
||||
|
||||
def as_curl(self):
|
||||
"""equivalent cURL command for the request"""
|
||||
parts = [f"curl -X {self.details['method']}"]
|
||||
"""equivalent cURL command for the request
|
||||
|
||||
every interpolated value is shell-quoted with shlex.quote, so headers,
|
||||
body, url, or proxy containing quotes/spaces/metacharacters produce a
|
||||
valid, non-injectable command rather than a broken or unsafe one.
|
||||
"""
|
||||
parts = [f"curl -X {shlex.quote(self.details['method'])}"]
|
||||
for header, value in (self.details["headers"] or {}).items():
|
||||
parts.append(f"-H '{header}: {value}'")
|
||||
parts.append(f"-H {shlex.quote(f'{header}: {value}')}")
|
||||
if self.details["data"]:
|
||||
parts.append(f"--data '{self.details['data']}'")
|
||||
parts.append(f"--data {shlex.quote(str(self.details['data']))}")
|
||||
elif self.details["json"]:
|
||||
parts.append(f"--data '{_json.dumps(self.details['json'])}'")
|
||||
parts.append(f"'{self.details['url']}'")
|
||||
parts.append(f"--data {shlex.quote(_json.dumps(self.details['json']))}")
|
||||
parts.append(shlex.quote(str(self.details["url"])))
|
||||
if self.details["proxy"]:
|
||||
parts.append(f"--proxy '{self.details['proxy']}'")
|
||||
parts.append(f"--proxy {shlex.quote(str(self.details['proxy']))}")
|
||||
return " \\\n ".join(parts)
|
||||
|
||||
+64
-34
@@ -17,16 +17,41 @@ sessions must be closed explicitly (async with, or await s.close()); there is no
|
||||
__del__ auto-close (that pattern is unsafe for async resources).
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import warnings
|
||||
|
||||
import aiohttp
|
||||
from yarl import URL
|
||||
from commons import aretry
|
||||
|
||||
from .preview import RequestPreview
|
||||
from .responses import Response, FailureResponse
|
||||
|
||||
|
||||
def _route_body(data):
|
||||
"""split a body into (data=, json=) kwargs
|
||||
|
||||
dict OR list bodies are valid JSON and route to json=; everything else
|
||||
(str/bytes/form) routes to data=. previously only dicts went to json=, so a
|
||||
JSON list was wrongly form-encoded.
|
||||
"""
|
||||
if isinstance(data, (dict, list)):
|
||||
return None, data
|
||||
return data, None
|
||||
|
||||
|
||||
class _RetryStatus(Exception):
|
||||
"""internal signal: a retryable HTTP status; carries the real Response
|
||||
|
||||
raised inside an attempt so commons.aretry drives the backoff + cap; the caller
|
||||
catches the final one to return the REAL last response, not a synthetic failure.
|
||||
"""
|
||||
|
||||
def __init__(self, response):
|
||||
super().__init__(f"retryable status {response.status_code}")
|
||||
self.response = response
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_ATTEMPTS = 3
|
||||
@@ -70,9 +95,15 @@ class ExtendedSession:
|
||||
proxy/retry/preview logic in this class never touches the session object
|
||||
directly (only _raw_request, the cookie methods, and close do), so those
|
||||
features work unchanged on any backend.
|
||||
|
||||
`headers` is the session-default header set; the default aiohttp backend
|
||||
does NOT bake it into the ClientSession (which would copy it into an
|
||||
immutable per-session map that update_headers/clear_headers can't touch).
|
||||
instead `_default_headers` is our own mutable layer that request() and
|
||||
preview() merge per call, so the mutable session-header API actually works.
|
||||
a backend that needs the defaults baked at construction may use `headers`.
|
||||
"""
|
||||
return aiohttp.ClientSession(
|
||||
headers=headers,
|
||||
timeout=aiohttp.ClientTimeout(
|
||||
total=timeout,
|
||||
connect=timeout / 2,
|
||||
@@ -208,10 +239,8 @@ class ExtendedSession:
|
||||
def preview(self, method, url, **kwargs):
|
||||
"""build a RequestPreview for a request without sending it"""
|
||||
proxy = self._get_proxy(url, kwargs.pop("proxies", None))
|
||||
if kwargs.get("headers"):
|
||||
headers = self._apply_overwrites(kwargs.pop("headers"))
|
||||
else:
|
||||
headers = dict(self.get_headers())
|
||||
merged = {**self._default_headers, **(kwargs.pop("headers", None) or {})}
|
||||
headers = self._apply_overwrites(merged)
|
||||
|
||||
timeout = kwargs.get("timeout")
|
||||
timeout_total = timeout if isinstance(timeout, (int, float)) else None
|
||||
@@ -266,7 +295,8 @@ class ExtendedSession:
|
||||
kwargs["proxy"] = self._get_proxy(url, kwargs.pop("proxies", None))
|
||||
debug = kwargs.pop("debug", False)
|
||||
|
||||
kwargs["headers"] = self._apply_overwrites(kwargs.get("headers"))
|
||||
merged = {**self._default_headers, **(kwargs.get("headers") or {})}
|
||||
kwargs["headers"] = self._apply_overwrites(merged)
|
||||
kwargs["headers"] = {str(k): str(v) for k, v in kwargs["headers"].items()}
|
||||
|
||||
timeout = kwargs.get("timeout")
|
||||
@@ -297,42 +327,42 @@ class ExtendedSession:
|
||||
(backoff_base ** attempt).
|
||||
"""
|
||||
attempts = attempts or DEFAULT_ATTEMPTS
|
||||
last_error = None
|
||||
body_data, body_json = _route_body(data)
|
||||
|
||||
if debug:
|
||||
preview = self.preview(
|
||||
method=method, url=url, params=params,
|
||||
data=None if isinstance(data, dict) else data,
|
||||
json=data if isinstance(data, dict) else None,
|
||||
data=body_data, json=body_json,
|
||||
headers=headers, proxies=proxies, timeout=timeout,
|
||||
).as_curl()
|
||||
log.info("[aioweb.debug]\n%s\nproxies: %s inject: %s", preview, self.proxies, self.inject)
|
||||
|
||||
for attempt in range(attempts):
|
||||
try:
|
||||
response = await self.request(
|
||||
method=method, url=url, params=params,
|
||||
data=None if isinstance(data, dict) else data,
|
||||
json=data if isinstance(data, dict) else None,
|
||||
headers=headers, proxies=proxies, timeout=timeout, debug=debug,
|
||||
)
|
||||
if response.status_code in retry_statuses:
|
||||
last_error = f"retryable status {response.status_code}"
|
||||
log.warning("attempt %d: %s for %s", attempt + 1, last_error, url)
|
||||
else:
|
||||
return response
|
||||
except aiohttp.ClientError as error:
|
||||
last_error = f"client error: {error}"
|
||||
log.warning("attempt %d: %s, retrying", attempt + 1, last_error)
|
||||
except Exception as error:
|
||||
last_error = f"unexpected error: {error}"
|
||||
log.exception("attempt %d: %s, retrying", attempt + 1, last_error)
|
||||
async def attempt():
|
||||
response = await self.request(
|
||||
method=method, url=url, params=params,
|
||||
data=body_data, json=body_json,
|
||||
headers=headers, proxies=proxies, timeout=timeout, debug=debug,
|
||||
)
|
||||
if response.status_code in retry_statuses:
|
||||
log.warning("retryable status %s for %s", response.status_code, url)
|
||||
raise _RetryStatus(response)
|
||||
return response
|
||||
|
||||
if attempt < attempts - 1:
|
||||
await asyncio.sleep(backoff_base ** attempt)
|
||||
|
||||
log.error("all %d attempts failed for %s", attempts, url)
|
||||
return FailureResponse(reason=last_error, url=url)
|
||||
try:
|
||||
return await aretry(
|
||||
attempt, attempts=attempts, backoff=1.0, factor=backoff_base,
|
||||
jitter=False, on=(Exception,),
|
||||
)
|
||||
except _RetryStatus as exhausted:
|
||||
log.error("all %d attempts failed for %s (last status %s)",
|
||||
attempts, url, exhausted.response.status_code)
|
||||
return exhausted.response
|
||||
except aiohttp.ClientError as error:
|
||||
log.error("all %d attempts failed for %s (client error: %s)", attempts, url, error)
|
||||
return FailureResponse(reason=f"client error: {error}", url=url)
|
||||
except Exception as error:
|
||||
log.error("all %d attempts failed for %s (unexpected: %s)", attempts, url, error)
|
||||
return FailureResponse(reason=f"unexpected error: {error}", url=url)
|
||||
|
||||
# -------------------------------------------------------------------------
|
||||
# lifecycle
|
||||
|
||||
Reference in New Issue
Block a user