def read_body(self, body): """Handles the response body for this request. If the response body includes a result, returns the result unwrapped from the response union. If the response contains an exception, raises that exception. """ result_spec = self.result_type.thrift_spec # raise application exception, if present for exc_spec in result_spec[1:]: exc = getattr(body, exc_spec[2]) if exc is not None: raise exc # success - non-void if len(result_spec) >= 1 and result_spec[0] is not None: # value expected, but got none # TODO - server side should use this same logic if body.success is None: raise ValueExpectedError( 'Expected a value to be returned for %s, ' 'but recieved None - only void procedures can ' 'return None.' % self.endpoint ) return body.success # success - void else: return None
def __call__(self, request, headers=None, timeout=None, retry_on=None, retry_limit=None): if not headers: headers = {} serializer = ThriftSerializer(request.result_type) # serialize try: headers = serializer.serialize_header(headers=headers) except (AttributeError, TypeError): raise ValueError( 'headers must be a map[string]string (a shallow dict' ' where keys and values are strings)') body = serializer.serialize_body(call_args=request.call_args) response = yield self._tchannel.call(scheme=self.NAME, service=request.service, arg1=request.endpoint, arg2=headers, arg3=body, timeout=timeout, retry_on=retry_on, retry_limit=retry_limit, hostport=request.hostport) # deserialize... response.headers = serializer.deserialize_header( headers=response.headers) body = serializer.deserialize_body(body=response.body) result_spec = request.result_type.thrift_spec # raise application exception, if present for exc_spec in result_spec[1:]: exc = getattr(body, exc_spec[2]) if exc is not None: raise exc # success - non-void if len(result_spec) >= 1 and result_spec[0] is not None: # value expected, but got none # TODO - server side should use this same logic if body.success is None: raise ValueExpectedError( 'Expected a value to be returned for %s, ' 'but recieved None - only void procedures can' 'return None.' % request.endpoint) response.body = body.success raise gen.Return(response) # success - void else: response.body = None raise gen.Return(response)
def read_body(self, body): response_spec = self.result_type.type_spec for exc_spec in response_spec.exception_specs: exc = getattr(body, exc_spec.name) if exc is not None: raise exc # success - non-void if response_spec.return_spec is not None: if body.success is None: raise ValueExpectedError( 'Expected a value to be returned for %s, ' 'but recieved None - only void procedures can ' 'return None.' % self.endpoint) return body.success # success - void else: return None