Don't raise error for batch requests with no items

This commit is contained in:
allegroai 2020-09-29 19:10:00 +03:00
parent ab9487c4e0
commit 8e859503f6
4 changed files with 32 additions and 14 deletions

View File

@ -51,17 +51,35 @@ class CallResult(object):
else:
self.__response_data = None
@classmethod
def _make_raw_response(cls, request_cls=None, service=None, action=None, status_code=200, text=None):
service = service or (request_cls._service if request_cls else 'unknown')
action = action or (request_cls._action if request_cls else 'unknown')
return cls(request_cls=request_cls, meta=ResponseMeta.from_raw_data(
status_code=status_code, text=text, endpoint='%(service)s.%(action)s' % locals()))
@classmethod
def from_result(cls, res, request_cls=None, logger=None, service=None, action=None, session=None):
""" From requests result """
response_cls = get_response_cls(request_cls)
if res is None:
return cls._make_raw_response(
request_cls=request_cls,
service=service,
action=action
)
try:
data = res.json()
except ValueError:
service = service or (request_cls._service if request_cls else 'unknown')
action = action or (request_cls._action if request_cls else 'unknown')
return cls(request_cls=request_cls, meta=ResponseMeta.from_raw_data(
status_code=res.status_code, text=res.text, endpoint='%(service)s.%(action)s' % locals()))
return cls._make_raw_response(
request_cls=request_cls,
service=service,
action=action,
status_code=res.status_code,
text=res.text
)
if 'meta' not in data:
raise ValueError('Missing meta section in response payload')
try:

View File

@ -30,7 +30,7 @@ class ResponseMeta(jsonmodels.models.Base):
return self._is_valid
@classmethod
def from_raw_data(cls, status_code, text, endpoint=None):
def from_raw_data(cls, status_code, text="", endpoint=None):
return cls(is_valid=False, result_code=status_code, result_subcode=0, result_msg=text,
endpoint=_ResponseEndpoint(name=(endpoint or 'unknown')))

View File

@ -341,9 +341,8 @@ class Session(TokenManager):
raise ValueError("Expecting list, tuple or generator in 'data' or 'json'")
if not data and not json:
raise ValueError(
"Missing data (data or json), batch requests are meaningless without it."
)
# Missing data (data or json), batch requests are meaningless without it.
return None
headers = headers.copy() if headers else {}
headers["Content-Type"] = "application/json-lines"
@ -439,11 +438,12 @@ class Session(TokenManager):
headers=headers,
)
# TODO: handle multiple results in this case
try:
res = next(r for r in res if r.status_code != 200)
except StopIteration:
# all are 200
res = res[0]
if res is not None:
try:
res = next(r for r in res if r.status_code != 200)
except StopIteration:
# all are 200
res = res[0]
else:
res = self.send_request(
service=req_obj._service,

View File

@ -1 +1 @@
__version__ = '0.16.2rc1'
__version__ = '0.16.2rc2'