Cache response

This commit is contained in:
2024-07-06 00:23:21 +02:00
parent 19feb42ef7
commit b3d331af31
2 changed files with 23 additions and 10 deletions

View File

@ -27,8 +27,7 @@
{% for webhook in webhooks %} {% for webhook in webhooks %}
<li class="list-group-item d-flex justify-content-between align-items-center"> <li class="list-group-item d-flex justify-content-between align-items-center">
<div> <div>
<a href="{{ webhook.webhook_url }}" target="_blank">{{ webhook.name }}</a> <a href="{{ webhook.url }}" target="_blank">{{ webhook.name }}</a> - <small>{{ webhook.status }}</small>
<small class="text-muted">added on {{ webhook.created_at|date:"F j, Y, g:i a" }}</small>
</div> </div>
<form method="post" action="" class="mb-0"> <form method="post" action="" class="mb-0">
{% csrf_token %} {% csrf_token %}

View File

@ -1,8 +1,11 @@
import datetime import datetime
import logging import logging
from dataclasses import dataclass 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.contrib import messages
from django.http import ( from django.http import (
HttpRequest, HttpRequest,
@ -21,9 +24,22 @@ from twitch_app.models import (
from .forms import DiscordSettingForm from .forms import DiscordSettingForm
if TYPE_CHECKING:
import httpx
logger: logging.Logger = logging.getLogger(__name__) 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 @dataclass
class DropContext: class DropContext:
"""The drop.""" """The drop."""
@ -210,14 +226,12 @@ class Webhooks(TemplateView):
webhooks: list[str] = cookie.split(",") webhooks: list[str] = cookie.split(",")
webhooks = list(filter(None, webhooks)) webhooks = list(filter(None, webhooks))
webhook_respones: list[WebhookData] = [] webhook_responses: list[WebhookData] = []
# Use httpx to connect to webhook url and get the response with hishel.CacheClient(storage=storage, controller=controller) as client:
# Use the response to get name of the webhook
with httpx.Client() as client:
for webhook in webhooks: for webhook in webhooks:
our_webhook = WebhookData(name="Unknown", url=webhook, status="Failed", response="No response") 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: if response.is_success:
our_webhook.name = response.json()["name"] our_webhook.name = response.json()["name"]
our_webhook.status = "Success" our_webhook.status = "Success"
@ -227,9 +241,9 @@ class Webhooks(TemplateView):
our_webhook.response = response.text our_webhook.response = response.text
# Add to the list of webhooks # 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 @require_POST