Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
aaxelb committed Nov 8, 2023
1 parent 0b31c37 commit c64fd65
Show file tree
Hide file tree
Showing 4 changed files with 406 additions and 9 deletions.
2 changes: 1 addition & 1 deletion how-to/use-the-api.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# How to use the API

(see [openapi docs](/trove/openapi.ui) for detail)
(see [openapi docs](/trove/docs/openapi.html) for detail)

## Sample and search for index-cards

Expand Down
58 changes: 55 additions & 3 deletions trove/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from primitive_metadata import primitive_rdf

from share.version import __version__
from trove.vocab.jsonapi import JSONAPI_MEMBERNAME
from trove.vocab.jsonapi import JSONAPI_MEMBERNAME, JSONAPI_MEDIATYPE
from trove.vocab.namespaces import TROVE, RDFS, RDF, DCTERMS
from trove.vocab.trove import TROVE_API_VOCAB

Expand Down Expand Up @@ -58,6 +58,7 @@ def get_trove_openapi() -> dict:
),
'components': {
'parameters': dict(_openapi_parameters(_path_iris, _api_graph)),
'examples': dict(_openapi_examples(_path_iris, _api_graph)),
},
}

Expand Down Expand Up @@ -92,25 +93,76 @@ def _openapi_parameters(path_iris: Iterable[str], api_graph: primitive_rdf.RdfGr
}


def _openapi_examples(path_iris: Iterable[str], api_graph: primitive_rdf.RdfGraph):
# assumes examples are blank nodes (frozenset of twoples)
_examples = set(itertools.chain(*(
api_graph.q(_path_iri, TROVE.example)
for _path_iri in path_iris
)))
for _example_blanknode in _examples:
_example = primitive_rdf.twopledict_from_twopleset(_example_blanknode)
_label = ''.join(
_literal.unicode_value
for _literal in _example.get(RDFS.label, ())
)
_comment = ' '.join(
_literal.unicode_value
for _literal in _example.get(RDFS.comment, ())
)
_description = '\n\n'.join(
_literal.unicode_value
for _literal in _example.get(DCTERMS.description, ())
)
_value = next(iter(_example[RDF.value]))
if isinstance(_value, str):
_valuekey = 'externalValue'
else:
_valuekey = 'value'
_value = (
json.loads(_value.unicode_value)
if RDF.JSON in _value.datatype_iris
else _value.unicode_value
)
yield _label, {
'summary': _comment,
'description': _description,
_valuekey: _value,
}


def _openapi_path(path_iri: str, api_graph: primitive_rdf.RdfGraph):
# TODO: better error message on absence
try:
_path = next(_text(path_iri, TROVE.iriPath, api_graph))
except StopIteration:
raise ValueError(f'could not find trove:iriPath for {path_iri}')
_labels = list(_text(path_iri, RDFS.label, api_graph))
_label = ' '.join(_text(path_iri, RDFS.label, api_graph))
_param_labels = list(_text(path_iri, {TROVE.hasParameter: {JSONAPI_MEMBERNAME}}, api_graph))
_example_labels = list(_text(path_iri, {TROVE.example: {RDFS.label}}, api_graph))
return _path, {
'get': { # TODO (if generalizability): separate metadata by verb
# 'tags':
'summary': _labels,
'summary': _label,
'description': _markdown_description(path_iri, api_graph),
# 'externalDocs':
'operationId': path_iri,
'parameters': [
{'$ref': f'#/components/parameters/{_param_label}'}
for _param_label in _param_labels
],
'responses': {
'200': {
'description': 'ok',
'content': {
JSONAPI_MEDIATYPE: {
'examples': [
{'$ref': f'#/components/examples/{_example_label}'}
for _example_label in _example_labels
],
},
},
},
},
},
}

Expand Down
6 changes: 6 additions & 0 deletions trove/vocab/osfmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,12 @@
literal('accessService', language='en'),
},
},
OSFMAP.hostingInstitution: {
RDF.type: {RDF.Property},
JSONAPI_MEMBERNAME: {
literal('hostingInstitution', language='en'),
},
},
RDFS.label: {
RDF.type: {RDF.Property},
RDFS.label: {
Expand Down
Loading

0 comments on commit c64fd65

Please sign in to comment.