Commit a1568400 authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Update InspirehepStore to consolidate exception reporting

parent 3dda33aa
""" inspirehepstore.py
"""
from .basestore import BaseStore, MSG_HTTP_DECODE, MSG_INVALID_RESPONSE
from .basestore import (BaseStore,
MSG_HTTP_DECODE,
MSG_HTTP_ERROR,
MSG_INVALID_RESPONSE)
from .exception import CdsException
from requests import HTTPError
SHELFS = ("literature", "conferences", "institutions")
......@@ -45,7 +49,7 @@ class InspirehepStore(BaseStore):
its *record id*.
Args:
rec_id (int):
rec_id (int or str):
record identifier in the store.
fieldname (str):
......@@ -59,18 +63,30 @@ class InspirehepStore(BaseStore):
CdsException:
* the server return an HTTP error.
* JSON object could not be decoded.
* the record is deleted
* ....
"""
kwargs = {
"q": f"recid={rec_id}",
"fields": fieldname}
kwargs = {"q": f"recid={rec_id}", "fields": fieldname}
obj = self.search(**kwargs)
try:
obj = self.search(**kwargs)
return obj[0]["metadata"][fieldname]
except (KeyError, TypeError):
raise CdsException(MSG_INVALID_RESPONSE)
except IndexError:
msg = "record not found. it can be deleted"
raise CdsException(msg)
except KeyError as e:
if "metadata" not in obj[0]:
msg = f"{MSG_INVALID_RESPONSE}: no 'metadata'"
elif fieldname not in obj[0]["metadata"]:
msg = f"{MSG_INVALID_RESPONSE}: no field '{fieldname}'"
raise CdsException(msg)
except TypeError as e:
msg = f"{MSG_INVALID_RESPONSE}: type error {e}"
raise CdsException(msg)
def get_ids(self, **kwargs):
"""Return a list of *record id* matching search criteria.
......@@ -128,20 +144,35 @@ class InspirehepStore(BaseStore):
"""
url = f"{self._api_record}/{rec_id}"
rep = self.interrogate(url, timeout=30)
# the response is a dict with 5 keys:
# 'id', 'created', 'updated' 'links' and 'metadata`
# More in https://github.com/inspirehep/rest-api-doc
try:
# the response is a dict with 5 keys:
# 'id', 'created', 'updated' 'links' and 'metadata`
# More in https://github.com/inspirehep/rest-api-doc
rep = self.interrogate(url, timeout=30)
obj = rep.json()
return obj["metadata"]
except HTTPError as e:
# the response is a dict with 2 keys: status, message
dct = e.response.json()
if isinstance(dct, dict) and "message" in dct:
msg = dct.get("message")
else:
msg = str(e)
raise CdsException(msg)
except ValueError:
raise CdsException(MSG_HTTP_DECODE)
msg = f"{MSG_HTTP_DECODE}: {e}"
raise CdsException(msg)
except KeyError as e:
msg = f"{MSG_INVALID_RESPONSE}: key error {e}"
raise CdsException(msg)
except (KeyError, TypeError):
raise CdsException(MSG_INVALID_RESPONSE)
except TypeError as e:
msg = f"{MSG_INVALID_RESPONSE}: type error {e}"
raise CdsException(msg)
def search(self, **kwargs):
"""Return a list of *JSON record* matching search criteria.
......@@ -193,18 +224,28 @@ class InspirehepStore(BaseStore):
#
# More in https://github.com/inspirehep/rest-api-doc
while url is not None:
rep = self.interrogate(url, timeout=30, **kwargs)
try:
rep = self.interrogate(url, timeout=30, **kwargs)
obj = rep.json()
records.extend(obj["hits"]["hits"])
url = obj["links"].get("next", None)
except HTTPError as e:
msg = f"{MSG_HTTP_ERROR}: {e}"
raise CdsException(msg)
except ValueError:
raise CdsException(MSG_HTTP_DECODE)
msg = f"{MSG_HTTP_DECODE}: {e}"
raise CdsException(msg)
except KeyError as e:
msg = f"{MSG_INVALID_RESPONSE}: key error {e}"
raise CdsException(msg)
except (KeyError, TypeError):
raise CdsException(MSG_INVALID_RESPONSE)
except TypeError as e:
msg = f"{MSG_INVALID_RESPONSE}: type error {e}"
raise CdsException(msg)
return records
......
......@@ -102,6 +102,14 @@ def test_get_record_ins_institutions_01024():
assert recjson.get("$schema", None) is not None
def test_get_record_ins_literature_01025():
# new inspirehep interface (March 2020)
# record has been deleted
store = build_store("inspirehep.net", shelf="literature")
with pytest.raises(CdsException):
store.get_record(1744709)
# ............................................................................
#
# get_field
......@@ -113,6 +121,21 @@ def test_get_field_ins_01030():
assert isinstance(value, int)
def test_get_field_ins_01031():
# new inspirehep interface (March 2020)
# record has been deleted
store = build_store("inspirehep.net", shelf="literature")
with pytest.raises(CdsException):
store.get_field(1744709, "citation_count")
def test_get_field_ins_01032():
# new inspirehep interface (March 2020)
store = build_store("inspirehep.net", shelf="literature")
with pytest.raises(CdsException):
store.get_field(1319638, "foo")
# ............................................................................
#
# search
......@@ -124,6 +147,14 @@ def test_search_ins_01040():
assert len(obj) == 1 and obj[0]["metadata"]["cnum"] == "C10-12-06"
def test_search_ins_01041():
# new inspirehep interface (March 2020)
# record has been deleted
store = build_store("inspirehep.net", shelf="conferences")
obj = store.search(q="recid:1744709")
assert len(obj) == 0
# ............................................................................
#
# test access to the collection HAL Hidden
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment