Skip to content

Commit

Permalink
feat: limit list of IDs size to 500 in get_papers()
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnsilva committed Jan 6, 2024
1 parent ff60635 commit 4c94f2f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
6 changes: 5 additions & 1 deletion semanticscholar/AsyncSemanticScholar.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ async def get_papers(
:calls: `POST /paper/batch <https://api.semanticscholar.org/api-docs/\
graph#tag/Paper-Data/operation/post_graph_get_papers>`_
:param str paper_ids: list of IDs (must be <= 1000) - S2PaperId,\
:param str paper_ids: list of IDs (must be <= 500) - S2PaperId,\
CorpusId, DOI, ArXivId, MAG, ACL, PMID, PMCID, or URL from:
- semanticscholar.org
Expand All @@ -143,6 +143,10 @@ async def get_papers(
:raises: BadQueryParametersException: if no paper was found.
'''

if len(paper_ids) > 500 or len(paper_ids) == 0:
raise ValueError(
'The paper_ids parameter must be a list of 1 to 500 IDs.')

if not fields:
fields = Paper.SEARCH_FIELDS

Expand Down
2 changes: 1 addition & 1 deletion semanticscholar/SemanticScholar.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def get_papers(
:calls: `POST /paper/batch <https://api.semanticscholar.org/api-docs/\
graph#tag/Paper-Data/operation/post_graph_get_papers>`_
:param str paper_ids: list of IDs (must be <= 1000) - S2PaperId,\
:param str paper_ids: list of IDs (must be <= 500) - S2PaperId,\
CorpusId, DOI, ArXivId, MAG, ACL, PMID, PMCID, or URL from:
- semanticscholar.org
Expand Down
18 changes: 18 additions & 0 deletions tests/test_semanticscholar.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ def test_get_papers(self):
self.assertIn(
'E. Duflo', [author.name for author in item.authors])

def test_get_papers_list_size_exceeded(self):
list_of_paper_ids = [str(i) for i in range(501)]
self.assertRaises(ValueError, self.sch.get_papers, list_of_paper_ids)

def test_get_papers_list_empty(self):
list_of_paper_ids = []
self.assertRaises(ValueError, self.sch.get_papers, list_of_paper_ids)

@test_vcr.use_cassette
def test_get_paper_authors(self):
data = self.sch.get_paper_authors('10.2139/ssrn.2250500')
Expand Down Expand Up @@ -498,6 +506,16 @@ async def test_get_papers_async(self):
with self.subTest(subtest=item.paperId):
self.assertIn(
'E. Duflo', [author.name for author in item.authors])

async def test_get_papers_list_size_exceeded_async(self):
list_of_paper_ids = [str(i) for i in range(501)]
with self.assertRaises(ValueError):
await self.sch.get_papers(list_of_paper_ids)

async def test_get_papers_list_empty_async(self):
list_of_paper_ids = []
with self.assertRaises(ValueError):
await self.sch.get_papers(list_of_paper_ids)

@test_vcr.use_cassette
async def test_get_paper_authors_async(self):
Expand Down

0 comments on commit 4c94f2f

Please sign in to comment.