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

Commit

Permalink
#11 Request package version (#27)
Browse files Browse the repository at this point in the history
#11 Request package version
  • Loading branch information
uilianries committed Jul 10, 2019
2 parents f7bc10a + bae2d5a commit a134253
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 18 deletions.
5 changes: 1 addition & 4 deletions .ci/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,4 @@ if [[ "$(uname -s)" == 'Darwin' ]]; then
fi

python setup.py sdist
pushd tests
pytest -v -s --cov=bintray
mv .coverage ..
popd
pytest -v -s --cov=bintray tests
118 changes: 112 additions & 6 deletions bintray/bintray.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,119 @@ def get_package_files(self, subject, repo, package, include_unpublished=False):
:return: List with all files
"""
parameters = {"include_unpublished": bool_to_number(include_unpublished)}
url = "{}/packages/{}/{}/{}/files?include_unpublished={}".format(Bintray.BINTRAY_URL,
subject,
repo,
package,
include_unpublished)
url = "{}/packages/{}/{}/{}/files".format(Bintray.BINTRAY_URL,
subject,
repo,
package)
return self._requester.get(url, parameters)

def get_version_files(self, subject, repo, package, version, include_unpublished=False):
""" Get all files in a given version.
Returns an array of results, where elements are similar to the result of getting
package files.
When called by a user with publishing rights on the package, includes unpublished
files in the list.
By default only published files are shown.
Security: Authenticated user with 'read' permission for private repositories,
or version read entitlement.
:param subject: username or organization
:param repo: repository name
:param package: package name
:param version: package version
:param include_unpublished: Show not published files
:return: List with all files
"""
parameters = {"include_unpublished": bool_to_number(include_unpublished)}
url = "{}/packages/{}/{}/{}/versions/{}/files".format(Bintray.BINTRAY_URL,
subject,
repo,
package,
version)
return self._requester.get(url, parameters)

def file_search_by_name(self, name, subject=None, repo=None, start_pos=None,
created_after=None):
""" Search for a file by its name. name can take the * and ? wildcard characters.
May take an optional subject and/or repo name to search in and/or created_after
search from the 'dateCreatedAfter' until today.
The 'dateCreatedAfter' is defined in the following ISO8601 format (yyyy-MM-
dd’T’HH:mm:ss.SSSZ). Returns an array of results, where elements are similar
to the result of getting package files. Search results will not contain private files.
Security: Authentication is not required
:param name: File to be searched
:param subject: File subject to filter
:param repo: File repo filter
:param start_pos: Start position name to filter
:param created_after: Creation date to filter
:return: Package found. Otherwise, error.
"""
parameters = {"name": name}
if subject:
parameters["subject"] = str(subject)
if repo:
parameters["repo"] = str(repo)
if start_pos:
parameters["start_pos"] = str(start_pos)
if created_after:
parameters["created_after"] = str(created_after)
url = "{}/search/file".format(Bintray.BINTRAY_URL)
return self._requester.get(url, parameters)

def file_search_by_checksum(self, sha1, subject=None, repo=None, start_pos=None,
created_after=None):
""" Search for a file by its sha1 checksum.
May take an optional subject and/or repo name to search in.
Returns an array of results, where elements are similar to the result of getting
package files. Search results will not contain private files.
Security: Authentication is not required
:param sha1: File SHA-1
:param subject: File subject to filter
:param repo: File repo filter
:param start_pos: Start position name to filter
:param created_after: Creation date to filter
:return: Package found. Otherwise, error.
"""
parameters = {"sha1": sha1}
if subject:
parameters["subject"] = str(subject)
if repo:
parameters["repo"] = str(repo)
if start_pos:
parameters["start_pos"] = str(start_pos)
if created_after:
parameters["created_after"] = str(created_after)
url = "{}/search/file".format(Bintray.BINTRAY_URL)
return self._requester.get(url, parameters)

def file_in_download_list(self, subject, repo, file_path, add_or_remove):
""" Add or remove a file from/to the 'Download List'.
Security: Authenticated user with 'publish' permission,
or version read/write entitlement.
:param subject: File subject to filter
:param repo: File repo filter
:param file_path: File path to be added or removed
:param add_or_remove: True to add in Download list. False to remove.
:return: Request response.
"""
action = 'true' if add_or_remove else 'false'
json_data = {'list_in_downloads': action}
url = "{}/file_metadata/{}/{}/{}".format(Bintray.BINTRAY_URL, subject, repo, file_path)
return self._requester.put(url, json=json_data)

# Content Uploading & Publishing

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

def get_oss_licenses(self):
""" Returns a list of all the OSS licenses.
Expand Down
10 changes: 8 additions & 2 deletions bintray/requester.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,21 @@ def download(self, url, params=None):
self._raise_error("Could not GET", response)
return self._add_status_code(response), response.content

def put(self, url, params=None, data=None):
def put(self, url, params=None, data=None, json=None):
""" Forward PUT method
:param url: URL address
:param params: URL params
:param data: Data content
:param json: JSON content
:return: JSON
"""
response = requests.put(url, auth=self._get_authentication(), params=params, data=data)
if data and json:
raise Exception("Only accept 'data' or 'json'")
if data:
response = requests.put(url, auth=self._get_authentication(), params=params, data=data)
else:
response = requests.put(url, auth=self._get_authentication(), params=params, json=json)
if not response.ok:
self._raise_error("Could not PUT", response)
return self._add_status_code(response)
Expand Down
87 changes: 86 additions & 1 deletion tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,89 @@ def test_bad_credentials_for_get_package_files():
error_message = str(error)
assert "Could not GET (401): 401 Client Error: Unauthorized for url: " \
"https://api.bintray.com/packages/uilianries/generic/statistics/files?" \
"include_unpublished=False&include_unpublished=0" == error_message
"include_unpublished=0" == error_message


def test_get_version_files():
bintray = Bintray()
response = bintray.get_version_files("uilianries", "generic", "statistics", "20190701")
assert {'error': False, 'statusCode': 200} in response
assert {'created': '2019-07-01T20:51:42.879Z',
'name': 'packages.json',
'owner': 'uilianries',
'package': 'statistics',
'path': 'packages.json',
'repo': 'generic',
'sha1': '85abc6aece02515e8bd87b9754a18af697527d88',
'sha256': '9537027db06c520b6eeb3b8317cef5c994ab93e5ad4b17fac3567fba7089b165',
'size': 1967,
'version': '20190701'} in response


def test_bad_credentials_for_get_version_files():
bintray = Bintray("foobar", "85abc6aece02515e8bd87b9754a18af697527d88")
error_message = ""
try:
bintray.get_version_files("uilianries", "generic", "statistics", "20190701")
except Exception as error:
error_message = str(error)
assert "Could not GET (401): 401 Client Error: Unauthorized for url: " \
"https://api.bintray.com/packages/uilianries/generic/statistics/versions" \
"/20190701/files?include_unpublished=0" == error_message


def test_file_search_by_name():
bintray = Bintray()
response = bintray.file_search_by_name("packages.json", subject="uilianries", repo="generic")
assert {'error': False, 'statusCode': 200} in response
assert {'created': '2019-07-01T20:51:42.879Z',
'name': 'packages.json',
'owner': 'uilianries',
'package': 'statistics',
'path': 'packages.json',
'repo': 'generic',
'sha1': '85abc6aece02515e8bd87b9754a18af697527d88',
'sha256': '9537027db06c520b6eeb3b8317cef5c994ab93e5ad4b17fac3567fba7089b165',
'size': 1967,
'version': '20190701'} in response


def test_bad_credentials_file_search_by_name():
bintray = Bintray("foobar", "85abc6aece02515e8bd87b9754a18af697527d88")
error_message = ""
try:
bintray.file_search_by_name("packages.json", subject="uilianries", repo="generic")
except Exception as error:
error_message = str(error)
assert "Could not GET (401): 401 Client Error: Unauthorized for url: " \
"https://api.bintray.com/search/file" \
"?name=packages.json&subject=uilianries&repo=generic" == error_message


def test_file_search_by_checksum():
bintray = Bintray()
response = bintray.file_search_by_checksum("85abc6aece02515e8bd87b9754a18af697527d88",
subject="uilianries", repo="generic",
created_after="2019-07-01")
assert {'error': False, 'statusCode': 200} in response


def test_bad_credentials_file_search_by_checksum():
bintray = Bintray("foobar", "85abc6aece02515e8bd87b9754a18af697527d88")
error_message = ""
try:
bintray.file_search_by_checksum(
"85abc6aece02515e8bd87b9754a18af697527d88",
subject="uilianries", repo="generic")
except Exception as error:
error_message = str(error)
assert "Could not GET (401): 401 Client Error: Unauthorized for url: " \
"https://api.bintray.com/search/file" \
"?sha1=85abc6aece02515e8bd87b9754a18af697527d88" \
"&subject=uilianries&repo=generic" == error_message


def test_file_in_download_list():
bintray = Bintray()
response = bintray.file_in_download_list("uilianries", "generic", "packages.json", True)
assert {'error': False, 'message': 'success', 'statusCode': 200} == response
11 changes: 6 additions & 5 deletions tests/test_licenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_update_org_proprietary_licenses():
error_message = str(error)

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


def test_update_user_proprietary_licenses():
Expand All @@ -88,7 +88,7 @@ def test_update_user_proprietary_licenses():
error_message = str(error)

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


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


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

0 comments on commit a134253

Please sign in to comment.