-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add django ninja base controller
- Loading branch information
1 parent
db015c3
commit 34d2084
Showing
8 changed files
with
138 additions
and
3 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from ara.controller.constants import HttpStatusCode | ||
|
||
|
||
class AraException(Exception): | ||
error_code: int | None | ||
error_reason: str | None | ||
|
||
|
||
class TooManyRequestException(AraException): | ||
"""too many requests exception""" | ||
|
||
error_code = HttpStatusCode.TOO_MANY_REQUESTS | ||
error_reason = "Too many requests" | ||
|
||
|
||
class InvalidRequestException(AraException): | ||
"""invalid request exception""" | ||
|
||
error_code = HttpStatusCode.BAD_REQUEST | ||
error_reason = "Invalid request" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from ninja import NinjaAPI | ||
from ninja.errors import ValidationError | ||
from ratelimit.exception import RateLimitException | ||
|
||
from ara.common.exceptions.ara_exception import ( | ||
InvalidRequestException, | ||
TooManyRequestException, | ||
) | ||
from ara.controller.constants import HttpStatusCode | ||
from ara.controller.ping import router as ping_router | ||
from ara.controller.response import AraErrorResponseBody | ||
from ara.settings import env | ||
|
||
docs_url = None if env("DJANGO_ENV") == "production" else "/docs" | ||
api = NinjaAPI(docs_url=docs_url) | ||
|
||
|
||
@api.exception_handler(RateLimitException) | ||
def too_many_requests(request, exception): | ||
return api.create_response( | ||
request, | ||
AraErrorResponseBody(TooManyRequestException()), | ||
status=HttpStatusCode.TOO_MANY_REQUESTS, | ||
) | ||
|
||
|
||
@api.exception_handler(ValidationError) | ||
def invalid_request(request, exception): | ||
return api.create_response( | ||
request, | ||
AraErrorResponseBody(InvalidRequestException()), | ||
status=HttpStatusCode.BAD_REQUEST, | ||
) | ||
|
||
|
||
api.add_router("/ping", ping_router) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,27 @@ | ||
class AuthLoggedInUser: | ||
# TODO | ||
pass | ||
from typing import Any | ||
|
||
from django.http import HttpRequest | ||
from ninja.security import HttpBearer | ||
from rest_framework.authentication import SessionAuthentication | ||
|
||
|
||
class AuthLoggedInUser(HttpBearer): | ||
def __call__(self, request: HttpRequest) -> Any | None: | ||
headers = request.headers | ||
auth_value = headers.get(self.header) | ||
if not auth_value: | ||
return None | ||
parts = auth_value.split(" ") | ||
|
||
if parts[0].lower() != self.openapi_scheme: | ||
return None | ||
return self.authenticate(request) | ||
|
||
def authenticate(self, request: HttpRequest, token: str) -> bool: | ||
result = SessionAuthentication().authenticate(request) | ||
if result is None: | ||
return False | ||
(user, _) = result | ||
request.user = user | ||
|
||
return bool(user and user.is_authenticated) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .ping_router import router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from django.http import HttpRequest | ||
from ninja import Router | ||
|
||
from ara.controller.authentication import AuthLoggedInUser | ||
from ara.controller.constants import HttpStatusCode | ||
from ara.controller.response import AraResponse | ||
|
||
router = Router() | ||
|
||
|
||
@router.get( | ||
"/", | ||
response={ | ||
HttpStatusCode.OK: str, | ||
HttpStatusCode.INTERNAL_SERVER_ERROR: str, | ||
}, | ||
) | ||
def ping(request: HttpRequest): | ||
return AraResponse( | ||
status_code=HttpStatusCode.OK, | ||
data="pong", | ||
) | ||
|
||
|
||
auth_router = Router(auth=AuthLoggedInUser()) | ||
|
||
|
||
@auth_router.get( | ||
"/auth", | ||
response={ | ||
HttpStatusCode.OK: str, | ||
HttpStatusCode.BAD_REQUEST: str, | ||
}, | ||
) | ||
def auth_ping(request: HttpRequest): | ||
return AraResponse( | ||
status_code=HttpStatusCode.OK, | ||
data="pong", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from django.urls import path | ||
|
||
from ara.controller.api import api | ||
|
||
urlpatterns = [ | ||
path( | ||
"", | ||
api.urls, | ||
) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters