Skip to content

Commit

Permalink
Disable direct calls to paper() and author()
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnsilva committed Sep 15, 2022
1 parent 9651627 commit 42d28ca
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 71 deletions.
8 changes: 8 additions & 0 deletions semanticscholar/ApiRequester.py
Expand Up @@ -16,6 +16,14 @@ def __init__(self, timeout) -> None:
'''
self._timeout = timeout

def get_timeout(self):
return self._timeout

def set_timeout(self, timeout: int):
self._timeout = timeout

timeout = property(get_timeout, set_timeout)

@retry(
wait=wait_fixed(30),
retry=retry_if_exception_type(ConnectionRefusedError),
Expand Down
11 changes: 11 additions & 0 deletions semanticscholar/SemanticScholar.py
@@ -1,3 +1,4 @@
from time import time
from semanticscholar.ApiRequester import ApiRequester
from semanticscholar.Author import Author
from semanticscholar.PaginatedResults import PaginatedResults
Expand Down Expand Up @@ -41,6 +42,15 @@ def __init__(
self._timeout = timeout
self._requester = ApiRequester(self._timeout)

def get_timeout(self):
return self._timeout

def set_timeout(self, timeout: int):
self._timeout = timeout
self._requester.timeout = timeout

timeout = property(get_timeout, set_timeout)

def get_paper(self, id: str, include_unknown_refs: bool=False, fields: list=None) -> dict:
'''Paper lookup
Expand All @@ -64,6 +74,7 @@ def get_paper(self, id: str, include_unknown_refs: bool=False, fields: list=None

data = self._requester.get_data(url, parameters, self.auth_header)
paper = Paper(data)
print(data)

return paper

Expand Down
1 change: 0 additions & 1 deletion semanticscholar/__init__.py
@@ -1,2 +1 @@
from .restful import paper, author
from .SemanticScholar import SemanticScholar
63 changes: 0 additions & 63 deletions semanticscholar/restful.py

This file was deleted.

19 changes: 12 additions & 7 deletions semanticscholar/tests/semanticscholar_tests.py
@@ -1,25 +1,30 @@
import semanticscholar as sch
import unittest
from requests.exceptions import Timeout

from semanticscholar.SemanticScholar import SemanticScholar


class SemanticScholarTest(unittest.TestCase):

def setUp(self) -> None:
self.sch = SemanticScholar(10)

def test_paper(self):
data = sch.paper('10.1093/mind/lix.236.433', timeout=5).get_raw_data()
data = self.sch.get_paper('10.1093/mind/lix.236.433').get_raw_data()
self.assertEqual(data['title'],
'Computing Machinery and Intelligence')

self.sch.timeout = 0.01
self.assertRaises(Timeout,
sch.paper,
'10.1093/mind/lix.236.433',
timeout=0.01)
self.sch.get_paper,
'10.1093/mind/lix.236.433')

def test_author(self):
data = sch.author(2262347, timeout=5).get_raw_data()
data = self.sch.get_author(2262347).get_raw_data()
self.assertEqual(data['name'], 'A. Turing')

def test_not_found(self):
data = sch.paper(0, timeout=5).get_raw_data()
data = self.sch.get_paper(0).get_raw_data()
self.assertEqual(len(data), 0)


Expand Down

0 comments on commit 42d28ca

Please sign in to comment.