Problem statement
Build an update check that reads the caller's version from a request header and their release channel from a cookie, reports how their build compares with the latest one, and pins the channel back for next time.
Sample Data
app/store.py publishes a latest version per channel: stable is 3.8.2, beta is 4.0.0 and nightly is 4.1.0.dev5.
API Endpoints to Implement
GET /updates/check
- X-Client-Version arrives as a request header, text, required. A header name is matched whatever its casing, so x-client-version and X-CLIENT-VERSION are read the same way.
- channel arrives as a cookie, text, optional, default stable.
- Hand the channel to latest_version in app/services/updates.py, which answers with that channel's latest version, falling back to the stable one for a channel it does not publish. The channel is still reported as it was sent.
- Response code: 200. The body holds exactly client_version as it was sent, channel, latest, and up_to_date, true only when the two versions are the same.
- The response also names the latest version in a header of its own, X-Latest-Version.
- The response pins the channel with exactly one cookie, channel=<the channel>; HttpOnly; Max-Age=2592000; Path=/; SameSite=lax.
Rejected requests
A request without the version header is answered with 422 and one detail entry carrying loc of ["header", "x-client-version"], an input of null, and the type and msg reported for the rule.
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 operation carries exactly one header parameter, x-client-version, required text, exactly one cookie parameter, channel, optional text defaulting to stable, and declares both 200 and 422.
Sample Requests & Responses
GET /updates/check, sending an X-Client-Version of 3.8.2 and no cookie
Response (200 OK):
{
"client_version": "3.8.2",
"channel": "stable",
"latest": "3.8.2",
"up_to_date": true
}
GET /updates/check, sending an X-Client-Version of 3.5.0 and a channel cookie of beta
Response (200 OK):
{
"client_version": "3.5.0",
"channel": "beta",
"latest": "4.0.0",
"up_to_date": false
}
GET /updates/check, sending a channel cookie of ancient, which is not published
Response (200 OK):
{
"client_version": "3.8.2",
"channel": "ancient",
"latest": "3.8.2",
"up_to_date": true
}
GET /updates/check, sending no version header
Response (422 Unprocessable Entity):
{
"detail": [
{
"type": "missing",
"loc": ["header", "x-client-version"],
"msg": "Field required",
"input": null
}
]
}
