Changeset View
Changeset View
Standalone View
Standalone View
swh/core/api/__init__.py
Show First 20 Lines • Show All 129 Lines • ▼ Show 20 Lines | def __add_endpoint(meth_name, meth, attributes): | ||||
post_data.pop('cur', None) | post_data.pop('cur', None) | ||||
post_data.pop('db', None) | post_data.pop('db', None) | ||||
# Send the request. | # Send the request. | ||||
return self.post(meth._endpoint_path, post_data) | return self.post(meth._endpoint_path, post_data) | ||||
attributes[meth_name] = meth_ | attributes[meth_name] = meth_ | ||||
def line_iterator(it): | |||||
for line in it: | |||||
yield line.decode().lstrip('\n') | |||||
olasd: So you're stripping leading newlines? That's a bit weird.
Do we care for non-ASCII lines? | |||||
class RPCClient(metaclass=MetaRPCClient): | class RPCClient(metaclass=MetaRPCClient): | ||||
"""Proxy to an internal SWH RPC | """Proxy to an internal SWH RPC | ||||
""" | """ | ||||
backend_class = None | backend_class = None | ||||
"""For each method of `backend_class` decorated with | """For each method of `backend_class` decorated with | ||||
:func:`remote_api_endpoint`, a method with the same prototype and | :func:`remote_api_endpoint`, a method with the same prototype and | ||||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | def raw_verb(self, verb, endpoint, **opts): | ||||
raise self.api_exception(e) | raise self.api_exception(e) | ||||
def post(self, endpoint, data, **opts): | def post(self, endpoint, data, **opts): | ||||
if isinstance(data, (collections.Iterator, collections.Generator)): | if isinstance(data, (collections.Iterator, collections.Generator)): | ||||
data = (encode_data(x) for x in data) | data = (encode_data(x) for x in data) | ||||
else: | else: | ||||
data = encode_data(data) | data = encode_data(data) | ||||
chunk_size = opts.pop('chunk_size', self.chunk_size) | 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( | response = self.raw_verb( | ||||
'post', endpoint, data=data, | 'post', endpoint, data=data, | ||||
headers={'content-type': 'application/x-msgpack', | headers={'content-type': 'application/x-msgpack', | ||||
'accept': 'application/x-msgpack'}, | 'accept': 'application/x-msgpack'}, | ||||
**opts) | **opts) | ||||
if opts.get('stream') or \ | if stream_lines: | ||||
response.headers.get('transfer-encoding') == 'chunked': | return line_iterator(response.iter_lines()) | ||||
elif (opts.get('stream') or | |||||
response.headers.get('transfer-encoding') == 'chunked'): | |||||
return response.iter_content(chunk_size) | return response.iter_content(chunk_size) | ||||
else: | else: | ||||
return self._decode_response(response) | return self._decode_response(response) | ||||
post_stream = post | post_stream = post | ||||
def get(self, endpoint, **opts): | def get(self, endpoint, **opts): | ||||
chunk_size = opts.pop('chunk_size', self.chunk_size) | 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( | response = self.raw_verb( | ||||
'get', endpoint, | 'get', endpoint, | ||||
headers={'accept': 'application/x-msgpack'}, | headers={'accept': 'application/x-msgpack'}, | ||||
**opts) | **opts) | ||||
if opts.get('stream') or \ | if stream_lines: | ||||
response.headers.get('transfer-encoding') == 'chunked': | return line_iterator(response.iter_lines()) | ||||
elif (opts.get('stream') or | |||||
response.headers.get('transfer-encoding') == 'chunked'): | |||||
return response.iter_content(chunk_size) | return response.iter_content(chunk_size) | ||||
else: | else: | ||||
return self._decode_response(response) | return self._decode_response(response) | ||||
def get_stream(self, endpoint, **opts): | def get_stream(self, endpoint, **opts): | ||||
return self.get(endpoint, stream=True, **opts) | return self.get(endpoint, stream=True, **opts) | ||||
def _decode_response(self, response): | def _decode_response(self, response): | ||||
▲ Show 20 Lines • Show All 123 Lines • Show Last 20 Lines |
So you're stripping leading newlines? That's a bit weird.
Do we care for non-ASCII lines? Should we try to get the expected encoding from the response headers instead?