Added new functions to Twitter Ads API, together with integration tests for them.
This commit is contained in:
parent
3219026432
commit
4b83630850
3 changed files with 70 additions and 11 deletions
|
|
@ -13,6 +13,14 @@ from twython.api_ads import TwythonAds
|
||||||
|
|
||||||
|
|
||||||
class TwythonEndpointsTestCase(unittest.TestCase):
|
class TwythonEndpointsTestCase(unittest.TestCase):
|
||||||
|
TEST_CAMPAIGN = {
|
||||||
|
'name': 'Test Twitter campaign - Twython',
|
||||||
|
'funding_instrument_id': test_funding_instrument_id,
|
||||||
|
'start_time': datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ'),
|
||||||
|
'daily_budget_amount_local_micro': 10 * 1000000,
|
||||||
|
'paused': True
|
||||||
|
}
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|
||||||
client_args = {
|
client_args = {
|
||||||
|
|
@ -45,6 +53,10 @@ class TwythonEndpointsTestCase(unittest.TestCase):
|
||||||
with self.assertRaises(TwythonError):
|
with self.assertRaises(TwythonError):
|
||||||
self.api.get_account('1234')
|
self.api.get_account('1234')
|
||||||
|
|
||||||
|
def test_get_account_features(self):
|
||||||
|
account_features = self.api.get_account_features(test_account_id)
|
||||||
|
self.assertTrue(len(account_features) > 0)
|
||||||
|
|
||||||
def test_get_funding_instruments(self):
|
def test_get_funding_instruments(self):
|
||||||
funding_instruments = self.api.get_funding_instruments(test_account_id)
|
funding_instruments = self.api.get_funding_instruments(test_account_id)
|
||||||
self.assertTrue(len(funding_instruments) > 0)
|
self.assertTrue(len(funding_instruments) > 0)
|
||||||
|
|
@ -56,17 +68,38 @@ class TwythonEndpointsTestCase(unittest.TestCase):
|
||||||
with self.assertRaises(TwythonError):
|
with self.assertRaises(TwythonError):
|
||||||
self.api.get_funding_instrument('1234', '1234')
|
self.api.get_funding_instrument('1234', '1234')
|
||||||
|
|
||||||
|
def test_get_iab_categories(self):
|
||||||
|
iab_categories = self.api.get_iab_categories()
|
||||||
|
self.assertTrue(len(iab_categories) > 0)
|
||||||
|
|
||||||
def test_get_campaigns(self):
|
def test_get_campaigns(self):
|
||||||
campaigns = self.api.get_campaigns(test_account_id)
|
campaigns = self.api.get_campaigns(test_account_id)
|
||||||
self.assertTrue(len(campaigns) > 0)
|
self.assertTrue(len(campaigns) > 0)
|
||||||
|
|
||||||
def test_create_campaign(self):
|
def test_create_and_delete_campaign(self):
|
||||||
new_campaign = {
|
campaign = self.api.create_campaign(test_account_id, **self.TEST_CAMPAIGN)
|
||||||
'name': 'Test Twitter campaign - Twython',
|
campaign_id = campaign['id']
|
||||||
'funding_instrument_id': test_funding_instrument_id,
|
|
||||||
'start_time': datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ'),
|
|
||||||
'daily_budget_amount_local_micro': 10 * 1000000
|
|
||||||
}
|
|
||||||
campaign = self.api.create_campaign(test_account_id, **new_campaign)
|
|
||||||
self.assertEqual(campaign['account_id'], test_account_id)
|
self.assertEqual(campaign['account_id'], test_account_id)
|
||||||
self.assertIsNotNone(campaign['id'])
|
self.assertIsNotNone(campaign_id)
|
||||||
|
campaign_check = self.api.get_campaign(test_account_id, campaign_id)
|
||||||
|
self.assertEqual(campaign_check['id'], campaign_id)
|
||||||
|
is_deleted = self.api.delete_campaign(test_account_id, campaign_id)
|
||||||
|
self.assertTrue(is_deleted)
|
||||||
|
|
||||||
|
def test_create_line_item(self):
|
||||||
|
campaign = self.api.create_campaign(test_account_id, **self.TEST_CAMPAIGN)
|
||||||
|
campaign_id = campaign['id']
|
||||||
|
self.assertEqual(campaign['account_id'], test_account_id)
|
||||||
|
website_clicks_line_item = {
|
||||||
|
'bid_type': 'MAX',
|
||||||
|
'bid_amount_local_micro': 2000000,
|
||||||
|
'product_type': 'PROMOTED_TWEETS',
|
||||||
|
'placements': 'ALL_ON_TWITTER',
|
||||||
|
'objective': 'WEBSITE_CLICKS',
|
||||||
|
'paused': True
|
||||||
|
}
|
||||||
|
response = self.api.create_line_item(test_account_id, campaign_id, **website_clicks_line_item)
|
||||||
|
self.assertEqual(response['account_id'], test_account_id)
|
||||||
|
self.assertEqual(response['campaign_id'], campaign_id)
|
||||||
|
campaign_check = self.api.get_campaign(test_account_id, campaign_id)
|
||||||
|
self.assertTrue(True)
|
||||||
|
|
|
||||||
|
|
@ -267,6 +267,10 @@ class TwythonAds(EndpointsAdsMixin, object):
|
||||||
"""Shortcut for POST requests via :class:`request`"""
|
"""Shortcut for POST requests via :class:`request`"""
|
||||||
return self.request(endpoint, 'POST', params=params, version=version)
|
return self.request(endpoint, 'POST', params=params, version=version)
|
||||||
|
|
||||||
|
def delete(self, endpoint, params=None, version='0'):
|
||||||
|
"""Shortcut for DELETE requests via :class:`request`"""
|
||||||
|
return self.request(endpoint, 'DELETE', params=params, version=version)
|
||||||
|
|
||||||
def get_lastfunction_header(self, header, default_return_value=None):
|
def get_lastfunction_header(self, header, default_return_value=None):
|
||||||
"""Returns a specific header from the last API call
|
"""Returns a specific header from the last API call
|
||||||
This will return None if the header is not present
|
This will return None if the header is not present
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,10 @@ class EndpointsAdsMixin(object):
|
||||||
response = self.get('accounts/%s' % account_id, params=params)
|
response = self.get('accounts/%s' % account_id, params=params)
|
||||||
return response['data']
|
return response['data']
|
||||||
|
|
||||||
|
def get_account_features(self, account_id, **params):
|
||||||
|
response = self.get('accounts/%s/features' % account_id, params=params)
|
||||||
|
return response['data']
|
||||||
|
|
||||||
def get_funding_instruments(self, account_id, **params):
|
def get_funding_instruments(self, account_id, **params):
|
||||||
response = self.get('accounts/%s/funding_instruments' % account_id, params=params)
|
response = self.get('accounts/%s/funding_instruments' % account_id, params=params)
|
||||||
return response['data']
|
return response['data']
|
||||||
|
|
@ -40,10 +44,28 @@ class EndpointsAdsMixin(object):
|
||||||
response = self.get('accounts/%s/funding_instruments/%s' % (account_id, funding_instrument_id), params=params)
|
response = self.get('accounts/%s/funding_instruments/%s' % (account_id, funding_instrument_id), params=params)
|
||||||
return response['data']
|
return response['data']
|
||||||
|
|
||||||
|
def get_iab_categories(self, **params):
|
||||||
|
response = self.get('iab_categories', params=params)
|
||||||
|
return response['data']
|
||||||
|
|
||||||
def get_campaigns(self, account_id, **params):
|
def get_campaigns(self, account_id, **params):
|
||||||
response = self.get('accounts/%s/campaigns' % account_id, params)
|
response = self.get('accounts/%s/campaigns' % account_id, params=params)
|
||||||
|
return response['data']
|
||||||
|
|
||||||
|
def get_campaign(self, account_id, campaign_id, **params):
|
||||||
|
response = self.get('accounts/%s/campaigns/%s' % (account_id, campaign_id), params=params)
|
||||||
return response['data']
|
return response['data']
|
||||||
|
|
||||||
def create_campaign(self, account_id, **params):
|
def create_campaign(self, account_id, **params):
|
||||||
response = self.post('accounts/%s/campaigns' % account_id, params)
|
response = self.post('accounts/%s/campaigns' % account_id, params=params)
|
||||||
|
return response['data']
|
||||||
|
|
||||||
|
def delete_campaign(self, account_id, campaign_id):
|
||||||
|
response = self.delete('accounts/%s/campaigns/%s' % (account_id, campaign_id))
|
||||||
|
return response['data']['deleted']
|
||||||
|
|
||||||
|
def create_line_item(self, account_id, campaign_id, **params):
|
||||||
|
params_extended = params.copy()
|
||||||
|
params_extended['campaign_id'] = campaign_id
|
||||||
|
response = self.post('accounts/%s/line_items' % account_id, params=params_extended)
|
||||||
return response['data']
|
return response['data']
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue