def ConfigureRemoteApi(app_id,
                       path,
                       auth_func,
                       servername=None,
                       rpc_server_factory=appengine_rpc.HttpRpcServer,
                       rtok=None,
                       secure=False,
                       services=None,
                       default_auth_domain=None):
  """Does necessary setup to allow easy remote access to App Engine APIs.

  Either servername must be provided or app_id must not be None.  If app_id
  is None and a servername is provided, this function will send a request
  to the server to retrieve the app_id.

  Args:
    app_id: The app_id of your app, as declared in app.yaml.
    path: The path to the remote_api handler for your app
      (for example, '/remote_api').
    auth_func: A function that takes no arguments and returns a
      (username, password) tuple. This will be called if your application
      requires authentication to access the remote_api handler (it should!)
      and you do not already have a valid auth cookie.
    servername: The hostname your app is deployed on. Defaults to
      <app_id>.appspot.com.
    rpc_server_factory: A factory to construct the rpc server for the datastore.
    rtok: The validation token to sent with app_id lookups. If None, a random
      token is used.
    secure: Use SSL when communicating with the server.
    services: A list of services to set up stubs for. If specified, only those
      services are configured; by default all supported services are configured.
    default_auth_domain: The authentication domain to use by default.

  Raises:
    urllib2.HTTPError: if app_id is not provided and there is an error while
      retrieving it.
    ConfigurationError: if there is a error configuring the DatstoreFileStub.
  """
  if not servername and not app_id:
    raise ConfigurationError('app_id or servername required')
  if not servername:
    servername = '%s.appspot.com' % (app_id,)
  server = rpc_server_factory(servername, auth_func, GetUserAgent(),
                              GetSourceName(), debug_data=False, secure=secure)
  if not app_id:
    if not rtok:
      random.seed()
      rtok = str(random.random())[2:]
    urlargs = {'rtok': rtok}
    response = server.Send(path, payload=None, **urlargs)
    if not response.startswith('{'):
      raise ConfigurationError(
          'Invalid response recieved from server: %s' % response)
    app_info = yaml.load(response)
    if not app_info or 'rtok' not in app_info or 'app_id' not in app_info:
      raise ConfigurationError('Error parsing app_id lookup response')
    if app_info['rtok'] != rtok:
      raise ConfigurationError('Token validation failed during app_id lookup. '
                               '(sent %s, got %s)' % (repr(rtok),
                                                      repr(app_info['rtok'])))
    app_id = app_info['app_id']

  if services is not None:
    services = set(services)
    unsupported = services.difference(ALL_SERVICES)
    if unsupported:
      raise ConfigurationError('Unsupported service(s): %s'
                               % (', '.join(unsupported),))
  else:
    services = set(ALL_SERVICES)

  os.environ['APPLICATION_ID'] = app_id
  if default_auth_domain:
    os.environ['AUTH_DOMAIN'] = default_auth_domain
  elif 'AUTH_DOMAIN' not in os.environ:
    os.environ['AUTH_DOMAIN'] = 'gmail.com'
  apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap()
  if 'datastore_v3' in services:
    services.remove('datastore_v3')
    datastore_stub = RemoteDatastoreStub(server, path)
    apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3', datastore_stub)
  stub = RemoteStub(server, path)
  for service in services:
    apiproxy_stub_map.apiproxy.RegisterStub(service, stub)
dataset/ETHPy150Open CollabQ/CollabQ/.google_appengine/google/appengine/ext/remote_api/remote_api_stub.py/ConfigureRemoteApi/original