diff --git a/swh/core/api/__init__.py b/swh/core/api/__init__.py --- a/swh/core/api/__init__.py +++ b/swh/core/api/__init__.py @@ -135,6 +135,11 @@ attributes[meth_name] = meth_ +def line_iterator(it): + for line in it: + yield line.decode().lstrip('\n') + + class RPCClient(metaclass=MetaRPCClient): """Proxy to an internal SWH RPC @@ -193,13 +198,18 @@ else: data = encode_data(data) chunk_size = opts.pop('chunk_size', self.chunk_size) + stream_lines = opts.pop('stream_lines', False) + if stream_lines: + opts['stream'] = True response = self.raw_verb( 'post', endpoint, data=data, headers={'content-type': 'application/x-msgpack', 'accept': 'application/x-msgpack'}, **opts) - if opts.get('stream') or \ - response.headers.get('transfer-encoding') == 'chunked': + if stream_lines: + return line_iterator(response.iter_lines()) + elif (opts.get('stream') or + response.headers.get('transfer-encoding') == 'chunked'): return response.iter_content(chunk_size) else: return self._decode_response(response) @@ -208,12 +218,17 @@ def get(self, endpoint, **opts): chunk_size = opts.pop('chunk_size', self.chunk_size) + stream_lines = opts.pop('stream_lines', False) + if stream_lines: + opts['stream'] = True response = self.raw_verb( 'get', endpoint, headers={'accept': 'application/x-msgpack'}, **opts) - if opts.get('stream') or \ - response.headers.get('transfer-encoding') == 'chunked': + if stream_lines: + return line_iterator(response.iter_lines()) + elif (opts.get('stream') or + response.headers.get('transfer-encoding') == 'chunked'): return response.iter_content(chunk_size) else: return self._decode_response(response)