def get_file(self, file_id=None): request_url = self.request_url + "/" + file_id results = {} try: results = requests.get(request_url, headers=self.auth_header) if results.status_code == 400: if json.loads(results.text)['error_code'] == 'NotUUID': raise NotFound('Wrong ID.') if results.status_code == 404: results = results.json() results.update({'status': 0}) return results if results.status_code == 403: raise AuthenticationFailed('Authentication Failed. ' 'Wrong API Key.') results.raise_for_status() if requests.codes.ok: results = results.json()['items'].pop() results.update({'status': 1}) return results except AuthenticationFailed as err: self.logger.error('AuthenticationFailed') utils.log_traceback(err) except NotFound as err: self.logger.error('Not Found') utils.log_traceback(err) except requests.exceptions.RequestException as err: self.logger.error('Error occured during request') self.logger.error('Response Status Code: %s', results.status_code) utils.log_traceback(err)
def __get_analysis_by_run_id(self, run_id): request_url = self.request_url.format(run_id=run_id) try: single_run_meta = self.runs.get_single_run(run_id) if not single_run_meta: raise CosmosidException('Response from service is empty for ' 'run id %s' % run_id) if not single_run_meta['status']: raise NotFoundException(single_run_meta['message']) results = requests.get(request_url, headers=self.header) if results.status_code == 403: raise AuthenticationFailed('Authentication Failed. ' 'Wrong API Key.') if results.status_code == 404: result_data = results.json() result_data.update({'status': 0}) result_data.update({'run_meta': single_run_meta}) return result_data if requests.codes.ok: result_data = results.json() result_data.update({'status': 1}) result_data.update({'run_meta': single_run_meta}) return result_data results.raise_for_status() except AuthenticationFailed: self.logger.error('Authentication Failed') except NotFoundException: self.logger.error('Not Found') except CosmosidException: self.logger.error('Got Analysis data exception.') except requests.exceptions.RequestException: self.logger.debug('Debug', exc_info=True) self.logger.error('Error occured during request') self.logger.error('Response Status Code: %s', results.status_code)
def get_list(self, parent_id=None): data = {'folder_id': parent_id} if parent_id else {} results = {} try: results = requests.get(self.request_url, headers=self.auth_header, params=data) if results.status_code == 400: if json.loads(results.text)['error_code'] == 'NotUUID': raise NotFound('Wrong ID.') if results.status_code == 404: results = results.json() results.update({'status': 0}) return results if results.status_code == 403: raise AuthenticationFailed('Authentication Failed. ' 'Wrong API Key.') results.raise_for_status() if requests.codes.ok: results = results.json() results.update({'status': 1}) #microbiom standart doesnt have an export TODO:export var results['items'] = [ i for i in results['items'] if i['content_type'] != 7 ] return results except AuthenticationFailed as err: utils.log_traceback(err) except NotFound as err: utils.log_traceback(err) except requests.exceptions.RequestException as err: self.logger.error('Error occured during request') utils.log_traceback(err)
def get_single_run(self, run_id): run_metadata_url = "{}{}".format( self.base_url, self.__single_run_path.format(run_id=run_id)) results = {} try: results = requests.get(run_metadata_url, headers=self.auth_header) if results.status_code == 400: if json.loads(results.text)['error_code'] == 'NotUUID': raise NotFound('Wrong ID.') if results.status_code == 404: results = results.json() results.update({'status': 0}) return results if results.status_code == 403: raise AuthenticationFailed('Authentication Failed. ' 'Wrong API Key.') results.raise_for_status() if requests.codes.ok: results = results.json() results.update({'status': 1}) return results except AuthenticationFailed as err: utils.log_traceback(err) except NotFound as err: utils.log_traceback(err) except requests.exceptions.RequestException as err: self.logger.error('Error occured during request') self.logger.error('Response Status Code: %s', results.status_code) utils.log_traceback(err)
def get_report_url(self): """Return URL for download.""" params = dict() try: request_url = "{}{}".format(self.base_url, self.__class__._resource_path) if not self.file_id: raise ValidationError('File ID is required') if self.run_id: if not self.__is_runid_in_file(): raise NotFound('Given Run id {} is not in ' 'given File {}'.format( self.run_id, self.file_id)) params.update(sample_run_id=self.run_id) request_url = request_url.format(file_id=self.file_id) results = requests.get(request_url, params=params, headers=self.header) if results.status_code == 403: raise AuthenticationFailed('Authentication Failed. ' 'Wrong API Key') if results.status_code == 406: raise StatusException('File has a NON SUCCESS status') if results.status_code == 404: raise NotFound('File with given ID does ' 'not exist: {}'.format(self.file_id)) if requests.codes.ok: resp = results.json() resp.update(status=1) return resp results.raise_for_status() except AuthenticationFailed: self.logger.error('Authentication Failed') return {'url': None} except StatusException: self.logger.error('StatusException') return {'url': 1} except NotFound: self.logger.error('Not Found') return {'url': None} except ValidationError: self.logger.error('Validation error') return except requests.exceptions.RequestException: self.logger.error('Error occured during request') self.logger.error('Response Status Code: %s', results.status_code) self.logger.debug('Debug', exc_info=True)
def get_runs_list(self, file_id=None): sample_runs_url = "{}{}".format( self.base_url, self.__resource_path.format(file_id=file_id)) results = {} try: file_metadata = self.get_file(file_id) if not file_metadata: raise CosmosidException('Response from service is empty ' 'for file id {}'.format(file_id)) if not file_metadata['status']: raise NotFoundException(file_metadata['message']) results = requests.get(sample_runs_url, headers=self.auth_header) if results.status_code == 400: if json.loads(results.text)['error_code'] == 'NotUUID': raise NotFound('Wrong ID.') if results.status_code == 404: results = results.json() results.update({'status': 0}) results.update({'file_name': file_metadata['name']}) return results if results.status_code == 403: raise AuthenticationFailed('Authentication Failed. ' 'Wrong API Key.') results.raise_for_status() if requests.codes.ok: results = results.json() results.update({'status': 1}) results.update({'file_name': file_metadata['name']}) return results except AuthenticationFailed as err: utils.log_traceback(err) except NotFound as err: utils.log_traceback(err) except requests.exceptions.RequestException as err: self.logger.error('Error occured during request') self.logger.error('Response Status Code: %s', results.status_code) utils.log_traceback(err) except NotFoundException as err: utils.log_traceback(err) except CosmosidException as err: self.logger.error('Got runs list exception') utils.log_traceback(err) except Exception as err: self.logger.error('Client exception occured') utils.log_traceback(err)
def upload_file(**kwargs): """Upload manager.""" filename = kwargs.pop('file') parent_id = kwargs.pop('parent_id', None) multipart_chunksize = file_size = os.stat(filename)[ 6] #get size of file in bytes client = kwargs['client'] if file_size > MULTIPART_THRESHOLD: #bigger that 1GB multipart_chunksize = min(int(file_size / 10), int(MAX_CHUNK_SIZE)) multipart_chunksize = max(multipart_chunksize, int(MIN_CHUNK_SIZE)) LOGGER.info('File size: %s MB', file_size / MB) LOGGER.info('Chunk size: %s MB', int(multipart_chunksize / MB)) config = TransferConfig(multipart_threshold=MULTIPART_THRESHOLD, max_concurrency=MAX_CONCURRENCY, multipart_chunksize=multipart_chunksize) osutil = OSUtilsWithCallbacks() # Check if given parent folder exists if parent_id: fl_obj = Files(base_url=kwargs['base_url'], api_key=kwargs['api_key']) res = fl_obj.get_list(parent_id=parent_id) if not res['status']: raise NotFoundException('Parent folder for upload does ' 'not exists.') transfer_manager = TransferManager(client, config=config, osutil=osutil) subscribers = [ ProgressSubscriber(filename), ] _, file_name = os.path.split(filename) try: init_url = client.base_url + urls.UPLOAD_INIT_URL response = requests_retry_session().put(init_url, json=dict(file_name=file_name), headers=client.header) if response.status_code == 402: raise NotEnoughCredits('Insufficient credits for upload.') if response.status_code == 403: raise AuthenticationFailed('Authentication Failed. Wrong API Key.') if response.status_code == requests.codes.ok: sources = response.json() future = transfer_manager.upload(filename, sources['upload_source'], sources['upload_key'], extra_args=None, subscribers=subscribers) else: LOGGER.error( 'File upload inititalisation Failed. ' 'Response code: %s', response.status_code) raise UploadException('File upload inititalisation Failed. ' 'Response code: %s' % response.status_code) try: future.result() except KeyboardInterrupt: do_not_retry_event.set() return return sources['upload_key'] # If a client error was raised, add the backwards compatibility layer # that raises a S3UploadFailedError. These specific errors were only # ever thrown for upload_parts but now can be thrown for any related # client error. except ClientError as error: raise S3UploadFailedError("Failed to upload {} to {}: {}".format( filename, '/'.join([sources['upload_source'], sources['upload_key']]), error))