Example #1
0
 def __call__(self,
              http,
              postproc,
              uri,
              method='GET',
              body=None,
              headers=None,
              methodId=None,
              resumable=None):
     """Implements the callable interface that discovery.build() expects
 of requestBuilder, which is to build an object compatible with
 HttpRequest.execute(). See that method for the description of the
 parameters and the expected response.
 """
     if methodId in self.responses:
         response = self.responses[methodId]
         resp, content = response[:2]
         if len(response) > 2:
             # Test the body against the supplied expected_body.
             expected_body = response[2]
             if bool(expected_body) != bool(body):
                 # Not expecting a body and provided one
                 # or expecting a body and not provided one.
                 raise UnexpectedBodyError(expected_body, body)
             if isinstance(expected_body, str):
                 expected_body = simplejson.loads(expected_body)
             body = simplejson.loads(body)
             if body != expected_body:
                 raise UnexpectedBodyError(expected_body, body)
         return HttpRequestMock(resp, content, postproc)
     elif self.check_unexpected:
         raise UnexpectedMethodError(methodId)
     else:
         model = JsonModel(False)
         return HttpRequestMock(None, '{}', model.response)
Example #2
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 = urlparse.urljoin(base, service['basePath'])
    if future:
        future = simplejson.loads(future)
        auth_discovery = future.get('auth', {})
    else:
        future = {}
        auth_discovery = {}
    schema = service.get('schemas', {})

    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
Example #3
0
 def __call__(self,
              http,
              postproc,
              uri,
              method='GET',
              body=None,
              headers=None,
              methodId=None):
     """Implements the callable interface that discovery.build() expects
 of requestBuilder, which is to build an object compatible with
 HttpRequest.execute(). See that method for the description of the
 parameters and the expected response.
 """
     if methodId in self.responses:
         resp, content = self.responses[methodId]
         return HttpRequestMock(resp, content, postproc)
     else:
         model = JsonModel(False)
         return HttpRequestMock(None, '{}', model.response)