diff --git a/semanticscholar/ApiRequester.py b/semanticscholar/ApiRequester.py index 51b9f76..9d553f1 100644 --- a/semanticscholar/ApiRequester.py +++ b/semanticscholar/ApiRequester.py @@ -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), diff --git a/semanticscholar/SemanticScholar.py b/semanticscholar/SemanticScholar.py index bfea274..8aa8223 100644 --- a/semanticscholar/SemanticScholar.py +++ b/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 @@ -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 @@ -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 diff --git a/semanticscholar/__init__.py b/semanticscholar/__init__.py index 249b2d2..ef199b1 100644 --- a/semanticscholar/__init__.py +++ b/semanticscholar/__init__.py @@ -1,2 +1 @@ -from .restful import paper, author from .SemanticScholar import SemanticScholar diff --git a/semanticscholar/restful.py b/semanticscholar/restful.py deleted file mode 100644 index 8721dd3..0000000 --- a/semanticscholar/restful.py +++ /dev/null @@ -1,63 +0,0 @@ -from .SemanticScholar import SemanticScholar -import warnings - - -def paper( - id: str, - timeout: int=2, - include_unknown_references: bool=False, - api_key: str=None, - api_url: str=None, - graph_api: bool=True - ) -> dict: - '''Paper lookup - - :param str id: S2PaperId, DOI or ArXivId. - :param float timeout: an exception is raised - if the server has not issued a response for timeout seconds. - :param bool include_unknown_references: - (optional) include non referenced paper. - :param str api_key: (optional) private API key. - :param str api_url: (optional) custom API url. - :param bool graph_api: (optional) whether use new Graph API. - :returns: paper data or empty :class:`dict` if not found. - :rtype: :class:`dict` - ''' - - warnings.warn( - "Direct calls to paper() will be disabled in the future." + - " Create an instance of SemanticScholar class instead.", - DeprecationWarning) - - sch = SemanticScholar(timeout, api_key, api_url, graph_api) - - return sch.get_paper(id, include_unknown_references) - - -def author( - id: str, - timeout: int=2, - api_key: str=None, - api_url: str=None, - graph_api: bool=True - ) -> dict: - '''Author lookup - - :param str id: S2AuthorId. - :param float timeout: an exception is raised - if the server has not issued a response for timeout seconds. - :param str api_key: (optional) private API key. - :param str api_url: (optional) custom API url. - :param bool graph_api: (optional) whether use new Graph API. - :returns: author data or empty :class:`dict` if not found. - :rtype: :class:`dict` - ''' - - warnings.warn( - "Direct calls to author() will be disabled in the future." + - " Create an instance of SemanticScholar class instead.", - DeprecationWarning) - - sch = SemanticScholar(timeout, api_key, api_url, graph_api) - - return sch.get_author(id) diff --git a/semanticscholar/tests/semanticscholar_tests.py b/semanticscholar/tests/semanticscholar_tests.py index c7cbb61..d1c58a0 100644 --- a/semanticscholar/tests/semanticscholar_tests.py +++ b/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)