async def send(conn, batch_list, timeout): batch_request = client_pb2.ClientBatchSubmitRequest() batch_request.batches.extend(list(batch_list.batches)) batch_request.wait_for_commit = True validator_response = await conn.send( validator_pb2.Message.CLIENT_BATCH_SUBMIT_REQUEST, batch_request.SerializeToString(), timeout ) client_response = client_pb2.ClientBatchSubmitResponse() client_response.ParseFromString(validator_response.content) resp = client_response.batch_statuses[0] if resp.status == client_pb2.BatchStatus.COMMITTED: return resp elif resp.status == client_pb2.BatchStatus.INVALID: raise ApiBadRequest('Bad Request: {}'.format( resp.invalid_transactions[0].message )) elif resp.status == client_pb2.BatchStatus.PENDING: raise ApiInternalError( 'Internal Error: Transaction submitted but timed out.' ) elif resp.status == client_pb2.BatchStatus.UNKNOWN: raise ApiInternalError( 'Internal Error: Something went wrong. Try again later.' )
def batches_post(self, request): """ Takes protobuf binary from HTTP POST, and sends it to the validator """ if request.headers['Content-Type'] != 'application/octet-stream': return errors.WrongBodyType() payload = yield from request.read() validator_response = self._try_validator_request( Message.CLIENT_BATCH_SUBMIT_REQUEST, payload) response = client.ClientBatchSubmitResponse() response.ParseFromString(validator_response) return RouteHandler._try_client_response(request.headers, response)
def batches(self, request): """ Takes protobuf binary from HTTP POST, and sends it to the validator """ mime_type = 'application/octet-stream' type_msg = 'Expected an octet-stream encoded Protobuf binary' type_error = web.HTTPBadRequest(reason=type_msg) if request.headers['Content-Type'] != mime_type: return type_error payload = yield from request.read() validator_response = self._try_validator_request( Message.CLIENT_BATCH_SUBMIT_REQUEST, payload ) response = client.ClientBatchSubmitResponse() response.ParseFromString(validator_response) return RouteHandler._try_client_response(request.headers, response)