Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b8cd184c64 | ||
|
|
74ed83cf73 | ||
|
|
14a3ee1456 | ||
|
|
3737af0cf5 |
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
# claude
|
# claude
|
||||||
CLAUDE.md
|
.claude/
|
||||||
|
|
||||||
# python
|
# python
|
||||||
__pycache__/
|
__pycache__/
|
||||||
|
|||||||
@@ -11,18 +11,18 @@ 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.4
|
aioweb @ git+ssh://git@git.rethinkstudios.io/rethink-public/aioweb.git@v0.1.6
|
||||||
```
|
```
|
||||||
|
|
||||||
Direct:
|
Direct:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
pip install "aioweb @ git+ssh://git@git.rethinkstudios.io/rethink-public/aioweb.git@v0.1.4"
|
pip install "aioweb @ git+ssh://git@git.rethinkstudios.io/rethink-public/aioweb.git@v0.1.6"
|
||||||
```
|
```
|
||||||
|
|
||||||
Requires `aiohttp` and `yarl` (pulled transitively).
|
Requires `aiohttp` and `yarl` (pulled transitively).
|
||||||
|
|
||||||
Drop the `@v0.1.4` suffix from the line above to install the latest unpinned.
|
Drop the `@v0.1.5` suffix from the line above to install the latest unpinned.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "aioweb"
|
name = "aioweb"
|
||||||
version = "0.1.4"
|
version = "0.1.6"
|
||||||
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 = [
|
||||||
|
|||||||
@@ -92,7 +92,9 @@ class Response:
|
|||||||
"""parsed JSON content, or None if not valid JSON"""
|
"""parsed JSON content, or None if not valid JSON"""
|
||||||
try:
|
try:
|
||||||
return _json.loads(self.text())
|
return _json.loads(self.text())
|
||||||
except _json.JSONDecodeError:
|
except (_json.JSONDecodeError, UnicodeDecodeError):
|
||||||
|
# text() decodes the body and can raise UnicodeDecodeError on a non-UTF-8
|
||||||
|
# payload — that's a "not valid JSON" outcome, not an error to propagate
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def raise_for_status(self):
|
def raise_for_status(self):
|
||||||
|
|||||||
+22
-8
@@ -306,6 +306,12 @@ class ExtendedSession:
|
|||||||
timeout = kwargs.get("timeout")
|
timeout = kwargs.get("timeout")
|
||||||
if isinstance(timeout, (int, float)):
|
if isinstance(timeout, (int, float)):
|
||||||
kwargs["timeout"] = aiohttp.ClientTimeout(total=timeout)
|
kwargs["timeout"] = aiohttp.ClientTimeout(total=timeout)
|
||||||
|
elif timeout is None and "timeout" in kwargs:
|
||||||
|
# an explicit timeout=None reaches aiohttp as ClientTimeout(total=None),
|
||||||
|
# which DISABLES the timeout and overrides the session default; drop it so
|
||||||
|
# the session-level timeout applies (matters for request_with_retries, whose
|
||||||
|
# timeout kwarg defaults to None)
|
||||||
|
del kwargs["timeout"]
|
||||||
|
|
||||||
url = self._apply_domain_overwrites(url)
|
url = self._apply_domain_overwrites(url)
|
||||||
if debug:
|
if debug:
|
||||||
@@ -316,10 +322,13 @@ class ExtendedSession:
|
|||||||
if debug and result.redirect_chain:
|
if debug and result.redirect_chain:
|
||||||
log.info("redirect chain: %s", result.redirect_chain)
|
log.info("redirect chain: %s", result.redirect_chain)
|
||||||
return result
|
return result
|
||||||
except (aiohttp.ClientError, asyncio.TimeoutError) as error:
|
except asyncio.TimeoutError as error:
|
||||||
# a total ClientTimeout raises a bare asyncio.TimeoutError, which is NOT an
|
# a total ClientTimeout raises a bare asyncio.TimeoutError, which is NOT an
|
||||||
# aiohttp.ClientError subclass — wrap it into the same typed path so direct
|
# aiohttp.ClientError subclass — wrap it as ServerTimeoutError (which IS both
|
||||||
# callers get a consistent failure instead of a raw timeout
|
# a ClientError AND a TimeoutError) so direct callers get a typed failure and
|
||||||
|
# request_with_retries can still label it a timeout
|
||||||
|
raise aiohttp.ServerTimeoutError(f"timeout for {url}: {error}") from error
|
||||||
|
except aiohttp.ClientError as error:
|
||||||
raise aiohttp.ClientError(f"client error for {url}: {error}") from error
|
raise aiohttp.ClientError(f"client error for {url}: {error}") from error
|
||||||
|
|
||||||
async def request_with_retries(
|
async def request_with_retries(
|
||||||
@@ -332,6 +341,10 @@ class ExtendedSession:
|
|||||||
returns a Response on success (or non-retryable status), or a falsy
|
returns a Response on success (or non-retryable status), or a falsy
|
||||||
FailureResponse if every attempt fails. backoff is exponential
|
FailureResponse if every attempt fails. backoff is exponential
|
||||||
(backoff_base ** attempt).
|
(backoff_base ** attempt).
|
||||||
|
|
||||||
|
timeout defaults to None, which falls back to the session-level timeout set
|
||||||
|
at construction (aioweb pops a None timeout so it does not reach aiohttp as an
|
||||||
|
infinite ClientTimeout); pass a number to override per call.
|
||||||
"""
|
"""
|
||||||
attempts = attempts or DEFAULT_ATTEMPTS
|
attempts = attempts or DEFAULT_ATTEMPTS
|
||||||
body_data, body_json = _route_body(data)
|
body_data, body_json = _route_body(data)
|
||||||
@@ -364,14 +377,15 @@ class ExtendedSession:
|
|||||||
log.error("all %d attempts failed for %s (last status %s)",
|
log.error("all %d attempts failed for %s (last status %s)",
|
||||||
attempts, url, exhausted.response.status_code)
|
attempts, url, exhausted.response.status_code)
|
||||||
return exhausted.response
|
return exhausted.response
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
# request() wraps a total timeout as ServerTimeoutError (a ClientError AND a
|
||||||
|
# TimeoutError); catch the timeout case first so it's labeled a timeout rather
|
||||||
|
# than falling into the generic client-error branch below
|
||||||
|
log.error("all %d attempts timed out for %s", attempts, url)
|
||||||
|
return FailureResponse(reason="timeout", url=url)
|
||||||
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user