Commit 91dce152 authored by Lauri Himanen's avatar Lauri Himanen
Browse files

Added gzip compression decorator and applied it to one API route in the encyclopedia.

parent 7b8dea01
Pipeline #77842 failed with stages
in 45 minutes and 45 seconds
Subproject commit 40bd67030256126a66d623cc5a0568fcab06b294
Subproject commit 63515c329818e98af95b70c97f96b1449bfc41ff
......@@ -17,6 +17,7 @@ Common data, variables, decorators, models used throughout the API.
from typing import Callable, IO, Set, Tuple, Iterable, Dict, Any
from flask_restplus import fields
from flask import request, jsonify, make_response
import zipstream
from flask import stream_with_context, Response, g, abort
from urllib.parse import urlencode
......@@ -25,6 +26,8 @@ import io
import sys
import os.path
import gzip
from functools import wraps
from nomad import search, config, datamodel
from import filterparser
......@@ -360,3 +363,29 @@ def query_api_curl(*args, **kwargs):
url = query_api_url(*args, **kwargs)
return 'curl -X POST %s -H "accept: application/json" --output "nomad.json"' % url
def enable_gzip(level: int = 5, min_size: int = 150):
level: The gzip compression level from 1-9
min_size: The minimum response size in bytes for which the compression
will be enabled.
def inner(function):
def wrapper(*args, **kwargs):
response = make_response(function(*args, **kwargs))
if response.status_code == 200:
accept_encoding = request.headers["Accept-Encoding"]
content_length = int(response.headers["Content-Length"])
if "gzip" in accept_encoding and content_length >= min_size:
data =
data = gzip.compress(data, level) = data
response.headers['Content-Length'] = len(data)
response.headers["Content-Encoding"] = "gzip"
return response
return response
return wrapper
return inner
......@@ -29,6 +29,7 @@ from nomad.units import ureg
from nomad.atomutils import get_hill_decomposition
from nomad.datamodel.datamodel import EntryArchive
from .api import api
from .common import enable_gzip
ns = api.namespace("encyclopedia", description="Access encyclopedia metadata.")
re_formula = re.compile(r"([A-Z][a-z]?)(\d*)")
......@@ -1079,6 +1080,7 @@ calculation_property_result = api.model("calculation_property_result", {
class EncCalculationResource(Resource):
@api.response(404, "Material or calculation not found")
@api.response(400, "Bad request")
@api.response(200, "Metadata send", fields.Raw)
......@@ -109,6 +109,7 @@ data:
conf.js: |
window.nomadEnv = {
host: "https://{{ }}{{ .Values.proxy.external.path }}",
path: "/api/encyclopedia/",
userCookieDomain: ".{{ }}",
guestUserToken: 'eyJhbGciOiJIUzI1NiIsImlhdCI6MTUyMzg4MDE1OSwiZXhwIjoxNjgxNTYwMTU5fQ.ey'+
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