def server_version(self): """ Parse the Server header from a Nexus request response and return as version information. The method expects the header Server to be present and formatted as, e.g., 'Nexus/3.19.1-01 (OSS)' :return: the parsed version. If it can't be determined, return None. :rtype: Union[None,semver.VersionInfo] """ if self._server_version is None: response = self.http_get(self.config.url) if response.status_code != 200: raise exception.NexusClientAPIError(response.reason) server = response.headers.get('Server') if server is None: return None try: maybe_semver = server.split(' ')[0].split('/')[1].split('-')[0] version = semver.parse_version_info(maybe_semver) except (IndexError, ValueError): return None self._server_version = version return self._server_version
def upload_file_raw(repository, src_file, dst_dir, dst_file): """ Upload a single file to a raw repository. :param repository: repository instance used to access Nexus 3 service. :type repository: nexuscli.api.repository.model.Repository :param src_file: path to the local file to be uploaded. :param dst_dir: directory under dst_repo to place file in. When None, the file is placed under the root of the raw repository :param dst_file: destination file name. :raises exception.NexusClientInvalidRepositoryPath: invalid repository path. :raises exception.NexusClientAPIError: unknown response from Nexus API. """ dst_dir = os.path.normpath(dst_dir or REMOTE_PATH_SEPARATOR) params = {'repository': repository.name} files = {'raw.asset1': open(src_file, 'rb').read()} data = { 'raw.directory': dst_dir, 'raw.asset1.filename': dst_file, } response = repository.nexus_client.http_post('components', files=files, data=data, params=params, stream=True) if response.status_code != 204: raise exception.NexusClientAPIError( f'Uploading to {repository.name}. Reason: {response.reason} ' f'Status code: {response.status_code} Text: {response.text}')
def upload_file_maven2(repository, src_file, dst_dir, dst_file): """ Upload a single file to a maven2 repository. :param repository: repository instance used to access Nexus 3 service. :type repository: nexuscli.api.repository.model.Repository :param src_file: path to the local file to be uploaded. :param dst_dir: directory under dst_repo to place file in. :param dst_file: destination file name. :raises exception.NexusClientAPIError: unknown response from Nexus API. """ dst_dir = dst_dir or REMOTE_PATH_SEPARATOR repository_path = REMOTE_PATH_SEPARATOR.join( ['repository', repository.name, dst_dir, dst_file]) with open(src_file, 'rb') as fh: response = repository.nexus_client.http_put( repository_path, data=fh, stream=True, service_url=repository.nexus_client.config.url) if response.status_code != 201: raise exception.NexusClientAPIError( f'Uploading to {repository_path}. Reason: {response.reason} ' f'Status code: {response.status_code} Text: {response.text}')
def upload_file_raw(repository, src_file, dst_dir, dst_file): """ Upload a single file to a raw repository. :param repository: repository instance used to access Nexus 3 service. :type repository: nexuscli.api.repository.model.Repository :param src_file: path to the local file to be uploaded. :param dst_dir: directory under dst_repo to place file in. :param dst_file: destination file name. :raises exception.NexusClientInvalidRepositoryPath: invalid repository path. :raises exception.NexusClientAPIError: unknown response from Nexus API. """ if dst_dir is None or dst_dir.startswith(REMOTE_PATH_SEPARATOR): raise exception.NexusClientInvalidRepositoryPath( 'Destination path does not contain a directory, which is ' 'required by raw repositories') params = {'repository': repository.name} files = {'raw.asset1': open(src_file, 'rb').read()} data = { 'raw.directory': dst_dir, 'raw.asset1.filename': dst_file, } response = repository.nexus_client.http_post( 'components', files=files, data=data, params=params, stream=True) if response.status_code != 204: raise exception.NexusClientAPIError( f'Uploading to {repository.name}. Reason: {response.reason}')
def test_get_by_name_exception(cleanup_policy_collection, faker): """ It raises the documented exception when the policy name isn't found""" xname = faker.pystr() cleanup_policy_collection._client.scripts.run.side_effect = \ exception.NexusClientAPIError(xname) with pytest.raises(exception.NexusClientInvalidCleanupPolicy): cleanup_policy_collection.get_by_name(xname)
def get(self, name): resp = self.client._get('script/{}'.format(name)) if resp.status_code == 200: return resp.json() elif resp.status_code == 404: return None else: raise exception.NexusClientAPIError(resp.content)
def run(self, script_name, data=''): headers = {'content-type': 'text/plain'} endpoint = 'script/{}/run'.format(script_name) resp = self.client._post(endpoint, headers=headers, data=data) if resp.status_code != 200: raise exception.NexusClientAPIError(resp.content) return resp.json()
def refresh(self): """ Refresh local list of repositories with latest from service. A raw representation of repositories can be fetched using :meth:`raw_list`. """ response = self._client.http_get('repositories') if response.status_code != 200: raise exception.NexusClientAPIError(response.content) self._repositories_json = response.json()
def _get_paginated(self, endpoint, **request_kwargs): """ Performs a GET request using the given args and kwargs. If the response is paginated, the method will repeat the request, manipulating the `params` keyword argument each time in order to receive all pages of the response. Items in the responses are sent in "batches": when all elements of a response have been yielded, a new request is made and the process repeated. :param request_kwargs: passed verbatim to the _request() method, except for the argument needed to paginate requests. :return: a generator that yields on response item at a time. :rtype: typing.Iterator[dict] """ response = self.http_request('get', endpoint, **request_kwargs) if response.status_code == 404: raise exception.NexusClientAPIError(response.reason) try: content = response.json() except json.decoder.JSONDecodeError: raise exception.NexusClientAPIError(response.content) while True: for item in content.get('items'): yield item continuation_token = content.get('continuationToken') if continuation_token is None: break request_kwargs['params'].update( {'continuationToken': continuation_token}) response = self.http_request('get', endpoint, **request_kwargs) try: content = response.json() except json.decoder.JSONDecodeError: raise exception.NexusClientAPIError(response.content)
def delete(self, script_name): """ Deletes a script from the Nexus 3 repository. :param script_name: name of script to be deleted. :raises exception.NexusClientAPIError: if the Nexus service fails to delete the script; i.e.: any HTTP code other than 204. """ endpoint = 'script/{}'.format(script_name) resp = self.client._delete(endpoint) if resp.status_code != 204: raise exception.NexusClientAPIError(resp.reason)
def create(self, script_dict): """ Create the given script in the Nexus 3 service. :param script_dict: instance of script to be created. :type script_dict: dict :raises exception.NexusClientAPIError: if the script creation isn't successful; i.e.: any HTTP code other than 204. """ resp = self.client._post('script', json=script_dict) if resp.status_code != 204: raise exception.NexusClientAPIError(resp.content)
def list(self): """ List of all script names on the Nexus 3 service. :return: a list of names :rtype: list :raises exception.NexusClientAPIError: if the script names cannot be retrieved; i.e.: any HTTP code other than 200. """ resp = self.client._get('script') if resp.status_code != 200: raise exception.NexusClientAPIError(resp.content) return resp.json()
def exists(self, name): """ Check if a script exists. :param name: of script to verify existence. :return: True if it exists, false otherwise :rtype: bool :raises exception.NexusClientAPIError: if the response from the Nexus service isn't recognised; i.e.: any HTTP code other than 200, 404. """ resp = self._client.http_head(f'script/{name}') if resp.status_code == 200: return True elif resp.status_code == 404: return False else: raise exception.NexusClientAPIError(resp.content)
def get(self, name): """ Get a Nexus 3 script by name. :param name: of script to be retrieved. :return: the script or None, if not found :rtype: dict, None :raises exception.NexusClientAPIError: if the response from the Nexus service isn't recognised; i.e.: any HTTP code other than 200, 404. """ resp = self.client._get('script/{}'.format(name)) if resp.status_code == 200: return resp.json() elif resp.status_code == 404: return None else: raise exception.NexusClientAPIError(resp.content)
def run(self, script_name, data=''): """ Runs an existing script on the Nexus 3 service. :param script_name: name of script to be run. :param data: parameters to be passed to the script, via HTTP POST. If the script being run requires a certain format or encoding, you need to prepare it yourself. :return: the content returned by the script, if any. :rtype: str, dict :raises exception.NexusClientAPIError: if the Nexus service fails to run the script; i.e.: any HTTP code other than 200. """ headers = {'content-type': 'text/plain'} endpoint = 'script/{}/run'.format(script_name) resp = self.client._post(endpoint, headers=headers, data=data) if resp.status_code != 200: raise exception.NexusClientAPIError(resp.content) return resp.json()
def create(self, script_name, script_content, script_type='groovy'): """ Create the given script in the Nexus 3 service. :param script_name: name of script to be created. :type script_name: str :param script_content: script code. :type script_content: str :param script_type: type of script to be created. :type script_type: str :raises exception.NexusClientAPIError: if the script creation isn't successful; i.e.: any HTTP code other than 204. """ script = { 'type': script_type, 'name': script_name, 'content': script_content, } resp = self._client.http_post('script', json=script) if resp.status_code != 204: raise exception.NexusClientAPIError(resp.content)
def list(self): resp = self.client._get('script') if resp.status_code != 200: raise exception.NexusClientAPIError(resp.content) return resp.json()
def create(self, script_dict): resp = self.client._post('script', json=script_dict) if resp.status_code != 204: raise exception.NexusClientAPIError(resp.content)
def delete(self, script_name): endpoint = 'script/{}'.format(script_name) resp = self.client._delete(endpoint) if resp.status_code != 204: raise exception.NexusClientAPIError(resp.reason)