Make most of the HTTP requests async
This commit is contained in:
parent
3e17e17e2a
commit
d371e3cd4f
12 changed files with 88 additions and 82 deletions
|
@ -7,7 +7,7 @@ from loguru import logger
|
|||
from app import config
|
||||
|
||||
|
||||
def webfinger(
|
||||
async def webfinger(
|
||||
resource: str,
|
||||
) -> dict[str, Any] | None: # noqa: C901
|
||||
"""Mastodon-like WebFinger resolution to retrieve the activity stream Actor URL."""
|
||||
|
@ -28,37 +28,38 @@ def webfinger(
|
|||
|
||||
is_404 = False
|
||||
|
||||
for i, proto in enumerate(protos):
|
||||
try:
|
||||
url = f"{proto}://{host}/.well-known/webfinger"
|
||||
resp = httpx.get(
|
||||
url,
|
||||
params={"resource": resource},
|
||||
headers={
|
||||
"User-Agent": config.USER_AGENT,
|
||||
},
|
||||
)
|
||||
break
|
||||
except httpx.HTTPStatusError as http_error:
|
||||
logger.exception("HTTP error")
|
||||
if http_error.response.status_code in [403, 404, 410]:
|
||||
is_404 = True
|
||||
continue
|
||||
raise
|
||||
except httpx.HTTPError:
|
||||
logger.exception("req failed")
|
||||
# If we tried https first and the domain is "http only"
|
||||
if i == 0:
|
||||
continue
|
||||
break
|
||||
async with httpx.AsyncClient() as client:
|
||||
for i, proto in enumerate(protos):
|
||||
try:
|
||||
url = f"{proto}://{host}/.well-known/webfinger"
|
||||
resp = await client.get(
|
||||
url,
|
||||
params={"resource": resource},
|
||||
headers={
|
||||
"User-Agent": config.USER_AGENT,
|
||||
},
|
||||
)
|
||||
break
|
||||
except httpx.HTTPStatusError as http_error:
|
||||
logger.exception("HTTP error")
|
||||
if http_error.response.status_code in [403, 404, 410]:
|
||||
is_404 = True
|
||||
continue
|
||||
raise
|
||||
except httpx.HTTPError:
|
||||
logger.exception("req failed")
|
||||
# If we tried https first and the domain is "http only"
|
||||
if i == 0:
|
||||
continue
|
||||
break
|
||||
if is_404:
|
||||
return None
|
||||
|
||||
return resp.json()
|
||||
|
||||
|
||||
def get_remote_follow_template(resource: str) -> str | None:
|
||||
data = webfinger(resource)
|
||||
async def get_remote_follow_template(resource: str) -> str | None:
|
||||
data = await webfinger(resource)
|
||||
if data is None:
|
||||
return None
|
||||
for link in data["links"]:
|
||||
|
@ -67,13 +68,13 @@ def get_remote_follow_template(resource: str) -> str | None:
|
|||
return None
|
||||
|
||||
|
||||
def get_actor_url(resource: str) -> str | None:
|
||||
async def get_actor_url(resource: str) -> str | None:
|
||||
"""Mastodon-like WebFinger resolution to retrieve the activity stream Actor URL.
|
||||
|
||||
Returns:
|
||||
the Actor URL or None if the resolution failed.
|
||||
"""
|
||||
data = webfinger(resource)
|
||||
data = await webfinger(resource)
|
||||
if data is None:
|
||||
return None
|
||||
for link in data["links"]:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue