Skip to content

Commit

Permalink
Add jsonld download links. Refs #78 (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
koenedaele committed Oct 15, 2020
1 parent f3b96e8 commit 3c084fc
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 15 deletions.
8 changes: 8 additions & 0 deletions pyramid_skosprovider/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ def includeme(config):
'skosprovider.conceptschemes',
'/conceptschemes'
)
config.add_route(
'skosprovider.conceptscheme.jsonld',
'/conceptschemes/{scheme_id}.jsonld'
)
config.add_route(
'skosprovider.conceptscheme',
'/conceptschemes/{scheme_id}'
Expand All @@ -156,6 +160,10 @@ def includeme(config):
'skosprovider.conceptscheme.display_top',
'/conceptschemes/{scheme_id}/displaytop'
)
config.add_route(
'skosprovider.c.jsonld',
'/conceptschemes/{scheme_id}/c/{c_id}.jsonld'
)
config.add_route(
'skosprovider.c',
'/conceptschemes/{scheme_id}/c/{c_id}'
Expand Down
99 changes: 84 additions & 15 deletions pyramid_skosprovider/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,29 @@ def get_context(self):
return CONTEXT


@view_defaults(renderer='skosjson', accept='application/json')
class ProviderView(RestView):
'''
A set of views that expose information from a certain provider.
'''

@view_config(
route_name='skosprovider.uri',
request_method='GET',
accept='application/json+ld'
route_name='skosprovider.uri',
request_method='GET',
accept='application/json+ld',
renderer='skosjson'
)
@view_config(
route_name='skosprovider.uri',
request_method='GET',
accept='application/json',
renderer='skosjson'
)
@view_config(
route_name='skosprovider.uri.deprecated',
request_method='GET',
accept='application/json',
renderer='skosjson'
)
@view_config(route_name='skosprovider.uri', request_method='GET')
@view_config(route_name='skosprovider.uri.deprecated', request_method='GET')
def get_uri(self):
uri = self.request.params.get('uri', self.request.matchdict.get('uri', None))
if not uri:
Expand Down Expand Up @@ -106,7 +116,12 @@ def get_uri(self):
}
}

@view_config(route_name='skosprovider.conceptschemes', request_method='GET')
@view_config(
route_name='skosprovider.conceptschemes',
request_method='GET',
accept='application/json',
renderer='skosjson'
)
@view_config(
route_name='skosprovider.conceptschemes',
request_method='GET',
Expand All @@ -132,7 +147,12 @@ def get_conceptschemes(self):
} for p in self.skos_registry.get_providers()
]

@view_config(route_name='skosprovider.conceptscheme', request_method='GET')
@view_config(
route_name='skosprovider.conceptscheme',
request_method='GET',
accept='application/json',
renderer='skosjson'
)
def get_conceptscheme(self):
scheme_id = self.request.matchdict['scheme_id']
provider = self.skos_registry.get_provider(scheme_id)
Expand All @@ -156,14 +176,28 @@ def get_conceptscheme(self):
renderer='skosjsonld',
accept='application/ld+json'
)
@view_config(
route_name='skosprovider.conceptscheme.jsonld',
request_method='GET',
renderer='skosjsonld'
)
def get_conceptscheme_jsonld(self):
scheme_id = self.request.matchdict['scheme_id']
log.debug(scheme_id)
provider = self.skos_registry.get_provider(scheme_id)
log.debug(provider)
if not provider:
log.warning('no provider')
log.debug(provider)
return HTTPNotFound()
return provider.concept_scheme

@view_config(route_name='skosprovider.conceptscheme.tc', request_method='GET')
@view_config(
route_name='skosprovider.conceptscheme.tc',
request_method='GET',
accept='application/json',
renderer='skosjson'
)
def get_conceptscheme_top_concepts(self):
scheme_id = self.request.matchdict['scheme_id']
provider = self.skos_registry.get_provider(scheme_id)
Expand All @@ -172,7 +206,12 @@ def get_conceptscheme_top_concepts(self):
language = self.request.params.get('language', self.request.locale_name)
return provider.get_top_concepts(language=language)

@view_config(route_name='skosprovider.conceptscheme.display_top', request_method='GET')
@view_config(
route_name='skosprovider.conceptscheme.display_top',
request_method='GET',
accept='application/json',
renderer='skosjson'
)
def get_conceptscheme_display_top(self):
scheme_id = self.request.matchdict['scheme_id']
provider = self.skos_registry.get_provider(scheme_id)
Expand All @@ -197,7 +236,12 @@ def _build_providers(self, request):
providers['subject'] = subject
return providers

@view_config(route_name='skosprovider.cs', request_method='GET')
@view_config(
route_name='skosprovider.cs',
request_method='GET',
accept='application/json',
renderer='skosjson'
)
def get_concepts(self):
qb = QueryBuilder(self.request)
query = qb()
Expand All @@ -215,7 +259,12 @@ def get_concepts(self):

return self._page_results(concepts)

@view_config(route_name='skosprovider.conceptscheme.cs', request_method='GET')
@view_config(
route_name='skosprovider.conceptscheme.cs',
request_method='GET',
accept='application/json',
renderer='skosjson'
)
def get_conceptscheme_concepts(self):
scheme_id = self.request.matchdict['scheme_id']
provider = self.skos_registry.get_provider(scheme_id)
Expand Down Expand Up @@ -276,13 +325,23 @@ def _page_results(self, concepts):
))
return cslice

@view_config(route_name='skosprovider.c', request_method='GET')
@view_config(
route_name='skosprovider.c',
request_method='GET',
accept='application/json',
renderer='skosjson'
)
@view_config(
route_name='skosprovider.c',
request_method='GET',
renderer='skosjsonld',
accept='application/ld+json'
)
@view_config(
route_name='skosprovider.c.jsonld',
request_method='GET',
renderer='skosjsonld'
)
def get_concept(self):
scheme_id = self.request.matchdict['scheme_id']
concept_id = self.request.matchdict['c_id']
Expand All @@ -294,7 +353,12 @@ def get_concept(self):
return HTTPNotFound()
return concept

@view_config(route_name='skosprovider.c.display_children', request_method='GET')
@view_config(
route_name='skosprovider.c.display_children',
request_method='GET',
accept='application/json',
renderer='skosjson'
)
def get_concept_display_children(self):
scheme_id = self.request.matchdict['scheme_id']
concept_id = self.request.matchdict['c_id']
Expand All @@ -307,7 +371,12 @@ def get_concept_display_children(self):
return HTTPNotFound()
return children

@view_config(route_name='skosprovider.c.expand', request_method='GET')
@view_config(
route_name='skosprovider.c.expand',
request_method='GET',
accept='application/json',
renderer='skosjson'
)
def get_expand(self):
scheme_id = self.request.matchdict['scheme_id']
concept_id = self.request.matchdict['c_id']
Expand Down
29 changes: 29 additions & 0 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,20 @@ def test_get_conceptscheme_jsonld(self):
assert '@context' in data
assert '/jsonld/context/skos' in data['@context']

def test_get_conceptscheme_jsonld_url(self):
res = self.testapp.get(
'/conceptschemes/TREES.jsonld'
)
assert res.status == '200 OK'
assert 'application/ld+json' in res.headers['Content-Type']
data = json.loads(res.body.decode('utf-8'))
res2 = self.testapp.get(
'/conceptschemes/TREES',
{},
{ascii_native_('Accept'): ascii_native_('application/ld+json')})
data2 = json.loads(res2.body.decode('utf-8'))
assert data == data2

def test_get_conceptschemes_trees_cs_json(self):
res = self.testapp.get(
'/conceptschemes/TREES/c',
Expand Down Expand Up @@ -286,6 +300,21 @@ def test_get_conceptschemes_trees_species_jsonld(self):
assert '@context' in data
assert '/jsonld/context/skos' in data['@context']

def test_get_conceptschemes_trees_species_jsonld_url(self):
res = self.testapp.get(
'/conceptschemes/TREES/c/3.jsonld'
)
assert res.status == '200 OK'
assert 'application/ld+json' in res.headers['Content-Type']
data = json.loads(res.body.decode('utf-8'))
res2 = self.testapp.get(
'/conceptschemes/TREES/c/3',
{},
{ascii_native_('Accept'): ascii_native_('application/ld+json')}
)
data2 = json.loads(res2.body.decode('utf-8'))
assert data == data2

def test_get_conceptscheme_concepts_search_dfs_label_star(self):
res = self.testapp.get(
'/conceptschemes/TREES/c?language=nl-BE',
Expand Down

0 comments on commit 3c084fc

Please sign in to comment.