Skip to content

Commit

Permalink
Responded to PR suggestions
Browse files Browse the repository at this point in the history
Added try catch and exlplict check for 200 responses from ENA API
  • Loading branch information
MGS-sails committed Aug 20, 2024
1 parent fcf92ff commit b1078a1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion ci/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ emg:
results_production_dir: '/dummy/path/results'
# metagenomics exchange
me_api: 'https://wwwdev.ebi.ac.uk/ena/registry/metagenome/api'
ena_api_password: None
ena_api_password: null
16 changes: 10 additions & 6 deletions emgapianns/management/lib/create_or_update_study.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,25 +297,29 @@ def check_if_study_is_public(self, study_id):
headers = {
"accept": "*/*"
}
response = requests.get(url, params=None, headers=headers)
if response.json() == []:
try:
response = requests.get(url, params=None, headers=headers)
response.raise_for_status() # Raise an HTTPError for bad responses
if response.status_code != 200 or response.json() == []:
is_public = False
else:
is_public = response.json()[0]['secondary_study_accession'] == study_id
except requests.RequestException as e:
logging.error(f"Error checking if study is public: {e}")
is_public = False
else:
is_public = response.json()[0]['secondary_study_accession'] == study_id

return is_public

def verify_study_ownership(self, study_id, submission_account_id):
url = f"https://www.ebi.ac.uk/ena/submit/report/studies/{study_id}"
ena_api_password = EMG_CONF['emg']['ena_api_password']
if not ena_api_password:
logging.warning("ENA API password is missing. Study ownership cannot be verified.")
logging.error("ENA API password is missing. Study ownership cannot be verified.")
return False
auth_string = f"mg-{submission_account_id}:{ena_api_password}"
headers = {
"accept": "*/*",
"Authorization": f"Basic {b64encode(auth_string.encode()).decode()}"
}
response = requests.get(url, headers=headers)
ownership_verified = False
return response.status_code == 200 and study_id in response.text

0 comments on commit b1078a1

Please sign in to comment.