Skip to content

Commit

Permalink
Add PUT and DELETE endpoint decorators with tests
Browse files Browse the repository at this point in the history
Signed-off-by: samadpls <[email protected]>
  • Loading branch information
samadpls committed Jan 27, 2025
1 parent 1798860 commit ecaed9f
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
58 changes: 58 additions & 0 deletions core/cat/mad_hatter/decorators/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,64 @@ def my_post_endpoint(item: Item):
**kwargs,
)

def put(
cls,
path,
prefix=default_prefix,
response_model=None,
tags=default_tags,
**kwargs,
) -> Callable:
"""
Define a custom API endpoint for PUT operation, parameters are the same as FastAPI path operation.
Examples:
.. code-block:: python
from cat.mad_hatter.decorators import endpoint
from pydantic import BaseModel
class Item(BaseModel):
name: str
description: str
@endpoint.put(path="/hello")
def my_put_endpoint(item: Item):
return {"Hello": item.name, "Description": item.description}
"""
return cls.endpoint(
path=path,
methods={"PUT"},
prefix=prefix,
response_model=response_model,
tags=tags,
**kwargs,
)

def delete(
cls,
path,
prefix=default_prefix,
response_model=None,
tags=default_tags,
**kwargs,
) -> Callable:
"""
Define a custom API endpoint for DELETE operation, parameters are the same as FastAPI path operation.
Examples:
.. code-block:: python
from cat.mad_hatter.decorators import endpoint
@endpoint.delete(path="/hello/{item_id}")
def my_delete_endpoint(item_id: int):
return {"message": f"Deleted item {item_id}"}
"""
return cls.endpoint(
path=path,
methods={"DELETE"},
prefix=prefix,
response_model=response_model,
tags=tags,
**kwargs,
)

endpoint = None

Expand Down
10 changes: 9 additions & 1 deletion core/tests/mocks/mock_plugin/mock_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@ def test_get(stray=check_permissions(AuthResource.PLUGINS, AuthPermission.LIST))

@endpoint.post(path="/crud", prefix="/tests", tags=["Tests"])
def test_post(item: Item) -> str:
return {"name": item.name, "description": item.description}
return {"name": item.name, "description": item.description}

@endpoint.put(path="/crud", prefix="/tests", tags=["Tests"])
def test_put(item: Item) -> str:
return {"name": item.name, "description": item.description}

@endpoint.delete(path="/crud/{item_id}", prefix="/tests", tags=["Tests"])
def test_delete(item_id: int):
return {"result": "ok", "deleted_id": item_id}
15 changes: 15 additions & 0 deletions core/tests/routes/test_custom_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ def test_custom_endpoint_post(client, just_installed_plugin):
assert response.json()["name"] == "the cat"
assert response.json()["description"] == "it's magic"

def test_custom_endpoint_put(client, just_installed_plugin):
payload = {"name": "the cat", "description": "it's magic"}
response = client.put("/tests/crud", json=payload)

assert response.status_code == 200
assert response.json()["name"] == "the cat"
assert response.json()["description"] == "it's magic"

def test_custom_endpoint_delete(client, just_installed_plugin):
response = client.delete("/tests/crud/123")

assert response.status_code == 200
assert response.json()["result"] == "ok"
assert response.json()["deleted_id"] == 123


@pytest.mark.parametrize("switch_type", ["deactivation", "uninstall"])
def test_custom_endpoints_on_plugin_deactivation_or_uninstall(
Expand Down

0 comments on commit ecaed9f

Please sign in to comment.