Skip to content

Commit 935e272

Browse files
phernandezclaude
andcommitted
refactor: Add explicit HTTP status codes for API clarity
Implements the optional improvement suggested in PR review to make HTTP status codes explicit rather than implicit. Changes: - Added explicit status_code=201 to POST /projects/projects decorator - Added Response parameter to override status code for idempotent requests - Returns 201 Created for new projects, 200 OK for existing (idempotent) - Updated tests to verify correct status codes (201 vs 200) This improves API documentation clarity and follows REST best practices for idempotent operations. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: phernandez <paul@basicmachines.co>
1 parent 0848009 commit 935e272

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

src/basic_memory/api/routers/project_router.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Router for project management."""
22

33
import os
4-
from fastapi import APIRouter, HTTPException, Path, Body, BackgroundTasks
4+
from fastapi import APIRouter, HTTPException, Path, Body, BackgroundTasks, Response
55
from typing import Optional
66
from loguru import logger
77

@@ -180,8 +180,9 @@ async def list_projects(
180180

181181

182182
# Add a new project
183-
@project_resource_router.post("/projects", response_model=ProjectStatusResponse)
183+
@project_resource_router.post("/projects", response_model=ProjectStatusResponse, status_code=201)
184184
async def add_project(
185+
response: Response,
185186
project_data: ProjectInfoRequest,
186187
project_service: ProjectServiceDep,
187188
) -> ProjectStatusResponse:
@@ -205,6 +206,7 @@ async def add_project(
205206

206207
if requested_path == existing_path:
207208
# Same name, same path - return 200 OK (idempotent)
209+
response.status_code = 200
208210
return ProjectStatusResponse( # pyright: ignore [reportCallIssue]
209211
message=f"Project '{project_data.name}' already exists",
210212
status="success",

tests/api/test_project_router.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,8 @@ async def test_create_project_idempotent_same_path(test_config, client, project_
571571
json={"name": test_project_name, "path": test_project_path, "set_default": False},
572572
)
573573

574-
# Should succeed
575-
assert response1.status_code == 200
574+
# Should succeed with 201 Created
575+
assert response1.status_code == 201
576576
data1 = response1.json()
577577
assert data1["status"] == "success"
578578
assert data1["new_project"]["name"] == test_project_name
@@ -610,8 +610,8 @@ async def test_create_project_fails_different_path(test_config, client, project_
610610
json={"name": test_project_name, "path": test_project_path1, "set_default": False},
611611
)
612612

613-
# Should succeed
614-
assert response1.status_code == 200
613+
# Should succeed with 201 Created
614+
assert response1.status_code == 201
615615

616616
# Try to create the same project with different path
617617
test_project_path2 = "/tmp/test-path-conflict-2"

0 commit comments

Comments
 (0)