diff --git a/src/pyDataverse/api.py b/src/pyDataverse/api.py index de4a828..cfa67d4 100644 --- a/src/pyDataverse/api.py +++ b/src/pyDataverse/api.py @@ -124,7 +124,7 @@ def get_request(self, url, params=None, auth=False): ) try: - resp = get(url, params=params) + resp = get(url, params=params, stream=True) if resp.status_code == 401: error_msg = resp.json()["message"] raise ApiAuthorizationError( @@ -180,7 +180,8 @@ def post_request(self, url, data=None, auth=False, params=None, files=None): raise ApiAuthorizationError("ERROR: POST - Api token not available.") try: - resp = post(url, data=data, params=params, files=files) + resp = post(url, data=data, params=params, files=files, + stream=True) if resp.status_code == 401: error_msg = resp.json()["message"] raise ApiAuthorizationError( @@ -1662,7 +1663,8 @@ def get_datafile_metadata( # CHECK: Its not really clear, if the version query can also be done via ID. return self.get_request(url, auth=auth) - def upload_datafile(self, identifier, filename, json_str=None, is_pid=True): + def upload_datafile(self, identifier, file_name, json_str=None, is_pid=True, + file_object=None): """Add file to a dataset. Add a file to an existing Dataset. Description and tags are optional: @@ -1683,13 +1685,16 @@ def upload_datafile(self, identifier, filename, json_str=None, is_pid=True): ---------- identifier : str Identifier of the dataset. - filename : str - Full filename with path. + file_name : str + File name and path. If file_object is ``None``, a file in this path is opened in + read binary mode for upload json_str : str Metadata as JSON string. is_pid : bool ``True`` to use persistent identifier. ``False``, if not. - + file_object : file + Defaults to ``None``. Otherwise, it is expected to be a file object which will be uploaded. + In this case, the filename is treated as text and passed on to dataverse Returns ------- dict @@ -1702,8 +1707,10 @@ def upload_datafile(self, identifier, filename, json_str=None, is_pid=True): url += "/datasets/:persistentId/add?persistentId={0}".format(identifier) else: url += "/datasets/{0}/add".format(identifier) - - files = {"file": open(filename, "rb")} + if file_object is None: + files = {"file": open(file_name, "rb")} + else: + files = {"file": (file_name, file_object)} return self.post_request( url, data={"jsonData": json_str}, files=files, auth=True )