diff --git a/nomad/api/raw.py b/nomad/api/raw.py index 42d2bb32cf8fe4971bc23bf15e145fe963707cc0..f291552295290944e08928e82ce57998568d13ea 100644 --- a/nomad/api/raw.py +++ b/nomad/api/raw.py @@ -102,13 +102,54 @@ def get_raw_files(upload_hash): :status 404: calc with given hash does not exist or one of the given files does not exist :returns: a streamed .zip archive with the raw data """ - logger = get_logger(__name__, endpoint='raw', action='get', upload_hash=upload_hash) - files_str = request.args.get('files', None) if files_str is None: abort(400, message="No files argument given.") files = [file.strip() for file in files_str.split(',')] + return respond_to_get_raw_files(upload_hash, files) + + +@app.route('%s/raw/<string:upload_hash>' % base_path, methods=['POST']) +def get_raw_files_post(upload_hash): + """ + Get multiple raw calculation files. + + .. :quickref: raw; Get multiple raw calculation files. + + **Example request**: + + .. sourcecode:: http + + POST /nomad/api/raw/W36aqCzAKxOCfIiMFsBJh3nHPb4a HTTP/1.1 + Accept: application/gz + Content-Type: application/json + + { + "files": ["Si/si.out", "Si/aux.txt"] + } + + :param string upload_hash: the hash based identifier of the upload + :jsonparam files: a comma separated list of file paths + :resheader Content-Type: application/json + :status 200: calc raw data successfully retrieved + :status 404: calc with given hash does not exist or one of the given files does not exist + :returns: a streamed .zip archive with the raw data + """ + json_data = request.get_json() + if json_data is None: + json_data = {} + + if 'files' not in json_data: + abort(400, message='No files given, use key "files" in json body to provide file paths.') + files = json_data['files'] + + return respond_to_get_raw_files(upload_hash, files) + + +def respond_to_get_raw_files(upload_hash, files): + logger = get_logger(__name__, endpoint='raw', action='get files', upload_hash=upload_hash) + repository_file = RepositoryFile(upload_hash) if not repository_file.exists(): abort(404, message='The upload with hash %s does not exist.' % upload_hash) diff --git a/tests/test_api.py b/tests/test_api.py index 4d87360984ddd31568c662754c302fb71b296ce5..6ee85869019fc8b43522c626dbfda25355b094bd 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -431,6 +431,20 @@ class TestRaw: assert zip_file.testzip() is None assert len(zip_file.namelist()) == 5 + def test_raw_files_post(self, client, example_repo_with_files): + repo_entry = example_repo_with_files + url = '/raw/%s' % repo_entry.upload_hash + rv = client.post( + url, + data=json.dumps(dict(files=list(repo_entry.aux_files))), + content_type='application/json') + + assert rv.status_code == 200 + assert len(rv.data) > 0 + with zipfile.ZipFile(io.BytesIO(rv.data)) as zip_file: + assert zip_file.testzip() is None + assert len(zip_file.namelist()) == 4 + def test_raw_files_missing_file(self, client, example_repo_with_files): repo_entry = example_repo_with_files url = '/raw/%s?files=%s,missing/file.txt' % (