def show(self): version_id = self.app.pargs.version_id validate_creds_exists(self.app) db = DBWrapper(self.app.creds) application_client = APIClient(db.get_configure()).get_application_api_client() enterprise_id = db.get_enterprise_id() if self.app.pargs.application: application_id = self.app.pargs.application else: application = db.get_application() if not application or not application.get('id'): self.app.log.debug('[version-show] There is no active application.') self.app.render('There is no active application.\n') return application_id = application.get('id') try: response = application_client.get_app_version(version_id, application_id, enterprise_id) except ApiException as e: self.app.log.error(f"[version-show] Failed to show details of an version: {e}") self.app.render(f"ERROR: {parse_error_message(self.app, e)}\n") return if not self.app.pargs.json: renderable = self._version_basic_response(response) self.app.render(renderable, format=OutputFormat.TABULATED.value, headers="keys", tablefmt="plain") else: renderable = self._version_basic_response(response, OutputFormat.JSON) self.app.render(renderable, format=OutputFormat.JSON.value)
def download(self): version_id = self.app.pargs.version_id validate_creds_exists(self.app) db = DBWrapper(self.app.creds) application_client = APIClient( db.get_configure()).get_application_api_client() enterprise_id = db.get_enterprise_id() if self.app.pargs.application: application_id = self.app.pargs.application else: application = db.get_application() if not application or not application.get('id'): self.app.log.debug( '[app-download] There is no active application.') self.app.render('There is no active application.') return application_id = application.get('id') if self.app.pargs.dest: destination = self.app.pargs.dest else: self.app.log.debug( '[app-download] destination file path cannot be empty.') self.app.render('destination file path cannot be empty.') return try: response = application_client.get_app_version( version_id, application_id, enterprise_id) except ApiException as e: self.app.log.error( f"[app-download] Failed to show details of an version: {e}") self.app.render(f"ERROR: {parse_error_message(self.app, e)}") return url = response.app_file file_size = int(response.size_in_mb * 1024 * 1024) first_byte = 0 pbar = tqdm(total=file_size, initial=first_byte, unit='B', unit_scale=True, desc='Downloading......') req = requests.get(url, stream=True) with (open(destination, 'ab')) as f: for chunk in req.iter_content(chunk_size=1024): if chunk: f.write(chunk) pbar.update(1024) time.sleep(0.001) pbar.close()