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

Release 0.1.7 #12

Merged
merged 2 commits into from
Mar 24, 2024
Merged
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "hexfrost-simpleapi"
version = "0.1.6"
version = "0.1.7"
description = "Library for simple creating endpoints"
authors = ["Kaziamov <[email protected]>"]
packages = [
Expand Down
126 changes: 64 additions & 62 deletions simpleapi/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
from fastapi import APIRouter as FastAPIRouter
from fastapi.responses import JSONResponse

HTTP_GET = "GET"
HTTP_POST = "POST"
HTTP_PUT = "PUT"
HTTP_PATCH = "PATCH"
HTTP_DELETE = "DELETE"
HTTP_HEAD = "HEAD"
HTTP_OPTIONS = "OPTIONS"
HTTP_TRACE = "TRACE"
HTTP_CONNECT = "CONNECT"


class APIErrors:

Expand All @@ -16,43 +26,43 @@ def _405():

class AbstractPathStyle:
PATHS = {
"OPTIONS": "/{id}",
"HEAD": "/{id}",
"DELETE": "/{id}",
"PATCH": "/{id}",
"PUT": "/{id}",
HTTP_OPTIONS: "/{id}",
HTTP_HEAD: "/{id}",
HTTP_DELETE: "/{id}",
HTTP_PATCH: "/{id}",
HTTP_PUT: "/{id}",
"POST": "/{id}",
"GET": "/{id}",
}


class DjangoPathStyle:
PATHS = {
"OPTIONS": "/{pk}",
"HEAD": "/{pk}",
"DELETE": "/{pk}",
"PATCH": "/{pk}",
"PUT": "/{pk}",
HTTP_OPTIONS: "/{pk}",
HTTP_HEAD: "/{pk}",
HTTP_DELETE: "/{pk}",
HTTP_PATCH: "/{pk}",
HTTP_PUT: "/{pk}",
"POST": "/{pk}",
"GET": "/{pk}",
HTTP_GET: "/{pk}",
}


class UserFrendlyPathStyle:
PATHS = {
"OPTIONS": "/{id}/options",
"HEAD": "/{id}/head",
"DELETE": "/{id}/delete",
"PATCH": "/{id}/edit",
"PUT": "/{id}/edit",
"POST": "/create",
"GET": "/list",
HTTP_OPTIONS: "/{id}/options",
HTTP_HEAD: "/{id}/head",
HTTP_DELETE: "/{id}/delete",
HTTP_PATCH: "/{id}/edit",
HTTP_PUT: "/{id}/edit",
HTTP_POST: "/create",
HTTP_GET: "/list",
}


class EndpointsRegister:

def __init__(self, app, endpoints: List["BaseEndpoint"]):
def __init__(self, app, endpoints: List["BaseEndpoint"], generate_head: bool = True, generate_options: bool = True):
for endpoint in endpoints:
prefix = endpoint.PREFIX
tags = endpoint.TAGS
Expand All @@ -63,29 +73,45 @@ def __init__(self, app, endpoints: List["BaseEndpoint"]):
path = endpoint.paths[http_method]
method = getattr(endpoint, http_method.lower())
router.add_api_route(methods=[http_method], path=path, endpoint=method)
if generate_head:
router.add_api_route(methods=[HTTP_HEAD], path=path, endpoint=method, include_in_schema=False)
if generate_options:
router.add_api_route(methods=[HTTP_OPTIONS], path=path, endpoint=method, include_in_schema=False)
app.include_router(router)


class AbstractEndpoint:
PREFIX: str = None
TAGS: List[str] = None


class BaseEndpoint:

def __init__(self, *args, **kwargs):
self.methods = []
self.paths = kwargs.get("paths", UserFrendlyPathStyle).PATHS

def _get_http_method(self):
raise NotImplementedError

def _get_content_type(self):
raise NotImplementedError

def set_paths(self, paths_stype: "AbstractPathStyle"):
self.paths = paths_stype.PATHS


class GetEndpoint(BaseEndpoint):
class BaseEndpoint(AbstractEndpoint):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.methods.append("GET")
self.methods.append(self._get_http_method())

def _get_content_type(self):
return JSONResponse.media_type


class GetEndpoint(BaseEndpoint):

def _get_http_method(self):
return HTTP_GET

def _get(self, *args, **kwargs):
raise NotImplementedError
Expand All @@ -96,9 +122,8 @@ def get(self, *args, **kwargs):

class PostEndpoint(BaseEndpoint):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.methods.append("POST")
def _get_http_method(self):
return HTTP_POST

def _post(self, *args, **kwargs):
raise NotImplementedError
Expand All @@ -109,9 +134,8 @@ def post(self, *args, **kwargs):

class PatchEndpoint(BaseEndpoint):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.methods.append("PATCH")
def _get_http_method(self):
return HTTP_PATCH

def _patch(self, *args, **kwargs):
raise NotImplementedError
Expand All @@ -122,9 +146,8 @@ def patch(self, *args, **kwargs):

class PutEndpoint(BaseEndpoint):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.methods.append("PUT")
def _get_http_method(self):
return HTTP_PUT

def _put(self, *args, **kwargs):
raise NotImplementedError
Expand All @@ -135,9 +158,8 @@ def put(self, *args, **kwargs):

class DeleteEndpoint(BaseEndpoint):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.methods.append("DELETE")
def _get_http_method(self):
return HTTP_DELETE

def _delete(self, *args, **kwargs):
raise NotImplementedError
Expand All @@ -148,9 +170,8 @@ def delete(self, *args, **kwargs):

class OptionsEndpoint(BaseEndpoint):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.methods.append("OPTIONS")
def _get_http_method(self):
return HTTP_OPTIONS

def _options(self, *args, **kwargs):
raise NotImplementedError
Expand All @@ -161,9 +182,8 @@ def options(self, *args, **kwargs):

class HeadEndpoint(BaseEndpoint):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.methods.append("HEAD")
def _get_http_method(self):
return HTTP_HEAD

def _head(self, *args, **kwargs):
raise NotImplementedError
Expand All @@ -174,9 +194,8 @@ def head(self, *args, **kwargs):

class TraceEndpoints(BaseEndpoint):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.methods.append("TRACE")
def _get_http_method(self):
return HTTP_TRACE

def _trace(self, *args, **kwargs):
raise NotImplementedError
Expand All @@ -185,19 +204,6 @@ def trace(self, *args, **kwargs):
return self._trace(*args, **kwargs)


class ConnectEndpoint(BaseEndpoint):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.methods.append("CONNECT")

def _connect(self, *args, **kwargs):
raise NotImplementedError

def connect(self, *args, **kwargs):
pass


class AbstractRouter(
GetEndpoint,
PostEndpoint,
Expand All @@ -212,7 +218,3 @@ class AbstractRouter(

class BaseWebhook(PostEndpoint, HeadEndpoint, OptionsEndpoint):
pass


class BaseEndpoint(AbstractEndpoint):
pass
Loading