Skip to content

Commit

Permalink
feat!: 🥅 replace empty return with ObjectNotFoundExeception
Browse files Browse the repository at this point in the history
`get_author()` and `get_paper()` should now raise an `ObjectNotFoundExeception` when the Paper or Author ID is not found.

BREAKING CHANGE: `ObjectNotFoundExeception` raised instead of returning an empty `dict`.
  • Loading branch information
danielnsilva committed Jan 3, 2023
1 parent 63acfcd commit ae50750
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 16 deletions.
5 changes: 4 additions & 1 deletion semanticscholar/ApiRequester.py
Expand Up @@ -6,7 +6,7 @@
wait_fixed)

from semanticscholar.SemanticScholarException import \
BadQueryParametersException
BadQueryParametersException, ObjectNotFoundExeception


class ApiRequester:
Expand Down Expand Up @@ -73,6 +73,9 @@ def get_data(
raise BadQueryParametersException(data['error'])
elif r.status_code == 403:
raise PermissionError('HTTP status 403 Forbidden.')
elif r.status_code == 404:
data = r.json()
raise ObjectNotFoundExeception(data['error'])
elif r.status_code == 429:
raise ConnectionRefusedError('HTTP status 429 Too Many Requests.')

Expand Down
10 changes: 6 additions & 4 deletions semanticscholar/SemanticScholar.py
Expand Up @@ -87,8 +87,9 @@ def get_paper(
:param bool include_unknown_refs: (optional) include non referenced \
paper.
:param list fields: (optional) list of the fields to be returned.
:returns: paper data or empty :class:`dict` if not found.
:rtype: :class:`dict`
:returns: paper data
:rtype: :class:`Paper`
:raises: ObjectNotFoundExeception: if Paper ID not found.
'''

if not fields:
Expand Down Expand Up @@ -208,8 +209,9 @@ def get_author(
graph#tag/Author-Data/operation/get_graph_get_author>`_
:param str author_id: S2AuthorId.
:returns: author data or empty :class:`dict` if not found.
:rtype: :class:`dict`
:returns: author data
:rtype: :class:`Author`
:raises: ObjectNotFoundExeception: if Author ID not found.
'''

if not fields:
Expand Down
4 changes: 4 additions & 0 deletions semanticscholar/SemanticScholarException.py
Expand Up @@ -7,3 +7,7 @@ def __init__(self, *args: object) -> None:

class BadQueryParametersException(SemanticScholarException):
'''Invalid query params or unsupported fields.'''


class ObjectNotFoundExeception(SemanticScholarException):
'''Paper or Author ID not found'''
64 changes: 58 additions & 6 deletions tests/data/test_not_found.yaml
Expand Up @@ -27,27 +27,79 @@ interactions:
Content-Type:
- application/json
Date:
- Wed, 21 Dec 2022 03:23:31 GMT
- Tue, 03 Jan 2023 16:00:34 GMT
Via:
- 1.1 b119d190fd68a1b5c82101503504cff2.cloudfront.net (CloudFront)
- 1.1 80e150c3cd619899dc38fb20936dc086.cloudfront.net (CloudFront)
X-Amz-Cf-Id:
- kSAigyJt4Ak0wWxJa3hPZDEU6oyyiSrFuyYFxIj8nhHeqhumllzvpQ==
- nBcZYrmZQkUBPMYfdHk09L1Og8Au_OA5x879R6rCWc8Y1uz_RgeV4g==
X-Amz-Cf-Pop:
- GRU3-P1
X-Cache:
- Error from cloudfront
x-amz-apigw-id:
- dehYAHw2vHcFchQ=
- eLGdYGpIvHcFWPQ=
x-amzn-Remapped-Connection:
- keep-alive
x-amzn-Remapped-Content-Length:
- '38'
x-amzn-Remapped-Date:
- Wed, 21 Dec 2022 03:23:31 GMT
- Tue, 03 Jan 2023 16:00:34 GMT
x-amzn-Remapped-Server:
- gunicorn
x-amzn-RequestId:
- aa41f970-44a0-4ca0-a988-08f26634e797
- e6531cc3-4df9-41c4-945c-4c33ecefffc4
status:
code: 404
message: Not Found
- request:
body: null
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- python-requests/2.28.1
method: GET
uri: https://api.semanticscholar.org/graph/v1/author/0?&fields=affiliations,aliases,authorId,citationCount,externalIds,hIndex,homepage,name,paperCount,papers,papers.abstract,papers.authors,papers.citationCount,papers.externalIds,papers.fieldsOfStudy,papers.influentialCitationCount,papers.isOpenAccess,papers.journal,papers.paperId,papers.publicationDate,papers.publicationTypes,papers.referenceCount,papers.s2FieldsOfStudy,papers.title,papers.url,papers.venue,papers.year,url
response:
body:
string: '{"error":"Author with id 0 not found"}
'
headers:
Access-Control-Allow-Origin:
- '*'
Connection:
- keep-alive
Content-Length:
- '39'
Content-Type:
- application/json
Date:
- Tue, 03 Jan 2023 16:00:35 GMT
Via:
- 1.1 556546966a883b579a433c9e90aa37f8.cloudfront.net (CloudFront)
X-Amz-Cf-Id:
- _uDqxTap17JNGWPUyZ0AjbQG9rglrKI65fCfOgnl_9tqlHb3Ihfg_w==
X-Amz-Cf-Pop:
- GRU3-P1
X-Cache:
- Error from cloudfront
x-amz-apigw-id:
- eLGdiE64vHcFRnQ=
x-amzn-Remapped-Connection:
- keep-alive
x-amzn-Remapped-Content-Length:
- '39'
x-amzn-Remapped-Date:
- Tue, 03 Jan 2023 16:00:35 GMT
x-amzn-Remapped-Server:
- gunicorn
x-amzn-RequestId:
- fe7744cf-fb2e-4e8f-9ac0-e5d094e2e410
status:
code: 404
message: Not Found
Expand Down
12 changes: 7 additions & 5 deletions tests/test_semanticscholar.py
Expand Up @@ -9,8 +9,8 @@
from semanticscholar.Journal import Journal
from semanticscholar.Paper import Paper
from semanticscholar.SemanticScholar import SemanticScholar
from semanticscholar.SemanticScholarException import \
BadQueryParametersException
from semanticscholar.SemanticScholarException import (
BadQueryParametersException, ObjectNotFoundExeception)
from semanticscholar.Tldr import Tldr

test_vcr = vcr.VCR(
Expand Down Expand Up @@ -114,7 +114,7 @@ def test_get_papers(self):
'0f40b1f08821e22e859c6050916cec3667778613']
data = self.sch.get_papers(list_of_paper_ids)
for item in data:
with self.subTest(line=item.paperId):
with self.subTest(subtest=item.paperId):
self.assertIn(
'E. Duflo', [author.name for author in item.authors])

Expand All @@ -141,8 +141,10 @@ def test_get_authors(self):

@test_vcr.use_cassette
def test_not_found(self):
data = self.sch.get_paper(0).raw_data
self.assertEqual(len(data), 0)
methods = [self.sch.get_paper, self.sch.get_author]
for method in methods:
with self.subTest(subtest=method.__name__):
self.assertRaises(ObjectNotFoundExeception, method, 0)

@test_vcr.use_cassette
def test_bad_query_parameters(self):
Expand Down

0 comments on commit ae50750

Please sign in to comment.