Created
April 26, 2018 17:55
-
-
Save tym-xqo/313c6db206627e4952d669312b49952d to your computer and use it in GitHub Desktop.
Async Nerium
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aiohttp import web | |
from nerium import Query, ResultFormat | |
import ujson | |
async def resultset(request): | |
query = Query(request.match_info['query_name'], | |
**dict(request.rel_url.query)) | |
query_result = query.result_set() | |
return web.json_response(query_result) | |
@web.middleware | |
async def formatter(request, handler): | |
resp = await handler(request) | |
formatter = ResultFormat(ujson.loads(resp.text), | |
request.rel_url.query['ne_format']) | |
payload = formatter.formatted_results() | |
return web.json_response(payload) | |
def main(): | |
app = web.Application(middlewares=[formatter, ]) | |
app.router.add_get('/v1/{query_name}/', resultset) | |
app.router.add_get('/v1/{query_name}', resultset) | |
web.run_app(app) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment