Skip to content

Commit f6e467e

Browse files
author
Brandon Black
committed
[minor] DRY-ing up resource subclasses a bit
1 parent 3662b7a commit f6e467e

File tree

6 files changed

+14
-120
lines changed

6 files changed

+14
-120
lines changed

bin/twitter-ads

+2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import atexit
1010

1111
try:
1212
import twitter_ads
13+
print('[INFO] using pip installed twitter-ads.')
1314
except ImportError:
1415
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
1516
import twitter_ads
17+
print('[INFO] using local clone of twitter-ads.')
1618

1719
from twitter_ads.client import Client
1820

twitter_ads/account.py

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ def __init__(self, client):
3333
def client(self):
3434
return self._client
3535

36+
@property
37+
def account(self):
38+
return NotImplementedError
39+
3640
@classmethod
3741
def load(klass, client, id, **kwargs):
3842
"""Returns an object instance for a given resource."""

twitter_ads/audience.py

-7
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@ class TailoredAudience(Resource):
1313
RESOURCE_UPDATE = '/0/accounts/{account_id}/tailored_audience_changes'
1414
OPT_OUT = '/0/accounts/{account_id}/tailored_audiences/global_opt_out'
1515

16-
def __init__(self, account):
17-
self._account = account
18-
19-
@property
20-
def account(self):
21-
return self._account
22-
2316
@classmethod
2417
def create(klass, account, file_path, name, list_type):
2518
raise NotImplementedError

twitter_ads/campaign.py

+1-43
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@ class TargetingCriteria(Resource, Persistence):
1313
RESOURCE_COLLECTION = '/0/accounts/{account_id}/targeting_criteria'
1414
RESOURCE = '/0/accounts/{account_id}/targeting_criteria/{id}'
1515

16-
def __init__(self, account):
17-
self._account = account
18-
19-
@property
20-
def account(self):
21-
return self._account
22-
2316
@classmethod
2417
def all(klass, account, line_item_id, **kwargs):
2518
"""Returns a Cursor instance for a given resource."""
@@ -51,13 +44,6 @@ class FundingInstrument(Resource, Persistence):
5144
RESOURCE_COLLECTION = '/0/accounts/{account_id}/funding_instruments'
5245
RESOURCE = '/0/accounts/{account_id}/funding_instruments/{id}'
5346

54-
def __init__(self, account):
55-
self._account = account
56-
57-
@property
58-
def account(self):
59-
return self._account
60-
6147
# funding instrument properties
6248
# read-only
6349
resource_property(FundingInstrument, 'id', readonly=True)
@@ -78,13 +64,6 @@ class PromotableUser(Resource):
7864
RESOURCE_COLLECTION = '/0/accounts/{account_id}/promotable_users'
7965
RESOURCE = '/0/accounts/{account_id}/promotable_users/{id}'
8066

81-
def __init__(self, account):
82-
self._account = account
83-
84-
@property
85-
def account(self):
86-
return self._account
87-
8867
# promotable user properties
8968
# read-only
9069
resource_property(PromotableUser, 'id', readonly=True)
@@ -100,13 +79,6 @@ class AppList(Resource, Persistence):
10079
RESOURCE_COLLECTION = '/0/accounts/{account_id}/app_lists'
10180
RESOURCE = '/0/accounts/{account_id}/app_lists/{id}'
10281

103-
def __init__(self, account):
104-
self._account = account
105-
106-
@property
107-
def account(self):
108-
return self._account
109-
11082
def create(self, name, *ids):
11183
if isinstance(ids, list):
11284
ids = ','.join(map(str, ids))
@@ -135,13 +107,6 @@ class Campaign(Resource, Persistence):
135107
RESOURCE_STATS = '/0/stats/accounts/{account_id}/campaigns'
136108
RESOURCE = '/0/accounts/{account_id}/campaigns/{id}'
137109

138-
def __init__(self, account):
139-
self._account = account
140-
141-
@property
142-
def account(self):
143-
return self._account
144-
145110
# campaign properties
146111
# read-only
147112
resource_property(Campaign, 'id', readonly=True)
@@ -168,13 +133,6 @@ class LineItem(Resource, Persistence, Analytics):
168133
RESOURCE_STATS = '/0/stats/accounts/{account_id}/line_items'
169134
RESOURCE = '/0/accounts/{account_id}/line_items/{id}'
170135

171-
def __init__(self, account):
172-
self._account = account
173-
174-
@property
175-
def account(self):
176-
return self._account
177-
178136
def targeting_criteria(self, id=None, **kwargs):
179137
"""
180138
Returns a collection of targeting criteria available to the
@@ -218,7 +176,7 @@ class Tweet(object):
218176
TWEET_CREATE = '/0/accounts/{account_id}/tweet'
219177

220178
def __init__(self):
221-
raise StandardError(
179+
raise NotImplementedError(
222180
'Error! {name} cannot be instantiated.'.format(name=self.__class__.__name__))
223181

224182
@classmethod

twitter_ads/creative.py

-70
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@ class PromotedAccount(Resource, Persistence):
1313
RESOURCE_STATS = '/0/stats/accounts/{account_id}/promoted_accounts'
1414
RESOURCE = '/0/accounts/{account_id}/promoted_accounts/{id}'
1515

16-
def __init__(self, account):
17-
self._account = account
18-
19-
@property
20-
def account(self):
21-
return self._account
22-
2316
# promoted account properties
2417
# read-only
2518
resource_property(PromotedAccount, 'id', readonly=True)
@@ -39,13 +32,6 @@ class PromotedTweet(Resource, Persistence, Analytics):
3932
RESOURCE_STATS = '/0/stats/accounts/{account_id}/promoted_tweets'
4033
RESOURCE = '/0/accounts/{account_id}/promoted_tweets/{id}'
4134

42-
def __init__(self, account):
43-
self._account = account
44-
45-
@property
46-
def account(self):
47-
return self._account
48-
4935
def save(self):
5036
"""
5137
Saves or updates the current object instance depending on the
@@ -87,13 +73,6 @@ class Video(Resource, Persistence):
8773
RESOURCE_COLLECTION = '/0/accounts/{account_id}/videos'
8874
RESOURCE = '/0/accounts/{account_id}/videos/{id}'
8975

90-
def __init__(self, account):
91-
self._account = account
92-
93-
@property
94-
def account(self):
95-
return self._account
96-
9776
# video properties
9877
# read-only
9978
resource_property(Video, 'id', readonly=True)
@@ -116,13 +95,6 @@ class WebsiteCard(Resource, Persistence):
11695
RESOURCE_COLLECTION = '/0/accounts/{account_id}/cards/website'
11796
RESOURCE = '/0/accounts/{account_id}/cards/website/{id}'
11897

119-
def __init__(self, account):
120-
self._account = account
121-
122-
@property
123-
def account(self):
124-
return self._account
125-
12698
# website card properties
12799
# read-only
128100
resource_property(WebsiteCard, 'id', readonly=True)
@@ -143,13 +115,6 @@ class LeadGenCard(Resource, Persistence):
143115
RESOURCE_COLLECTION = '/0/accounts/{account_id}/cards/lead_gen'
144116
RESOURCE = '/0/accounts/{account_id}/cards/lead_gen/{id}'
145117

146-
def __init__(self, account):
147-
self._account = account
148-
149-
@property
150-
def account(self):
151-
return self._account
152-
153118
# lead gen card properties
154119
# read-only
155120
resource_property(LeadGenCard, 'id', readonly=True)
@@ -178,13 +143,6 @@ class AppDownloadCard(Resource, Persistence):
178143
RESOURCE_COLLECTION = '/0/accounts/{account_id}/cards/app_download'
179144
RESOURCE = '/0/accounts/{account_id}/cards/app_download/{id}'
180145

181-
def __init__(self, account):
182-
self._account = account
183-
184-
@property
185-
def account(self):
186-
return self._account
187-
188146
# app download card properties
189147
# read-only
190148
resource_property(AppDownloadCard, 'id', readonly=True)
@@ -211,13 +169,6 @@ class ImageAppDownloadCard(Resource, Persistence):
211169
RESOURCE_COLLECTION = '/0/accounts/{account_id}/cards/image_app_download'
212170
RESOURCE = '/0/accounts/{account_id}/cards/image_app_download/{id}'
213171

214-
def __init__(self, account):
215-
self._account = account
216-
217-
@property
218-
def account(self):
219-
return self._account
220-
221172
# image app download card properties
222173
# read-only
223174
resource_property(ImageAppDownloadCard, 'id', readonly=True)
@@ -243,13 +194,6 @@ class VideoAppDownloadCard(Resource, Persistence):
243194
RESOURCE_COLLECTION = '/0/accounts/{account_id}/cards/video_app_download'
244195
RESOURCE = '/0/accounts/{account_id}/cards/video_app_download/{id}'
245196

246-
def __init__(self, account):
247-
self._account = account
248-
249-
@property
250-
def account(self):
251-
return self._account
252-
253197
# video app download card properties
254198
# read-only
255199
resource_property(VideoAppDownloadCard, 'id', readonly=True)
@@ -278,13 +222,6 @@ class ImageConversationCard(Resource, Persistence):
278222
RESOURCE_COLLECTION = '/0/accounts/{account_id}/cards/image_conversation'
279223
RESOURCE = '/0/accounts/{account_id}/cards/image_conversation/{id}'
280224

281-
def __init__(self, account):
282-
self._account = account
283-
284-
@property
285-
def account(self):
286-
return self._account
287-
288225
# image conversation card properties
289226
# read-only
290227
resource_property(ImageConversationCard, 'id', readonly=True)
@@ -309,13 +246,6 @@ class VideoConversationCard(Resource, Persistence):
309246
RESOURCE_COLLECTION = '/0/accounts/{account_id}/cards/video_conversation'
310247
RESOURCE = '/0/accounts/{account_id}/cards/video_conversation/{id}'
311248

312-
def __init__(self, account):
313-
self._account = account
314-
315-
@property
316-
def account(self):
317-
return self._account
318-
319249
# video conversation card properties
320250
# read-only
321251
resource_property(VideoConversationCard, 'id', readonly=True)

twitter_ads/resource.py

+7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ class Resource(object):
3131

3232
PROPERTIES = {}
3333

34+
def __init__(self, account):
35+
self._account = account
36+
37+
@property
38+
def account(self):
39+
return self._account
40+
3441
def from_response(self, response):
3542
"""
3643
Populates a given objects attributes from a parsed JSON API response.

0 commit comments

Comments
 (0)