Skip to content

Commit

Permalink
feat(openapi): add to all methods
Browse files Browse the repository at this point in the history
  • Loading branch information
VishnuSanal committed Jul 12, 2024
1 parent 0bb4441 commit fd10f66
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 13 deletions.
119 changes: 107 additions & 12 deletions robyn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,109 +323,204 @@ def get(
"""

def inner(handler):
if endpoint not in self.openapi_schema["paths"]:
openapi_obj = get_openapi_obj(endpoint, HttpMethod.GET, openapi_summary, openapi_tags)
openapi_obj = get_openapi_obj(endpoint, openapi_summary, openapi_tags)

if endpoint not in self.openapi_schema["paths"]:
self.openapi_schema["paths"][endpoint] = {}

self.openapi_schema["paths"][endpoint]["get"] = openapi_obj
self.openapi_schema["paths"][endpoint]["get"] = openapi_obj

return self.add_route(HttpMethod.GET, endpoint, handler, const, auth_required)

return inner

def post(self, endpoint: str, auth_required: bool = False):
def post(
self,
endpoint: str,
auth_required: bool = False,
openapi_summary: str = "",
openapi_tags: list = ["default"],
):
"""
The @app.post decorator to add a route with POST method
:param endpoint str: endpoint to server the route
"""

def inner(handler):
openapi_obj = get_openapi_obj(endpoint, openapi_summary, openapi_tags)

if endpoint not in self.openapi_schema["paths"]:
self.openapi_schema["paths"][endpoint] = {}
self.openapi_schema["paths"][endpoint]["post"] = openapi_obj

return self.add_route(HttpMethod.POST, endpoint, handler, auth_required=auth_required)

return inner

def put(self, endpoint: str, auth_required: bool = False):
def put(
self,
endpoint: str,
auth_required: bool = False,
openapi_summary: str = "",
openapi_tags: list = ["default"],
):
"""
The @app.put decorator to add a get route with PUT method
:param endpoint str: endpoint to server the route
"""

def inner(handler):
openapi_obj = get_openapi_obj(endpoint, openapi_summary, openapi_tags)

if endpoint not in self.openapi_schema["paths"]:
self.openapi_schema["paths"][endpoint] = {}
self.openapi_schema["paths"][endpoint]["put"] = openapi_obj

return self.add_route(HttpMethod.PUT, endpoint, handler, auth_required=auth_required)

return inner

def delete(self, endpoint: str, auth_required: bool = False):
def delete(
self,
endpoint: str,
auth_required: bool = False,
openapi_summary: str = "",
openapi_tags: list = ["default"],
):
"""
The @app.delete decorator to add a route with DELETE method
:param endpoint str: endpoint to server the route
"""

def inner(handler):
openapi_obj = get_openapi_obj(endpoint, openapi_summary, openapi_tags)

if endpoint not in self.openapi_schema["paths"]:
self.openapi_schema["paths"][endpoint] = {}
self.openapi_schema["paths"][endpoint]["delete"] = openapi_obj

return self.add_route(HttpMethod.DELETE, endpoint, handler, auth_required=auth_required)

return inner

def patch(self, endpoint: str, auth_required: bool = False):
def patch(
self,
endpoint: str,
auth_required: bool = False,
openapi_summary: str = "",
openapi_tags: list = ["default"],
):
"""
The @app.patch decorator to add a route with PATCH method
:param endpoint [str]: [endpoint to server the route]
"""

def inner(handler):
openapi_obj = get_openapi_obj(endpoint, openapi_summary, openapi_tags)

if endpoint not in self.openapi_schema["paths"]:
self.openapi_schema["paths"][endpoint] = {}
self.openapi_schema["paths"][endpoint]["patch"] = openapi_obj

return self.add_route(HttpMethod.PATCH, endpoint, handler, auth_required=auth_required)

return inner

def head(self, endpoint: str, auth_required: bool = False):
def head(
self,
endpoint: str,
auth_required: bool = False,
openapi_summary: str = "",
openapi_tags: list = ["default"],
):
"""
The @app.head decorator to add a route with HEAD method
:param endpoint str: endpoint to server the route
"""

def inner(handler):
openapi_obj = get_openapi_obj(endpoint, openapi_summary, openapi_tags)

if endpoint not in self.openapi_schema["paths"]:
self.openapi_schema["paths"][endpoint] = {}
self.openapi_schema["paths"][endpoint]["head"] = openapi_obj

return self.add_route(HttpMethod.HEAD, endpoint, handler, auth_required=auth_required)

return inner

def options(self, endpoint: str, auth_required: bool = False):
def options(
self,
endpoint: str,
auth_required: bool = False,
openapi_summary: str = "",
openapi_tags: list = ["default"],
):
"""
The @app.options decorator to add a route with OPTIONS method
:param endpoint str: endpoint to server the route
"""

def inner(handler):
openapi_obj = get_openapi_obj(endpoint, openapi_summary, openapi_tags)

if endpoint not in self.openapi_schema["paths"]:
self.openapi_schema["paths"][endpoint] = {}
self.openapi_schema["paths"][endpoint]["options"] = openapi_obj

return self.add_route(HttpMethod.OPTIONS, endpoint, handler, auth_required=auth_required)

return inner

def connect(self, endpoint: str, auth_required: bool = False):
def connect(
self,
endpoint: str,
auth_required: bool = False,
openapi_summary: str = "",
openapi_tags: list = ["default"],
):
"""
The @app.connect decorator to add a route with CONNECT method
:param endpoint str: endpoint to server the route
"""

def inner(handler):
openapi_obj = get_openapi_obj(endpoint, openapi_summary, openapi_tags)

if endpoint not in self.openapi_schema["paths"]:
self.openapi_schema["paths"][endpoint] = {}
self.openapi_schema["paths"][endpoint]["connect"] = openapi_obj

return self.add_route(HttpMethod.CONNECT, endpoint, handler, auth_required=auth_required)

return inner

def trace(self, endpoint: str, auth_required: bool = False):
def trace(
self,
endpoint: str,
auth_required: bool = False,
openapi_summary: str = "",
openapi_tags: list = ["default"],
):
"""
The @app.trace decorator to add a route with TRACE method
:param endpoint str: endpoint to server the route
"""

def inner(handler):
openapi_obj = get_openapi_obj(endpoint, openapi_summary, openapi_tags)

if endpoint not in self.openapi_schema["paths"]:
self.openapi_schema["paths"][endpoint] = {}
self.openapi_schema["paths"][endpoint]["trace"] = openapi_obj

return self.add_route(HttpMethod.TRACE, endpoint, handler, auth_required=auth_required)

return inner
Expand Down
2 changes: 1 addition & 1 deletion robyn/openapi_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def extract_path_params(path: str):
return re.findall(r"{(.*?)}", path)


def get_openapi_obj(path: str, _: str, summary: str, tags: list):
def get_openapi_obj(path: str, summary: str, tags: list):
path_params = extract_path_params(path)
parameters = []
for param in path_params:
Expand Down
5 changes: 5 additions & 0 deletions testing_application/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@ async def get_user(name: str):
return {"message": f"User {name}"}


@app.delete("/users/{name}", openapi_summary="Delete User by ID", openapi_tags=["Users"])
async def delete_user(name: str):
return f"Successfully deleted {name}"


if __name__ == "__main__":
app.start()

0 comments on commit fd10f66

Please sign in to comment.