diff options
author | Bryan Newbold <bnewbold@robocracy.org> | 2018-04-23 19:16:16 -0700 |
---|---|---|
committer | Bryan Newbold <bnewbold@robocracy.org> | 2018-04-23 19:16:16 -0700 |
commit | 88de9b35a388c4a690f46a24dfbfdb261fb520c0 (patch) | |
tree | e533cd884b1e8d44335257bd039f68368847927a | |
parent | 2f5cd249ff0f7a6621885ec751349575dd0f1c9d (diff) | |
download | fatcat-88de9b35a388c4a690f46a24dfbfdb261fb520c0.tar.gz fatcat-88de9b35a388c4a690f46a24dfbfdb261fb520c0.zip |
basic DOI lookup
-rw-r--r-- | fatcat/api.py | 13 | ||||
-rw-r--r-- | tests/api.py | 26 |
2 files changed, 39 insertions, 0 deletions
diff --git a/fatcat/api.py b/fatcat/api.py index 838168af..52b0e21a 100644 --- a/fatcat/api.py +++ b/fatcat/api.py @@ -101,6 +101,19 @@ def api_release_random(): entity = ReleaseIdent.query.order_by(db.func.random()).first() return redirect('/v0/release/{}'.format(entity.id)) +@app.route('/v0/release/lookup', methods=['GET']) +def api_release_lookup(): + params = request.get_json() + doi = params['doi'].lower() + 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)\ + .first_or_404() + return release_schema.jsonify(entity) + @app.route('/v0/creator/<int:ident>', methods=['GET']) def api_creator_get(ident): diff --git a/tests/api.py b/tests/api.py index 6823aa6d..d9158b87 100644 --- a/tests/api.py +++ b/tests/api.py @@ -208,3 +208,29 @@ def test_api_rich_create(app): # test that editor's active edit group is now invalid editor = Editor.query.first() assert editor.active_edit_group == None + +def test_api_release_lookup(rich_app): + app = rich_app + + rv = app.get('/v0/release/1', + headers={"content-type": "application/json"}) + assert rv.status_code == 200 + obj = json.loads(rv.data.decode('utf-8')) + + rv = app.get('/v0/release/lookup', + data=json.dumps(dict(doi="10.1234/5678")), + headers={"content-type": "application/json"}) + assert rv.status_code == 200 + obj = json.loads(rv.data.decode('utf-8')) + assert obj['doi'] == "10.1234/5678" + assert obj.get('id') != None + + rv = app.get('/v0/release/lookup', + data=json.dumps(dict(doi="10.1234/5678_noexit")), + headers={"content-type": "application/json"}) + assert rv.status_code == 404 + + rv = app.get('/v0/release/lookup', + data=json.dumps(dict(doi="not_even_valid_doi")), + headers={"content-type": "application/json"}) + assert rv.status_code == 400 |