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

Commit a134253

Browse files
authored
#11 Request package version (#27)
#11 Request package version
2 parents f7bc10a + bae2d5a commit a134253

File tree

5 files changed

+213
-18
lines changed

5 files changed

+213
-18
lines changed

.ci/run.sh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,4 @@ if [[ "$(uname -s)" == 'Darwin' ]]; then
1111
fi
1212

1313
python setup.py sdist
14-
pushd tests
15-
pytest -v -s --cov=bintray
16-
mv .coverage ..
17-
popd
14+
pytest -v -s --cov=bintray tests

bintray/bintray.py

Lines changed: 112 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,119 @@ def get_package_files(self, subject, repo, package, include_unpublished=False):
4848
:return: List with all files
4949
"""
5050
parameters = {"include_unpublished": bool_to_number(include_unpublished)}
51-
url = "{}/packages/{}/{}/{}/files?include_unpublished={}".format(Bintray.BINTRAY_URL,
52-
subject,
53-
repo,
54-
package,
55-
include_unpublished)
51+
url = "{}/packages/{}/{}/{}/files".format(Bintray.BINTRAY_URL,
52+
subject,
53+
repo,
54+
package)
5655
return self._requester.get(url, parameters)
5756

57+
def get_version_files(self, subject, repo, package, version, include_unpublished=False):
58+
""" Get all files in a given version.
59+
60+
Returns an array of results, where elements are similar to the result of getting
61+
package files.
62+
63+
When called by a user with publishing rights on the package, includes unpublished
64+
files in the list.
65+
66+
By default only published files are shown.
67+
68+
Security: Authenticated user with 'read' permission for private repositories,
69+
or version read entitlement.
70+
71+
:param subject: username or organization
72+
:param repo: repository name
73+
:param package: package name
74+
:param version: package version
75+
:param include_unpublished: Show not published files
76+
:return: List with all files
77+
"""
78+
parameters = {"include_unpublished": bool_to_number(include_unpublished)}
79+
url = "{}/packages/{}/{}/{}/versions/{}/files".format(Bintray.BINTRAY_URL,
80+
subject,
81+
repo,
82+
package,
83+
version)
84+
return self._requester.get(url, parameters)
85+
86+
def file_search_by_name(self, name, subject=None, repo=None, start_pos=None,
87+
created_after=None):
88+
""" Search for a file by its name. name can take the * and ? wildcard characters.
89+
90+
May take an optional subject and/or repo name to search in and/or created_after
91+
search from the 'dateCreatedAfter' until today.
92+
The 'dateCreatedAfter' is defined in the following ISO8601 format (yyyy-MM-
93+
dd’T’HH:mm:ss.SSSZ). Returns an array of results, where elements are similar
94+
to the result of getting package files. Search results will not contain private files.
95+
96+
Security: Authentication is not required
97+
98+
:param name: File to be searched
99+
:param subject: File subject to filter
100+
:param repo: File repo filter
101+
:param start_pos: Start position name to filter
102+
:param created_after: Creation date to filter
103+
:return: Package found. Otherwise, error.
104+
"""
105+
parameters = {"name": name}
106+
if subject:
107+
parameters["subject"] = str(subject)
108+
if repo:
109+
parameters["repo"] = str(repo)
110+
if start_pos:
111+
parameters["start_pos"] = str(start_pos)
112+
if created_after:
113+
parameters["created_after"] = str(created_after)
114+
url = "{}/search/file".format(Bintray.BINTRAY_URL)
115+
return self._requester.get(url, parameters)
116+
117+
def file_search_by_checksum(self, sha1, subject=None, repo=None, start_pos=None,
118+
created_after=None):
119+
""" Search for a file by its sha1 checksum.
120+
121+
May take an optional subject and/or repo name to search in.
122+
123+
Returns an array of results, where elements are similar to the result of getting
124+
package files. Search results will not contain private files.
125+
126+
Security: Authentication is not required
127+
128+
:param sha1: File SHA-1
129+
:param subject: File subject to filter
130+
:param repo: File repo filter
131+
:param start_pos: Start position name to filter
132+
:param created_after: Creation date to filter
133+
:return: Package found. Otherwise, error.
134+
"""
135+
parameters = {"sha1": sha1}
136+
if subject:
137+
parameters["subject"] = str(subject)
138+
if repo:
139+
parameters["repo"] = str(repo)
140+
if start_pos:
141+
parameters["start_pos"] = str(start_pos)
142+
if created_after:
143+
parameters["created_after"] = str(created_after)
144+
url = "{}/search/file".format(Bintray.BINTRAY_URL)
145+
return self._requester.get(url, parameters)
146+
147+
def file_in_download_list(self, subject, repo, file_path, add_or_remove):
148+
""" Add or remove a file from/to the 'Download List'.
149+
150+
Security: Authenticated user with 'publish' permission,
151+
or version read/write entitlement.
152+
153+
:param subject: File subject to filter
154+
:param repo: File repo filter
155+
:param file_path: File path to be added or removed
156+
:param add_or_remove: True to add in Download list. False to remove.
157+
:return: Request response.
158+
"""
159+
action = 'true' if add_or_remove else 'false'
160+
json_data = {'list_in_downloads': action}
161+
url = "{}/file_metadata/{}/{}/{}".format(Bintray.BINTRAY_URL, subject, repo, file_path)
162+
return self._requester.put(url, json=json_data)
163+
58164
# Content Uploading & Publishing
59165

60166
def upload_content(self, subject, repo, package, version, remote_file_path, local_file_path,
@@ -188,7 +294,7 @@ def delete_user_proprietary_license(self, user, custom_license_name):
188294
:return: request answer
189295
"""
190296
url = "{}/users/{}/licenses/{}".format(Bintray.BINTRAY_URL, user, custom_license_name)
191-
return self._requester.patch(url)
297+
return self._requester.delete(url)
192298

193299
def get_oss_licenses(self):
194300
""" Returns a list of all the OSS licenses.

bintray/requester.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,21 @@ def download(self, url, params=None):
6666
self._raise_error("Could not GET", response)
6767
return self._add_status_code(response), response.content
6868

69-
def put(self, url, params=None, data=None):
69+
def put(self, url, params=None, data=None, json=None):
7070
""" Forward PUT method
7171
7272
:param url: URL address
7373
:param params: URL params
7474
:param data: Data content
75+
:param json: JSON content
7576
:return: JSON
7677
"""
77-
response = requests.put(url, auth=self._get_authentication(), params=params, data=data)
78+
if data and json:
79+
raise Exception("Only accept 'data' or 'json'")
80+
if data:
81+
response = requests.put(url, auth=self._get_authentication(), params=params, data=data)
82+
else:
83+
response = requests.put(url, auth=self._get_authentication(), params=params, json=json)
7884
if not response.ok:
7985
self._raise_error("Could not PUT", response)
8086
return self._add_status_code(response)

tests/test_files.py

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,89 @@ def test_bad_credentials_for_get_package_files():
2828
error_message = str(error)
2929
assert "Could not GET (401): 401 Client Error: Unauthorized for url: " \
3030
"https://api.bintray.com/packages/uilianries/generic/statistics/files?" \
31-
"include_unpublished=False&include_unpublished=0" == error_message
31+
"include_unpublished=0" == error_message
32+
33+
34+
def test_get_version_files():
35+
bintray = Bintray()
36+
response = bintray.get_version_files("uilianries", "generic", "statistics", "20190701")
37+
assert {'error': False, 'statusCode': 200} in response
38+
assert {'created': '2019-07-01T20:51:42.879Z',
39+
'name': 'packages.json',
40+
'owner': 'uilianries',
41+
'package': 'statistics',
42+
'path': 'packages.json',
43+
'repo': 'generic',
44+
'sha1': '85abc6aece02515e8bd87b9754a18af697527d88',
45+
'sha256': '9537027db06c520b6eeb3b8317cef5c994ab93e5ad4b17fac3567fba7089b165',
46+
'size': 1967,
47+
'version': '20190701'} in response
48+
49+
50+
def test_bad_credentials_for_get_version_files():
51+
bintray = Bintray("foobar", "85abc6aece02515e8bd87b9754a18af697527d88")
52+
error_message = ""
53+
try:
54+
bintray.get_version_files("uilianries", "generic", "statistics", "20190701")
55+
except Exception as error:
56+
error_message = str(error)
57+
assert "Could not GET (401): 401 Client Error: Unauthorized for url: " \
58+
"https://api.bintray.com/packages/uilianries/generic/statistics/versions" \
59+
"/20190701/files?include_unpublished=0" == error_message
60+
61+
62+
def test_file_search_by_name():
63+
bintray = Bintray()
64+
response = bintray.file_search_by_name("packages.json", subject="uilianries", repo="generic")
65+
assert {'error': False, 'statusCode': 200} in response
66+
assert {'created': '2019-07-01T20:51:42.879Z',
67+
'name': 'packages.json',
68+
'owner': 'uilianries',
69+
'package': 'statistics',
70+
'path': 'packages.json',
71+
'repo': 'generic',
72+
'sha1': '85abc6aece02515e8bd87b9754a18af697527d88',
73+
'sha256': '9537027db06c520b6eeb3b8317cef5c994ab93e5ad4b17fac3567fba7089b165',
74+
'size': 1967,
75+
'version': '20190701'} in response
76+
77+
78+
def test_bad_credentials_file_search_by_name():
79+
bintray = Bintray("foobar", "85abc6aece02515e8bd87b9754a18af697527d88")
80+
error_message = ""
81+
try:
82+
bintray.file_search_by_name("packages.json", subject="uilianries", repo="generic")
83+
except Exception as error:
84+
error_message = str(error)
85+
assert "Could not GET (401): 401 Client Error: Unauthorized for url: " \
86+
"https://api.bintray.com/search/file" \
87+
"?name=packages.json&subject=uilianries&repo=generic" == error_message
88+
89+
90+
def test_file_search_by_checksum():
91+
bintray = Bintray()
92+
response = bintray.file_search_by_checksum("85abc6aece02515e8bd87b9754a18af697527d88",
93+
subject="uilianries", repo="generic",
94+
created_after="2019-07-01")
95+
assert {'error': False, 'statusCode': 200} in response
96+
97+
98+
def test_bad_credentials_file_search_by_checksum():
99+
bintray = Bintray("foobar", "85abc6aece02515e8bd87b9754a18af697527d88")
100+
error_message = ""
101+
try:
102+
bintray.file_search_by_checksum(
103+
"85abc6aece02515e8bd87b9754a18af697527d88",
104+
subject="uilianries", repo="generic")
105+
except Exception as error:
106+
error_message = str(error)
107+
assert "Could not GET (401): 401 Client Error: Unauthorized for url: " \
108+
"https://api.bintray.com/search/file" \
109+
"?sha1=85abc6aece02515e8bd87b9754a18af697527d88" \
110+
"&subject=uilianries&repo=generic" == error_message
111+
112+
113+
def test_file_in_download_list():
114+
bintray = Bintray()
115+
response = bintray.file_in_download_list("uilianries", "generic", "packages.json", True)
116+
assert {'error': False, 'message': 'success', 'statusCode': 200} == response

tests/test_licenses.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def test_update_org_proprietary_licenses():
7575
error_message = str(error)
7676

7777
assert "Could not PATCH (405): 405 Client Error: Method Not Allowed for url: " \
78-
"https://api.bintray.com/orgs/jfrog/licenses/foobar"
78+
"https://api.bintray.com/orgs/jfrog/licenses/foobar" == error_message
7979

8080

8181
def test_update_user_proprietary_licenses():
@@ -88,7 +88,7 @@ def test_update_user_proprietary_licenses():
8888
error_message = str(error)
8989

9090
assert "Could not PATCH (405): 405 Client Error: Method Not Allowed for url: " \
91-
"https://api.bintray.com/users/uilianries/licenses/foobar"
91+
"https://api.bintray.com/users/uilianries/licenses/foobar" == error_message
9292

9393

9494
def test_delete_org_proprietary_licenses():
@@ -99,7 +99,8 @@ def test_delete_org_proprietary_licenses():
9999
except Exception as error:
100100
error_message = str(error)
101101
assert "Could not DELETE (403): 403 Client Error: Forbidden for url: " \
102-
"https://api.bintray.com/orgs/jfrog/licenses/foobar"
102+
"https://api.bintray.com/orgs/jfrog/licenses/foobar" == error_message
103+
103104

104105
def test_delete_user_proprietary_licenses():
105106
bintray = Bintray()
@@ -108,5 +109,5 @@ def test_delete_user_proprietary_licenses():
108109
bintray.delete_user_proprietary_license(user="uilianries", custom_license_name="foobar")
109110
except Exception as error:
110111
error_message = str(error)
111-
assert "Could not DELETE (403): 403 Client Error: Forbidden for url: " \
112-
"https://api.bintray.com/users/uilianries/licenses/foobar"
112+
assert "Could not DELETE (400): 400 Client Error: Bad Request for url: " \
113+
"https://api.bintray.com/users/uilianries/licenses/foobar" == error_message

0 commit comments

Comments
 (0)