Skip to content

Commit

Permalink
optionally support simple endpoint names
Browse files Browse the repository at this point in the history
  • Loading branch information
JakNowy committed Jun 19, 2024
1 parent d10b6e0 commit 22949ce
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions fastcrud/endpoint/endpoint_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,18 @@ async def endpoint(db: AsyncSession = Depends(self.session), **pkeys):

return endpoint

def _get_full_path(self, operation: str):
endpoint_name = self._get_endpoint_name(operation)
_primary_keys_path_suffix = "/".join(
f"{{{n}}}" for n in self.primary_key_names)

if operation in {'read', 'update', 'delete', 'db_delete'}:
return f"{self.path}/{endpoint_name}" \
if endpoint_name \
else f"{self.path}/{_primary_keys_path_suffix}"
else:
return f"{self.path}/{endpoint_name}" if endpoint_name else self.path

def _get_endpoint_name(self, operation: str) -> str:
"""Get the endpoint name for a given CRUD operation, using defaults if not overridden by the user."""
return self.endpoint_names.get(
Expand Down Expand Up @@ -497,12 +509,9 @@ def get_current_user(...):
if self.delete_schema:
delete_description = "Soft delete a"

_primary_keys_path_suffix = "/".join(f"{{{n}}}" for n in self.primary_key_names)

if ("create" in included_methods) and ("create" not in deleted_methods):
endpoint_name = self._get_endpoint_name("create")
self.router.add_api_route(
f"{self.path}/{endpoint_name}",
self._get_full_path(operation='create'),
self._create_item(),
methods=["POST"],
include_in_schema=self.include_in_schema,
Expand All @@ -512,10 +521,8 @@ def get_current_user(...):
)

if ("read" in included_methods) and ("read" not in deleted_methods):
endpoint_name = self._get_endpoint_name("read")

self.router.add_api_route(
f"{self.path}/{endpoint_name}/{_primary_keys_path_suffix}",
self._get_full_path(operation='read'),
self._read_item(),
methods=["GET"],
include_in_schema=self.include_in_schema,
Expand All @@ -539,9 +546,8 @@ def get_current_user(...):
if ("read_paginated" in included_methods) and (
"read_paginated" not in deleted_methods
):
endpoint_name = self._get_endpoint_name("read_paginated")
self.router.add_api_route(
f"{self.path}/{endpoint_name}",
self._get_full_path(operation='read_paginated'),
self._read_paginated(),
methods=["GET"],
include_in_schema=self.include_in_schema,
Expand All @@ -553,7 +559,7 @@ def get_current_user(...):
if ("update" in included_methods) and ("update" not in deleted_methods):
endpoint_name = self._get_endpoint_name("update")
self.router.add_api_route(
f"{self.path}/{endpoint_name}/{_primary_keys_path_suffix}",
self._get_full_path(operation='update'),
self._update_item(),
methods=["PATCH"],
include_in_schema=self.include_in_schema,
Expand All @@ -563,9 +569,8 @@ def get_current_user(...):
)

if ("delete" in included_methods) and ("delete" not in deleted_methods):
endpoint_name = self._get_endpoint_name("delete")
self.router.add_api_route(
f"{self.path}/{endpoint_name}/{_primary_keys_path_suffix}",
self._get_full_path(operation='delete'),
self._delete_item(),
methods=["DELETE"],
include_in_schema=self.include_in_schema,
Expand All @@ -579,9 +584,8 @@ def get_current_user(...):
and ("db_delete" not in deleted_methods)
and self.delete_schema
):
endpoint_name = self._get_endpoint_name("db_delete")
self.router.add_api_route(
f"{self.path}/{endpoint_name}/{_primary_keys_path_suffix}",
self._get_full_path(operation='db_delete'),
self._db_delete(),
methods=["DELETE"],
include_in_schema=self.include_in_schema,
Expand Down

0 comments on commit 22949ce

Please sign in to comment.