def decorate(self, response: StoqResponse) -> Optional[DecoratorResponse]: if self.RAISE_EXCEPTION: raise Exception('Test exception please ignore') dr = DecoratorResponse({'simple_decoration': 123}) if self.RETURN_ERRORS: dr.errors += ['Test error please ignore'] return dr
async def decorate(self, response: StoqResponse) -> Optional[DecoratorResponse]: if self.RAISE_EXCEPTION: raise Exception('Test exception please ignore') dr = DecoratorResponse({'simple_decoration': 123}) if self.RETURN_ERRORS: dr.errors.append( Error(plugin_name='simple_decorator', error='Test error please ignore')) return dr
def decorate(self, response: StoqResponse) -> DecoratorResponse: """ Decorate results using a template """ results = None try: dirname = os.path.dirname(self.template) basename = os.path.basename(self.template) env = Environment( loader=FileSystemLoader(dirname), trim_blocks=True, lstrip_blocks=True, autoescape=select_autoescape(default_for_string=True, default=True), ) results = env.get_template(basename).render(response=response) except TemplateNotFound: raise StoqPluginException(f'Template path not found: {self.template}') return DecoratorResponse(results)
def test_decoratorresponse_to_str(self): response = DecoratorResponse() response_str = str(response) response_dict = json.loads(response_str) self.assertIsInstance(response_str, str) self.assertIsInstance(response_dict, dict)