Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows for uploading a file object. #62

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
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
23 changes: 15 additions & 8 deletions src/pyDataverse/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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
)
Expand Down