2
2
3
3
"""Container for all audience management logic used by the Ads API SDK."""
4
4
5
- from twitter_ads .enum import TA_OPERATIONS , TRANSFORM
5
+ from twitter_ads .enum import TRANSFORM
6
6
from twitter_ads .resource import resource_property , Resource
7
- from twitter_ads .http import TONUpload , Request
7
+ from twitter_ads .http import Request
8
8
from twitter_ads .error import BadRequest
9
9
from twitter_ads .cursor import Cursor
10
10
from twitter_ads import API_VERSION
15
15
class TailoredAudience (Resource ):
16
16
17
17
PROPERTIES = {}
18
-
19
18
RESOURCE_COLLECTION = '/' + API_VERSION + '/accounts/{account_id}/tailored_audiences'
20
19
RESOURCE = '/' + API_VERSION + '/accounts/{account_id}/tailored_audiences/{id}'
21
- RESOURCE_UPDATE = '/' + API_VERSION + '/accounts/{account_id}/tailored_audience_changes'
20
+ RESOURCE_USERS = '/' + API_VERSION + '/accounts/{account_id}/tailored_audiences/\
21
+ {id}/users'
22
22
RESOURCE_PERMISSIONS = '/' + API_VERSION + '/accounts/{account_id}/tailored_audiences/\
23
23
{id}/permissions'
24
- OPT_OUT = '/' + API_VERSION + '/accounts/{account_id}/tailored_audiences/global_opt_out'
25
24
26
25
@classmethod
27
- def create (klass , account , file_path , name , list_type ):
26
+ def create (klass , account , name ):
28
27
"""
29
- Uploads and creates a new tailored audience.
28
+ Creates a new tailored audience.
30
29
"""
31
- upload = TONUpload (account .client , file_path )
32
30
audience = klass (account )
33
- getattr (audience , '__create_audience__' )(name , list_type )
31
+ getattr (audience , '__create_audience__' )(name )
34
32
try :
35
- getattr (audience , '__update_audience__' )(upload .perform (), list_type , TA_OPERATIONS .ADD )
36
33
return audience .reload ()
37
34
except BadRequest as e :
38
35
audience .delete ()
39
36
raise e
40
37
41
- @classmethod
42
- def opt_out (klass , account , file_path , list_type ):
38
+ def users (self , params ):
43
39
"""
44
- Updates the global opt-out list for the specified advertiser account.
40
+ This is a private API and requires whitelisting from Twitter.
41
+ This endpoint will allow partners to add, update and remove users from a given
42
+ tailored_audience_id.
43
+ The endpoint will also accept multiple user identifier types per user as well.
45
44
"""
46
- upload = TONUpload (account .client , file_path )
47
- params = {'input_file_path' : upload .perform (), 'list_type' : list_type }
48
- resource = klass .OPT_OUT .format (account_id = account .id )
49
- Request (account .client , 'put' , resource , params = params ).perform ()
50
- return True
51
-
52
- def update (self , file_path , list_type , operation = TA_OPERATIONS .ADD ):
53
- """
54
- Updates the current tailored audience instance.
55
- """
56
- upload = TONUpload (self .account .client , file_path )
57
- getattr (self , '__update_audience__' )(upload .perform (), list_type , operation )
58
- return self .reload ()
45
+ resource = self .RESOURCE_USERS .format (account_id = self .account .id , id = self .id )
46
+ headers = {'Content-Type' : 'application/json' }
47
+ response = Request (self .account .client ,
48
+ 'post' ,
49
+ resource ,
50
+ headers = headers ,
51
+ body = json .dumps (params )).perform ()
52
+ success_count = response .body ['data' ]['success_count' ]
53
+ total_count = response .body ['data' ]['total_count' ]
54
+ return (success_count , total_count )
59
55
60
56
def delete (self ):
61
57
"""
@@ -65,43 +61,19 @@ def delete(self):
65
61
response = Request (self .account .client , 'delete' , resource ).perform ()
66
62
return self .from_response (response .body ['data' ])
67
63
68
- def status (self ):
69
- """
70
- Returns the status of all changes for the current tailored audience instance.
71
- """
72
- if not self .id :
73
- return None
74
-
75
- resource = self .RESOURCE_UPDATE .format (account_id = self .account .id )
76
- request = Request (self .account .client , 'get' , resource , params = self .to_params ())
77
- cursor = list (Cursor (None , request ))
78
-
79
- return filter (lambda change : change ['tailored_audience_id' ] == self .id , cursor )
80
-
81
64
def permissions (self , ** kwargs ):
82
65
"""
83
66
Returns a collection of permissions for the curent tailored audience.
84
67
"""
85
68
self ._validate_loaded ()
86
69
return TailoredAudiencePermission .all (self .account , self .id , ** kwargs )
87
70
88
- def __create_audience__ (self , name , list_type ):
89
- params = {'name' : name , 'list_type' : list_type }
71
+ def __create_audience__ (self , name ):
72
+ params = {'name' : name }
90
73
resource = self .RESOURCE_COLLECTION .format (account_id = self .account .id )
91
74
response = Request (self .account .client , 'post' , resource , params = params ).perform ()
92
75
return self .from_response (response .body ['data' ])
93
76
94
- def __update_audience__ (self , location , list_type , operation ):
95
- params = {
96
- 'tailored_audience_id' : self .id ,
97
- 'input_file_path' : location ,
98
- 'list_type' : list_type ,
99
- 'operation' : operation
100
- }
101
-
102
- resource = self .RESOURCE_UPDATE .format (account_id = self .account .id )
103
- return Request (self .account .client , 'post' , resource , params = params ).perform ()
104
-
105
77
106
78
# tailored audience properties
107
79
# read-only
0 commit comments