Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
#1 Add dynamic download (#28)
Browse files Browse the repository at this point in the history
#1 Add dynamic download
  • Loading branch information
uilianries committed Jul 10, 2019
2 parents 0523298 + 8e5272c commit f62019b
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
57 changes: 57 additions & 0 deletions bintray/bintray.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,63 @@ def download_content(self, subject, repo, remote_file_path, local_file_path):
self._logger.info("Download successfully: {}".format(url))
return response

def dynamic_download(self, subject, repo, remote_file_path, local_file_path, bt_package=None):
""" Download a file based on a dynamic file_path .
This resource is only available for Bintray Premium repositories.
Currently, only the $latest token is supported, which is useful for downloading the
latest file published under a specified package.
Package name can be supplied as a:
The bt_package query parameter
The bt_package matrix parameter
The X-Bintray-Package request header
A successful call will return a 302 redirect to a generated signed URL
(with 15 second expiry) to the resolved file path.
:param subject: username or organization
:param repo: repository name
:param remote_file_path: file name to be downloaded from Bintray
:param local_file_path: file name to be stored in local storage
:param bt_package: query parameter
"""

parameters = {"bt_package": bt_package} if bt_package else None
download_base_url = "https://dl.bintray.com"
url = "{}/{}/{}/{}".format(download_base_url, subject, repo, remote_file_path)
response, content = self._requester.download(url, params=parameters)

with open(local_file_path, 'wb') as local_fd:
local_fd.write(content)

self._logger.info("Download successfully: {}".format(url))
return response

def url_signing(self, subject, repo, file_path, json_data, encrypt=False):
""" Generates an anonymous, signed download URL with an expiry date.
Caller must be an owner of the repository or a publisher in the organization owning
the repository.
Encrypted download is possible - encryption will be done using AES 256 CBC, see below
documentation.
This resource is only available to Bintray Premium users.
:param subject: username or organization
:param repo: repository name
:param file_path: signed path
:param json_data: URL data
:param encrypt: encrypted download
"""

parameters = {"encrypt": str(encrypt).lower()}
url = "{}/signed_url/{}/{}/{}".format(Bintray.BINTRAY_URL, subject, repo, file_path)
response = self._requester.post(url, json=json_data, params=parameters)
return response

# Licenses

def get_org_proprietary_licenses(self, org):
Expand Down
33 changes: 33 additions & 0 deletions tests/test_content_downloading.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,36 @@ def test_bad_credentials_for_download_content():
error_message = str(error)
assert "Could not GET (401): 401 Client Error: Unauthorized for url: "\
"https://dl.bintray.com/uilianries/generic/packages.json" == error_message


def test_dynamic_download():
json_file = "packages.json"
bintray = Bintray()
response = bintray.dynamic_download("uilianries", "generic", json_file, json_file)
assert os.path.exists(json_file)
assert False == response["error"]


def test_bad_credentials_for_dynamic_download():
json_file = "packages.json"
bintray = Bintray("foobar", "85abc6aece02515e8bd87b9754a18af697527d88")
error_message = ""
try:
bintray.dynamic_download("uilianries", "generic", json_file, json_file)
except Exception as error:
error_message = str(error)
assert "Could not GET (401): 401 Client Error: Unauthorized for url: "\
"https://dl.bintray.com/uilianries/generic/packages.json" == error_message


def test_url_signing():
json_file = "packages.json"
bintray = Bintray()
error_message = ""
try:
bintray.url_signing("uilianries", "generic", json_file, {})
except Exception as error:
error_message = str(error)
assert "Could not POST (403): 403 Client Error: Forbidden for url: " \
"https://api.bintray.com/signed_url/uilianries/generic/packages.json" \
"?encrypt=false" == error_message

0 comments on commit f62019b

Please sign in to comment.