def process(self, response:Response, responseCnt:ResponseContent, **keyargs): ''' @see: HandlerProcessorProceed.process ''' assert isinstance(response, Response), 'Invalid response %s' % response assert isinstance(responseCnt, ResponseContent), 'Invalid response content %s' % responseCnt if response.isSuccess is False: return # Skip in case the response is in error if Response.encoder not in response: return # Skip in case there is no encoder to render assert callable(response.renderFactory), 'Invalid response renderer factory %s' % response.renderFactory output = BytesIO() render = response.renderFactory(output) assert isinstance(render, IRender), 'Invalid render %s' % render resolve = Resolve(response.encoder).request(value=response.obj, render=render, **response.encoderData or {}) if not self.allowChunked and ResponseContent.length not in responseCnt: while resolve.has(): resolve.do() content = output.getvalue() responseCnt.length = len(content) responseCnt.source = (content,) output.close() else: responseCnt.source = self.renderAsGenerator(resolve, output, self.bufferSize)
def process(self, response:Response, responseCnt:ResponseContent, **keyargs): ''' @see: HandlerProcessorProceed.process Process the encoder rendering. ''' assert isinstance(response, Response), 'Invalid response %s' % response assert isinstance(responseCnt, ResponseContent), 'Invalid response content %s' % responseCnt if response.isSuccess is False: return # Skip in case the response is in error if response.encoder is None: return # Skip in case there is no encoder to render assert callable(response.renderFactory), 'Invalid response renderer factory %s' % response.renderFactory output = BytesIO() render = response.renderFactory(output) assert isinstance(render, IRender), 'Invalid render %s' % render resolve = Resolve(response.encoder).request(value=response.obj, render=render, **response.encoderData or {}) if not self.allowChunked and responseCnt.length is None: while resolve.has(): resolve.do() content = output.getvalue() responseCnt.length = len(content) responseCnt.source = (content,) output.close() else: responseCnt.source = self.renderAsGenerator(resolve, output, self.bufferSize)
def renderAsGenerator(self, value, encoder, renderFactory, data, bufferSize=1024): ''' Create a generator for rendering the encoder. ''' output = BytesIO() render = renderFactory(output) assert isinstance(render, IRender), 'Invalid render %s' % render resolve = Resolve(encoder).request(value=value, render=render, **data) while resolve.has(): if output.tell() >= bufferSize: yield output.getvalue() output.seek(0) output.truncate() resolve.do() yield output.getvalue()
def testEncode(self): transformer = CreateEncoderHandler() ioc.initialize(transformer) resolve = Resolve(transformer.encoderFor(typeFor(ModelId))) render = RenderToObject() context = dict(render=render, converter=ConverterPath(), converterId=ConverterPath(), normalizer=ConverterPath()) model = ModelId() model.Id = 12 render.obj = None resolve.request(value=model, **context).doAll() self.assertEqual({'Id': '12'}, render.obj) model.ModelKey = 'The key' render.obj = None resolve.request(value=model, **context).doAll() self.assertEqual({ 'Id': '12', 'ModelKey': { 'Key': 'The key' } }, render.obj) model.Name = 'Uau Name' model.Flags = ['1', '2', '3'] render.obj = None resolve.request(value=model, **context).doAll() self.assertEqual( { 'ModelKey': { 'Key': 'The key' }, 'Flags': { 'Flags': ['1', '2', '3'] }, 'Id': '12', 'Name': 'Uau Name' }, render.obj) transformer = CreateEncoderHandler() ioc.initialize(transformer) resolve = Resolve(transformer.encoderFor(typeFor(List(ModelId)))) render.obj = None resolve.request(value=[model], **context).doAll() self.assertEqual( { 'ModelIdList': [{ 'ModelKey': { 'Key': 'The key' }, 'Flags': { 'Flags': ['1', '2', '3'] }, 'Id': '12', 'Name': 'Uau Name' }] }, render.obj) render.obj = None resolve.request(value=[model, model, model], **context) resolve.do() self.assertEqual({'ModelIdList': []}, render.obj) resolve.do() self.assertEqual( { 'ModelIdList': [{ 'ModelKey': { 'Key': 'The key' }, 'Flags': { 'Flags': ['1', '2', '3'] }, 'Id': '12', 'Name': 'Uau Name' }] }, render.obj) resolve.do() self.assertEqual( { 'ModelIdList': [{ 'ModelKey': { 'Key': 'The key' }, 'Flags': { 'Flags': ['1', '2', '3'] }, 'Id': '12', 'Name': 'Uau Name' }, { 'ModelKey': { 'Key': 'The key' }, 'Flags': { 'Flags': ['1', '2', '3'] }, 'Id': '12', 'Name': 'Uau Name' }] }, render.obj) resolve.do() self.assertEqual( { 'ModelIdList': [{ 'ModelKey': { 'Key': 'The key' }, 'Flags': { 'Flags': ['1', '2', '3'] }, 'Id': '12', 'Name': 'Uau Name' }, { 'ModelKey': { 'Key': 'The key' }, 'Flags': { 'Flags': ['1', '2', '3'] }, 'Id': '12', 'Name': 'Uau Name' }, { 'ModelKey': { 'Key': 'The key' }, 'Flags': { 'Flags': ['1', '2', '3'] }, 'Id': '12', 'Name': 'Uau Name' }] }, render.obj) resolve.do() self.assertFalse(resolve.has())
def testEncode(self): transformer = CreateEncoderHandler() ioc.initialize(transformer) resolve = Resolve(transformer.encoderFor(typeFor(ModelId))) render = RenderToObject() context = dict(render=render, converter=ConverterPath(), converterId=ConverterPath(), normalizer=ConverterPath()) model = ModelId() model.Id = 12 render.obj = None resolve.request(value=model, **context).doAll() self.assertEqual({'Id':'12'}, render.obj) model.ModelKey = 'The key' render.obj = None resolve.request(value=model, **context).doAll() self.assertEqual({'Id':'12', 'ModelKey': {'Key': 'The key'}}, render.obj) model.Name = 'Uau Name' model.Flags = ['1', '2', '3'] render.obj = None resolve.request(value=model, **context).doAll() self.assertEqual({'ModelKey': {'Key': 'The key'}, 'Flags': {'Flags': ['1', '2', '3']}, 'Id': '12', 'Name': 'Uau Name'}, render.obj) transformer = CreateEncoderHandler() ioc.initialize(transformer) resolve = Resolve(transformer.encoderFor(typeFor(List(ModelId)))) render.obj = None resolve.request(value=[model], **context).doAll() self.assertEqual({'ModelIdList': [{'ModelKey': {'Key': 'The key'}, 'Flags': {'Flags': ['1', '2', '3']}, 'Id': '12', 'Name': 'Uau Name'}]}, render.obj) render.obj = None resolve.request(value=[model, model, model], **context) resolve.do() self.assertEqual({'ModelIdList': []}, render.obj) resolve.do() self.assertEqual({'ModelIdList': [ {'ModelKey': {'Key': 'The key'}, 'Flags': {'Flags': ['1', '2', '3']}, 'Id': '12', 'Name': 'Uau Name'} ]}, render.obj) resolve.do() self.assertEqual({'ModelIdList': [ {'ModelKey': {'Key': 'The key'}, 'Flags': {'Flags': ['1', '2', '3']}, 'Id': '12', 'Name': 'Uau Name'}, {'ModelKey': {'Key': 'The key'}, 'Flags': {'Flags': ['1', '2', '3']}, 'Id': '12', 'Name': 'Uau Name'} ]}, render.obj) resolve.do() self.assertEqual({'ModelIdList': [ {'ModelKey': {'Key': 'The key'}, 'Flags': {'Flags': ['1', '2', '3']}, 'Id': '12', 'Name': 'Uau Name'}, {'ModelKey': {'Key': 'The key'}, 'Flags': {'Flags': ['1', '2', '3']}, 'Id': '12', 'Name': 'Uau Name'}, {'ModelKey': {'Key': 'The key'}, 'Flags': {'Flags': ['1', '2', '3']}, 'Id': '12', 'Name': 'Uau Name'} ]}, render.obj) resolve.do() self.assertFalse(resolve.has())