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

Commit f62019b

Browse files
authored
#1 Add dynamic download (#28)
#1 Add dynamic download
2 parents 0523298 + 8e5272c commit f62019b

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

bintray/bintray.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,63 @@ def download_content(self, subject, repo, remote_file_path, local_file_path):
211211
self._logger.info("Download successfully: {}".format(url))
212212
return response
213213

214+
def dynamic_download(self, subject, repo, remote_file_path, local_file_path, bt_package=None):
215+
""" Download a file based on a dynamic file_path .
216+
217+
This resource is only available for Bintray Premium repositories.
218+
219+
Currently, only the $latest token is supported, which is useful for downloading the
220+
latest file published under a specified package.
221+
222+
Package name can be supplied as a:
223+
The bt_package query parameter
224+
The bt_package matrix parameter
225+
The X-Bintray-Package request header
226+
227+
A successful call will return a 302 redirect to a generated signed URL
228+
(with 15 second expiry) to the resolved file path.
229+
230+
:param subject: username or organization
231+
:param repo: repository name
232+
:param remote_file_path: file name to be downloaded from Bintray
233+
:param local_file_path: file name to be stored in local storage
234+
:param bt_package: query parameter
235+
"""
236+
237+
parameters = {"bt_package": bt_package} if bt_package else None
238+
download_base_url = "https://dl.bintray.com"
239+
url = "{}/{}/{}/{}".format(download_base_url, subject, repo, remote_file_path)
240+
response, content = self._requester.download(url, params=parameters)
241+
242+
with open(local_file_path, 'wb') as local_fd:
243+
local_fd.write(content)
244+
245+
self._logger.info("Download successfully: {}".format(url))
246+
return response
247+
248+
def url_signing(self, subject, repo, file_path, json_data, encrypt=False):
249+
""" Generates an anonymous, signed download URL with an expiry date.
250+
251+
Caller must be an owner of the repository or a publisher in the organization owning
252+
the repository.
253+
254+
Encrypted download is possible - encryption will be done using AES 256 CBC, see below
255+
documentation.
256+
257+
This resource is only available to Bintray Premium users.
258+
259+
:param subject: username or organization
260+
:param repo: repository name
261+
:param file_path: signed path
262+
:param json_data: URL data
263+
:param encrypt: encrypted download
264+
"""
265+
266+
parameters = {"encrypt": str(encrypt).lower()}
267+
url = "{}/signed_url/{}/{}/{}".format(Bintray.BINTRAY_URL, subject, repo, file_path)
268+
response = self._requester.post(url, json=json_data, params=parameters)
269+
return response
270+
214271
# Licenses
215272

216273
def get_org_proprietary_licenses(self, org):

tests/test_content_downloading.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,36 @@ def test_bad_credentials_for_download_content():
2222
error_message = str(error)
2323
assert "Could not GET (401): 401 Client Error: Unauthorized for url: "\
2424
"https://dl.bintray.com/uilianries/generic/packages.json" == error_message
25+
26+
27+
def test_dynamic_download():
28+
json_file = "packages.json"
29+
bintray = Bintray()
30+
response = bintray.dynamic_download("uilianries", "generic", json_file, json_file)
31+
assert os.path.exists(json_file)
32+
assert False == response["error"]
33+
34+
35+
def test_bad_credentials_for_dynamic_download():
36+
json_file = "packages.json"
37+
bintray = Bintray("foobar", "85abc6aece02515e8bd87b9754a18af697527d88")
38+
error_message = ""
39+
try:
40+
bintray.dynamic_download("uilianries", "generic", json_file, json_file)
41+
except Exception as error:
42+
error_message = str(error)
43+
assert "Could not GET (401): 401 Client Error: Unauthorized for url: "\
44+
"https://dl.bintray.com/uilianries/generic/packages.json" == error_message
45+
46+
47+
def test_url_signing():
48+
json_file = "packages.json"
49+
bintray = Bintray()
50+
error_message = ""
51+
try:
52+
bintray.url_signing("uilianries", "generic", json_file, {})
53+
except Exception as error:
54+
error_message = str(error)
55+
assert "Could not POST (403): 403 Client Error: Forbidden for url: " \
56+
"https://api.bintray.com/signed_url/uilianries/generic/packages.json" \
57+
"?encrypt=false" == error_message

0 commit comments

Comments
 (0)