takahe/hatchway/urls.py
Andrew Godwin 5d2ed9edfe
Hatchway API Rewrite (#499)
Removes django-ninja and replaces it with a new API framework, "hatchway".

I plan to move hatchway into its own project very soon.
2023-02-07 12:07:15 -07:00

32 lines
922 B
Python

from collections.abc import Callable
from typing import Any
from django.http import HttpResponseNotAllowed
class Methods:
"""
Allows easy multi-method dispatch to different functions
"""
csrf_exempt = True
def __init__(self, **callables: Callable):
self.callables = {
method.lower(): callable for method, callable in callables.items()
}
unknown_methods = set(self.callables.keys()).difference(
{"get", "post", "patch", "put", "delete"}
)
if unknown_methods:
raise ValueError(f"Cannot route methods: {unknown_methods}")
def __call__(self, request, *args, **kwargs) -> Any:
method = request.method.lower()
if method in self.callables:
return self.callables[method](request, *args, **kwargs)
else:
return HttpResponseNotAllowed(self.callables.keys())
methods = Methods