Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add req decorator #25

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/django_twc_toolbox/decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from __future__ import annotations

import logging
from collections.abc import Callable
from functools import wraps
from typing import Any
from typing import TypeVar
from urllib.parse import urlparse

import httpx
from django.conf import settings

logger = logging.getLogger(__name__)

T = TypeVar("T")


class req:
"""
A decorator that sends a HTTP request to the specified endpoint after the
decorated function has been called. This is useful for health checks on
periodic tasks or dispatching webhooks.

Args:
url: The URL of the endpoint to send the request to.

Returns:
The decorated function.
"""

def __init__(self, url: str, *, client: httpx.Client | None = None) -> None:
self.url = url
self.client = client or httpx.Client()

def __call__(self, func: Callable[..., T]) -> Callable[..., T]:
parsed_url = urlparse(self.url)
if not parsed_url.scheme or not parsed_url.netloc:
msg = f"Invalid URL: {self.url}"
logger.error(msg)

@wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> T:
# Execute the decorated function
result = func(*args, **kwargs)

# Send the HTTP request if not in DEBUG mode
if not settings.DEBUG:
self.client.get(self.url)

return result

return wrapper
33 changes: 33 additions & 0 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from __future__ import annotations

from unittest import mock

import httpx
import pytest

from django_twc_toolbox.decorators import req


def test_req(monkeypatch):
monkeypatch.setattr("django.conf.settings.DEBUG", False)
url = "https://example.com/test-hc"

@req(url)
def foo():
pass

with mock.patch.object(httpx, "get") as mock_httpx_get:
foo()

mock_httpx_get.assert_called_once_with(url, timeout=5)


@pytest.mark.parametrize("url", ["", "invalid"])
def test_req_invalid_url(url, caplog):
with caplog.at_level("ERROR"):

@req(url)
def foo():
pass

assert "Invalid URL" in caplog.text
Loading