diff --git a/swh/graph/server/app.py b/swh/graph/server/app.py --- a/swh/graph/server/app.py +++ b/swh/graph/server/app.py @@ -22,9 +22,14 @@ from async_generator import asynccontextmanager # type: ignore +JSON_CT = 'application/json' +NDJSON_CT = 'application/x-ndjson' # see http://ndjson.org/ + + @asynccontextmanager -async def stream_response(request, *args, **kwargs): +async def stream_response(request, content_type='text/plain', *args, **kwargs): response = aiohttp.web.StreamResponse(*args, **kwargs) + response.content_type = content_type await response.prepare(request) yield response await response.write_eof() @@ -48,7 +53,7 @@ async def stats(request): stats = request.app['backend'].stats() - return aiohttp.web.Response(body=stats, content_type='application/json') + return aiohttp.web.Response(body=stats, content_type=JSON_CT) def get_simple_traversal_handler(ttype): @@ -101,7 +106,7 @@ src_node = backend.pid2node[src] it = backend.visit_paths(direction, edges, src_node) - async with stream_response(request) as response: + async with stream_response(request, content_type=NDJSON_CT) as response: async for res_path in it: res_path_pid = [backend.node2pid[n] for n in res_path] line = json.dumps(res_path_pid) @@ -121,8 +126,7 @@ src_node = backend.pid2node[src] cnt = await loop.run_in_executor( None, backend.count, ttype, direction, edges, src_node) - return aiohttp.web.Response(body=str(cnt), - content_type='application/json') + return aiohttp.web.Response(body=str(cnt), content_type=JSON_CT) return count