def report(self, file_id=None, run_id=None, output_file=None, output_dir=None): """Upload single file.""" report = Reports(base_url=self.base_url, api_key=self.api_key, file_id=file_id, run_id=run_id) try: file_obj = Files(base_url=self.base_url, api_key=self.api_key) res = file_obj.get_file(file_id=file_id) if not res: raise CosmosidException('Response from service is empty ' 'for file id {}'.format(file_id)) results = report.save_report(out_file=output_file, out_dir=output_dir) if results['status']: return results else: raise CosmosidException('{} File id: {}'.format( results['message'], file_id)) except CosmosidException as err: self.logger.error('Save report error') 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_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 directory_list(self, parent): """"get listing of appropriate directory.""" file_obj = Files(base_url=self.base_url, api_key=self.api_key) try: res = file_obj.get_list(parent_id=parent) if res: if res['status']: return res else: raise NotFoundException(res['message']) else: raise CosmosidException('Response from service is empty ' 'for directory {}'.format(parent)) except NotFoundException as err: utils.log_traceback(err) except CosmosidException as err: self.logger.error('Get directory list exception') utils.log_traceback(err) except Exception as err: self.logger.error("Failed to get listing of directory %s", parent) utils.log_traceback(err)
def get_last_run_for_file(self, file_id): try: runs_list = self.get_runs_list(file_id=file_id) if not runs_list: raise CosmosidException('Error occurred on get list of runs ' 'for a File: {}'.format(file_id)) if not runs_list['status']: raise NotFoundException(runs_list['message']) sorted_runs_list = sorted(runs_list['runs'], key=itemgetter('created'), reverse=True) return sorted_runs_list[0] except NotFoundException as err: self.logger.error('NotFound') utils.log_traceback(err) except CosmosidException: self.logger.error('Runs list exception.') return None except Exception as err: self.logger.error('Client exception occured') utils.log_traceback(err)
def sample_run_list(self, file_id): """Get list of runs for a given file id.""" sample_runs = Runs(base_url=self.base_url, api_key=self.api_key) try: sample_run_list = sample_runs.get_runs_list(file_id=file_id) if sample_run_list: if sample_run_list['status']: return sample_run_list else: raise NotFoundException(sample_run_list['message']) else: raise CosmosidException('Error occurred on get list of runs ' 'for a File: %s' % file_id) except NotFoundException as err: self.logger.error('NotFound') utils.log_traceback(err) except CosmosidException as err: self.logger.error('Get runs list exception') utils.log_traceback(err) except Exception as err: self.logger.error('Client exception occured') utils.log_traceback(err)
def analysis_list(self, file_id=None, run_id=None): """Get list of analysis for a given file id.""" analysis = Analysis(base_url=self.base_url, api_key=self.api_key) try: analysis_list = analysis.get_list(file_id=file_id, run_id=run_id) if analysis_list: if analysis_list['status']: return analysis_list else: raise NotFoundException(analysis_list['message']) else: raise CosmosidException('Error occurred on get list of ' 'analysis for a File: %s' % file_id) except NotFoundException as err: self.logger.error('NotFound') utils.log_traceback(err) except CosmosidException as err: self.logger.error('Get analysis list exception') utils.log_traceback(err) except Exception as err: self.logger.error('Client exception occured') utils.log_traceback(err)