diff --git a/examples/unified_cards.py b/examples/unified_cards.py new file mode 100644 index 0000000..a835add --- /dev/null +++ b/examples/unified_cards.py @@ -0,0 +1,82 @@ +from twitter_ads.client import Client +from twitter_ads.creative import Card +from twitter_ads.http import Request + + +CONSUMER_KEY = 'your consumer key' +CONSUMER_SECRET = 'your consumer secret' +ACCESS_TOKEN = 'user access token' +ACCESS_TOKEN_SECRET = 'user access token secret' +ACCOUNT_ID = 'ads account id' + +# initialize the client +client = Client(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET) + +# load the advertiser account instance +account = client.accounts(ACCOUNT_ID) + +# fetch all +card = Card.all(account, card_ids="1502039998987587584").first + +# fetch by card-id +card = Card.load(account=account, id="1502039998987587584") + +# edit card destination.url +card.components= [ + { + "media_key": "13_794652834998325248", + "media_metadata": { + "13_794652834998325248": { + "type": "VIDEO", + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", + "width": 640, + "height": 360, + "video_duration": 7967, + "video_aspect_ratio": "16:9" + } + }, + "type": "MEDIA" + }, + { + "title": "Twitter", + "destination": { + "url": "http://twitter.com/newvalue", + "type": "WEBSITE" + }, + "type": "DETAILS" + } + ] + +card.save() +print(card.components) + +# create new card +newcard = Card(account=account) +newcard.name="my new card" +components= [ + { + "media_key": "13_794652834998325248", + "media_metadata": { + "13_794652834998325248": { + "type": "VIDEO", + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", + "width": 640, + "height": 360, + "video_duration": 7967, + "video_aspect_ratio": "16:9" + } + }, + "type": "MEDIA" + }, + { + "title": "Twitter", + "destination": { + "url": "http://twitter.com/login", + "type": "WEBSITE" + }, + "type": "DETAILS" + } + ] +newcard.components=components +newcard.save() +print(newcard.id) \ No newline at end of file diff --git a/tests/fixtures/cards_all.json b/tests/fixtures/cards_all.json new file mode 100644 index 0000000..1feec12 --- /dev/null +++ b/tests/fixtures/cards_all.json @@ -0,0 +1,836 @@ +{ + "request": { + "params": { + "account_id": "2iqph" + } + }, + "next_cursor": null, + "data": [ + { + "name": "website carousel", + "components": [ + { + "media_key": "13_794652834998325248", + "media_metadata": { + "13_794652834998325248": { + "type": "VIDEO", + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", + "width": 640, + "height": 360, + "video_duration": 7967, + "video_aspect_ratio": "16:9" + } + }, + "type": "MEDIA" + }, + { + "title": "Twitter", + "destination": { + "url": "https://www.dell.de/", + "type": "WEBSITE" + }, + "type": "DETAILS" + } + ], + "id": "1340029888649076737", + "created_at": "2020-12-18T20:23:16Z", + "card_uri": "card://1340029888649076737", + "updated_at": "2021-08-26T19:09:58Z", + "deleted": false, + "card_type": "VIDEO_WEBSITE" + }, + { + "name": "video website card", + "components": [ + { + "media_key": "13_794652834998325248", + "media_metadata": { + "13_794652834998325248": { + "type": "VIDEO", + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", + "width": 640, + "height": 360, + "video_duration": 7967, + "video_aspect_ratio": "16:9" + } + }, + "type": "MEDIA" + }, + { + "title": "Twitter", + "destination": { + "url": "http://twitter.com/", + "type": "WEBSITE" + }, + "type": "DETAILS" + } + ], + "id": "1410461519343591424", + "created_at": "2021-07-01T04:53:25Z", + "card_uri": "card://1410461519343591424", + "updated_at": "2021-08-26T19:09:58Z", + "deleted": false, + "card_type": "VIDEO_WEBSITE" + }, + { + "name": "video website card", + "components": [ + { + "media_key": "13_794652834998325248", + "media_metadata": { + "13_794652834998325248": { + "type": "VIDEO", + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", + "width": 640, + "height": 360, + "video_duration": 7967, + "video_aspect_ratio": "16:9" + } + }, + "type": "MEDIA" + }, + { + "title": "Twitter", + "destination": { + "url": "http://twitter.com/", + "type": "WEBSITE" + }, + "type": "DETAILS" + } + ], + "id": "1410461595289853954", + "created_at": "2021-07-01T04:53:44Z", + "card_uri": "card://1410461595289853954", + "updated_at": "2021-08-26T19:09:58Z", + "deleted": false, + "card_type": "VIDEO_WEBSITE" + }, + { + "name": "video website card", + "components": [ + { + "media_key": "13_794652834998325248", + "media_metadata": { + "13_794652834998325248": { + "type": "VIDEO", + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", + "width": 640, + "height": 360, + "video_duration": 7967, + "video_aspect_ratio": "16:9" + } + }, + "type": "MEDIA" + }, + { + "title": "Twitter", + "destination": { + "url": "http://twitter.com/", + "type": "WEBSITE" + }, + "type": "DETAILS" + } + ], + "id": "1410461798390591491", + "created_at": "2021-07-01T04:54:32Z", + "card_uri": "card://1410461798390591491", + "updated_at": "2021-08-26T19:09:58Z", + "deleted": false, + "card_type": "VIDEO_WEBSITE" + }, + { + "name": "video website card", + "components": [ + { + "media_key": "13_794652834998325248", + "media_metadata": { + "13_794652834998325248": { + "type": "VIDEO", + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", + "width": 640, + "height": 360, + "video_duration": 7967, + "video_aspect_ratio": "16:9" + } + }, + "type": "MEDIA" + }, + { + "title": "Twitter", + "destination": { + "url": "http://twitter.com/", + "type": "WEBSITE" + }, + "type": "DETAILS" + } + ], + "id": "1410461877499359237", + "created_at": "2021-07-01T04:54:51Z", + "card_uri": "card://1410461877499359237", + "updated_at": "2021-08-26T19:09:58Z", + "deleted": false, + "card_type": "VIDEO_WEBSITE" + }, + { + "name": "video website card", + "components": [ + { + "media_key": "13_794652834998325248", + "media_metadata": { + "13_794652834998325248": { + "type": "VIDEO", + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", + "width": 640, + "height": 360, + "video_duration": 7967, + "video_aspect_ratio": "16:9" + } + }, + "type": "MEDIA" + }, + { + "title": "Twitter", + "destination": { + "url": "http://twitter.com/", + "type": "WEBSITE" + }, + "type": "DETAILS" + } + ], + "id": "1410461997494276100", + "created_at": "2021-07-01T04:55:19Z", + "card_uri": "card://1410461997494276100", + "updated_at": "2021-08-26T19:09:58Z", + "deleted": false, + "card_type": "VIDEO_WEBSITE" + }, + { + "name": "video website card", + "components": [ + { + "media_key": "13_794652834998325248", + "media_metadata": { + "13_794652834998325248": { + "type": "VIDEO", + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", + "width": 640, + "height": 360, + "video_duration": 7967, + "video_aspect_ratio": "16:9" + } + }, + "type": "MEDIA" + }, + { + "title": "Twitter", + "destination": { + "url": "http://twitter.com/", + "type": "WEBSITE" + }, + "type": "DETAILS" + } + ], + "id": "1410462120282521603", + "created_at": "2021-07-01T04:55:49Z", + "card_uri": "card://1410462120282521603", + "updated_at": "2021-08-26T19:09:58Z", + "deleted": false, + "card_type": "VIDEO_WEBSITE" + }, + { + "name": "video website card", + "components": [ + { + "media_key": "13_794652834998325248", + "media_metadata": { + "13_794652834998325248": { + "type": "VIDEO", + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", + "width": 640, + "height": 360, + "video_duration": 7967, + "video_aspect_ratio": "16:9" + } + }, + "type": "MEDIA" + }, + { + "title": "Twitter", + "destination": { + "url": "http://twitter.com/", + "type": "WEBSITE" + }, + "type": "DETAILS" + } + ], + "id": "1410462217586102272", + "created_at": "2021-07-01T04:56:12Z", + "card_uri": "card://1410462217586102272", + "updated_at": "2021-08-26T19:09:58Z", + "deleted": false, + "card_type": "VIDEO_WEBSITE" + }, + { + "name": "video website card", + "components": [ + { + "media_key": "13_794652834998325248", + "media_metadata": { + "13_794652834998325248": { + "type": "VIDEO", + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", + "width": 640, + "height": 360, + "video_duration": 7967, + "video_aspect_ratio": "16:9" + } + }, + "type": "MEDIA" + }, + { + "title": "Twitter", + "destination": { + "url": "http://twitter.com/", + "type": "WEBSITE" + }, + "type": "DETAILS" + } + ], + "id": "1410462382632042496", + "created_at": "2021-07-01T04:56:51Z", + "card_uri": "card://1410462382632042496", + "updated_at": "2021-08-26T19:09:58Z", + "deleted": false, + "card_type": "VIDEO_WEBSITE" + }, + { + "name": "video website card", + "components": [ + { + "media_key": "13_794652834998325248", + "media_metadata": { + "13_794652834998325248": { + "type": "VIDEO", + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", + "width": 640, + "height": 360, + "video_duration": 7967, + "video_aspect_ratio": "16:9" + } + }, + "type": "MEDIA" + }, + { + "title": "Twitter", + "destination": { + "url": "http://twitter.com/", + "type": "WEBSITE" + }, + "type": "DETAILS" + } + ], + "id": "1410702122887237632", + "created_at": "2021-07-01T20:49:30Z", + "card_uri": "card://1410702122887237632", + "updated_at": "2021-08-26T19:09:58Z", + "deleted": false, + "card_type": "VIDEO_WEBSITE" + }, + { + "name": "video website card", + "components": [ + { + "media_key": "13_794652834998325248", + "media_metadata": { + "13_794652834998325248": { + "type": "VIDEO", + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", + "width": 640, + "height": 360, + "video_duration": 7967, + "video_aspect_ratio": "16:9" + } + }, + "type": "MEDIA" + }, + { + "title": "Twitter", + "destination": { + "url": "http://twitter.com/", + "type": "WEBSITE" + }, + "type": "DETAILS" + } + ], + "id": "1410702325044301827", + "created_at": "2021-07-01T20:50:18Z", + "card_uri": "card://1410702325044301827", + "updated_at": "2021-08-26T19:09:58Z", + "deleted": false, + "card_type": "VIDEO_WEBSITE" + }, + { + "name": "video website card", + "components": [ + { + "media_key": "13_794652834998325248", + "media_metadata": { + "13_794652834998325248": { + "type": "VIDEO", + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", + "width": 640, + "height": 360, + "video_duration": 7967, + "video_aspect_ratio": "16:9" + } + }, + "type": "MEDIA" + }, + { + "title": "Twitter", + "destination": { + "url": "http://twitter.com/", + "type": "WEBSITE" + }, + "type": "DETAILS" + } + ], + "id": "1410702487212883969", + "created_at": "2021-07-01T20:50:57Z", + "card_uri": "card://1410702487212883969", + "updated_at": "2021-08-26T19:09:58Z", + "deleted": false, + "card_type": "VIDEO_WEBSITE" + }, + { + "name": "video website card", + "components": [ + { + "media_key": "13_794652834998325248", + "media_metadata": { + "13_794652834998325248": { + "type": "VIDEO", + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", + "width": 640, + "height": 360, + "video_duration": 7967, + "video_aspect_ratio": "16:9" + } + }, + "type": "MEDIA" + }, + { + "title": "Twitter", + "destination": { + "url": "http://twitter.com/", + "type": "WEBSITE" + }, + "type": "DETAILS" + } + ], + "id": "1410702742541148162", + "created_at": "2021-07-01T20:51:58Z", + "card_uri": "card://1410702742541148162", + "updated_at": "2021-08-26T19:09:58Z", + "deleted": false, + "card_type": "VIDEO_WEBSITE" + }, + { + "name": "video website card", + "components": [ + { + "media_key": "13_794652834998325248", + "media_metadata": { + "13_794652834998325248": { + "type": "VIDEO", + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", + "width": 640, + "height": 360, + "video_duration": 7967, + "video_aspect_ratio": "16:9" + } + }, + "type": "MEDIA" + }, + { + "title": "Twitter", + "destination": { + "url": "http://twitter.com/", + "type": "WEBSITE" + }, + "type": "DETAILS" + } + ], + "id": "1410703177888849923", + "created_at": "2021-07-01T20:53:41Z", + "card_uri": "card://1410703177888849923", + "updated_at": "2021-08-26T19:09:58Z", + "deleted": false, + "card_type": "VIDEO_WEBSITE" + }, + { + "name": "video website card", + "components": [ + { + "media_key": "13_794652834998325248", + "media_metadata": { + "13_794652834998325248": { + "type": "VIDEO", + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", + "width": 640, + "height": 360, + "video_duration": 7967, + "video_aspect_ratio": "16:9" + } + }, + "type": "MEDIA" + }, + { + "title": "Twitter", + "destination": { + "url": "http://twitter.com/", + "type": "WEBSITE" + }, + "type": "DETAILS" + } + ], + "id": "1410703377667821568", + "created_at": "2021-07-01T20:54:29Z", + "card_uri": "card://1410703377667821568", + "updated_at": "2021-08-26T19:09:58Z", + "deleted": false, + "card_type": "VIDEO_WEBSITE" + }, + { + "name": "new card pytest", + "components": [ + { + "media_key": "13_794652834998325248", + "media_metadata": { + "13_794652834998325248": { + "type": "VIDEO", + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", + "width": 640, + "height": 360, + "video_duration": 7967, + "video_aspect_ratio": "16:9" + } + }, + "type": "MEDIA" + }, + { + "title": "Twitter", + "destination": { + "url": "http://twitter.com/", + "type": "WEBSITE" + }, + "type": "DETAILS" + } + ], + "id": "1502039535651196928", + "created_at": "2022-03-10T21:51:46Z", + "card_uri": "card://1502039535651196928", + "updated_at": "2022-03-10T21:51:46Z", + "deleted": false, + "card_type": "VIDEO_WEBSITE" + }, + { + "name": "new card pytest", + "components": [ + { + "media_key": "13_794652834998325248", + "media_metadata": { + "13_794652834998325248": { + "type": "VIDEO", + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", + "width": 640, + "height": 360, + "video_duration": 7967, + "video_aspect_ratio": "16:9" + } + }, + "type": "MEDIA" + }, + { + "title": "Twitter", + "destination": { + "url": "http://twitter.com/", + "type": "WEBSITE" + }, + "type": "DETAILS" + } + ], + "id": "1502039980394254337", + "created_at": "2022-03-10T21:53:32Z", + "card_uri": "card://1502039980394254337", + "updated_at": "2022-03-10T21:53:32Z", + "deleted": false, + "card_type": "VIDEO_WEBSITE" + }, + { + "name": "new card pytest", + "components": [ + { + "media_key": "13_794652834998325248", + "media_metadata": { + "13_794652834998325248": { + "type": "VIDEO", + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", + "width": 640, + "height": 360, + "video_duration": 7967, + "video_aspect_ratio": "16:9" + } + }, + "type": "MEDIA" + }, + { + "title": "Twitter", + "destination": { + "url": "http://twitter.com/newvalue", + "type": "WEBSITE" + }, + "type": "DETAILS" + } + ], + "id": "1502039983049232388", + "created_at": "2022-03-10T21:53:33Z", + "card_uri": "card://1502039983049232388", + "updated_at": "2022-03-10T21:53:33Z", + "deleted": false, + "card_type": "VIDEO_WEBSITE" + }, + { + "name": "new card pytest", + "components": [ + { + "media_key": "13_794652834998325248", + "media_metadata": { + "13_794652834998325248": { + "type": "VIDEO", + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", + "width": 640, + "height": 360, + "video_duration": 7967, + "video_aspect_ratio": "16:9" + } + }, + "type": "MEDIA" + }, + { + "title": "Twitter", + "destination": { + "url": "http://twitter.com/", + "type": "WEBSITE" + }, + "type": "DETAILS" + } + ], + "id": "1502039996441628673", + "created_at": "2022-03-10T21:53:36Z", + "card_uri": "card://1502039996441628673", + "updated_at": "2022-03-10T21:53:36Z", + "deleted": false, + "card_type": "VIDEO_WEBSITE" + }, + { + "name": "new card pytest", + "components": [ + { + "media_key": "13_794652834998325248", + "media_metadata": { + "13_794652834998325248": { + "type": "VIDEO", + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", + "width": 640, + "height": 360, + "video_duration": 7967, + "video_aspect_ratio": "16:9" + } + }, + "type": "MEDIA" + }, + { + "title": "Twitter", + "destination": { + "url": "http://twitter.com/newvalue", + "type": "WEBSITE" + }, + "type": "DETAILS" + } + ], + "id": "1502039998987587584", + "created_at": "2022-03-10T21:53:36Z", + "card_uri": "card://1502039998987587584", + "updated_at": "2022-03-10T21:53:36Z", + "deleted": false, + "card_type": "VIDEO_WEBSITE" + }, + { + "name": "my new card", + "components": [ + { + "media_key": "13_794652834998325248", + "media_metadata": { + "13_794652834998325248": { + "type": "VIDEO", + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", + "width": 640, + "height": 360, + "video_duration": 7967, + "video_aspect_ratio": "16:9" + } + }, + "type": "MEDIA" + }, + { + "title": "Twitter", + "destination": { + "url": "http://twitter.com/login", + "type": "WEBSITE" + }, + "type": "DETAILS" + } + ], + "id": "1503829009708175360", + "created_at": "2022-03-15T20:22:30Z", + "card_uri": "card://1503829009708175360", + "updated_at": "2022-03-15T20:22:30Z", + "deleted": false, + "card_type": "VIDEO_WEBSITE" + }, + { + "name": "my new card", + "components": [ + { + "media_key": "13_794652834998325248", + "media_metadata": { + "13_794652834998325248": { + "type": "VIDEO", + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", + "width": 640, + "height": 360, + "video_duration": 7967, + "video_aspect_ratio": "16:9" + } + }, + "type": "MEDIA" + }, + { + "title": "Twitter", + "destination": { + "url": "http://twitter.com/login", + "type": "WEBSITE" + }, + "type": "DETAILS" + } + ], + "id": "1503829523753697280", + "created_at": "2022-03-15T20:24:32Z", + "card_uri": "card://1503829523753697280", + "updated_at": "2022-03-15T20:24:32Z", + "deleted": false, + "card_type": "VIDEO_WEBSITE" + }, + { + "name": "my new card", + "components": [ + { + "media_key": "13_794652834998325248", + "media_metadata": { + "13_794652834998325248": { + "type": "VIDEO", + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", + "width": 640, + "height": 360, + "video_duration": 7967, + "video_aspect_ratio": "16:9" + } + }, + "type": "MEDIA" + }, + { + "title": "Twitter", + "destination": { + "url": "http://twitter.com/login", + "type": "WEBSITE" + }, + "type": "DETAILS" + } + ], + "id": "1503829695233617920", + "created_at": "2022-03-15T20:25:13Z", + "card_uri": "card://1503829695233617920", + "updated_at": "2022-03-15T20:25:13Z", + "deleted": false, + "card_type": "VIDEO_WEBSITE" + }, + { + "name": "my new card", + "components": [ + { + "media_key": "13_794652834998325248", + "media_metadata": { + "13_794652834998325248": { + "type": "VIDEO", + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", + "width": 640, + "height": 360, + "video_duration": 7967, + "video_aspect_ratio": "16:9" + } + }, + "type": "MEDIA" + }, + { + "title": "Twitter", + "destination": { + "url": "http://twitter.com/login", + "type": "WEBSITE" + }, + "type": "DETAILS" + } + ], + "id": "1503829883746598912", + "created_at": "2022-03-15T20:25:58Z", + "card_uri": "card://1503829883746598912", + "updated_at": "2022-03-15T20:25:58Z", + "deleted": false, + "card_type": "VIDEO_WEBSITE" + }, + { + "name": "my new card", + "components": [ + { + "media_key": "13_794652834998325248", + "media_metadata": { + "13_794652834998325248": { + "type": "VIDEO", + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", + "width": 640, + "height": 360, + "video_duration": 7967, + "video_aspect_ratio": "16:9" + } + }, + "type": "MEDIA" + }, + { + "title": "Twitter", + "destination": { + "url": "http://twitter.com/login", + "type": "WEBSITE" + }, + "type": "DETAILS" + } + ], + "id": "1503831318555086849", + "created_at": "2022-03-15T20:31:40Z", + "card_uri": "card://1503831318555086849", + "updated_at": "2022-03-15T20:31:40Z", + "deleted": false, + "card_type": "VIDEO_WEBSITE" + } + ] + } + \ No newline at end of file diff --git a/tests/fixtures/cards_load.json b/tests/fixtures/cards_load.json new file mode 100644 index 0000000..502f874 --- /dev/null +++ b/tests/fixtures/cards_load.json @@ -0,0 +1,42 @@ +{ + "request": { + "params": { + "account_id": "2iqph", + "card_id": "1503831318555086849" + } + }, + "data": { + "name": "my new card", + "components": [ + { + "media_key": "13_794652834998325248", + "media_metadata": { + "13_794652834998325248": { + "type": "VIDEO", + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", + "width": 640, + "height": 360, + "video_duration": 7967, + "video_aspect_ratio": "16:9" + } + }, + "type": "MEDIA" + }, + { + "title": "Twitter", + "destination": { + "url": "http://twitter.com/login", + "type": "WEBSITE" + }, + "type": "DETAILS" + } + ], + "id": "1503831318555086849", + "created_at": "2022-03-15T20:31:40Z", + "card_uri": "card://1503831318555086849", + "updated_at": "2022-03-15T20:31:40Z", + "deleted": false, + "card_type": "VIDEO_WEBSITE" + } + } + \ No newline at end of file diff --git a/tests/test_cards.py b/tests/test_cards.py new file mode 100644 index 0000000..f168b62 --- /dev/null +++ b/tests/test_cards.py @@ -0,0 +1,69 @@ +import responses +import unittest + +from tests.support import with_resource, with_fixture, characters + +from twitter_ads.account import Account +from twitter_ads.campaign import Campaign +from twitter_ads.creative import Card +from twitter_ads.client import Client +from twitter_ads.cursor import Cursor +from twitter_ads import API_VERSION + + +@responses.activate +def test_cards_all(): + responses.add(responses.GET, + with_resource('/' + API_VERSION + '/accounts/2iqph'), + body=with_fixture('accounts_load'), + content_type='application/json') + + responses.add(responses.GET, + with_resource('/' + API_VERSION + '/accounts/2iqph/cards'), + body=with_fixture('cards_all'), + content_type='application/json') + + client = Client( + characters(40), + characters(40), + characters(40), + characters(40) + ) + + account = Account.load(client, '2iqph') + + cursor = Card.all(account) + assert cursor is not None + assert isinstance(cursor, Cursor) + + card = cursor.next() + assert card.id == '1340029888649076737' + assert card.card_type == 'VIDEO_WEBSITE' + assert len(card.components) == 2 + + +@responses.activate +def test_card_load(): + responses.add(responses.GET, + with_resource('/' + API_VERSION + '/accounts/2iqph'), + body=with_fixture('accounts_load'), + content_type='application/json') + + responses.add(responses.GET, + with_resource('/' + API_VERSION + '/accounts/2iqph/cards/1503831318555086849'), + body=with_fixture('cards_load'), + content_type='application/json') + + client = Client( + characters(40), + characters(40), + characters(40), + characters(40) + ) + + account = Account.load(client, '2iqph') + + card = Card.load(account, '1503831318555086849') + assert card.id == '1503831318555086849' + assert card.card_type == 'VIDEO_WEBSITE' + assert card.card_uri == 'card://1503831318555086849' diff --git a/twitter_ads/creative.py b/twitter_ads/creative.py index ab150f0..52cf24e 100644 --- a/twitter_ads/creative.py +++ b/twitter_ads/creative.py @@ -596,34 +596,44 @@ class Card(Resource): PROPERTIES = {} + RESOURCE = '/' + API_VERSION + '/accounts/{account_id}/cards/{id}' RESOURCE_COLLECTION = '/' + API_VERSION + '/accounts/{account_id}/cards' - @classmethod - def create(klass, account, name, components): - method = 'post' - resource = klass.RESOURCE_COLLECTION.format(account_id=account.id) + def save(self): + if self.id: + method = 'put' + resource = self.RESOURCE.format(account_id=self.account.id, id=self.id) + else: + method = 'post' + resource = self.RESOURCE_COLLECTION.format(account_id=self.account.id) + headers = {'Content-Type': 'application/json'} - payload = {'name': name, 'components': components} - response = Request( - account.client, method, resource, - headers=headers, body=json.dumps(payload) - ).perform() - return klass(account).from_response(response.body['data']) + payload = {'name': self.name, 'components': self.components} + response = Request(self.account.client, method, resource, headers=headers, + body=json.dumps(payload) + ).perform() + return self.from_response(response.body['data']) - def load(klass): - raise AttributeError("'Card' object has no attribute 'load'") + @classmethod + @FlattenParams + def load(klass, account, id, **kwargs): + resource = klass.RESOURCE.format(account_id=account.id, id=id) + response = Request(account.client, 'get', resource, params=kwargs).perform() + return klass(account).from_response(response.body['data']) - def reload(klass): - raise AttributeError("'Card' object has no attribute 'reload'") + def reload(self): + if self.id: + self.load(self.account, card_id=self.id) # card properties # read-only +resource_property(Card, 'id', readonly=True) resource_property(Card, 'card_uri', readonly=True) resource_property(Card, 'card_type', readonly=True) resource_property(Card, 'created_at', readonly=True, transform=TRANSFORM.TIME) resource_property(Card, 'deleted', readonly=True, transform=TRANSFORM.BOOL) resource_property(Card, 'updated_at', readonly=True, transform=TRANSFORM.TIME) -# these are writable, but not in the sense that they can be set on an object and then saved -resource_property(Card, 'name', readonly=True) -resource_property(Card, 'components', readonly=True, transform=TRANSFORM.LIST) +# these are writable +resource_property(Card, 'name') +resource_property(Card, 'components', transform=TRANSFORM.LIST)