Skip to content

Commit 9f36624

Browse files
Add OpenAPI schema, get accept header from request rather than as parameter
1 parent e5997b1 commit 9f36624

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

src/daq_config_server/app.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
import os
33
from enum import StrEnum
44
from pathlib import Path
5-
from typing import Annotated
65

76
import uvicorn
8-
from fastapi import FastAPI, Header, HTTPException
7+
from fastapi import FastAPI, HTTPException, Request
98
from fastapi.middleware.cors import CORSMiddleware
109
from fastapi.responses import JSONResponse, Response
1110

@@ -34,10 +33,39 @@ class ValidAcceptHeaders(StrEnum):
3433
RAW_BYTES = "application/octet-stream"
3534

3635

37-
@app.get(ENDPOINTS.CONFIG + "/{file_path:path}")
36+
@app.get(
37+
ENDPOINTS.CONFIG + "/{file_path:path}",
38+
responses={
39+
200: {
40+
"description": "Returns JSON, plain text, or binary file.",
41+
"content": {
42+
"application/json": {
43+
"schema": {
44+
"type": "object",
45+
"additionalProperties": True,
46+
"example": {
47+
"key": "value",
48+
"list": [1, 2, 3],
49+
"nested": {"a": 1},
50+
},
51+
}
52+
},
53+
"text/plain": {
54+
"schema": {
55+
"type": "string",
56+
"example": "This is a plain text response",
57+
}
58+
},
59+
"application/octet-stream": {
60+
"schema": {"type": "string", "format": "binary"},
61+
},
62+
},
63+
},
64+
},
65+
)
3866
def get_configuration(
3967
file_path: Path,
40-
accept: Annotated[ValidAcceptHeaders, Header()] = ValidAcceptHeaders.PLAIN_TEXT,
68+
request: Request,
4169
):
4270
"""
4371
Read a file and return its contents in a format specified by the accept header.
@@ -46,6 +74,7 @@ def get_configuration(
4674
raise HTTPException(status_code=404, detail=f"File {file_path} cannot be found")
4775

4876
file_name = os.path.basename(file_path)
77+
accept = request.headers.get("accept", ValidAcceptHeaders.PLAIN_TEXT)
4978

5079
try:
5180
match accept:

0 commit comments

Comments
 (0)