diff options
author | Bryan Newbold <bnewbold@robocracy.org> | 2018-04-23 19:26:56 -0700 |
---|---|---|
committer | Bryan Newbold <bnewbold@robocracy.org> | 2018-04-23 19:26:56 -0700 |
commit | 1f43e85569bd8b23445f9cd6361ea64387d84fa5 (patch) | |
tree | d4bc9c7a6890915b9a505b662b531794a0f80623 | |
parent | 88de9b35a388c4a690f46a24dfbfdb261fb520c0 (diff) | |
download | fatcat-1f43e85569bd8b23445f9cd6361ea64387d84fa5.tar.gz fatcat-1f43e85569bd8b23445f9cd6361ea64387d84fa5.zip |
quick and dirty ISSN+ORCID lookups
-rw-r--r-- | fatcat/api.py | 31 | ||||
-rw-r--r-- | tests/api.py | 53 |
2 files changed, 82 insertions, 2 deletions
diff --git a/fatcat/api.py b/fatcat/api.py index 52b0e21a..6037b250 100644 --- a/fatcat/api.py +++ b/fatcat/api.py @@ -104,10 +104,10 @@ def api_release_random(): @app.route('/v0/release/lookup', methods=['GET']) def api_release_lookup(): params = request.get_json() - doi = params['doi'].lower() + doi = params['doi'].strip().lower() + # TODO: proper regex if not (doi.startswith("10.") and len(doi.split('/')) == 2): abort(400) - # TODO: proper regex entity = ReleaseIdent.query\ .join(ReleaseIdent.rev)\ .filter(ReleaseRev.doi==doi)\ @@ -137,6 +137,19 @@ def api_creator_create(): db.session.commit() return creator_schema.jsonify(ident) +@app.route('/v0/creator/lookup', methods=['GET']) +def api_creator_lookup(): + params = request.get_json() + orcid = params['orcid'].strip() + # TODO: proper regex + if not (len(orcid) == len("0000-0002-1825-0097") and len(orcid.split('-')) == 4): + abort(400) + entity = CreatorIdent.query\ + .join(CreatorIdent.rev)\ + .filter(CreatorRev.orcid==orcid)\ + .first_or_404() + return creator_schema.jsonify(entity) + @app.route('/v0/container/<int:ident>', methods=['GET']) def api_container_get(ident): @@ -150,6 +163,7 @@ def api_container_create(): rev = ContainerRev( name=params.get('name', None), publisher=params.get('publisher', None), + issn=params.get('issn', None), ) ident = ContainerIdent(is_live=False, rev=rev) edit = ContainerEdit(edit_group=edit_group, ident=ident, rev=rev) @@ -160,6 +174,19 @@ def api_container_create(): db.session.commit() return container_schema.jsonify(ident) +@app.route('/v0/container/lookup', methods=['GET']) +def api_container_lookup(): + params = request.get_json() + issn = params['issn'].strip() + # TODO: proper regex + if not (len(issn) == 9 and issn[0:4].isdigit() and issn[5:7].isdigit()): + abort(400) + entity = ContainerIdent.query\ + .join(ContainerIdent.rev)\ + .filter(ContainerRev.issn==issn)\ + .first_or_404() + return container_schema.jsonify(entity) + @app.route('/v0/file/<int:ident>', methods=['GET']) def api_file_get(ident): diff --git a/tests/api.py b/tests/api.py index d9158b87..8b495c61 100644 --- a/tests/api.py +++ b/tests/api.py @@ -234,3 +234,56 @@ def test_api_release_lookup(rich_app): data=json.dumps(dict(doi="not_even_valid_doi")), headers={"content-type": "application/json"}) assert rv.status_code == 400 + +def test_api_creator_lookup(rich_app): + app = rich_app + + rv = app.get('/v0/creator/1', + headers={"content-type": "application/json"}) + assert rv.status_code == 200 + obj = json.loads(rv.data.decode('utf-8')) + + rv = app.get('/v0/creator/lookup', + data=json.dumps(dict(orcid="0000-0002-1825-0097")), + headers={"content-type": "application/json"}) + assert rv.status_code == 200 + obj = json.loads(rv.data.decode('utf-8')) + assert obj['orcid'] == "0000-0002-1825-0097" + assert obj.get('id') != None + + rv = app.get('/v0/creator/lookup', + data=json.dumps(dict(orcid="0000-0002-1825-0098")), + headers={"content-type": "application/json"}) + assert rv.status_code == 404 + + rv = app.get('/v0/creator/lookup', + data=json.dumps(dict(orcid="not_even_valid_orcid")), + headers={"content-type": "application/json"}) + assert rv.status_code == 400 + + +def test_api_container_lookup(rich_app): + app = rich_app + + rv = app.get('/v0/container/1', + headers={"content-type": "application/json"}) + assert rv.status_code == 200 + obj = json.loads(rv.data.decode('utf-8')) + + rv = app.get('/v0/container/lookup', + data=json.dumps(dict(issn="2222-3333")), + headers={"content-type": "application/json"}) + assert rv.status_code == 200 + obj = json.loads(rv.data.decode('utf-8')) + assert obj['issn'] == "2222-3333" + assert obj.get('id') != None + + rv = app.get('/v0/container/lookup', + data=json.dumps(dict(issn="2222-3334")), + headers={"content-type": "application/json"}) + assert rv.status_code == 404 + + rv = app.get('/v0/container/lookup', + data=json.dumps(dict(issn="not_even_valid_issn")), + headers={"content-type": "application/json"}) + assert rv.status_code == 400 |