Problem statement
Without types, item URLs accept anything: /items/abc reaches the handler as the text "abc". Declare the routes with typed path parameters, so FastAPI converts each value and rejects a bad one before the handler runs.
Requirements
The app, the GET / home route and the three handlers, read_item, delete_item and read_user, are already provided, together with what they return. Your task has one part, in app/main.py: make each of the three handlers a path operation.
A path operation connects an HTTP method and a path to a handler, so one path can lead to different handlers for different methods. When a path parameter has a type, FastAPI converts the text from the URL to that type, and rejects a value that does not fit before the handler runs.
- read_item handles GET /items/{item_id}, with item_id typed as int. /items/123 returns {"item_id": 123}, with the id as a number
- A non-numeric id, such as /items/abc, is rejected by FastAPI with a 422 validation error, and read_item does not run
- delete_item handles DELETE on the same path, /items/{item_id}, with item_id typed as int. DELETE /items/5 returns {"deleted": 5}
- read_user handles GET /users/{username}, with username typed as str. It accepts any text, even text that looks like a number: /users/123 returns {"username": "123"}
- Any other method on these paths answers 405, for example POST /items/5 or DELETE /users/bob
Sample Requests & Responses
GET /items/123
Response (200 OK):
{"item_id": 123}
GET /items/abc
Response (422 Unprocessable Entity):
{"detail": [{"type": "int_parsing", "loc": ["path", "item_id"], "msg": "Input should be a valid integer, unable to parse string as an integer", "input": "abc"}]}
DELETE /items/5
Response (200 OK):
{"deleted": 5}
GET /users/john.doe
Response (200 OK):
{"username": "john.doe"}
GET /users/123
Response (200 OK):
{"username": "123"}
POST /items/5
Response (405 Method Not Allowed):
{"detail": "Method Not Allowed"}
