From b3d331af31edd3b59f7fe905893fc2503784e6e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Hells=C3=A9n?= Date: Sat, 6 Jul 2024 00:23:21 +0200 Subject: [PATCH] Cache response --- core/templates/webhooks.html | 3 +-- core/views.py | 30 ++++++++++++++++++++++-------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/core/templates/webhooks.html b/core/templates/webhooks.html index e4c143a..76a82b9 100644 --- a/core/templates/webhooks.html +++ b/core/templates/webhooks.html @@ -27,8 +27,7 @@ {% for webhook in webhooks %}
  • - {{ webhook.name }} - added on {{ webhook.created_at|date:"F j, Y, g:i a" }} + {{ webhook.name }} - {{ webhook.status }}
    {% csrf_token %} diff --git a/core/views.py b/core/views.py index 61e1b66..928611a 100644 --- a/core/views.py +++ b/core/views.py @@ -1,8 +1,11 @@ import datetime import logging from dataclasses import dataclass +from pathlib import Path +from typing import TYPE_CHECKING -import httpx +import hishel +from django.conf import settings from django.contrib import messages from django.http import ( HttpRequest, @@ -21,9 +24,22 @@ from twitch_app.models import ( from .forms import DiscordSettingForm +if TYPE_CHECKING: + import httpx + logger: logging.Logger = logging.getLogger(__name__) +cache_dir: Path = settings.DATA_DIR / "cache" +cache_dir.mkdir(exist_ok=True, parents=True) +storage = hishel.FileStorage(base_path=cache_dir) +controller = hishel.Controller( + cacheable_status_codes=[200, 203, 204, 206, 300, 301, 308, 404, 405, 410, 414, 501], + allow_stale=True, + always_revalidate=True, +) + + @dataclass class DropContext: """The drop.""" @@ -210,14 +226,12 @@ class Webhooks(TemplateView): webhooks: list[str] = cookie.split(",") webhooks = list(filter(None, webhooks)) - webhook_respones: list[WebhookData] = [] + webhook_responses: list[WebhookData] = [] - # Use httpx to connect to webhook url and get the response - # Use the response to get name of the webhook - with httpx.Client() as client: + with hishel.CacheClient(storage=storage, controller=controller) as client: for webhook in webhooks: our_webhook = WebhookData(name="Unknown", url=webhook, status="Failed", response="No response") - response: httpx.Response = client.get(url=webhook) + response: httpx.Response = client.get(url=webhook, extensions={"cache_metadata": True}) if response.is_success: our_webhook.name = response.json()["name"] our_webhook.status = "Success" @@ -227,9 +241,9 @@ class Webhooks(TemplateView): our_webhook.response = response.text # Add to the list of webhooks - webhook_respones.append(our_webhook) + webhook_responses.append(our_webhook) - return {"webhooks": webhook_respones, "form": DiscordSettingForm()} + return {"webhooks": webhook_responses, "form": DiscordSettingForm()} @require_POST