Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
nomad-lab
nomad-FAIR
Commits
ddc0b19b
Commit
ddc0b19b
authored
Jan 22, 2021
by
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
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
nomad/app/dcat/api.py
View file @
ddc0b19b
...
...
@@ -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
})
nomad/app/dcat/catalog.py
View file @
ddc0b19b
...
...
@@ -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'
,
''
)
...
...
nomad/app/dcat/datasets.py
View file @
ddc0b19b
...
...
@@ -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'
})
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment