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
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)
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)
class SchemasTest(unittest.TestCase): def setUp(self): f = open(datafile('zoo.json')) discovery = f.read() f.close() discovery = simplejson.loads(discovery) self.sc = Schemas(discovery) def test_basic_formatting(self): self.assertEqual( sorted(LOAD_FEED.splitlines()), sorted(self.sc.prettyPrintByName('LoadFeed').splitlines())) def test_empty_edge_case(self): self.assertTrue('Unknown type' in self.sc.prettyPrintSchema({})) def test_simple_object(self): self.assertEqual({}, eval(self.sc.prettyPrintSchema({'type': 'object'}))) def test_string(self): self.assertEqual( type(""), type(eval(self.sc.prettyPrintSchema({'type': 'string'})))) def test_integer(self): self.assertEqual( type(20), type(eval(self.sc.prettyPrintSchema({'type': 'integer'})))) def test_number(self): self.assertEqual( type(1.2), type(eval(self.sc.prettyPrintSchema({'type': 'number'})))) def test_boolean(self): self.assertEqual( type(True), type(eval(self.sc.prettyPrintSchema({'type': 'boolean'})))) def test_string_default(self): self.assertEqual( 'foo', eval( self.sc.prettyPrintSchema({ 'type': 'string', 'default': 'foo' }))) def test_integer_default(self): self.assertEqual( 20, eval(self.sc.prettyPrintSchema({ 'type': 'integer', 'default': 20 }))) def test_number_default(self): self.assertEqual( 1.2, eval(self.sc.prettyPrintSchema({ 'type': 'number', 'default': 1.2 }))) def test_boolean_default(self): self.assertEqual( False, eval( self.sc.prettyPrintSchema({ 'type': 'boolean', 'default': False }))) def test_null(self): self.assertEqual(None, eval(self.sc.prettyPrintSchema({'type': 'null'}))) def test_any(self): self.assertEqual('', eval(self.sc.prettyPrintSchema({'type': 'any'}))) def test_array(self): self.assertEqual([{}], eval( self.sc.prettyPrintSchema({ 'type': 'array', 'items': { 'type': 'object' } }))) def test_nested_references(self): feed = { 'items': [{ 'photo': { 'hash': 'A String', 'hashAlgorithm': 'A String', 'filename': 'A String', 'type': 'A String', 'size': 42 }, 'kind': 'zoo#animal', 'etag': 'A String', 'name': 'A String' }], 'kind': 'zoo#animalFeed', 'etag': 'A String' } self.assertEqual(feed, eval(self.sc.prettyPrintByName('AnimalFeed'))) def test_additional_properties(self): items = { 'animals': { 'a_key': { 'photo': { 'hash': 'A String', 'hashAlgorithm': 'A String', 'filename': 'A String', 'type': 'A String', 'size': 42 }, 'kind': 'zoo#animal', 'etag': 'A String', 'name': 'A String' } }, 'kind': 'zoo#animalMap', 'etag': 'A String' } self.assertEqual(items, eval(self.sc.prettyPrintByName('AnimalMap'))) def test_unknown_name(self): self.assertRaises(KeyError, self.sc.prettyPrintByName, 'UknownSchemaThing')
class SchemasTest(unittest.TestCase): def setUp(self): f = open(datafile('zoo.json')) discovery = f.read() f.close() discovery = simplejson.loads(discovery) self.sc = Schemas(discovery) def test_basic_formatting(self): self.assertEqual(sorted(LOAD_FEED.splitlines()), sorted(self.sc.prettyPrintByName('LoadFeed').splitlines())) def test_empty_edge_case(self): self.assertTrue('Unknown type' in self.sc.prettyPrintSchema({})) def test_simple_object(self): self.assertEqual({}, eval(self.sc.prettyPrintSchema({'type': 'object'}))) def test_string(self): self.assertEqual(type(""), type(eval(self.sc.prettyPrintSchema({'type': 'string'})))) def test_integer(self): self.assertEqual(type(20), type(eval(self.sc.prettyPrintSchema({'type': 'integer'})))) def test_number(self): self.assertEqual(type(1.2), type(eval(self.sc.prettyPrintSchema({'type': 'number'})))) def test_boolean(self): self.assertEqual(type(True), type(eval(self.sc.prettyPrintSchema({'type': 'boolean'})))) def test_string_default(self): self.assertEqual('foo', eval(self.sc.prettyPrintSchema({'type': 'string', 'default': 'foo'}))) def test_integer_default(self): self.assertEqual(20, eval(self.sc.prettyPrintSchema({'type': 'integer', 'default': 20}))) def test_number_default(self): self.assertEqual(1.2, eval(self.sc.prettyPrintSchema({'type': 'number', 'default': 1.2}))) def test_boolean_default(self): self.assertEqual(False, eval(self.sc.prettyPrintSchema({'type': 'boolean', 'default': False}))) def test_null(self): self.assertEqual(None, eval(self.sc.prettyPrintSchema({'type': 'null'}))) def test_any(self): self.assertEqual('', eval(self.sc.prettyPrintSchema({'type': 'any'}))) def test_array(self): self.assertEqual([{}], eval(self.sc.prettyPrintSchema({'type': 'array', 'items': {'type': 'object'}}))) def test_nested_references(self): feed = { 'items': [ { 'photo': { 'hash': 'A String', 'hashAlgorithm': 'A String', 'filename': 'A String', 'type': 'A String', 'size': 42 }, 'kind': 'zoo#animal', 'etag': 'A String', 'name': 'A String' } ], 'kind': 'zoo#animalFeed', 'etag': 'A String' } self.assertEqual(feed, eval(self.sc.prettyPrintByName('AnimalFeed'))) def test_additional_properties(self): items = { 'animals': { 'a_key': { 'photo': { 'hash': 'A String', 'hashAlgorithm': 'A String', 'filename': 'A String', 'type': 'A String', 'size': 42 }, 'kind': 'zoo#animal', 'etag': 'A String', 'name': 'A String' } }, 'kind': 'zoo#animalMap', 'etag': 'A String' } self.assertEqual(items, eval(self.sc.prettyPrintByName('AnimalMap'))) def test_unknown_name(self): self.assertRaises(KeyError, self.sc.prettyPrintByName, 'UknownSchemaThing')