Skip to content

Commit

Permalink
Merge pull request #7 from fabiogibson/master
Browse files Browse the repository at this point in the history
Fixing issues 5 and 6.
  • Loading branch information
jonimoreira authored Feb 18, 2018
2 parents 259e882 + c00c6c0 commit ce08342
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/.pytest_cache/v/cache/lastfailed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
43 changes: 28 additions & 15 deletions src/mapper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from datetime import datetime

from pyld import jsonld
import falcon


CONST_BASE_URL = 'https://trng-b2share.eudat.eu/api/'
Expand Down Expand Up @@ -60,38 +61,50 @@ def from_dict(cls, _dict):

instance = cls()

if not _dict:
_dict = {}

for name, field in cls.get_fields():
value = _dict.get(field.name)

if value is not None:
setattr(instance, name, field.parse(value))
setattr(instance, name, field.parse(value))

return instance

@classmethod
def get_from_file(cls, _filename):
with open(_filename) as json_data:
d = json.load(json_data)
return cls.from_dict(d)

@staticmethod
def load_document(uri):
doc = jsonld.get_document_loader()(uri)

if doc['document'].get('status', 200) != 200:
raise falcon.HTTPBadRequest(
'Proxy Error',
'An error has ocurred while requesting this resource.'
)

return doc

@classmethod
def get_all(cls):
def get_all(cls, query=''):
"""gets all model intances from remote endpoint.
:returns: list of cls instances.
"""
url = '{url}{resource}'.format(url=CONST_BASE_URL, resource=cls.resource_name)
doc = jsonld.get_document_loader()(url)
uri = '{url}{resource}?{query}'.format(url=CONST_BASE_URL,
resource=cls.resource_name, query=query,)
doc = cls.load_document(uri)

for hit in doc['document']['hits']['hits']:
# TODO: check if it is a valid path for all resources.
yield cls.from_dict(hit)

@classmethod
def get_from_file(cls, _filename):
with open(_filename) as json_data:
d = json.load(json_data)
return cls.from_dict(d)

@classmethod
def get_id(cls, object_id):
url = '{url}{resource}/{id}'.format(url=CONST_BASE_URL, resource=cls.resource_name, id=object_id)
doc = jsonld.get_document_loader()(url)
uri = '{url}{resource}/{id}'.format(url=CONST_BASE_URL, resource=cls.resource_name, id=object_id)
doc = cls.load_document(uri)
return cls.from_dict(doc['document'])

@classmethod
Expand Down
3 changes: 0 additions & 3 deletions src/proxy/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,4 @@
api.add_route('/catalogs/{_id}', CommnunityResource())
api.add_route('/datasets/', RecordResource())
api.add_route('/datasets/{_id}', RecordResource())
api.add_route('/datasets/?q={_query}', RecordResource())
api.add_route('/datasets/?q=community:{_community_id}', RecordResource())
api.add_route('/datasets/?drafts', RecordResource()) #Requires user to be authenticated in B2SHARE
api.add_route('/distributions/{_id}', FileResource())
10 changes: 7 additions & 3 deletions src/proxy/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ def _get_by_id(cls, _id):
return cls.translator(community)

@classmethod
def _get_all(cls):
communities = cls.model.get_all()
def _get_all(cls, query):
communities = cls.model.get_all(query)
return [cls.translator(c) for c in communities]

def on_get(self, req, resp, _id=None):
resp.media = self._get_by_id(_id) if _id else self._get_all()
if _id:
resp.media = self._get_by_id(_id)
else:
resp.media = self._get_all(req.query_string)


class ReadResource:
@classmethod
Expand Down
4 changes: 2 additions & 2 deletions src/proxy/tests/test_communities.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
def test_get_all_communities(client):
result = client.simulate_get('/community')
result = client.simulate_get('/catalogs')

assert len(result.json) == 14


def test_get_community_by_id(client):
result = client.simulate_get(
'/community/c4234f93-da96-4d2f-a2c8-fa83d0775212')
'/catalogs/c4234f93-da96-4d2f-a2c8-fa83d0775212')

assert result.json['dct:title'] == 'Aalto'

0 comments on commit ce08342

Please sign in to comment.