Ejemplo n.º 1
0
    def testRequestWithOAuth2FromFile(self):
        """Tests making a request against AdWords using cached OAuth2 values."""
        path = tempfile.mkdtemp()
        auth_credentials = {
            'clientCustomerId': CLIENT_CUSTOMER_ID,
            'developerToken': DEVELOPER_TOKEN,
            'userAgent': USER_AGENT,
            'clientId': CLIENT_ID,
            'clientSecret': CLIENT_SECRET,
            'refreshToken': REFRESH_TOKEN
        }
        with open(os.path.join(path, 'adwords_api_auth.pkl'), 'w') as handle:
            pickle.dump(auth_credentials, handle)
            handle.close()

        with open(os.path.join(path, 'adwords_api_config.pkl'), 'w') as handle:
            pickle.dump({}, handle)
            handle.close()

        budget = {
            'name': 'Interplanetary budget #%s' % Utils.GetUniqueName(),
            'amount': {
                'microAmount': '50000000'
            },
            'deliveryMethod': 'STANDARD',
            'period': 'DAILY'
        }

        budget_operations = [{'operator': 'ADD', 'operand': budget}]

        client = AdWordsClient(path=path)
        budget_service = client.GetBudgetService()
        response = budget_service.Mutate(budget_operations)[0]
        self.assertEqual('BudgetReturnValue',
                         response.get('ListReturnValue_Type'))
        self.assertEqual(1, len(response.get('value', [])))
class AdWordsClientServiceTest(unittest.TestCase):
    """Tests for retrieving SOAP services via AdWordsClient."""
    def setUp(self):
        """Prepare unittest."""
        self.client = AdWordsClient(headers={
            'authToken': ' ',
            'userAgent': ' ',
            'developerToken': ' '
        })

    def testGetBudgetService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetBudgetService()
            self.assertEquals('BudgetService', service._service_name)

    def testGetInfoService_notAllowed(self):
        self.assertRaises(ValidationError,
                          self.client.GetInfoService,
                          version='v201302')

    def testGetInfoService_allowed(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetInfoService(version='v201209')
            self.assertEquals('InfoService', service._service_name)

    def testGetAdGroupFeedService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetAdGroupFeedService()
            self.assertEquals('AdGroupFeedService', service._service_name)

    def testGetCampaignFeedService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetCampaignFeedService()
            self.assertEquals('CampaignFeedService', service._service_name)

    def testGetFeedItemService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetFeedItemService()
            self.assertEquals('FeedItemService', service._service_name)

    def testGetFeedMappingService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetFeedMappingService()
            self.assertEquals('FeedMappingService', service._service_name)

    def testGetFeedService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetFeedService()
            self.assertEquals('FeedService', service._service_name)

    def testGetCampaignSharedSetService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetCampaignSharedSetService()
            self.assertEquals('CampaignSharedSetService',
                              service._service_name)

    def testGetSharedSetService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetSharedSetService()
            self.assertEquals('SharedSetService', service._service_name)

    def testGetSharedCriterionService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetSharedCriterionService()
            self.assertEquals('SharedCriterionService', service._service_name)

    def testGetAdGroupBidModifierService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetAdGroupBidModifierService()
            self.assertEquals('AdGroupBidModifierService',
                              service._service_name)
Ejemplo n.º 3
0
class APIHandler(object):
    """Handler for the AdWords API using the Ads Python Client Libraries."""

    # The URL used to refresh the access token.
    _REFRESH_URL = 'https://accounts.google.com/o/oauth2/token'
    # The user-agent sent in requests from this demo.
    _USER_AGENT = 'AppEngine Demo v%s' % VERSION

    def __init__(self,
                 client_id,
                 client_secret,
                 refresh_token,
                 mcc_cid,
                 dev_token,
                 debug=False):
        """Initializes an APIHandler.

    Args:
      client_id:
      client_secret:
      refresh_token:
      mcc_cid:
      dev_token:
      debug:
    """
        credentials = OAuth2Credentials(None, client_id, client_secret,
                                        refresh_token, None, self._REFRESH_URL,
                                        self._USER_AGENT)
        credentials.refresh(httplib2.Http())

        headers = {
            'clientCustomerId': mcc_cid,
            'userAgent': self._USER_AGENT,
            'developerToken': dev_token,
            'validateOnly': 'n',
            'partialFailure': 'n',
            'oauth2credentials': credentials
        }
        # App Engine doesn't allow you to write to the disk. Thus, we need to
        # explicitly turn off logging. Though, if debugging is enabled, the
        # data from STDOUT will be redirected to app engine's debugging console.
        # Note that we also specify that we want to use ElementTree here; see the
        # README for more information if you would rather use PyXML.
        config = {
            'debug': Utils.BoolTypeConvert(debug),
            'xml_log': 'n',
            'request_log': 'n',
            'pretty_xml': 'n',
            'xml_parser': '2',  # We're using ElementTree.
            'compress': 'n'
        }

        self.client = AdWordsClient(headers, config)
        self.client.use_mcc = True

    def AddAdGroup(self, client_customer_id, campaign_id, name, status):
        """Create a new ad group.

    Args:
      client_customer_id: str Client Customer Id used to create the AdGroup.
      campaign_id: str Id of the campaign to use.
      name: str Name to assign to the AdGroup.
      status: str Status to assign to the AdGroup when it is created.
    """
        self.client.use_mcc = False
        self.client.SetClientCustomerId(client_customer_id)

        ad_group_service = self.client.GetAdGroupService()
        operations = [{
            'operator': 'ADD',
            'operand': {
                'campaignId': campaign_id,
                'name': name,
                'status': status
            }
        }]
        ad_group_service.Mutate(operations)

    def AddBudget(self, client_customer_id, micro_amount):
        """Create a new Budget with the given microAmount.

    Args:
      client_customer_id: str Client Customer Id used to create Budget.
      micro_amount: str The budget represented in micros.

    Returns:
      str BudgetId of the newly created Budget.
    """
        self.client.use_mcc = False
        self.client.SetClientCustomerId(client_customer_id)

        budget_service = self.client.GetBudgetService()

        operations = [{
            'operator': 'ADD',
            'operand': {
                'name': 'Budget #%s' % time.time(),
                'period': 'DAILY',
                'amount': {
                    'microAmount': micro_amount
                },
                'deliveryMethod': 'STANDARD'
            }
        }]

        return budget_service.Mutate(operations)[0]['value'][0]['budgetId']

    def AddCampaign(self, client_customer_id, campaign_name, ad_channel_type,
                    budget):
        """Add a Campaign to the client account.

    Args:
      ad_channel_type: str Primary serving target the campaign's ads.
      budget: str a budget amount (in micros) to use.
      campaign_name: str Name of the campaign to be added.
      client_customer_id: str Client Customer Id to use when creating Campaign.
    """
        self.client.use_mcc = False
        self.client.SetClientCustomerId(client_customer_id)
        campaign_service = self.client.GetCampaignService()
        budget_id = self.AddBudget(client_customer_id, budget)

        operations = [{
            'operator': 'ADD',
            'operand': {
                'name': campaign_name,
                'status': 'PAUSED',
                'biddingStrategyConfiguration': {
                    'biddingStrategyType': 'MANUAL_CPC',
                    'biddingScheme': {
                        'xsi_type': 'ManualCpcBiddingScheme',
                        'enhancedCpcEnabled': 'false'
                    }
                },
                'budget': {
                    'budgetId': budget_id
                },
                'advertisingChannelType': ad_channel_type,
                'settings': [{
                    'xsi_type': 'KeywordMatchSetting',
                    'optIn': 'false'
                }]
            }
        }]

        campaign_service.Mutate(operations)

    def GetAccounts(self):
        """Return the client accounts associated with the user's MCC account.

    Returns:
      list List of ManagedCustomer data objects.
    """
        selector = {'fields': ['CustomerId', 'Login', 'CanManageClients']}

        self.client.use_mcc = True
        accounts = self.client.GetManagedCustomerService().Get(selector)[0]
        self.client.use_mcc = False

        return accounts['entries']

    def GetAdGroups(self, client_customer_id, campaign_id):
        """Retrieves all AdGroups for the given campaign that haven't been removed.

    Args:
      client_customer_id: str Client Customer Id being used in API request.
      campaign_id: str id of the campaign for which to fetch ad groups.

    Returns:
      list List of AdGroup data objects.
    """
        self.client.use_mcc = False
        self.client.SetClientCustomerId(client_customer_id)
        selector = {
            'fields': ['Id', 'Name', 'Status'],
            'predicates': [{
                'field': 'CampaignId',
                'operator': 'EQUALS',
                'values': [campaign_id]
            }, {
                'field': 'Status',
                'operator': 'NOT_EQUALS',
                'values': ['REMOVED']
            }]
        }
        adgroups = self.client.GetAdGroupService().Get(selector)[0]

        if int(adgroups['totalNumEntries']) > 0:
            return adgroups['entries']
        else:
            return None

    def GetBudget(self, client_customer_id, budget_id):
        """Return a Budget with the associated budgetId.

    Args:
      client_customer_id: str Client Customer Id to which the budget belongs.
      budget_id: str id of the budget we want to examine.

    Returns:
      Budget A Budget data object.
    """
        self.client.use_mcc = False
        self.client.SetClientCustomerId(client_customer_id)
        selector = {
            'fields': [
                'BudgetId', 'BudgetName', 'BudgetStatus', 'Amount',
                'DeliveryMethod', 'BudgetReferenceCount',
                'IsBudgetExplicitlyShared'
            ],
            'predicates': [{
                'field': 'BudgetId',
                'operator': 'EQUALS',
                'values': [budget_id]
            }]
        }
        budgets = self.client.GetBudgetService().Get(selector)[0]

        if int(budgets['totalNumEntries']) > 0:
            return budgets['entries'][0]
        else:
            return None

    def GetCampaigns(self, client_customer_id):
        """Returns a client account's Campaigns that haven't been removed.

    Args:
      client_customer_id: str Client Customer Id used to retrieve Campaigns.

    Returns:
      list List of Campaign data objects.
    """
        self.client.SetClientCustomerId(client_customer_id)
        self.client.use_mcc = False
        # A somewhat hackish workaround for "The read operation timed out" error,
        # which could be triggered on AppEngine's end if the request is too large
        # and is taking too long.
        max_tries = 3
        today = time.strftime('%Y%m%d', time.localtime())
        for i in xrange(1, max_tries + 1):
            try:
                selector = {
                    'fields': ['Id', 'Name', 'Status', 'BudgetId', 'Amount'],
                    'predicates': [{
                        'field': 'Status',
                        'operator': 'NOT_EQUALS',
                        'values': ['REMOVED']
                    }],
                    'dateRange': {
                        'min': today,
                        'max': today
                    }
                }
                campaigns = self.client.GetCampaignService().Get(selector)[0]
                if int(campaigns['totalNumEntries']) > 0:
                    return campaigns['entries']
                else:
                    return None
            except Exception, e:
                if i == max_tries:
                    raise AdWordsError(e)
                continue
Ejemplo n.º 4
0
class AdWordsClientServiceTest(unittest.TestCase):
    """Tests for retrieving SOAP services via AdWordsClient."""
    def setUp(self):
        """Prepare unittest."""
        self.client = AdWordsClient(
            headers={
                'oauth2credentials': 'credential!',
                'userAgent': 'USER AGENT',
                'developerToken': 'DEV TOKEN'
            })

    def testGetAlertService(self):
        """AlertService shouldn't be created as of v201409"""
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            with self.assertRaises(ValidationError):
                service = self.client.GetAlertService()

    def testGetAlertService_v201406(self):
        """AlertService should be created in v201406 or earlier."""
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetAlertService(version='v201406')
            self.assertEquals('AlertService', service._service_name)

    def testGetBudgetService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetBudgetService()
            self.assertEquals('BudgetService', service._service_name)

    def testGetAdGroupFeedService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetAdGroupFeedService()
            self.assertEquals('AdGroupFeedService', service._service_name)

    def testGetCampaignFeedService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetCampaignFeedService()
            self.assertEquals('CampaignFeedService', service._service_name)

    def testGetCustomerFeedService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetCustomerFeedService()
            self.assertEquals('CustomerFeedService', service._service_name)

    def testGetFeedItemService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetFeedItemService()
            self.assertEquals('FeedItemService', service._service_name)

    def testGetFeedMappingService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetFeedMappingService()
            self.assertEquals('FeedMappingService', service._service_name)

    def testGetFeedService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetFeedService()
            self.assertEquals('FeedService', service._service_name)

    def testGetCampaignSharedSetService(self):
        """CampaignSharedSetService should be created after v201402."""
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetCampaignSharedSetService()
            self.assertEquals('CampaignSharedSetService',
                              service._service_name)

    def testGetCampaignSharedSetService_v201402(self):
        """CampaignSharedSetService shouldn't be created in v201402."""
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            with self.assertRaises(ValidationError):
                self.client.GetCampaignSharedSetService(version='v201402')

    def testGetSharedSetService(self):
        """SharedSetService now available in both v201402 and v201406."""
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetSharedSetService(version='v201406')
            self.assertEquals('SharedSetService', service._service_name)

    def testGetSharedCriterionService_v201402(self):
        """SharedCriterionService shouldn't be created in v201402."""
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            with self.assertRaises(ValidationError):
                self.client.GetSharedCriterionService(version='v201402')

    def testGetSharedCriterionService_v201406(self):
        """SharedCriterionService should be created in v201406."""
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetSharedCriterionService(version='v201406')
            self.assertEquals('SharedCriterionService', service._service_name)

    def testGetSharedCriterionService_v201406(self):
        """SharedCriterionService should be created in v201406."""
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetSharedCriterionService(version='v201406')
            self.assertEquals('SharedCriterionService', service._service_name)

    def testGetAdGroupBidModifierService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetAdGroupBidModifierService()
            self.assertEquals('AdGroupBidModifierService',
                              service._service_name)

    def testGetOfflineConversionFeedService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetOfflineConversionFeedService()
            self.assertEquals('OfflineConversionFeedService',
                              service._service_name)

    def testGetLabelService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetLabelService()
            self.assertEquals('LabelService', service._service_name)
class AdWordsClientServiceTest(unittest.TestCase):
    """Tests for retrieving SOAP services via AdWordsClient."""
    def setUp(self):
        """Prepare unittest."""
        self.client = AdWordsClient(
            headers={
                'authToken': 'AUTH TOKEN',
                'userAgent': 'USER AGENT',
                'developerToken': 'DEV TOKEN'
            })

    def testGetBudgetService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetBudgetService()
            self.assertEquals('BudgetService', service._service_name)

    def testGetAdGroupFeedService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetAdGroupFeedService()
            self.assertEquals('AdGroupFeedService', service._service_name)

    def testGetCampaignFeedService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetCampaignFeedService()
            self.assertEquals('CampaignFeedService', service._service_name)

    def testGetFeedItemService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetFeedItemService()
            self.assertEquals('FeedItemService', service._service_name)

    def testGetFeedMappingService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetFeedMappingService()
            self.assertEquals('FeedMappingService', service._service_name)

    def testGetFeedService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetFeedService()
            self.assertEquals('FeedService', service._service_name)

    def testGetCampaignSharedSetService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetCampaignSharedSetService()
            self.assertEquals('CampaignSharedSetService',
                              service._service_name)

    def testGetSharedSetService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetSharedSetService()
            self.assertEquals('SharedSetService', service._service_name)

    def testGetSharedCriterionService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetSharedCriterionService()
            self.assertEquals('SharedCriterionService', service._service_name)

    def testGetAdGroupBidModifierService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetAdGroupBidModifierService()
            self.assertEquals('AdGroupBidModifierService',
                              service._service_name)

    def testGetOfflineConversionFeedService(self):
        with mock.patch('adspygoogle.SOAPpy.WSDL.Proxy'):
            service = self.client.GetOfflineConversionFeedService()
            self.assertEquals('OfflineConversionFeedService',
                              service._service_name)