HTTP clients tweaks

This commit is contained in:
Thomas Sileo 2022-07-13 20:05:15 +02:00
parent 6616343cd3
commit 88b57f29af
5 changed files with 49 additions and 6 deletions

View file

@ -14,6 +14,8 @@ from app.key import get_pubkey_as_pem
if TYPE_CHECKING:
from app.actor import Actor
_HTTPX_TRANSPORT = httpx.AsyncHTTPTransport(retries=1)
RawObject = dict[str, Any]
AS_CTX = "https://www.w3.org/ns/activitystreams"
AS_PUBLIC = "https://www.w3.org/ns/activitystreams#Public"
@ -49,6 +51,10 @@ class ObjectIsGoneError(Exception):
pass
class ObjectNotFoundError(Exception):
pass
class VisibilityEnum(str, enum.Enum):
PUBLIC = "public"
UNLISTED = "unlisted"
@ -104,7 +110,7 @@ class NotAnObjectError(Exception):
async def fetch(url: str, params: dict[str, Any] | None = None) -> RawObject:
async with httpx.AsyncClient() as client:
async with httpx.AsyncClient(transport=_HTTPX_TRANSPORT) as client:
resp = await client.get(
url,
headers={
@ -119,6 +125,8 @@ async def fetch(url: str, params: dict[str, Any] | None = None) -> RawObject:
# Special handling for deleted object
if resp.status_code == 410:
raise ObjectIsGoneError(f"{url} is gone")
elif resp.status_code == 404:
raise ObjectNotFoundError(f"{url} not found")
resp.raise_for_status()
try:

View file

@ -138,8 +138,8 @@ async def httpsig_checker(
try:
k = await _get_public_key(db_session, hsig["keyId"])
except ap.ObjectIsGoneError:
logger.info("Actor is gone")
except (ap.ObjectIsGoneError, ap.ObjectNotFoundError):
logger.info("Actor is gone or not found")
return HTTPSigInfo(has_valid_signature=False, is_ap_actor_gone=True)
except Exception:
logger.exception(f'Failed to fetch HTTP sig key {hsig["keyId"]}')

View file

@ -66,6 +66,9 @@ _RESIZED_CACHE: MutableMapping[tuple[str, int], tuple[bytes, str, Any]] = LFUCac
# TODO(ts):
#
# Next:
# - incoming activity worker
# - handle remove activity
# - retries httpx?
# - DB models for webmentions
# - allow to undo follow requests
# - indieauth tweaks
@ -760,7 +763,7 @@ async def nodeinfo(
)
proxy_client = httpx.AsyncClient(follow_redirects=True)
proxy_client = httpx.AsyncClient(follow_redirects=True, http2=True)
@app.get("/proxy/media/{encoded_url}")