Skip to content

Commit b898b29

Browse files
authored
Merge pull request #8 from jlab/uncouple_fetch
add an interception to automatically fetch files from qiita central
2 parents 57f031e + 5635f2b commit b898b29

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

qiita_client/qiita_client.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import fnmatch
1818
from io import BytesIO
1919
from zipfile import ZipFile
20+
import re
2021

2122

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

416+
def _fetch_artifact_files(self, ainfo):
417+
"""helper method to fetch all files of an artifact from Qiita main.
418+
419+
Parameters
420+
----------
421+
ainfo : json dict
422+
Information about Qiita artifact
423+
424+
Returns
425+
-------
426+
Same as input BUT filepaths are adapated after downloading files from
427+
Qiita main to local IF protocol coupling != filesystem. Otherwise, no
428+
change occurs.
429+
"""
430+
if self._plugincoupling != 'filesystem':
431+
if 'files' in ainfo.keys():
432+
ainfo['files'] = {
433+
filetype: [
434+
{
435+
k: self.fetch_file_from_central(v)
436+
if k == 'filepath' else v
437+
for k, v
438+
in file.items()}
439+
for file
440+
in ainfo['files'][filetype]]
441+
for filetype
442+
in ainfo['files'].keys()
443+
}
444+
return ainfo
445+
else:
446+
return ainfo
447+
415448
def get(self, url, rettype='json', **kwargs):
416449
"""Execute a get request against the Qiita server
417450
@@ -431,9 +464,28 @@ def get(self, url, rettype='json', **kwargs):
431464
The JSON response from the server
432465
"""
433466
logger.debug('Entered QiitaClient.get()')
434-
return self._request_retry(
467+
result = self._request_retry(
435468
self._session.get, url, rettype=rettype, **kwargs)
436469

470+
if self._plugincoupling != 'filesystem':
471+
# intercept get requests from plugins that request metadata or
472+
# artifact files and ensure they get transferred from Qiita
473+
# central, when not using "filesystem"
474+
if re.search(r"/qiita_db/prep_template/\d+/?$", url):
475+
# client is requesting filepath to a prep/metadata file, see
476+
# qiita/qiita_db/handlers/prep_template.py::
477+
# PrepTemplateDBHandler::get
478+
# for the "result" data-structure
479+
for fp in ['prep-file', 'sample-file']:
480+
result[fp] = self.fetch_file_from_central(result[fp])
481+
elif re.search(r"/qiita_db/artifacts/\d+/?$", url):
482+
# client is requesting an artifact, see
483+
# qiita/qiita_db/handlers/artifact.py::ArtifactHandler::get
484+
# for the "result" data-structure
485+
result = self._fetch_artifact_files(result)
486+
487+
return result
488+
437489
def post(self, url, **kwargs):
438490
"""Execute a post request against the Qiita server
439491

0 commit comments

Comments
 (0)