Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- def format_url_keys(name, items):
- return ['{name}[{x}]'.format(name=name, x=x) for x in items]
- def get_formatted_fields(name, keys, values=None):
- values = values is not None and values or keys
- return dict(zip(format_url_keys(name, keys), values))
- api_url = "http://mailer.blah.com.au/admin/api.php"
- api_key = "..."
- # define the request paramaters for a 'subscriber_view' call
- # the 'id' is an email
- view_params = {'api_key': api_key, 'api_action': 'subscriber_view',
- 'api_output': 'json', 'id': user.email}
- # do our GET call to the API
- r = requests.get(api_url, params=view_params)
- # get the returned JSON as dict
- subscriber = r.json()
- # if the subscriber dict doesn't have 'lists' then it is not subscribed to anything
- if not 'lists' in subscriber:
- add_params = {'api_key': api_key, 'api_action': 'subscriber_add',
- 'api_output': 'json'}
- # the payload should be the subscriber details and any lists
- add_payload = {'email': user.email,
- 'first_name': user.firstName,
- 'last_name': user.lastName}
- # this bit is confusing, AC has a very weird way of handling listids and status
- # listids is just a list of ids as strings and yep both p and status are required
- add_payload.update(get_formatted_fields('p', listids, listids))
- add_payload.update(get_formatted_fields('status', listids, [1] * len(listids)))
- # finally do the call
- r = requests.post(api_url, params=add_params, data=add_payload)
- result = r.json()
- # any logging or result verification etc.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement