You've already forked Promocode-API
mirror of
https://github.com/devitq/Promocode-API.git
synced 2026-05-22 22:07:12 +00:00
init: initialized project struct
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
from django.urls import path
|
||||
from health_check.views import MainView
|
||||
|
||||
from api.v1.router import router as api_v1_router
|
||||
|
||||
urlpatterns = [
|
||||
path("v1/", api_v1_router.urls),
|
||||
# Health endpoint
|
||||
path("health", MainView.as_view(), name="health_check_home"),
|
||||
]
|
||||
@@ -0,0 +1,10 @@
|
||||
from django.http import HttpRequest
|
||||
from ninja.security import HttpBearer
|
||||
|
||||
|
||||
class BearerAuth(HttpBearer):
|
||||
def authenticate(self, request: HttpRequest, token: str) -> str | None:
|
||||
if token == "will implement later":
|
||||
return token
|
||||
|
||||
return None
|
||||
@@ -0,0 +1,77 @@
|
||||
import logging
|
||||
from http import HTTPStatus as status
|
||||
|
||||
import django.core.exceptions
|
||||
import django.http
|
||||
from django.http import HttpRequest, HttpResponse
|
||||
from ninja import NinjaAPI, errors
|
||||
|
||||
logger = logging.getLogger("django")
|
||||
|
||||
|
||||
def handle_django_validation_error(
|
||||
request: HttpRequest,
|
||||
exc: django.core.exceptions.ValidationError,
|
||||
router: NinjaAPI,
|
||||
) -> HttpResponse:
|
||||
detail = list(exc)
|
||||
|
||||
if hasattr(exc, "error_dict"):
|
||||
detail = dict(exc)
|
||||
|
||||
return router.create_response(
|
||||
request,
|
||||
{"detail": detail},
|
||||
status=status.UNPROCESSABLE_ENTITY,
|
||||
)
|
||||
|
||||
|
||||
def handle_authentication_error(
|
||||
request: HttpRequest, exc: errors.AuthenticationError, router: NinjaAPI
|
||||
) -> HttpResponse:
|
||||
return router.create_response(
|
||||
request,
|
||||
{"detail": status.UNAUTHORIZED.phrase},
|
||||
status=status.UNAUTHORIZED,
|
||||
)
|
||||
|
||||
|
||||
def handle_validation_error(
|
||||
request: HttpRequest, exc: errors.ValidationError, router: NinjaAPI
|
||||
) -> HttpResponse:
|
||||
return router.create_response(
|
||||
request,
|
||||
{"detail": exc.errors},
|
||||
status=status.UNPROCESSABLE_ENTITY,
|
||||
)
|
||||
|
||||
|
||||
def handle_not_found_error(
|
||||
request: HttpRequest, exc: Exception, router: NinjaAPI
|
||||
) -> HttpResponse:
|
||||
return router.create_response(
|
||||
request,
|
||||
{"detail": status.NOT_FOUND.phrase},
|
||||
status=status.NOT_FOUND,
|
||||
)
|
||||
|
||||
|
||||
def handle_unknown_exception(
|
||||
request: HttpRequest, exc: Exception, router: NinjaAPI
|
||||
) -> HttpResponse:
|
||||
logger.exception(exc)
|
||||
|
||||
return router.create_response(
|
||||
request,
|
||||
{"detail": status.INTERNAL_SERVER_ERROR.phrase},
|
||||
status=status.INTERNAL_SERVER_ERROR,
|
||||
)
|
||||
|
||||
|
||||
exception_handlers = [
|
||||
(django.core.exceptions.ValidationError, handle_django_validation_error),
|
||||
(errors.AuthenticationError, handle_authentication_error),
|
||||
(errors.ValidationError, handle_validation_error),
|
||||
(django.http.Http404, handle_not_found_error),
|
||||
(Exception, handle_unknown_exception),
|
||||
]
|
||||
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class PingConfig(AppConfig):
|
||||
name = "api.v1.ping"
|
||||
label = "api_v1_ping"
|
||||
@@ -0,0 +1,8 @@
|
||||
from ninja import Schema
|
||||
|
||||
|
||||
class PingOut(Schema):
|
||||
message_from_basement: str
|
||||
|
||||
|
||||
__all__ = ["PingOut"]
|
||||
@@ -0,0 +1,21 @@
|
||||
from http import HTTPStatus as status
|
||||
|
||||
from django.conf import settings
|
||||
from django.http import HttpRequest
|
||||
from ninja import Router
|
||||
|
||||
from api.v1.ping import schemas
|
||||
|
||||
router = Router(tags=["ping"])
|
||||
|
||||
|
||||
@router.get(
|
||||
"",
|
||||
response={status.OK: schemas.PingOut},
|
||||
summary="Ping server",
|
||||
)
|
||||
def index(
|
||||
request: HttpRequest,
|
||||
) -> schemas.PingOut:
|
||||
settings.LOGGER.info("кто-то стучится в пинг")
|
||||
return schemas.PingOut(message_from_basement="АЛЕКСАНДР ШАХОВ Я ВАШ ФОНАТ")
|
||||
@@ -0,0 +1,28 @@
|
||||
from functools import partial
|
||||
|
||||
from ninja import NinjaAPI
|
||||
|
||||
from api.v1 import handlers
|
||||
from api.v1.ping.views import router as ping_router
|
||||
|
||||
router = NinjaAPI(
|
||||
title="Promocode API",
|
||||
version="1",
|
||||
description="API docs for Promocode",
|
||||
openapi_url="/docs/openapi.json",
|
||||
# csrf=True, noqa: ERA001
|
||||
)
|
||||
|
||||
|
||||
# Register application's routers
|
||||
|
||||
router.add_router(
|
||||
"ping",
|
||||
ping_router,
|
||||
)
|
||||
|
||||
|
||||
# Register exception handlers
|
||||
|
||||
for exception, handler in handlers.exception_handlers:
|
||||
router.add_exception_handler(exception, partial(handler, router=router))
|
||||
@@ -0,0 +1,11 @@
|
||||
from http import HTTPStatus as status
|
||||
|
||||
from ninja import Schema
|
||||
|
||||
|
||||
class UnauthorizedError(Schema):
|
||||
detail: str = status.UNAUTHORIZED.phrase
|
||||
|
||||
|
||||
class NotFoundError(Schema):
|
||||
detail: str = status.NOT_FOUND.phrase
|
||||
Reference in New Issue
Block a user