Skip to content

Commit

Permalink
Merge pull request #56 from gabriel-trigo/master
Browse files Browse the repository at this point in the history
Async functionality implemented.
  • Loading branch information
danielnsilva authored Nov 17, 2023
2 parents 1ee68d0 + 2741953 commit 4c930db
Show file tree
Hide file tree
Showing 57 changed files with 431,961 additions and 542,404 deletions.
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
requests
tenacity
httpx
asyncio
nest_asyncio
43 changes: 33 additions & 10 deletions semanticscholar/ApiRequester.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import json
from typing import List, Union

import requests
import httpx
import asyncio
import warnings
from tenacity import (retry, retry_if_exception_type, stop_after_attempt,
wait_fixed)

Expand All @@ -10,16 +11,13 @@


class ApiRequester:
'''
This class handles calls to Semantic Scholar API.
'''

def __init__(self, timeout) -> None:
'''
:param float timeout: an exception is raised \
if the server has not issued a response for timeout seconds.
'''
self._timeout = timeout
self.timeout = timeout

@property
def timeout(self) -> int:
Expand All @@ -40,7 +38,7 @@ def timeout(self, timeout: int) -> None:
retry=retry_if_exception_type(ConnectionRefusedError),
stop=stop_after_attempt(10)
)
def get_data(
async def get_data_async(
self,
url: str,
parameters: str,
Expand All @@ -59,9 +57,10 @@ def get_data(

url = f'{url}?{parameters}'
method = 'POST' if payload else 'GET'
payload = json.dumps(payload) if payload else None
r = requests.request(
method, url, timeout=self._timeout, headers=headers, data=payload)

async with httpx.AsyncClient() as client:
r = await client.request(
method, url, timeout=self._timeout, headers=headers, json=payload)

data = {}
if r.status_code == 200:
Expand All @@ -83,3 +82,27 @@ def get_data(
raise Exception(data['message'])

return data

def get_data(
self,
url: str,
parameters: str,
headers: dict,
payload: dict = None
) -> Union[dict, List[dict]]:
warnings.warn(
"get_data() is deprecated and will be disabled in the future," +
" use the async version instead.",
DeprecationWarning
)

loop = asyncio.get_event_loop()
loop.run_until_complete(
self.get_data_async(
url=url,
parameters=parameters,
headers=headers,
payload=payload
)
)

Loading

0 comments on commit 4c930db

Please sign in to comment.