Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion qiita_client/qiita_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import fnmatch
from io import BytesIO
from zipfile import ZipFile
import re


try:
Expand Down Expand Up @@ -412,6 +413,38 @@ def _request_retry(self, req, url, rettype='json', **kwargs):
"Request '%s %s' did not succeed. Status code: %d. Message: %s"
% (req.__name__, url, r.status_code, r.text))

def _fetch_artifact_files(self, ainfo):
"""helper method to fetch all files of an artifact from Qiita main.

Parameters
----------
ainfo : json dict
Information about Qiita artifact

Returns
-------
Same as input BUT filepaths are adapated after downloading files from
Qiita main to local IF protocol coupling != filesystem. Otherwise, no
change occurs.
"""
if self._plugincoupling != 'filesystem':
if 'files' in ainfo.keys():
ainfo['files'] = {
filetype: [
{
k: self.fetch_file_from_central(v)
if k == 'filepath' else v
for k, v
in file.items()}
for file
in ainfo['files'][filetype]]
for filetype
in ainfo['files'].keys()
}
return ainfo
else:
return ainfo

def get(self, url, rettype='json', **kwargs):
"""Execute a get request against the Qiita server

Expand All @@ -431,9 +464,28 @@ def get(self, url, rettype='json', **kwargs):
The JSON response from the server
"""
logger.debug('Entered QiitaClient.get()')
return self._request_retry(
result = self._request_retry(
self._session.get, url, rettype=rettype, **kwargs)

if self._plugincoupling != 'filesystem':
# intercept get requests from plugins that request metadata or
# artifact files and ensure they get transferred from Qiita
# central, when not using "filesystem"
if re.search(r"/qiita_db/prep_template/\d+/?$", url):
# client is requesting filepath to a prep/metadata file, see
# qiita/qiita_db/handlers/prep_template.py::
# PrepTemplateDBHandler::get
# for the "result" data-structure
for fp in ['prep-file', 'sample-file']:
result[fp] = self.fetch_file_from_central(result[fp])
elif re.search(r"/qiita_db/artifacts/\d+/?$", url):
# client is requesting an artifact, see
# qiita/qiita_db/handlers/artifact.py::ArtifactHandler::get
# for the "result" data-structure
result = self._fetch_artifact_files(result)

return result

def post(self, url, **kwargs):
"""Execute a post request against the Qiita server

Expand Down
Loading