From 2fa6f1732a45403930790ba6b491547d0c14b3b0 Mon Sep 17 00:00:00 2001 From: Marko Novak Date: Thu, 29 Oct 2015 11:55:57 +0100 Subject: [PATCH] Added new functions to Twitter Ads API, together with integration tests for them. --- tests/test_endpoints_ads.py | 51 ++++++++++++++++++++++++++++++++++++- twython/endpoints_ads.py | 14 +++++++++- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/tests/test_endpoints_ads.py b/tests/test_endpoints_ads.py index 796cd17..3b43756 100644 --- a/tests/test_endpoints_ads.py +++ b/tests/test_endpoints_ads.py @@ -105,10 +105,16 @@ class TwythonEndpointsTestCase(unittest.TestCase): def test_create_line_item(self): campaign_id = self._create_test_campaign() + self._create_test_line_item(campaign_id) + self._delete_test_campaign(campaign_id) + + def _create_test_line_item(self, campaign_id): response = self.api.create_line_item(test_account_id, campaign_id, **self.TEST_WEBSITE_CLICKS_LINE_ITEM) + line_item_id = response['id'] self.assertEqual(response['account_id'], test_account_id) self.assertEqual(response['campaign_id'], campaign_id) - self._delete_test_campaign(campaign_id) + self.assertIsNotNone(line_item_id) + return line_item_id def test_upload_image(self): response = self._upload_test_image() @@ -130,6 +136,12 @@ class TwythonEndpointsTestCase(unittest.TestCase): self.assertTrue(len(response) >= 0) def test_create_and_delete_website_card(self): + card_id = self._create_test_website_card() + card = self.api.get_website_card(test_account_id, card_id) + self.assertEqual(card['id'], card_id) + self._delete_test_website_card(card_id) + + def _create_test_website_card(self): uploaded_image = self._upload_test_image() test_website_card = { 'name': 'Zemanta Partnered with AdsNative for Programmatic Native Supply', @@ -142,6 +154,43 @@ class TwythonEndpointsTestCase(unittest.TestCase): card_id = response_create['id'] self.assertEqual(response_create['account_id'], test_account_id) self.assertIsNotNone(card_id) + return card_id + + def _delete_test_website_card(self, card_id): response_delete = self.api.delete_website_card(test_account_id, card_id) self.assertEqual(response_delete['id'], card_id) + def test_create_promoted_only_tweet(self): + card_id, tweet_id = self._create_test_promoted_only_tweet() + self._delete_test_website_card(card_id) + + def _create_test_promoted_only_tweet(self): + card_id = self._create_test_website_card() + card = self.api.get_website_card(test_account_id, card_id) + test_promoted_only_tweet = { + 'status': 'This is test tweet for website card: %s' % card['preview_url'], + # 'as_user_id': '', + } + response = self.api.create_promoted_only_tweet(test_account_id, **test_promoted_only_tweet) + tweet_id = response['id'] + self.assertIsNotNone(tweet_id) + return card_id, tweet_id + + def test_promote_and_unpromote_tweet(self): + campaign_id = self._create_test_campaign() + line_item_id = self._create_test_line_item(campaign_id) + card_id, tweet_id = self._create_test_promoted_only_tweet() + test_tweet_promotion = { + 'line_item_id': line_item_id, + 'tweet_ids': [tweet_id] + } + result_promote = self.api.promote_tweet(test_account_id, **test_tweet_promotion) + self.assertTrue(len(result_promote) > 0) + self.assertEqual(int(result_promote[0]['tweet_id']), tweet_id) + promotion_id = result_promote[0]['id'] + self.assertIsNotNone(promotion_id) + result_unpromotion = self.api.unpromote_tweet(test_account_id, promotion_id) + self.assertTrue(result_unpromotion['deleted']) + self.assertEqual(result_unpromotion['id'], promotion_id) + self._delete_test_campaign(campaign_id) + self._delete_test_website_card(card_id) \ No newline at end of file diff --git a/twython/endpoints_ads.py b/twython/endpoints_ads.py index 9a3bab9..6a90941 100644 --- a/twython/endpoints_ads.py +++ b/twython/endpoints_ads.py @@ -77,7 +77,7 @@ class EndpointsAdsMixin(object): return response['data'] def get_website_card(self, account_id, card_id, **params): - response = self.get('account/%s/cards/website/%s' % (account_id, card_id), params=params) + response = self.get('accounts/%s/cards/website/%s' % (account_id, card_id), params=params) return response['data'] def create_website_card(self, account_id, **params): @@ -92,3 +92,15 @@ class EndpointsAdsMixin(object): def upload_image(self, **params): response = self.post('https://upload.twitter.com/1.1/media/upload.json', params=params) return response + + def create_promoted_only_tweet(self, account_id, **params): + response = self.post('accounts/%s/tweet' % account_id, params=params) + return response['data'] + + def promote_tweet(self, account_id, **params): + response = self.post('accounts/%s/promoted_tweets' % account_id, params=params) + return response['data'] + + def unpromote_tweet(self, account_id, promotion_id): + response = self.delete('accounts/%s/promoted_tweets/%s' % (account_id, promotion_id)) + return response['data'] \ No newline at end of file