Exemplo n.º 1
0
def build_from_document(
    service,
    base,
    future=None,
    http=None,
    developerKey=None,
    model=None,
    requestBuilder=HttpRequest):
  """Create a Resource for interacting with an API.

  Same as `build()`, but constructs the Resource object
  from a discovery document that is it given, as opposed to
  retrieving one over HTTP.

  Args:
    service: string, discovery document
    base: string, base URI for all HTTP requests, usually the discovery URI
    future: string, discovery document with future capabilities
    auth_discovery: dict, information about the authentication the API supports
    http: httplib2.Http, An instance of httplib2.Http or something that acts
      like it that HTTP requests will be made through.
    developerKey: string, Key for controlling API usage, generated
      from the API Console.
    model: Model class instance that serializes and
      de-serializes requests and responses.
    requestBuilder: Takes an http request and packages it up to be executed.

  Returns:
    A Resource object with methods for interacting with
    the service.
  """

  service = simplejson.loads(service)
  base = six.moves.urllib.parse.urljoin(base, service['basePath'])
  if future:
    future = simplejson.loads(future)
    auth_discovery = future.get('auth', {})
  else:
    future = {}
    auth_discovery = {}
  schema = Schemas(service)

  if model is None:
    features = service.get('features', [])
    model = JsonModel('dataWrapper' in features)
  resource = createResource(http, base, model, requestBuilder, developerKey,
                       service, future, schema)

  def auth_method():
    """Discovery information about the authentication the API uses."""
    return auth_discovery

  setattr(resource, 'auth_discovery', auth_method)

  return resource
Exemplo n.º 2
0
def build_from_document(service,
                        base=None,
                        future=None,
                        http=None,
                        developerKey=None,
                        model=None,
                        requestBuilder=HttpRequest):
    """Create a Resource for interacting with an API.

  Same as `build()`, but constructs the Resource object from a discovery
  document that is it given, as opposed to retrieving one over HTTP.

  Args:
    service: string or object, the JSON discovery document describing the API.
      The value passed in may either be the JSON string or the deserialized
      JSON.
    base: string, base URI for all HTTP requests, usually the discovery URI.
      This parameter is no longer used as rootUrl and servicePath are included
      within the discovery document. (deprecated)
    future: string, discovery document with future capabilities (deprecated).
    http: httplib2.Http, An instance of httplib2.Http or something that acts
      like it that HTTP requests will be made through.
    developerKey: string, Key for controlling API usage, generated
      from the API Console.
    model: Model class instance that serializes and de-serializes requests and
      responses.
    requestBuilder: Takes an http request and packages it up to be executed.

  Returns:
    A Resource object with methods for interacting with the service.
  """

    # future is no longer used.
    future = {}

    if isinstance(service, str):
        service = simplejson.loads(service)
    base = urllib.parse.urljoin(service['rootUrl'], service['servicePath'])
    schema = Schemas(service)

    if model is None:
        features = service.get('features', [])
        model = JsonModel('dataWrapper' in features)
    return Resource(http=http,
                    baseUrl=base,
                    model=model,
                    developerKey=developerKey,
                    requestBuilder=requestBuilder,
                    resourceDesc=service,
                    rootDesc=service,
                    schema=schema)
Exemplo n.º 3
0
def build_from_document(service,
                        base=None,
                        future=None,
                        http=None,
                        developerKey=None,
                        model=None,
                        requestBuilder=HttpRequest,
                        credentials=None):
    """Create a Resource for interacting with an API.

  Same as `build()`, but constructs the Resource object from a discovery
  document that is it given, as opposed to retrieving one over HTTP.

  Args:
    service: string or object, the JSON discovery document describing the API.
      The value passed in may either be the JSON string or the deserialized
      JSON.
    base: string, base URI for all HTTP requests, usually the discovery URI.
      This parameter is no longer used as rootUrl and servicePath are included
      within the discovery document. (deprecated)
    future: string, discovery document with future capabilities (deprecated).
    http: httplib2.Http, An instance of httplib2.Http or something that acts
      like it that HTTP requests will be made through.
    developerKey: string, Key for controlling API usage, generated
      from the API Console.
    model: Model class instance that serializes and de-serializes requests and
      responses.
    requestBuilder: Takes an http request and packages it up to be executed.
    credentials: object, credentials to be used for authentication.

  Returns:
    A Resource object with methods for interacting with the service.
  """

    # future is no longer used.
    future = {}

    if isinstance(service, basestring):
        service = json.loads(service)
    base = urlparse.urljoin(service['rootUrl'], service['servicePath'])
    schema = Schemas(service)

    if credentials:
        # If credentials were passed in, we could have two cases:
        # 1. the scopes were specified, in which case the given credentials
        #    are used for authorizing the http;
        # 2. the scopes were not provided (meaning the Application Default
        #    Credentials are to be used). In this case, the Application Default
        #    Credentials are built and used instead of the original credentials.
        #    If there are no scopes found (meaning the given service requires no
        #    authentication), there is no authorization of the http.
        if (isinstance(credentials, GoogleCredentials)
                and credentials.create_scoped_required()):
            scopes = service.get('auth', {}).get('oauth2',
                                                 {}).get('scopes', {})
            if scopes:
                credentials = credentials.create_scoped(scopes.keys())
            else:
                # No need to authorize the http object
                # if the service does not require authentication.
                credentials = None

        if credentials:
            http = credentials.authorize(http)

    if model is None:
        features = service.get('features', [])
        model = JsonModel('dataWrapper' in features)
    return Resource(http=http,
                    baseUrl=base,
                    model=model,
                    developerKey=developerKey,
                    requestBuilder=requestBuilder,
                    resourceDesc=service,
                    rootDesc=service,
                    schema=schema)
 def setUp(self):
     f = open(datafile('zoo.json'))
     discovery = f.read()
     f.close()
     discovery = simplejson.loads(discovery)
     self.sc = Schemas(discovery)