Example #1
0
    def ExecuteRequest(self, request):
        """Executes an API invocation and returns the response object."""
        service = request.service_name()
        method = request.method()
        service_methods = SERVICE_PB_MAP.get(service, {})
        request_class, response_class = service_methods.get(
            method, (None, None))
        if not request_class:
            raise apiproxy_errors.CallNotFoundError()

        request_data = request_class()
        request_data.ParseFromString(request.request().contents())
        response_data = response_class()

        if service in self.LOCAL_STUBS:
            self.LOCAL_STUBS[service].MakeSyncCall(service, method,
                                                   request_data, response_data)
        else:
            apiproxy_stub_map.MakeSyncCall(service, method, request_data,
                                           response_data)

        return response_data
def _ExecuteRequest(request):
  """Executes an API method call and returns the response object.

  Args:
    request: A remote_api.Request object representing the API call e.g. a call
        to memcache.Get.

  Returns:
    A ProtocolBuffer.ProtocolMessage representing the API response e.g. a
    memcache_service_pb.MemcacheGetResponse.

  Raises:
    apiproxy_errors.CallNotFoundError: if the requested method doesn't exist.
    apiproxy_errors.ApplicationError: if the API method calls fails.
  """
  service = request.service_name()
  method = request.method()
  service_methods = remote_api_services.SERVICE_PB_MAP.get(service, {})
  request_class, response_class = service_methods.get(method, (None, None))
  if not request_class:
    raise apiproxy_errors.CallNotFoundError('%s.%s does not exist' % (service,
                                                                      method))

  request_data = request_class()
  request_data.ParseFromString(request.request())
  response_data = response_class()

  def MakeRequest():
    apiproxy_stub_map.MakeSyncCall(service, method, request_data,
                                   response_data)



  if service in THREAD_SAFE_SERVICES:
    MakeRequest()
  else:
    with GLOBAL_API_LOCK:
      MakeRequest()
  return response_data
Example #3
0
def _execute_request(request, use_proto3=False):
  """Executes an API method call and returns the response object.

  Args:
    request: A remote_api_pb.Request object representing the API call e.g. a
        call to memcache.Get.
    use_proto3: A boolean representing is request is in proto3.

  Returns:
    A ProtocolBuffer.ProtocolMessage representing the API response e.g. a
    memcache_service_pb.MemcacheGetResponse.

  Raises:
    apiproxy_errors.CallNotFoundError: if the requested method doesn't exist.
    apiproxy_errors.ApplicationError: if the API method calls fails.
  """
  logging.debug('API server executing remote_api_pb.Request: \n%s', request)

  if use_proto3:
    service = request.service_name
    method = request.method
    if request.request_id:
      request_id = request.request_id
    else:
      # This logging could be time consuming. Hence set as debug level.
      logging.debug('Received a request without request_id: %s', request)
      request_id = None
  else:
    service = request.service_name()
    method = request.method()
    if request.has_request_id():
      request_id = request.request_id()
    else:
      logging.error('Received a request without request_id: %s', request)
      request_id = None

  service_methods = (
      _DATASTORE_V4_METHODS if service == 'datastore_v4'
      else remote_api_services.STUB_SERVICE_PB_MAP.get(service, {}))
  # We do this rather than making a new map that is a superset of
  # remote_api_services.SERVICE_PB_MAP because that map is not initialized
  # all in one place, so we would have to be careful about where we made
  # our new map.

  request_class, response_class = service_methods.get(method, (None, None))
  if not request_class:
    raise apiproxy_errors.CallNotFoundError('%s.%s does not exist' % (service,
                                                                      method))

  # TODO: Remove after the Files API is really gone.
  if not _FILESAPI_ENABLED and service == 'file':
    raise apiproxy_errors.CallNotFoundError(
        'Files API method %s.%s is disabled. Further information: '
        'https://cloud.google.com/appengine/docs/deprecations/files_api'
        % (service, method))

  request_data = request_class()
  if use_proto3:
    request_data.ParseFromString(request.request)
  else:
    request_data.ParseFromString(request.request())
  response_data = response_class()
  service_stub = apiproxy_stub_map.apiproxy.GetStub(service)

  def make_request():
    # TODO: Remove after the Files API is really gone.
    if (_FILESAPI_USE_TRACKER is not None
        and service == 'file' and request_id is not None):
      _FILESAPI_USE_TRACKER.set_filesapi_used(request_id)
    service_stub.MakeSyncCall(service,
                              method,
                              request_data,
                              response_data,
                              request_id)

  # If the service has not declared itself as threadsafe acquire
  # GLOBAL_API_LOCK.
  if service_stub.THREADSAFE:
    make_request()
  else:
    with GLOBAL_API_LOCK:
      make_request()
  metrics.GetMetricsLogger().LogOnceOnStop(
      metrics.API_STUB_USAGE_CATEGORY,
      metrics.API_STUB_USAGE_ACTION_TEMPLATE % service)

  logging.debug('API server responding with remote_api_pb.Response: \n%s',
                response_data)
  return response_data