-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
273 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from kilombo.service.external.ncbi.ncbi_request import NCBIRequest | ||
|
||
|
||
class ESearch(NCBIRequest): | ||
def __init__(self): | ||
super().__init__() | ||
self.base_url = f"{self.utils_base_url}/esearch.fcgi?retmode=json" | ||
|
||
def esearch_get_studies(self, db, term): ## Unit test | ||
study_id_batches = self.paginated_request("GET", self.base_url + f"&db={db}&term={term}") | ||
study_ids = [] | ||
for batch in study_id_batches: | ||
study_ids += study_id_batches[batch]["esearchresult"]["idlist"] | ||
return study_ids | ||
|
||
def esearch_post_webenv(self, db: str, term: str): | ||
payload = {"db": db, "usehistory": "n", "retmode": "json", "term": term} | ||
response = self.paginated_request("POST", self.base_url, payload) | ||
return response["esearchresult"]["webenv"] | ||
|
||
def _is_pending_data_to_retrieve(self, responses: {}): ## Unit test | ||
if not responses: | ||
return True | ||
for response in responses.values(): | ||
response_length = len(response["esearchresult"]["idlist"]) | ||
if response_length < self.batch_size: | ||
return False | ||
else: | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import json | ||
import logging | ||
|
||
import aiohttp | ||
|
||
from kilombo.service.external.ncbi.ncbi_request import NCBIRequest | ||
|
||
|
||
class ESummary(NCBIRequest): | ||
def __init__(self): | ||
super().__init__() | ||
self.base_url = f"{self.utils_base_url}/esummary.fcgi?retmode=json" | ||
|
||
# def _is_all_data_retrieved(self, responses: {}): | ||
# return True | ||
|
||
async def esummary_get_study(self, db: str, study_id: int): ## TODO pending pagination here and split this method responsibility | ||
logging.debug(f"Started get summary for study ==> {study_id}") | ||
unauthenticated_url = f"{self.base_url}&db={db}&id={study_id}&retmax=10000" | ||
retries_count = 1 | ||
while retries_count < self.retry_max: | ||
api_key = self.api_keys[0] if retries_count % 2 == 0 else self.api_keys[1] | ||
url = unauthenticated_url + f"&api_key={api_key}" | ||
async with aiohttp.ClientSession() as session: | ||
logging.debug(f"HTTP GET Started ==> {url}") | ||
async with session.get(url) as response: | ||
if response.status == 200: | ||
logging.debug(f"Done get summary in retry #{retries_count} ==> {study_id}") | ||
response = json.loads(await response.text()) | ||
logging.debug(f"HTTP GET Done ==> {url} with response {response}") | ||
return response | ||
else: | ||
retries_count += 1 | ||
logging.debug(f"Get a {response.status} from {url}, retries count incremented to {retries_count}") | ||
raise Exception(f"Unable to fetch {study_id} in {self.retry_max} attempts") |
Oops, something went wrong.