Problem statement
A file-storage browser needs its whole route table: object ids, a recent view, owners, categories and nested file paths. Build the table so every URL reaches its own handler with the right types, and no route catches another route's URLs.
Requirements
The app, the GET / home route, the StorageCategory enum in app/schemas.py, the data in app/store.py and the six handlers, read_object, delete_object, list_recent_objects, read_owner, list_category and read_file, are already provided, together with what they return. Your task has one part, in app/main.py: make each of the six handlers a path operation, so that all of the rules below hold at the same time.
This question uses everything from the module: typed path parameters, methods, route order, the path converter and Enum parameters.
- read_object handles GET /objects/{object_id}, with object_id as an int, and returns that object: /objects/1 returns {"object_id": 1, "name": "budget.xlsx", "size_bytes": 2048, "category": "documents"}
- A non-numeric object id other than recent, such as /objects/abc, is rejected by FastAPI with a 422 validation error
- delete_object handles DELETE /objects/{object_id}, with object_id as an int: DELETE /objects/2 returns {"deleted": 2}
- Any other method on /objects/{object_id}, such as POST or PUT, answers 405
- list_recent_objects handles GET /objects/recent and returns {"recent": [7, 2, 1], "is_recent": true}. The word recent is never read as an object id
- read_owner handles GET /owners/{owner}, with owner as a string that accepts any text: /owners/alice returns {"owner": "alice", "object_count": 3}, and /owners/123 returns {"owner": "123", "object_count": 0}
- list_category handles GET /categories/{category}, with category as the StorageCategory enum, and returns that category's listing: /categories/images returns {"category": "images", "files": ["photo.png", "avatar.jpg"], "count": 2}
- Any other category, such as music or Images, is rejected by FastAPI with a 422 validation error that lists the allowed values
- The API docs show the three allowed values for category: images, documents and videos
- read_file handles GET /files/{file_path}, where file_path takes the whole rest of the URL, slashes included: /files/reports/2024/q1.pdf returns {"file_path": "reports/2024/q1.pdf"}
Sample Requests & Responses
GET /objects/1
Response (200 OK):
{"object_id": 1, "name": "budget.xlsx", "size_bytes": 2048, "category": "documents"}
GET /objects/abc
Response (422 Unprocessable Entity):
{"detail": [{"type": "int_parsing", "loc": ["path", "object_id"], "msg": "Input should be a valid integer, unable to parse string as an integer", "input": "abc"}]}
DELETE /objects/2
Response (200 OK):
{"deleted": 2}
GET /objects/recent
Response (200 OK):
{"recent": [7, 2, 1], "is_recent": true}
GET /owners/alice
Response (200 OK):
{"owner": "alice", "object_count": 3}
GET /owners/123
Response (200 OK):
{"owner": "123", "object_count": 0}
GET /categories/images
Response (200 OK):
{"category": "images", "files": ["photo.png", "avatar.jpg"], "count": 2}
GET /categories/music
Response (422 Unprocessable Entity):
{"detail": [{"type": "enum", "loc": ["path", "category"], "msg": "Input should be 'images', 'documents' or 'videos'", "input": "music", "ctx": {"expected": "'images', 'documents' or 'videos'"}}]}
GET /files/reports/2024/q1.pdf
Response (200 OK):
{"file_path": "reports/2024/q1.pdf"}
GET /openapi.json
Response (200 OK), the part that describes category:
{"type": "string", "enum": ["images", "documents", "videos"], "title": "StorageCategory", "description": "Permitted values for the category path segment."}
