aiorest icon indicating copy to clipboard operation
aiorest copied to clipboard

custom response serialization

Open imbolc opened this issue 10 years ago • 9 comments

What do you think?

imbolc avatar Sep 14 '14 13:09 imbolc

I think request handler should return dict instance. The instance should be converted into binary data by some processor (you can call it serializer or renderer). Processor should support template engines (I mean jinja2 for example).

asvetlov avatar Sep 15 '14 08:09 asvetlov

Do you mean that handler should return something like: {'foo': 'bar', '__processor': 'jinja2'} ?

imbolc avatar Sep 15 '14 10:09 imbolc

No, I don't like to return processor name from handler.

My vision is closer to:

@renderer(template='page.jinja2')
def handler(request):
    return {'foo': 'bar'}

asvetlov avatar Sep 16 '14 11:09 asvetlov

So it's the current way with some decorator's sugar? But what about html or binary (eg pictures) data? Why we have to wrap it into a dict?

imbolc avatar Sep 16 '14 12:09 imbolc

Hi,

I'm sorry @asvetlov, but I prefer the solution of @imbolc, because it's simple and easy to customize for our needs. We make WebServices REST/JSON oriented, but sometimes, we need to generate XML, SOAP or a very ugly format you don't want to know that it exists, because our clients are blocked with a specific piece of software. Moreover, maybe you'll receive a Pull Request to define routes via a decorator. It means you'll have:

@server.add_url('GET', '/hello')
@asyncio.coroutine
@renderer(template='page.jinja2')
def handler(request):
    return {'foo': 'bar'}

Maybe should be better to try to reduce the number of decorators, it's harder to debug.

(FYI, we already use aiohttp on production, and we want to replace our WebServices, based on Flask, by something based on aiohttp, because the performances are better with aiohttp. For now, aiorest is a good candidate for us)

ludovic-gasc avatar Sep 22 '14 21:09 ludovic-gasc

Moreover, we have another scenario that doesn't fit with decorator: For some WebServices, we read "Accept" header to know if we push a XML or JSON document.

ludovic-gasc avatar Sep 26 '14 11:09 ludovic-gasc

Just added decorator version

imbolc avatar Oct 04 '14 03:10 imbolc

@imbolc: FYI, I've merged your PR in our aiorest fork: https://github.com/Eyepea/aiorest

ludovic-gasc avatar Oct 04 '14 08:10 ludovic-gasc

:+1:

samuraisam avatar Jan 16 '15 23:01 samuraisam