Commit ddc0b19b authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Added accept header handling for dcat API format parameter.

parent 90002052
Pipeline #91932 passed with stages
in 29 minutes and 20 seconds
......@@ -16,7 +16,7 @@
# limitations under the License.
#
from flask import Blueprint, Response
from flask import Blueprint, Response, request
from flask_restplus import Api, reqparse
import urllib.parse
from rdflib import Graph
......@@ -62,13 +62,52 @@ arg_parser.add_argument('format', type=str, choices=[
'pretty-xml',
'trig'])
all_repsonse_types = {
'application/xml': 'xml',
'application/rdf+prettyxml': 'pretty-xml',
'application/rdf': 'xml',
'application/rdf+xml': 'xml',
'text/plain': 'n3',
'text/turtle': 'turtle',
'text/nt': 'nt',
'text/n3': 'n3',
'text/rdf+n3': 'n3',
'text/rdf+nt': 'nt',
'text/rdf+turtle': 'turtle',
'application/x-trig': 'trig'
}
response_types = [
'application/xml',
'application/rdf+xml',
'application/rdf+pretty-xml',
'text/plain',
'text/turtle',
'text/rdf+n3',
'text/rdf+nt',
'application/x-trig']
def rdf_respose(g: Graph) -> Response:
args = arg_parser.parse_args()
format_ = args.get('format')
accept_header = None
if format_ is None:
format_ = 'pretty-xml'
content_type = 'application/xml' if format in ['xml', 'pretty-xml'] else 'text/%s' % format_
accept_header = request.headers.get('Accept', None)
if accept_header is not None:
format_ = all_repsonse_types.get(accept_header, 'pretty-xml')
else:
accept_header = None
format_ = 'pretty-xml'
if accept_header is not None:
content_type = accept_header
else:
try:
content_type = next(key for key, value in all_repsonse_types.items() if value == format_)
except StopIteration:
content_type = 'application/xml' if format in ['xml', 'pretty-xml'] else 'text/%s' % format_
return Response(
g.serialize(format=format_).decode('utf-8'), 200,
{'Content-Type': content_type})
......@@ -21,7 +21,7 @@ from elasticsearch_dsl import Q
from nomad import search
from .api import api, arg_parser, rdf_respose
from .api import api, arg_parser, rdf_respose, response_types
from .mapping import Mapping
ns = api.namespace('catalog', description='The API for DCAT catalog.')
......@@ -39,13 +39,13 @@ arg_parser.add_argument(
class Catalog(Resource):
@api.doc('get_dcat_datasets')
@api.expect(arg_parser)
@api.produces(['application/xml'])
@api.representation('application/xml')
@api.produces(response_types)
@api.response(404, 'There is no entry with the given id.')
@api.response(401, 'This entry is not publically accessible.')
@api.response(200, 'Data send', headers={'Content-Type': 'application/xml'})
def get(self):
''' Returns a page of DCAT datasets. '''
args = arg_parser.parse_args()
modified_since = args.get('modified_since', None)
after = args.get('after', '')
......
......@@ -20,7 +20,7 @@ from elasticsearch.exceptions import NotFoundError
from nomad import search
from .api import api, arg_parser, rdf_respose
from .api import api, arg_parser, rdf_respose, response_types
from .mapping import Mapping
ns = api.namespace('datasets', description='The API for DCAT datasets.')
......@@ -30,7 +30,8 @@ ns = api.namespace('datasets', description='The API for DCAT datasets.')
class Dataset(Resource):
@api.doc('get_dcat_dataset')
@api.expect(arg_parser)
@api.produces(['application/xml'])
@api.representation('application/xml')
@api.produces(response_types)
@api.response(404, 'There is no entry with the given id.')
@api.response(401, 'This entry is not publically accessible.')
@api.response(200, 'Data send', headers={'Content-Type': 'application/xml'})
......
Markdown is supported
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