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

Add support for filename, content-type and headers when uploading files #129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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: 16 additions & 7 deletions aptly_api/parts/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os
from typing import Sequence, List, Tuple, BinaryIO, cast, Optional # noqa: F401
from typing import Sequence, List, Tuple, TextIO, BinaryIO, cast, Optional, Union, Dict # noqa: F401

from aptly_api.base import BaseAPIClient, AptlyAPIException

_tuplefiletype = Union[
Tuple[str, Union[TextIO, BinaryIO, str, bytes]],
Tuple[str, Union[TextIO, BinaryIO, str, bytes], str],
Tuple[str, Union[TextIO, BinaryIO, str, bytes], str, Dict[str, str]]
]


class FilesAPISection(BaseAPIClient):
def list(self, directory: Optional[str] = None) -> Sequence[str]:
Expand All @@ -18,13 +24,16 @@ def list(self, directory: Optional[str] = None) -> Sequence[str]:

return cast(List[str], resp.json())

def upload(self, destination: str, *files: str) -> Sequence[str]:
to_upload = [] # type: List[Tuple[str, BinaryIO]]
def upload(self, destination: str, *files: Union[str, _tuplefiletype]) -> Sequence[str]:
to_upload = [] # type: List[Tuple[str, Union[BinaryIO, _tuplefiletype]]]
for f in files:
if not os.path.exists(f) or not os.access(f, os.R_OK):
if isinstance(f, tuple):
to_upload.append((f[0], f),)
elif not os.path.exists(f) or not os.access(f, os.R_OK):
raise AptlyAPIException("File to upload %s can't be opened or read" % f)
fh = open(f, mode="rb")
to_upload.append((f, fh),)
else:
fh = open(f, mode="rb")
to_upload.append((f, fh),)

try:
resp = self.do_post("api/files/%s" % destination,
Expand All @@ -33,7 +42,7 @@ def upload(self, destination: str, *files: str) -> Sequence[str]:
raise
finally:
for fn, to_close in to_upload:
if not to_close.closed:
if not isinstance(to_close, tuple) and not to_close.closed:
to_close.close()

return cast(List[str], resp.json())
Expand Down
8 changes: 8 additions & 0 deletions aptly_api/tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ def test_upload_failed(self, *, rmock: requests_mock.Mocker) -> None:
with self.assertRaises(AptlyAPIException):
self.fapi.upload("test", os.path.join(os.path.dirname(__file__), "testpkg.deb"))

def test_upload_with_tuples(self, *, rmock: requests_mock.Mocker) -> None:
rmock.post("http://test/api/files/test", text='["test/otherpkg.deb", "test/binpkg.deb"]')
with open(os.path.join(os.path.dirname(__file__), "testpkg.deb"), "rb") as pkgf:
self.assertSequenceEqual(
self.fapi.upload("test", ("otherpkg.deb", pkgf), ("binpkg.deb", b"dpkg-contents")),
['test/otherpkg.deb', 'test/binpkg.deb'],
)

def test_delete(self, *, rmock: requests_mock.Mocker) -> None:
rmock.delete("http://test/api/files/test",
text='{}')
Expand Down