def test_request_is_post(self): m = model.JsonModel() request = http.HttpRequest( None, m.response, 'https://www.googleapis.com/someapi/v1/collection/?foo=bar', method='GET', body='{}', headers={'content-type': 'application/json'}) c = push.Channel('my_channel', {}) push.Subscription.for_request(request, c) self.assertEqual('POST', request.method)
def test_non_get_error(self): m = model.JsonModel() request = http.HttpRequest( None, m.response, 'https://www.googleapis.com/someapi/v1/collection/?foo=bar', method='POST', body='{}', headers={'content-type': 'application/json'}) c = push.Channel('my_channel', {}) self.assertRaises(push.InvalidSubscriptionRequestError, push.Subscription.for_request, request, c)
def test_do_subscribe(self): m = model.JsonModel() request = http.HttpRequest( None, m.response, 'https://www.googleapis.com/someapi/v1/collection/?foo=bar', method='GET', body='{}', headers={'content-type': 'application/json'}) h = http.HttpMockSequence([({ 'status': 200, 'X-Goog-Subscription-ID': 'my_subscription' }, '{}')]) c = push.Channel('my_channel', {}) s = push.Subscription.for_request(request, c) request.execute(http=h) self.assertEqual('my_subscription', s.subscription_id)
def LoadApi(self, http, api_server, api_version, args): """Load an API either from a discovery document or a local file. Args: http: An httplib2.Http object for making HTTP requests. api_server: The location of the http server. api_version: The api_version to load. args: The args object from the CLI. Returns: The newly created DNS API Service Object. Raises: ToolException: If an error occurs in loading the API service object. """ # Load the discovery document. discovery_document = None # Try to download the discovery document url = '{server}/discovery/v1/apis/dns/{version}/rest'.format( server=api_server.rstrip('/'), version=api_version) response, content = http.request(url) if response.status == 200: discovery_document = content if discovery_document is None: raise c_exc.ToolException('Couldn\'t load discovery') try: discovery_document = json.loads(discovery_document) except ValueError: raise errors.InvalidJsonError() api = discovery.build_from_document(discovery_document, http=http, model=model.JsonModel()) return WrapApiIfNeeded(api, args)
def CreateComputeApi( http, api_version, download=False, server=None): """Builds the Google Compute Engine API to use. Args: http: a httplib2.Http like object for communication. api_version: the version of the API to create. download: bool. Download the discovery document from the discovery service. server: URL of the API service host. Returns: The ComputeApi object to use. Raises: gcutil_errors.CommandError: If loading the discovery doc fails. """ # Use default server if none provided. default_server = 'https://www.googleapis.com/' server = server or default_server # Load the discovery document. discovery_document = None # Try to download the discovery document if download or server != default_server: url = '{server}/discovery/v1/apis/compute/{version}/rest'.format( server=server.rstrip('/'), version=api_version) response, content = http.request(url) if response.status == 200: discovery_document = content else: raise gcutil_errors.CommandError( 'Could not load discovery document from %s:\n%s' % ( url, content)) else: # Try to load locally discovery_path = os.path.join( os.path.dirname(__file__), 'compute', '%s.json' % api_version) try: with open(discovery_path) as discovery_file: discovery_document = discovery_file.read() except IOError as err: raise gcutil_errors.CommandError( 'Could not load discovery document from %s:\n%s' % ( discovery_path, err)) try: discovery_document = json.loads(discovery_document) except ValueError: raise errors.InvalidJsonError() api = discovery.build_from_document( discovery_document, http=http, model=model.JsonModel()) api = WrapApiIfNeeded(api) return ComputeApi( api, version.get(discovery_document.get('version')), GetSetOfApiMethods(discovery_document))