Skip to content

Commit

Permalink
Merge pull request #10 from hexfrost/dev
Browse files Browse the repository at this point in the history
Release 0.1.6
  • Loading branch information
kaziamov authored Mar 23, 2024
2 parents 3a55069 + 59bcf70 commit cd4ace1
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 3 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
build:
rm ./dist/simpleapi-* && \
poetry build
publish:
poetry publish --dry-run
Expand Down
5 changes: 2 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
[tool.poetry]
name = "hexfrost-simpleapi"
version = "0.1.0"
version = "0.1.6"
description = "Library for simple creating endpoints"
authors = ["Kaziamov <[email protected]>"]
packages = [
{ include = "simpleapi" },
]
readme = "README.md"


[tool.poetry.dependencies]
python = "^3.10"
fastapi = "^0.110.0"
fastapi = ">=0.70.0"

[tool.poetry.group.dev.dependencies]
flake8 = "^7.0.0"
Expand Down
11 changes: 11 additions & 0 deletions simpleapi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from .templates import (
EndpointsRegister,
BaseEndpoint,
GetEndpoint,
PostEndpoint,
PutEndpoint,
PatchEndpoint,
DeleteEndpoint,
HeadEndpoint,
OptionsEndpoint,
)
218 changes: 218 additions & 0 deletions simpleapi/templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
from typing import List

from fastapi import APIRouter as FastAPIRouter
from fastapi.responses import JSONResponse


class APIErrors:

@staticmethod
def _405():
return JSONResponse(
status_code=405,
content={"message": "Method Not Allowed"},
)


class AbstractPathStyle:
PATHS = {
"OPTIONS": "/{id}",
"HEAD": "/{id}",
"DELETE": "/{id}",
"PATCH": "/{id}",
"PUT": "/{id}",
"POST": "/{id}",
"GET": "/{id}",
}


class DjangoPathStyle:
PATHS = {
"OPTIONS": "/{pk}",
"HEAD": "/{pk}",
"DELETE": "/{pk}",
"PATCH": "/{pk}",
"PUT": "/{pk}",
"POST": "/{pk}",
"GET": "/{pk}",
}


class UserFrendlyPathStyle:
PATHS = {
"OPTIONS": "/{id}/options",
"HEAD": "/{id}/head",
"DELETE": "/{id}/delete",
"PATCH": "/{id}/edit",
"PUT": "/{id}/edit",
"POST": "/create",
"GET": "/list",
}


class EndpointsRegister:

def __init__(self, app, endpoints: List["BaseEndpoint"]):
for endpoint in endpoints:
prefix = endpoint.PREFIX
tags = endpoint.TAGS
methods = endpoint.methods
print(methods)
router = FastAPIRouter(prefix=prefix, tags=tags)
for http_method in methods:
path = endpoint.paths[http_method]
method = getattr(endpoint, http_method.lower())
router.add_api_route(methods=[http_method], path=path, endpoint=method)
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 set_paths(self, paths_stype: "AbstractPathStyle"):
self.paths = paths_stype.PATHS


class GetEndpoint(BaseEndpoint):

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

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

def get(self, *args, **kwargs):
return self._get(*args, **kwargs)


class PostEndpoint(BaseEndpoint):

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

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

def post(self, *args, **kwargs):
return self._post(*args, **kwargs)


class PatchEndpoint(BaseEndpoint):

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

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

def patch(self, *args, **kwargs):
return self.patch(*args, **kwargs)


class PutEndpoint(BaseEndpoint):

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

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

def put(self, *args, **kwargs):
return self._put(*args, **kwargs)


class DeleteEndpoint(BaseEndpoint):

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

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

def delete(self, *args, **kwargs):
return self._delete(*args, **kwargs)


class OptionsEndpoint(BaseEndpoint):

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

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

def options(self, *args, **kwargs):
return self._options(*args, **kwargs)


class HeadEndpoint(BaseEndpoint):

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

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

def head(self, *args, **kwargs):
return self._head(*args, **kwargs)


class TraceEndpoints(BaseEndpoint):

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

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

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,
PutEndpoint,
PatchEndpoint,
DeleteEndpoint,
HeadEndpoint,
OptionsEndpoint,
):
pass


class BaseWebhook(PostEndpoint, HeadEndpoint, OptionsEndpoint):
pass


class BaseEndpoint(AbstractEndpoint):
pass

0 comments on commit cd4ace1

Please sign in to comment.