Skip to content
Snippets Groups Projects
Commit ddc0b19b authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Added accept header handling for dcat API format parameter.

parent 90002052
Branches
Tags
2 merge requests!254Merging these two branches concerning the encylopedia complex search,!246Merge for release
Pipeline #91932 passed
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
# limitations under the License. # limitations under the License.
# #
from flask import Blueprint, Response from flask import Blueprint, Response, request
from flask_restplus import Api, reqparse from flask_restplus import Api, reqparse
import urllib.parse import urllib.parse
from rdflib import Graph from rdflib import Graph
...@@ -62,13 +62,52 @@ arg_parser.add_argument('format', type=str, choices=[ ...@@ -62,13 +62,52 @@ arg_parser.add_argument('format', type=str, choices=[
'pretty-xml', 'pretty-xml',
'trig']) '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: def rdf_respose(g: Graph) -> Response:
args = arg_parser.parse_args() args = arg_parser.parse_args()
format_ = args.get('format') format_ = args.get('format')
accept_header = None
if format_ is None: if format_ is None:
format_ = 'pretty-xml' accept_header = request.headers.get('Accept', None)
content_type = 'application/xml' if format in ['xml', 'pretty-xml'] else 'text/%s' % format_ 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( return Response(
g.serialize(format=format_).decode('utf-8'), 200, g.serialize(format=format_).decode('utf-8'), 200,
{'Content-Type': content_type}) {'Content-Type': content_type})
...@@ -21,7 +21,7 @@ from elasticsearch_dsl import Q ...@@ -21,7 +21,7 @@ from elasticsearch_dsl import Q
from nomad import search 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 from .mapping import Mapping
ns = api.namespace('catalog', description='The API for DCAT catalog.') ns = api.namespace('catalog', description='The API for DCAT catalog.')
...@@ -39,13 +39,13 @@ arg_parser.add_argument( ...@@ -39,13 +39,13 @@ arg_parser.add_argument(
class Catalog(Resource): class Catalog(Resource):
@api.doc('get_dcat_datasets') @api.doc('get_dcat_datasets')
@api.expect(arg_parser) @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(404, 'There is no entry with the given id.')
@api.response(401, 'This entry is not publically accessible.') @api.response(401, 'This entry is not publically accessible.')
@api.response(200, 'Data send', headers={'Content-Type': 'application/xml'}) @api.response(200, 'Data send', headers={'Content-Type': 'application/xml'})
def get(self): def get(self):
''' Returns a page of DCAT datasets. ''' ''' Returns a page of DCAT datasets. '''
args = arg_parser.parse_args() args = arg_parser.parse_args()
modified_since = args.get('modified_since', None) modified_since = args.get('modified_since', None)
after = args.get('after', '') after = args.get('after', '')
......
...@@ -20,7 +20,7 @@ from elasticsearch.exceptions import NotFoundError ...@@ -20,7 +20,7 @@ from elasticsearch.exceptions import NotFoundError
from nomad import search 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 from .mapping import Mapping
ns = api.namespace('datasets', description='The API for DCAT datasets.') ns = api.namespace('datasets', description='The API for DCAT datasets.')
...@@ -30,7 +30,8 @@ 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): class Dataset(Resource):
@api.doc('get_dcat_dataset') @api.doc('get_dcat_dataset')
@api.expect(arg_parser) @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(404, 'There is no entry with the given id.')
@api.response(401, 'This entry is not publically accessible.') @api.response(401, 'This entry is not publically accessible.')
@api.response(200, 'Data send', headers={'Content-Type': 'application/xml'}) @api.response(200, 'Data send', headers={'Content-Type': 'application/xml'})
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment