Skip to content

Instantly share code, notes, and snippets.

@rchaganti
Created February 15, 2026 09:03
Show Gist options
  • Select an option

  • Save rchaganti/8449850eb83bc7e7834dded9ff275395 to your computer and use it in GitHub Desktop.

Select an option

Save rchaganti/8449850eb83bc7e7834dded9ff275395 to your computer and use it in GitHub Desktop.
A mock OpenAPI server
from fastapi import FastAPI, HTTPException, Query, Path
from pydantic import BaseModel, Field
from typing import Optional
from datetime import datetime
from enum import Enum
import uuid
import uvicorn
app = FastAPI(
title="Task Manager API",
version="1.0.0",
servers=[{"url": "http://localhost:3000", "description": "Local development server"}]
)
class TaskStatus(str, Enum):
pending = "pending"
in_progress = "in-progress"
completed = "completed"
class NewTask(BaseModel):
title: str
description: Optional[str] = None
status: TaskStatus
class Task(BaseModel):
id: str = Field(default_factory=lambda: str(uuid.uuid4()))
title: str
description: Optional[str] = None
status: TaskStatus
createdAt: datetime = Field(default_factory=datetime.utcnow)
updatedAt: Optional[datetime] = None
class Error(BaseModel):
code: int
message: str
# In-memory task storage with some mock data
tasks_db: dict[str, Task] = {}
# Add some initial mock tasks
def init_mock_data():
mock_tasks = [
Task(
id="550e8400-e29b-41d4-a716-446655440001",
title="Review pull request",
description="Review the new feature PR from the team",
status=TaskStatus.in_progress,
createdAt=datetime(2026, 2, 14, 10, 0, 0),
),
Task(
id="550e8400-e29b-41d4-a716-446655440002",
title="Write documentation",
description="Document the API endpoints",
status=TaskStatus.pending,
createdAt=datetime(2026, 2, 13, 9, 0, 0),
),
Task(
id="550e8400-e29b-41d4-a716-446655440003",
title="Fix login bug",
description="Users unable to login with special characters in password",
status=TaskStatus.completed,
createdAt=datetime(2026, 2, 12, 14, 30, 0),
updatedAt=datetime(2026, 2, 14, 16, 0, 0),
),
Task(
id="550e8400-e29b-41d4-a716-446655440004",
title="Deploy to staging",
description="Deploy the latest build to staging environment",
status=TaskStatus.in_progress,
createdAt=datetime(2026, 2, 15, 8, 0, 0),
),
]
for task in mock_tasks:
tasks_db[task.id] = task
init_mock_data()
@app.get("/api/tasks", response_model=list[Task])
async def list_tasks(
status: Optional[TaskStatus] = Query(None, description="Filter tasks by status"),
limit: Optional[int] = Query(None, ge=1, le=100, description="Maximum number of tasks to return"),
):
"""Get all tasks, optionally filtered by status."""
result = list(tasks_db.values())
if status:
result = [t for t in result if t.status == status]
if limit:
result = result[:limit]
return result
@app.post("/api/tasks", response_model=Task, status_code=201)
async def create_task(new_task: NewTask):
"""Create a new task."""
task = Task(
title=new_task.title,
description=new_task.description,
status=new_task.status,
)
tasks_db[task.id] = task
return task
@app.get("/api/tasks/{taskId}", response_model=Task)
async def get_task_by_id(
taskId: str = Path(..., description="Unique identifier of the task"),
):
"""Get a task by ID."""
if taskId not in tasks_db:
raise HTTPException(status_code=404, detail="Task not found")
return tasks_db[taskId]
@app.put("/api/tasks/{taskId}", response_model=Task)
async def update_task_by_id(
taskId: str = Path(..., description="Unique identifier of the task"),
updated_task: NewTask = ...,
):
"""Update a task by ID."""
if taskId not in tasks_db:
raise HTTPException(status_code=404, detail="Task not found")
existing = tasks_db[taskId]
existing.title = updated_task.title
existing.description = updated_task.description
existing.status = updated_task.status
existing.updatedAt = datetime.utcnow()
return existing
@app.delete("/api/tasks/{taskId}", status_code=204)
async def delete_task_by_id(
taskId: str = Path(..., description="Unique identifier of the task"),
):
"""Delete a task by ID."""
if taskId not in tasks_db:
raise HTTPException(status_code=404, detail="Task not found")
del tasks_db[taskId]
return None
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=3000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment