示例#1
0
    def MakeSyncCallForRemoteApi(self, request):
        """Translate remote_api_pb.Request to gRPC call.

    Args:
      request: A remote_api_pb.Request message.

    Returns:
      A remote_api_pb.Response message.
    """
        # Translate remote_api_pb.Request into grpc_service_pb2.Request
        request_pb = grpc_service_pb2.Request(
            service_name=request.service_name(),
            method=request.method(),
            request=request.request(),
            txn_add_task_callback_hostport=self._txn_add_task_callback_hostport
        )
        if request.has_request_id():
            request_pb.request_id = request.request_id()

        response_pb = self.get_or_set_call_handler_stub().HandleCall(
            request_pb, _TIMEOUT)

        # Translate grpc_service_pb2.Response back to remote_api_pb.Response
        response = remote_api_pb.Response()

        response.set_response(response_pb.response)
        if response_pb.HasField('rpc_error'):
            response.mutable_rpc_error().ParseFromString(
                response_pb.rpc_error.SerializeToString())
        if response_pb.HasField('application_error'):
            response.mutable_application_error().ParseFromString(
                response_pb.application_error.SerializeToString())
        return response
示例#2
0
    def MakeSyncCall(self, service, call, request, response, request_id=None):
        """An interface similar to those exposed by traditional api proxy stubs.

    Args:
      service: Must be 'datastore_v3'.
      call: A string representing the rpc to make. Must be one of the datastore
        v3 methods.
      request: A protocol buffer of the type corresponding to 'call'.
      response: A protocol buffer of the type corresponding to 'call'.
      request_id: A unique string identifying the request associated with the
          API call.
    """
        assert service == 'datastore_v3'
        self.CheckRequest(service, call, request)

        request_pb = grpc_service_pb2.Request(service_name=service,
                                              method=call,
                                              request=request.Encode())
        if call == 'Commit':
            request_pb.txn_add_task_callback_hostport = self._txn_add_task_callback_hostport  # pylint: disable=line-too-long
        if request_id:
            request_pb.request_id = request.request_id()

        response_pb = self.get_or_set_call_handler_stub().HandleCall(
            request_pb, _TIMEOUT)

        if response_pb.HasField('application_error'):
            app_err = response_pb.application_error
            raise apiproxy_errors.ApplicationError(app_err.code,
                                                   app_err.detail)

        response.ParseFromString(response_pb.response)
示例#3
0
def make_grpc_call_from_remote_api(stub, request):
  """Translate remote_api_pb.Request to gRPC call.

  Args:
    stub: A grpc_service_pb2.beta_create_CallHandler_stub object.
    request: A remote_api_pb.Request message.

  Returns:
    A remote_api_pb.Response message.
  """
  # Translate remote_api_pb.Request into grpc_service_pb2.Request
  request_pb = grpc_service_pb2.Request(
      service_name=request.service_name(),
      method=request.method(),
      request=request.request())
  if request.has_request_id():
    request_pb.request_id = request.request_id()

  response_pb = stub.HandleCall(request_pb, remote_api_stub.TIMEOUT_SECONDS)

  # Translate grpc_service_pb2.Response back to remote_api_pb.Response
  response = remote_api_pb.Response()
  # TODO: b/36590656#comment3 continuously complete exception handling.
  response.set_response(response_pb.response)
  if response_pb.HasField('rpc_error'):
    response.mutable_rpc_error().ParseFromString(
        response_pb.rpc_error.SerializeToString())
  if response_pb.HasField('application_error'):
    response.mutable_application_error().ParseFromString(
        response_pb.application_error.SerializeToString())
  return response
示例#4
0
    def MakeSyncCallForRemoteApi(self, request):
        """Translate remote_api_pb.Request to gRPC call.

    Args:
      request: A remote_api_pb.Request message.

    Returns:
      A remote_api_pb.Response message.
    """
        # Translate remote_api_pb.Request into grpc_service_pb2.Request
        request_pb = grpc_service_pb2.Request(
            service_name=request.service_name(),
            method=request.method(),
            request=request.request(),
            txn_add_task_callback_hostport=self._txn_add_task_callback_hostport
        )
        if request.has_request_id():
            request_pb.request_id = request.request_id()

        response = remote_api_pb.Response()

        try:
            response_pb = self.get_or_set_call_handler_stub().HandleCall(
                request_pb, _TIMEOUT)
        except Exception:  # pylint: disable=broad-except

            response.set_exception(
                pickle.dumps(
                    # Raising built-in Exception instead of ConnectionError, because the
                    # later can not be parsed by remote_api.
                    Exception(
                        'Cannot connect to Cloud Datastore Emulator on {}'.
                        format(self.grpc_apiserver_host))))
            return response

        response.set_response(response_pb.response)
        if response_pb.HasField('rpc_error'):
            rpc_error = response_pb.rpc_error
            response_rpc_error = response.mutable_rpc_error()
            response_rpc_error.set_code(rpc_error.code)
            response_rpc_error.set_detail(rpc_error.detail)
        if response_pb.HasField('application_error'):
            app_err = response_pb.application_error
            response_app_err = response.mutable_application_error()
            response_app_err.set_code(app_err.code)
            response_app_err.set_detail(app_err.detail)
        return response
示例#5
0
def make_grpc_call_from_remote_api(stub, request):
    """Translate remote_api_pb.Request to gRPC call.

  Args:
    stub: A grpc_service_pb2.beta_create_CallHandler_stub object.
    request: A remote_api_pb.Request message.

  Returns:
    A remote_api_pb.Response message.
  """
    # Translate remote_api_pb.Request into grpc_service_pb2.Request
    request_pb = grpc_service_pb2.Request(service_name=request.service_name(),
                                          method=request.method(),
                                          request=request.request())
    if request.has_request_id():
        request_pb.request_id = request.request_id()

    response_pb = stub.HandleCall(request_pb, remote_api_stub.TIMEOUT_SECONDS)

    # Translate grpc_service_pb2.Response back to remote_api_pb.Response
    response = remote_api_pb.Response()
    # TODO: b/36590656#comment3 add exception handling logic.
    response.set_response(response_pb.response)
    return response