Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 328dbcf

Browse files
committedJul 30, 2019
introduce PromotedTweet.attach() method
- and deprecate .save() method
1 parent 33c5c4b commit 328dbcf

File tree

3 files changed

+67
-14
lines changed

3 files changed

+67
-14
lines changed
 
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"data_type": "promoted_tweet",
3+
"data": [
4+
{
5+
"line_item_id": "2b7xw",
6+
"id": "6thl4",
7+
"entity_status": "ACTIVE",
8+
"created_at": "2015-04-11T20:50:25Z",
9+
"updated_at": "2015-04-11T20:50:25Z",
10+
"approval_status": "ACCEPTED",
11+
"tweet_id": "585127452231467008",
12+
"deleted": false
13+
}
14+
],
15+
"request": {
16+
"params": {
17+
"line_item_id": "2b7xw",
18+
"tweet_ids": [
19+
585127452231467008
20+
],
21+
"account_id": "2iqph"
22+
}
23+
},
24+
"total_count": 1
25+
}

‎tests/test_promoted_tweets.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,34 @@ def test_promoted_tweets_load():
6565
promoted_tweet = PromotedTweet.load(account, '6thl4')
6666
assert promoted_tweet.id == '6thl4'
6767
assert promoted_tweet.entity_status == 'ACTIVE'
68+
69+
70+
@responses.activate
71+
def test_promoted_tweets_attach():
72+
responses.add(responses.GET,
73+
with_resource('/' + API_VERSION + '/accounts/2iqph'),
74+
body=with_fixture('accounts_load'),
75+
content_type='application/json')
76+
77+
responses.add(responses.POST,
78+
with_resource('/' + API_VERSION + '/accounts/2iqph/promoted_tweets'),
79+
body=with_fixture('promoted_tweets_attach'),
80+
content_type='application/json')
81+
82+
client = Client(
83+
characters(40),
84+
characters(40),
85+
characters(40),
86+
characters(40)
87+
)
88+
89+
account = Account.load(client, '2iqph')
90+
response = PromotedTweet.attach(
91+
account,
92+
line_item_id='2b7xw',
93+
tweet_ids=['585127452231467008']
94+
)
95+
96+
assert isinstance(response, Cursor)
97+
assert response.count == 1
98+
assert response.first.id == '6thl4'

‎twitter_ads/creative.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,18 @@ class PromotedTweet(Resource, Persistence, Analytics):
3939
RESOURCE_COLLECTION = '/' + API_VERSION + '/accounts/{account_id}/promoted_tweets'
4040
RESOURCE = '/' + API_VERSION + '/accounts/{account_id}/promoted_tweets/{id}'
4141

42-
def save(self):
42+
@classmethod
43+
def attach(klass, account, line_item_id=None, tweet_ids=None):
4344
"""
44-
Saves or updates the current object instance depending on the
45-
presence of `object.id`.
45+
Associate one or more Tweets with the specified line item.
4646
"""
47-
params = self.to_params()
48-
if 'tweet_id' in params:
49-
params['tweet_ids'] = [params['tweet_id']]
50-
del params['tweet_id']
51-
52-
if self.id:
53-
raise HTTPError("Method PUT not allowed.")
47+
params = {}
48+
params['line_item_id'] = line_item_id
49+
params['tweet_ids'] = ",".join(map(str, tweet_ids))
5450

55-
resource = self.RESOURCE_COLLECTION.format(account_id=self.account.id)
56-
response = Request(self.account.client, 'post', resource, params=params).perform()
57-
return self.from_response(response.body['data'][0])
51+
resource = klass.RESOURCE_COLLECTION.format(account_id=account.id)
52+
request = Request(account.client, 'post', resource, params=params)
53+
return Cursor(klass, request, init_with=[account])
5854

5955

6056
# promoted tweet properties
@@ -65,9 +61,10 @@ def save(self):
6561
resource_property(PromotedTweet, 'entity_status', readonly=True)
6662
resource_property(PromotedTweet, 'id', readonly=True)
6763
resource_property(PromotedTweet, 'updated_at', readonly=True, transform=TRANSFORM.TIME)
64+
resource_property(PromotedTweet, 'tweet_id', readonly=True)
6865
# writable
6966
resource_property(PromotedTweet, 'line_item_id')
70-
resource_property(PromotedTweet, 'tweet_id') # SDK limitation
67+
resource_property(PromotedTweet, 'tweet_ids')
7168

7269

7370
class AccountMedia(Resource, Persistence):

0 commit comments

Comments
 (0)
Please sign in to comment.