def testCallMethodDirect(self): """Test whether we can call an API method directly.""" headers = client.GetAuthCredentials() config = client.GetConfigValues() url = 'https://sandbox.google.com/api/adwords/v13/AccountService' op_config = { 'server': self.__class__.SERVER, 'version': self.__class__.VERSION, 'http_proxy': None } lock = thread.allocate_lock() service = WebService(headers, config, op_config, url, lock) soap_lib = config['soap_lib'] method_name = 'getAccountInfo' if soap_lib == SOAPPY: self.assert_(isinstance(service.CallMethod(method_name, ()), tuple)) elif soap_lib == ZSI: web_services = __import__( 'aw_api.zsi_toolkit.v13.AccountService_services', globals(), locals(), ['']) loc = web_services.AccountServiceLocator() request = eval('web_services.%sRequest()' % method_name) self.assert_( isinstance( service.CallMethod(method_name, (), 'Account', loc, request), tuple))
def __init__(self, headers, config, op_config, lock, logger): """Inits AccountService. Args: headers: dict dictionary object with populated authentication credentials. config: dict dictionary object with populated configuration values. op_config: dict dictionary object with additional configuration values for this operation. lock: thread.lock the thread lock logger: Logger the instance of Logger """ url = [ op_config['server'], 'api/adwords', op_config['version'], self.__class__.__name__ ] if glob_sanity_check.IsNewApi(op_config['version']): url.insert(2, 'account') if config['access']: url.insert(len(url) - 1, config['access']) self.__service = WebService(headers, config, op_config, '/'.join(url), lock, logger) self.__config = config self.__op_config = op_config if self.__config['soap_lib'] == SOAPPY: from aw_api.soappy_toolkit import MessageHandler from aw_api.soappy_toolkit import SanityCheck self.__web_services = None self.__message_handler = MessageHandler elif self.__config['soap_lib'] == ZSI: from aw_api import API_VERSIONS from aw_api.zsi_toolkit import SanityCheck if op_config['version'] in API_VERSIONS: module = '%s_services' % self.__class__.__name__ try: web_services = __import__( 'aw_api.zsi_toolkit.%s.%s' % (op_config['version'], module), globals(), locals(), ['']) except ImportError, e: # If one of library's required modules is missing, re raise exception. if str(e).find(module) < 0: raise ImportError(e) msg = ( 'The version \'%s\' is not compatible with \'%s\'.' % (op_config['version'], self.__class__.__name__)) raise ValidationError(msg) else: msg = 'Invalid API version, not one of %s.' % str( list(API_VERSIONS)) raise ValidationError(msg) self.__web_services = web_services self.__loc = eval('web_services.%sLocator()' % self.__class__.__name__)
def CallRawMethod(self, soap_message, url, http_proxy): """Call API method directly, using raw SOAP message. For API calls performed with this method, outgoing data is not run through library's validation logic. Args: soap_message: str SOAP XML message. url: str URL of the API service for the method to call. http_proxy: str HTTP proxy to use for this API call. Returns: tuple response from the API method (SOAP XML response message). """ headers = self.__GetAuthCredentialsForAccessLevel() # Load additional configuration data. op_config = {'http_proxy': http_proxy} service = WebService(headers, self.__config, op_config, url, self.__lock, self.__logger) return service.CallRawMethod(soap_message)
def __init__(self, headers, config, op_config, lock, logger): """Inits BulkMutateJobService. Args: headers: dict dictionary object with populated authentication credentials. config: dict dictionary object with populated configuration values. op_config: dict dictionary object with additional configuration values for this operation. lock: thread.lock the thread lock. logger: Logger the instance of Logger """ # NOTE(api.sgrinberg): Custom handling for BulkMutateJobService, whose # group in URL is 'job/' which is different from its namespace 'cm/'. url = [ op_config['server'], 'api/adwords', 'job', op_config['version'], self.__class__.__name__ ] if config['access']: url.insert(len(url) - 1, config['access']) self.__service = WebService(headers, config, op_config, '/'.join(url), lock, logger) self.__config = config self.__op_config = op_config if self.__config['soap_lib'] == SOAPPY: from aw_api.soappy_toolkit import SanityCheck self.__web_services = None elif self.__config['soap_lib'] == ZSI: from aw_api import API_VERSIONS from aw_api.zsi_toolkit import SanityCheck if op_config['version'] in API_VERSIONS: module = '%s_services' % self.__class__.__name__ try: web_services = __import__( 'aw_api.zsi_toolkit.%s.%s' % (op_config['version'], module), globals(), locals(), ['']) except ImportError, e: # If one of library's required modules is missing, re raise exception. if str(e).find(module) < 0: raise ImportError(e) msg = ( 'The version \'%s\' is not compatible with \'%s\'.' % (op_config['version'], self.__class__.__name__)) raise ValidationError(msg) else: msg = 'Invalid API version, not one of %s.' % str( list(API_VERSIONS)) raise ValidationError(msg) self.__web_services = web_services self.__loc = eval('web_services.%sLocator()' % self.__class__.__name__)
def CallMethod(self, url, method, params, http_proxy): """Call API method directly, using its service's URL. For API calls performed with this method, outgoing data is not run through library's validation logic. Args: url: str URL of the API service for the method to call. method: str name of the API method to call. params: list list of parameters to send to the API method. http_proxy: str HTTP proxy to use for this API call. Returns: tuple response from the API method. """ headers = self.__GetAuthCredentialsForAccessLevel() # Load additional configuration data. op_config = { 'server': Utils.GetServerFromUrl(url), 'version': Utils.GetVersionFromUrl(url), 'http_proxy': http_proxy } service = WebService(headers, self.__config, op_config, url, self.__lock, self.__logger) if self.__config['soap_lib'] == ZSI: # Check format of parameters. Example of valid formats, # - () # - ({'dummy': 0},) # - ({'campaignIds': ['11111']}, # {'startDay': '2008-07-01'}, # {'endDay': '2008-07-31'}) # # TODO(api.sgrinberg: Figure out how to match the order of params with # those in Holder object below. Then, we don't need to require client code # to provide key/value pairs, just values will be enough (see, issue# 31). try: SanityCheck.ValidateTypes(((params, tuple), )) for item in params: SanityCheck.ValidateTypes(((item, dict), )) except ValidationError: msg = 'Invalid format of parameters, expecting a tuple of dicts.' raise ValidationError(msg) # From the URL, get service being accessed and version used. url_parts = url.split('/') service_name = url_parts[len(url_parts) - 1].split('Service')[0] version = url_parts[len(url_parts) - 2] from aw_api import API_VERSIONS if version in API_VERSIONS: web_services = __import__( 'aw_api.zsi_toolkit.%s.%sService_services' % (version, service_name), globals(), locals(), ['']) else: msg = 'Invalid API version, not one of %s.' % str( list(API_VERSIONS)) raise ValidationError(msg) eval('%sService' % service_name).web_services = web_services self.__loc = eval(('%sService.web_services.%sServiceLocator()' % (service_name, service_name))) request = eval('%sService.web_services.%sRequest()' % (service_name, method)) return service.CallMethod(method, (params), service_name, self.__loc, request) else: return service.CallMethod(method, (params))