def test_none_on_404(mocker): response = MagicMock(status_code=404) mocker.patch('time.sleep') mocker.patch('requests.get', return_value=response) host = 'host' endpoint = 'endpoint' with pytest.raises(HTTPErrorCode): request_factory(host, 'GET', endpoint, return_none_on_404=False) assert request_factory(host, 'GET', endpoint, return_none_on_404=True) is None
def delete_package(self, package, version, retries=30, sleep=3): """Delete package ona specific version Args: package (str): the package name version (str): the package version to delete """ target = dict() for pkg in self.get_versions(package): if pkg["version"] == version: target = pkg try: endpoint = "api/package/{0}/{1}".format(package, target["filename"]) except KeyError: return None for _ in xrange(retries): resp = request_factory(self.host, "DELETE", endpoint, auth=self.auth) if not any([_ for pkg in self.get_versions(package) if pkg["version"] == version]): return resp time.sleep(sleep) raise PypicloudError("The delete package failed, check your cache backend or pypicloud logs")
def get_versions(self, package, sort_by=None, desc=True, filters=None, strict=True, exclude=False, limit=None): """Get the wheel versions of an package, ordered by date from most recent to least recent. Args: package: Package whose versions to get (eg: foobar-package) sort_by: Sort the output by a field present in the output(eg: last_modified) desc: Ordering the output list filters: Provide a key-value pair to filter the versions (eg: {"version": "75"}). It is important that both, the key and value should be a part of the version. The value is matched using re, so a regular expression like {"version": "^beta-."} could also be passed. strict: Strict matching, True by default. Setting this as True will match exactly the contents of the filter and return the result exclude: If True, excludes the matches of the filters limit: Return a certain number of results Returns: list of dict: List of package info. """ endpoint = "/api/package/{package}/".format(package=package) versions = request_factory(self.host, "GET", endpoint, auth=self.auth) pkg_versions = versions["packages"] if filters: if exclude: pkg_versions = [pkg for pkg in pkg_versions if not all(re.search(search_value, pkg[search_field]) for search_field, search_value in filters.items())] elif strict: pkg_versions = [pkg for pkg in pkg_versions if set(filters.items()).issubset(set(pkg.items()))] else: pkg_versions = [pkg for pkg in pkg_versions if all(re.search(search_value, pkg[search_field]) for search_field, search_value in filters.items())] sort_key = sort_by if sort_by else "last_modified" return sorted(pkg_versions, key=lambda pkg: pkg[sort_key], reverse=desc)[:limit]
def test_unknown_http_word(mocker): mocker.patch('time.sleep') host = 'host' endpoint = 'endpoint' response = {'request_factory': 'Unknown request type: AWESOME_HTTP_WORD'} with pytest.raises(HTTPErrorCode): assert request_factory(host, 'AWESOME_HTTP_WORD', endpoint, return_none_on_404=False) == response
def test_request_factory(mocker, status_code): response = MagicMock(status_code=status_code) mocker.patch('requests.get', return_value=response) mocker.patch('requests.post', return_value=response) mocker.patch('requests.put', return_value=response) mocker.patch('requests.delete', return_value=response) host = 'host' endpoint = 'endpoint' request_factory(host, 'GET', endpoint) request_factory(host, 'POST', endpoint) request_factory(host, 'PUT', endpoint) request_factory(host, 'DELETE', endpoint)