Problem statement
Build a document intake that takes a form, a document, any number of attachments, a client header and a submitter cookie in one submission, routes it to the desk that owns the department, and turns away an unknown department, an unsupported file and one that is too big.
Sample Data
app/store.py maps each department to its desk: finance to FIN-2, legal to LEG-7 and records to REC-1. app/services/intake.py holds the limits: ALLOWED_CONTENT_TYPES, which is application/pdf and text/plain, and MAX_DOCUMENT_BYTES, which is 2048.
API Endpoints to Implement
POST /documents/intake
Everything arrives in one multipart submission, never as JSON.
- title and department arrive as form fields, text, both required.
- document arrives as an uploaded file, required.
- attachments arrives as a list of uploaded files, optional, empty when none are sent.
- X-Portal-Client arrives as a request header, text, required. A header name is matched whatever its casing.
- submitter arrives as a cookie, text, optional, default anonymous.
- Hand the department to desk_for in app/services/intake.py, which answers with its desk or with nothing.
- A department no desk owns is answered with 404 and {"detail": "Unknown department marketing"}, naming the department.
- A document whose declared content type is not one of the allowed two is answered with 415 and {"detail": "Unsupported content type image/png; expected application/pdf or text/plain"}, naming the type. The declared type decides this, not the file name.
- A document longer than MAX_DOCUMENT_BYTES is answered with 413 and {"detail": "File budget.pdf exceeds the 2048 byte limit"}, naming the file. A document of exactly 2048 bytes is accepted; one byte more is not.
- Response code: 200. The body holds exactly title, department, desk, client, submitter, document, attachments and attachment_count. document and each entry of attachments hold filename, content_type and size, and the attachments keep the order they were sent. No limit applies to an attachment.
- The response also names the desk in a header of its own, X-Intake-Desk, and pins the submitter with exactly one cookie, submitter=<the submitter>; HttpOnly; Max-Age=86400; Path=/; SameSite=lax.
Rejected requests
A missing required value is answered with 422 and one detail entry per absent one, carrying loc of ["body", "title"], ["body", "department"], ["body", "document"] or ["header", "x-portal-client"], an input of null, and the type and msg reported for the rule. A JSON body supplies none of the three form parts, so all three are reported absent.
The parts, the header and the cookie must be declared on the handler rather than read out of the request, since the service publishes the contract at GET /openapi.json, where the request body is required, is offered only as multipart and not as JSON, and points at a named schema of its own listing title, department, document and attachments with the first three required; the operation carries exactly one header parameter, x-portal-client, required text, and exactly one cookie parameter, submitter, optional text defaulting to anonymous; and both 200 and 422 are declared.
Sample Requests & Responses
POST /documents/intake, sending a finance form with budget.pdf, two attachments, an X-Portal-Client of portal-web/2.4 and a submitter cookie of clerk-7781
Response (200 OK):
{
"title": "Quarterly budget report",
"department": "finance",
"desk": "FIN-2",
"client": "portal-web/2.4",
"submitter": "clerk-7781",
"document": {"filename": "budget.pdf", "content_type": "application/pdf", "size": 41},
"attachments": [
{"filename": "appendix.pdf", "content_type": "application/pdf", "size": 23},
{"filename": "notes.txt", "content_type": "text/plain", "size": 5}
],
"attachment_count": 2
}
POST /documents/intake, sending a department of marketing
Response (404 Not Found):
{
"detail": "Unknown department marketing"
}
POST /documents/intake, sending a document declared as image/png
Response (415 Unsupported Media Type):
{
"detail": "Unsupported content type image/png; expected application/pdf or text/plain"
}
POST /documents/intake, sending nothing at all
Response (422 Unprocessable Entity):
{
"detail": [
{"type": "missing", "loc": ["header", "x-portal-client"], "msg": "Field required", "input": null},
{"type": "missing", "loc": ["body", "title"], "msg": "Field required", "input": null},
{"type": "missing", "loc": ["body", "department"], "msg": "Field required", "input": null},
{"type": "missing", "loc": ["body", "document"], "msg": "Field required", "input": null}
]
}
