Commit 135b6ac8 authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Add orjson encoder to fast api. #754

parent 02d8428b
Pipeline #123665 passed with stages
in 61 minutes and 38 seconds
......@@ -16,10 +16,12 @@
# limitations under the License.
#
from typing import Any
from fastapi import FastAPI, status, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse, RedirectResponse
import traceback
import orjson
from nomad import config, utils
......@@ -30,6 +32,13 @@ from .routers import users, entries, materials, auth, info, datasets, uploads, s
logger = utils.get_logger(__name__)
class ORJSONResponse(JSONResponse):
media_type = "application/json"
def render(self, content: Any) -> bytes:
return orjson.dumps(content, option=orjson.OPT_INDENT_2 | orjson.OPT_NON_STR_KEYS) # type: ignore
app = FastAPI(
root_path=root_path,
openapi_url='/openapi.json',
......@@ -41,7 +50,8 @@ app = FastAPI(
description=utils.strip(f'''
Please visit the [API section of the NOMAD documentation]({config.api_url(True, 'docs/api.html')})
for a introduction and examples.
'''))
'''),
default_response_class=ORJSONResponse)
app.add_middleware(
CORSMiddleware,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment