Skip to content

Commit

Permalink
refactor: use async for httpx requests
Browse files Browse the repository at this point in the history
  • Loading branch information
julienc91 committed Nov 15, 2024
1 parent cde7423 commit d8f2985
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 27 deletions.
3 changes: 2 additions & 1 deletion backend/caviardeul/management/commands/check_next_article.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from asgiref.sync import async_to_sync
from django.core.management.base import BaseCommand, CommandError
from django.utils import timezone

Expand All @@ -16,6 +17,6 @@ def handle(self, *args, **options):
raise CommandError("No daily article left")

try:
get_article_html_from_wikipedia(next_article.page_id)
async_to_sync(get_article_html_from_wikipedia)(next_article.page_id)
except ArticleFetchError:
raise CommandError("Error when retrieving daily article")
45 changes: 23 additions & 22 deletions backend/caviardeul/services/articles.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,48 @@
from caviardeul.services.parsing import strip_html_article


def get_article_content(article: Article) -> str:
content = _get_article_content_from_cache(article.page_id)
async def get_article_content(article: Article) -> str:
content = await _get_article_content_from_cache(article.page_id)
if content is not None:
logger.debug("retrieved article from cache", extra={"page_id": article.page_id})
return content

_, html_content = get_article_html_from_wikipedia(article.page_id)
_, html_content = await get_article_html_from_wikipedia(article.page_id)
logger.info("retrieved article from wikipedia", extra={"page_id": article.page_id})

article_content = _prepare_article_content_from_html(
article.page_name, html_content
)
_set_article_to_cache(article.page_id, article_content)
await _set_article_to_cache(article.page_id, article_content)
return article_content


def _get_article_content_from_cache(page_id: str) -> str | None:
return cache.get(f"wikipedia::{page_id}")
async def _get_article_content_from_cache(page_id: str) -> str | None:
return await cache.aget(f"wikipedia::{page_id}")


def _set_article_to_cache(page_id: str, content: str) -> None:
async def _set_article_to_cache(page_id: str, content: str) -> None:
now = timezone.now()
tomorrow = (now + timedelta(days=1)).replace(
hour=0, minute=0, second=0, microsecond=0
)
cache_timeout = int((tomorrow - now).total_seconds())
cache.set(f"wikipedia::{page_id}", content, timeout=cache_timeout)


def get_article_html_from_wikipedia(page_id: str) -> tuple[str, str]:
response = httpx.get(
"https://fr.wikipedia.org/w/api.php",
params={
"action": "parse",
"format": "json",
"prop": "text",
"formatversion": 2,
"origin": "*",
"page": page_id,
},
)
await cache.aset(f"wikipedia::{page_id}", content, timeout=cache_timeout)


async def get_article_html_from_wikipedia(page_id: str) -> tuple[str, str]:
async with httpx.AsyncClient() as client:
response = await client.get(
"https://fr.wikipedia.org/w/api.php",
params={
"action": "parse",
"format": "json",
"prop": "text",
"formatversion": 2,
"origin": "*",
"page": page_id,
},
)
if response.status_code != 200:
raise ArticleFetchError(f"Unexected response from API: {response.status_code}")

Expand Down
6 changes: 3 additions & 3 deletions backend/caviardeul/views/custom_article.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async def get_custom_article(request: HttpRequest, public_id: str) -> CustomArti
article = await aget_object_or_404(CustomArticle, public_id=public_id)

try:
content = get_article_content(article)
content = await get_article_content(article)
except ArticleFetchError:
raise HttpError(400, "L'article n'a pas été trouvé")

Expand All @@ -42,7 +42,7 @@ async def create_custom_article(
request: HttpRequest, payload: CustomArticleCreateSchema, response: HttpResponse
) -> CustomArticle:
try:
page_title, _ = get_article_html_from_wikipedia(payload.page_id)
page_title, _ = await get_article_html_from_wikipedia(payload.page_id)
except ArticleFetchError:
raise HttpError(400, "L'article n'a pas été trouvé")

Expand All @@ -63,5 +63,5 @@ async def create_custom_article(
),
)

article.content = get_article_content(article)
article.content = await get_article_content(article)
return article
2 changes: 1 addition & 1 deletion backend/caviardeul/views/daily_article.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ async def _get_daily_article_response(queryset: QuerySet[DailyArticle]):
return 404, {"detail": "L'article n'a pas été trouvé"}

try:
article.content = get_article_content(article)
article.content = await get_article_content(article)
except ArticleFetchError:
logger.exception(
"Error encountered with daily article", extra={"article_id": article.id}
Expand Down

0 comments on commit d8f2985

Please sign in to comment.