Skip to content

Commit

Permalink
Add header parameter to upload (#176)
Browse files Browse the repository at this point in the history
Co-authored-by: Saugat Pachhai (सौगात) <[email protected]>
  • Loading branch information
erikman and skshetry authored Jul 13, 2024
1 parent 9ea9844 commit 0424572
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/webdav4/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,7 @@ def upload_file( # noqa: PLR0913
overwrite: bool = False,
chunk_size: Optional[int] = None,
callback: Optional[Callable[[int], Any]] = None,
headers: Optional[Dict[str, str]] = None,
) -> None:
"""Upload file from local path to a given remote path."""
with open(from_path, mode="rb") as fobj:
Expand All @@ -634,6 +635,7 @@ def upload_file( # noqa: PLR0913
overwrite=overwrite,
chunk_size=chunk_size,
callback=callback,
headers=headers,
)

def upload_fileobj( # noqa: PLR0913
Expand All @@ -644,8 +646,12 @@ def upload_fileobj( # noqa: PLR0913
callback: Optional[Callable[[int], Any]] = None,
chunk_size: Optional[int] = None,
size: Optional[int] = None,
headers: Optional[Dict[str, str]] = None,
) -> None:
"""Upload file from file object to given path."""
if headers is None:
headers = {}

# we try to avoid chunked transfer as much as possible
# so we try to use size as a hint if provided.
# else, we will try to find that out from the file object
Expand All @@ -654,7 +660,9 @@ def upload_fileobj( # noqa: PLR0913
if size is None:
size = peek_filelike_length(file_obj)

headers = {"Content-Length": str(size)} if size is not None else None
if size is not None:
headers = {"Content-Length": str(size), **headers}

if not overwrite and self.exists(to_path):
raise ResourceAlreadyExists(to_path)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ def test_upload_fobj_size_hints(client: Client):

wrapped = ReadWrapper(BytesIO(b"foobar")) # type: ignore
client.upload_fileobj(wrapped, "foobar") # type: ignore
assert m.call_args[1]["headers"] is None
assert m.call_args[1]["headers"] == {}

wrapped = ReadWrapper(BytesIO(b"foobar")) # type: ignore
client.upload_fileobj(wrapped, "foobar", size=6) # type: ignore
Expand Down

0 comments on commit 0424572

Please sign in to comment.