-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
handle long requests with POST #42
Open
ESapenaVentura
wants to merge
4
commits into
master
Choose a base branch
from
esv-request-too-long
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import os | ||
|
||
version = '1.0.22' | ||
version = '1.0.23' | ||
from .config import Config | ||
|
||
config = Config(os.environ) |
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 |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
from xml.etree import ElementTree as xm | ||
|
||
import requests | ||
import requests as rq | ||
|
||
from geo_to_hca import config | ||
from geo_to_hca.utils import handle_errors | ||
|
@@ -48,13 +47,17 @@ def call_esummary(accession, db='gds'): | |
|
||
def get_entrez_esearch(term, db="sra"): | ||
throttle() | ||
esearch_response = requests.get(url=f'{config.EUTILS_BASE_URL}/esearch.fcgi', | ||
params={ | ||
"db": db, | ||
"term": term, | ||
"usehistory": "y", | ||
"format": "json", | ||
}) | ||
params={ | ||
"db": db, | ||
"usehistory": "y", | ||
"format": "json", | ||
} | ||
if len(term) <= 200: | ||
params['term'] = term | ||
esearch_response = requests.get(url=f'{config.EUTILS_BASE_URL}/esearch.fcgi', params=params) | ||
else: | ||
esearch_response = requests.post(url=f'{config.EUTILS_BASE_URL}/esearch.fcgi', params=params, data=f"term={term}") | ||
|
||
log.debug(f'esearch url: {esearch_response.url}') | ||
log.debug(f'esearch response status: {esearch_response.status_code}') | ||
log.debug(f'esearch response content: {esearch_response.text}') | ||
|
@@ -84,8 +87,8 @@ def call_efetch(db, accessions=[], | |
params= { | ||
'db': db, | ||
} | ||
if accessions: | ||
params['id'] = ",".join(accessions) | ||
|
||
accessions_string = ",".join(accessions) | ||
if webenv: | ||
params['WebEnv'] = webenv | ||
if query_key: | ||
|
@@ -95,14 +98,25 @@ def call_efetch(db, accessions=[], | |
if retmode: | ||
params['retmode'] = retmode | ||
if mode == 'call': | ||
efetch_response = rq.get(url, params=params) | ||
if len(accessions_string) <= 200: | ||
if accessions_string: | ||
params['id'] = accessions_string | ||
efetch_response = requests.get(url, params=params) | ||
else: | ||
efetch_response = requests.post(url, params=params, data=f"id={accessions_string}") | ||
if efetch_response.status_code == STATUS_ERROR_CODE: | ||
raise handle_errors.NotFoundSRA(efetch_response, accessions) | ||
return efetch_response | ||
elif mode == 'prepare': | ||
return Request(method='GET', | ||
if len(accessions_string) <= 200: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My earlier comment about the '200' magic number is amplified here. |
||
if accessions_string: | ||
params['id'] = accessions_string | ||
return Request(method='GET', | ||
url=f'{config.EUTILS_BASE_URL}/efetch.fcgi', | ||
params=params).prepare() | ||
return Request(method='POST', | ||
url=f'{config.EUTILS_BASE_URL}/efetch.fcgi', | ||
params=params, data=f'id={accessions_string}').prepare() | ||
else: | ||
raise ValueError(f'unsupported call mode for efetch: {mode}') | ||
|
||
|
@@ -112,7 +126,7 @@ def request_bioproject_metadata(bioproject_accession: str): | |
Function to request metadata at the project level given an SRA Bioproject accession. | ||
""" | ||
throttle() | ||
srp_bioproject_url = rq.get( | ||
srp_bioproject_url = requests.get( | ||
f'{config.EUTILS_BASE_URL}/efetch/fcgi?db=bioproject&id={bioproject_accession}') | ||
if srp_bioproject_url.status_code == STATUS_ERROR_CODE: | ||
raise handle_errors.NotFoundSRA(srp_bioproject_url, bioproject_accession) | ||
|
@@ -124,7 +138,7 @@ def request_pubmed_metadata(project_pubmed_id: str): | |
Function to request metadata at the publication level given a pubmed ID. | ||
""" | ||
throttle() | ||
pubmed_url = rq.get( | ||
pubmed_url = requests.get( | ||
f'{config.EUTILS_BASE_URL}/efetch/fcgi?db=pubmed&id={project_pubmed_id}&rettype=xml') | ||
if pubmed_url.status_code == STATUS_ERROR_CODE: | ||
raise handle_errors.NotFoundSRA(pubmed_url, project_pubmed_id) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't use "magic numbers" in the code.
Define it as a constant.
If the number's source's external documentation, please add a link to the relevant document.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation is very cryptic, literally says
more than several hundred characters long
for eSearch, andif more than about 200 UIDs are to be provided, the request should be made using the HTTP POST method
for EFetch. That's why I chose 200, theterms
field can probably take more characters but I'd need to make tests to see how many hundred.The documentation is here https://www.ncbi.nlm.nih.gov/books/NBK25499/#chapter4.ESearch for Esearch and https://www.ncbi.nlm.nih.gov/books/NBK25499/#_chapter4_EFetch_ for EFetch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will add it as a constant, though!