Problem statement
Build a tool library API with documentation ready to hand to partners: its own identity, described groups and operations, example loans, and its own documentation paths.
Sample Data
The library in app/store.py starts with four tools, filed under the codes TL-118, TL-204, TL-331 and TL-476, so TL-999 and TL-000 are filed under nobody. Two loans are already on the desk, and the next loan filed takes the id 3. Here is one tool record:
{"code": "TL-118", "name": "Cordless Drill", "shelf": "A2", "condition": "good", "on_loan": false}
API Endpoints to Implement
The work is in four files:
- app/core/application.py: create_app, which builds the application
- app/api/routes/tools.py: the three tool handlers
- app/api/routes/loans.py: the two loan handlers
- app/models/loan.py: the drafted loan, LoanDraft
The handlers call list_tools, find_tool, list_shelves, list_loans and add_loan in app/services/, and answer with Tool, Shelf, Loan and Message from app/models/.
Service identity
- The published name is Tool Library Partner API and the published version is 3.1.0.
- The published description is exactly this line, markdown included:
Public access to the **Tool Library** catalogue. Terms at [toollibrary.example.com/terms](https://toollibrary.example.com/terms).
- The contact is published under three keys, name, url and email, holding Library Partner Desk, https://toollibrary.example.com/partners and partners@toollibrary.example.com.
- The license is published under two keys, name and url, holding MIT and https://opensource.org/license/mit.
- The published document reports all five under info, taken from the application itself, not written by hand.
Tag descriptions
The three tags the operations are filed under are published in this order, each carrying a name and a description and nothing else:
- loans, described as Loans the lending desk has filed.
- tools, described as Tools the library lends out.
- legacy, described as Retired listings kept for older callers.
The application carries this list itself, in this order, which is neither alphabetical nor the order the operations are declared in.
Documentation paths
- The application carries three documentation paths of its own: the document at /public/schema.json, the interactive page at /public/docs, and the reference page at /public/reference.
- Both pages read the document from /public/schema.json and take their heading from the published name.
- The three framework defaults, /docs, /redoc and /openapi.json, are retired. Nothing is registered at any of them, so each answers 404 with the body {"detail": "Not Found"}.
GET /tools
- Answers 200 with every tool in the order the library shelves them.
- Summary: List every tool
- Description: Returns the whole catalogue, one entry per tool, in the order the library shelves them.
- Tag: tools
- Response description: Every tool the library lends out
- Answer: an array of the registered Tool shape.
GET /tools/{tool_code}
- tool_code comes from the URL as text, and is published as the operation's only path parameter, required, of type string, described as Code the library files the tool under.
- Answers 200 with the tool filed under that code.
- Summary: Look up one tool
- Description: Returns the one tool filed under the given code, and reports a miss when no tool is filed under it.
- Tag: tools
- Response description: The tool filed under that code
- Answer: the registered Tool shape.
- A code no tool is filed under is answered with 404 and the body {"detail": "Tool TL-999 was not found"}, naming the code that was asked for.
- The document publishes that 404 answer as the registered Message shape, described as No tool is filed under that code.
GET /shelves
- Answers 200 with the same four tools in the retired shelf shape, each carrying only code, bay and available.
- Summary: List every shelf slot
- Description: Returns the whole catalogue in the retired shelf shape, kept for callers that have not moved to the tool listing.
- Tag: legacy
- Response description: Every shelf slot in the retired shape
- Answer: an array of the registered Shelf shape.
- Marked as deprecated. The other four operations are not.
GET /loans
- Answers 200 with every loan in the order the desk filed them.
- Summary: List every loan
- Description: Returns every loan the desk has filed, in the order it filed them.
- Tag: loans
- Response description: Every loan the desk holds
- Answer: an array of the registered Loan shape.
POST /loans
- Takes the body as a LoanDraft and answers 201 with the filed loan: the drafted values plus the id it was given.
- A filed loan joins the desk, so a fresh listing reports it after the two the desk started with.
- Summary: File a loan
- Description: Files a drafted loan against the desk and answers with the loan as it was filed.
- Tag: loans
- Response description: The loan the desk filed
- Answer: the registered Loan shape.
- tool_code is text, 5 to 8 characters, required.
- member is text, 2 to 40 characters, required.
- days is a whole number, 1 to 21, required.
- note is text of at most 80 characters, optional, default null.
- A draft missing one of its three required values, or carrying a value of the wrong kind or outside its stated range, is answered with 422 and one detail entry per broken rule, carrying loc of ["body", "value name"], the input that was received, and the type and msg reported for the rule.
The published examples
LoanDraft publishes examples, a list of one entry, the whole draft worked out:
[{"tool_code": "TL-331", "member": "Sage Ilori", "days": 7, "note": "Weekend build"}]
Each of its four values publishes an examples list of its own, holding one entry:
- tool_code names TL-118
- member names Noa Brand
- days names 5
- note names Weekend build
These five entries belong to the draft itself, not to the operation that takes it, and the draft still publishes the limits above alongside them. Both the whole-draft entry and a draft built from the four per-value entries are filed when sent.
POST /loans publishes examples of its own on the JSON body it takes, three entries in this order, each holding a summary and a value:
{
"one_tool": {"summary": "A single tool for a few days", "value": {"tool_code": "TL-118", "member": "Rowan Vale", "days": 3}},
"long_hold": {"summary": "The longest hold the desk allows", "value": {"tool_code": "TL-204", "member": "Kai Ostrom", "days": 21, "note": "Kitchen refit"}},
"past_the_limit": {"summary": "A hold the desk refuses", "value": {"tool_code": "TL-204", "member": "Kai Ostrom", "days": 30}}
}
one_tool and long_hold are filed when sent. past_the_limit is refused, like any other draft over the limit.
The published document
The service publishes its description at GET /public/schema.json. Every fact above must be declared on the operation or on the draft itself, not written into that document by hand.
A registered shape is listed once under components/schemas and referenced from the operation, not spelled out in place. Tool, Shelf, Loan and Message each publish every field as required, with a type and a description. The lookup and the filing also publish the framework's 422 answer, described as Validation Error and pointing at the registered HTTPValidationError shape.
The document is pinned against drift. Any change to the following is a failure:
- the published paths: the four endpoint paths above plus the provided home route
- the identity block
- the tag list
- for each of the five operations: its summary, description, tag, deprecation, the answers it describes with the description and registered shape of each, and the names of its request examples, in order
Sample Requests & Responses
GET /tools
Response (200 OK):
[
{"code": "TL-118", "name": "Cordless Drill", "shelf": "A2", "condition": "good", "on_loan": false},
{"code": "TL-204", "name": "Tile Cutter", "shelf": "B1", "condition": "fair", "on_loan": true},
{"code": "TL-331", "name": "Orchard Ladder", "shelf": "C4", "condition": "good", "on_loan": false},
{"code": "TL-476", "name": "Bench Vice", "shelf": "A1", "condition": "worn", "on_loan": false}
]
POST /loans
Request:
{"tool_code": "TL-118", "member": "Rowan Vale", "days": 3}
Response (201 Created):
{"id": 3, "tool_code": "TL-118", "member": "Rowan Vale", "days": 3, "note": null}
POST /loans
Request:
{"tool_code": "TL-204", "member": "Kai Ostrom", "days": 30}
Response (422 Unprocessable Entity):
{"detail": [{"type": "less_than_equal", "loc": ["body", "days"], "msg": "Input should be less than or equal to 21", "input": 30, "ctx": {"le": 21}}]}
