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 +# content-type for NDJSON, see http://ndjson.org/ +NDJSON_CT = 'application/x-ndjson' + + @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() @@ -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='text/plain') return count