Problem statement
The order endpoint takes neither the supplier from the URL nor a body, so no order can be placed. Declare the body model with a rule on every field, bound the supplier in the URL, and answer a created order with 201, an unknown supplier with 404, and anything that breaks a rule with a report naming the field.
Sample Data
The suppliers in app/store.py hold the ids 1_, _2, 3_, _5 and 8_, so _4 and 99 belong to no supplier. Here is one supplier record:
{
"id": 1,
"name": "Northwind Timber"
}
API Endpoints to Implement
OrderRequest
The body model, in app/models/order.py, holding three fields and permitting no others:
- quantity, a whole number, required, greater than 0.
- unit_price, a number, required, at least 0.01 and at most 10000.
- note, text, required, at least 5_ and at most _40 characters.
Any other key is refused, supplier_id included, even when it repeats the one in the URL.
POST /suppliers/{supplier_id}/orders
Places an order with one supplier.
- supplier_id comes from the URL as a whole number of at least 1.
- Look the supplier up with find_supplier in app/services/orders.py, then hand it with the three body values to build_order, which answers with supplier_id, supplier_name, quantity, unit_price, note and total_cost, the quantity times the price rounded to two decimal places.
- Response code: 201, and 201 only. A value readable as its declared type is converted to it, so a quantity of "8" and a unit_price of "12.25" arrive as the numbers 8_ and _12.25.
- A supplier id no supplier holds is answered with 404 and the body {"detail": "Supplier 4 was not found"}, naming the id that was asked for.
- The URL and body rules are checked before the supplier is looked up, so a broken rule is answered with 422 even when the supplier does not exist.
Rejected requests
A request is answered with 422 when the URL or body breaks a rule. The body holds one key detail, a list with one entry per broken rule, each carrying loc, ["path", "supplier_id"], ["body", "field name"], or ["body"] for the body itself, input, the value received, which for a missing field is the whole body, and a type with its msg and, where a bound was compared against, a ctx:
- missing, Field required, no ctx.
- extra_forbidden, Extra inputs are not permitted, no ctx.
- greater_than on quantity, Input should be greater than 0, ctx {"gt": 0}.
- greater_than_equal on unit_price, Input should be greater than or equal to 0.01, ctx {"ge": 0.01}.
- greater_than_equal on supplier_id, Input should be greater than or equal to 1, ctx {"ge": 1}.
- less_than_equal on unit_price, Input should be less than or equal to 10000, ctx {"le": 10000.0}.
- string_too_short, String should have at least 5 characters, ctx {"min_length": 5}.
- string_too_long, String should have at most 40 characters, ctx {"max_length": 40}.
- string_type, Input should be a valid string, no ctx.
- int_parsing, Input should be a valid integer, unable to parse string as an integer, no ctx.
- int_from_float, Input should be a valid integer, got a number with a fractional part, no ctx.
- float_parsing, Input should be a valid number, unable to parse string as a number, no ctx.
The rules must sit on the model and the path parameter rather than be checked inside the handler, since the service publishes them at GET /openapi.json, where the body schema reports the three fields, requires all three, permits no others, the path parameter reports its lower bound, and 201 is the only response code the operation declares.
Sample Requests & Responses
POST /suppliers/1/orders
Request:
{
"quantity": 12,
"unit_price": 24.5,
"note": "Restock for spring"
}
Response (201 Created):
{
"supplier_id": 1,
"supplier_name": "Northwind Timber",
"quantity": 12,
"unit_price": 24.5,
"note": "Restock for spring",
"total_cost": 294.0
}
POST /suppliers/4/orders
Response (404 Not Found):
{
"detail": "Supplier 4 was not found"
}
POST /suppliers/1/orders
Request:
{
"quantity": 0,
"unit_price": 24.5,
"note": "Restock for spring",
"supplier_id": 2
}
Response (422 Unprocessable Entity):
{
"detail": [
{
"type": "greater_than",
"loc": ["body", "quantity"],
"msg": "Input should be greater than 0",
"input": 0,
"ctx": {"gt": 0}
},
{
"type": "extra_forbidden",
"loc": ["body", "supplier_id"],
"msg": "Extra inputs are not permitted",
"input": 2
}
]
}
