def GetPublicEndpointResponse(
    query_id=None, requested_format=None, transform=None):
  """Returns the public response for an external user request.

  This handles all the steps required to get the latest successful API
  response for an API Query.
    1) Check Memcache, if found skip to #4.
    2) If not in memcache, check if the stored response is abandoned and needs
       to be refreshed.
    3) Retrieve response from datastore.
    4) Perform any transforms and return the formatted response to the user.

  Args:
    query_id: The query id to retrieve the response for.
    requested_format: The format type requested for the response.
    transform: The transform instance to use to transform the content to the
               requested format, if required.

  Returns:
    A tuple contatining the response content, and status code to
    render. e.g. (CONTENT, 200)
  """
  transformed_response_content = None
  schedule_query = False

  if not requested_format or requested_format not in co.SUPPORTED_FORMATS:
    requested_format = co.DEFAULT_FORMAT

  response = GetApiQueryResponseFromMemcache(query_id, requested_format)

  # 1. Check Memcache
  if response and response.get('api_query') and response.get('content'):
    api_query = response.get('api_query')
    response_content = response.get('content')
    transformed_response_content = response.get('transformed_content')
    response_status = 200
  else:
    api_query = GetApiQuery(query_id)

    # 2. Check if this is an abandoned query
    if (api_query is not None and api_query.is_active
        and not api_query.is_error_limit_reached
        and api_query.is_abandoned):
      RefreshApiQueryResponse(api_query)

    # 3. Retrieve response from datastore
    response = GetApiQueryResponseFromDb(api_query)
    response_content = response.get('content')
    response_status = response.get('status')

    # Flag to schedule query later on if there is a successful response.
    if api_query:
      schedule_query = not api_query.in_queue

  # 4. Return the formatted response.
  if response_status == 200:
    UpdateApiQueryCounter(query_id)
    UpdateApiQueryTimestamp(query_id)

    if co.ANONYMIZE_RESPONSES:
      response_content = transformers.RemoveKeys(response_content)

    if not transformed_response_content:
      try:
        transformed_response_content = transform.Transform(response_content)
      except (KeyError, TypeError, AttributeError):
        # If the transformation fails then return the original content.
        transformed_response_content = response_content

    memcache_keys = {
        'api_query': api_query,
        co.DEFAULT_FORMAT: response_content,
        requested_format: transformed_response_content
    }

    memcache.add_multi(memcache_keys,
                       key_prefix=query_id,
                       time=api_query.refresh_interval)

    # Attempt to schedule query if required.
    if schedule_query:
      schedule_helper.ScheduleApiQuery(api_query)

    response_content = transformed_response_content
  else:
    raise errors.GaSuperProxyHttpError(response_content, response_status)

  return (response_content, response_status)
dataset/ETHPy150Open googleanalytics/google-analytics-super-proxy/src/controllers/util/query_helper.py/GetPublicEndpointResponse/original