def subscriber(self, group_id, subscriber_id, as_json=False): """Get one subscriber in a specified group. https://developers.mailerlite.com/v2/reference#a-subscriber-of-a-group Parameters ---------- group_id : int group id subscriber_id : int subscriber id as_json : bool return result as json format Returns ------- subscriber: :class:Subscriber a single subscriber """ url = client.build_url('groups', group_id, 'subscribers', subscriber_id) _, res_json = client.get(url, headers=self.headers) if as_json or not res_json: return res_json res_json['fields'] = [Field(**res) for res in res_json['fields']] return Subscriber(**res_json)
def update(self, field_id, title, as_json=False): """Update custom field in account. https://developers.mailerlite.com/v2/reference#update-field Parameters ---------- field_id : int field id title : str Title of field Returns ------- field : :class:Field field object updated """ url = client.build_url('fields', field_id) body = {"title": title} _, res_json = client.put(url, body=body, headers=self.headers) if as_json or not res_json: return res_json return Field(**res_json)
def update(self, data, as_json=False, **identifier): """Update single subscriber. https://developers.mailerlite.com/v2/reference#update-subscriber Parameters ---------- data : dict subscriber object. only the email is required. you can use the following example: data = {'name' : 'John', 'fields' : {'company': 'MailerLite'} } as_json : bool return result as json format identifier : str should be subscriber id or email. e.g: id=1343965485 or email='*****@*****.**' Returns ------- response : int response value content : dict The JSON output from the API Notes ----- The email of a subscriber can not be updated """ path = get_id_or_email_identifier(**identifier) if path is None: raise IOError('An identifier must be define') if 'email' in data.keys(): raise ValueError("Subscriber email can not be updated. Please, " "remove this field or create a new Subscriber. " "For more informations, look at " "http://help.mailerlite.com/article/show" "/29233-how-to-edit-a-subscribers-data") optional_keys = ['name', 'type', 'fields', 'resend_autoresponders'] unknown_keys = [ d for d in data.keys() if d not in optional_keys if d not in ['groups', 'segments'] ] if unknown_keys: raise ValueError( "The following keys are unknown: {}".format(unknown_keys)) url = client.build_url('subscribers', path) _, res_json = client.put(url, body=data, headers=self.headers) if as_json or not res_json: return res_json res_json['fields'] = [Field(**res) for res in res_json['fields']] return Subscriber(**res_json)
def get(self, as_json=False, **identifier): """Get a single subscriber from your account. https://developers.mailerlite.com/v2/reference#single-subscriber Parameters ---------- identifier : str should be subscriber id or email. e.g: id=1343965485 or email='*****@*****.**' as_json : bool return result as json format Returns ------- subscriber: :class:Subscriber a single subscriber """ path = get_id_or_email_identifier(**identifier) if path is None: raise IOError('An identifier must be define') url = client.build_url('subscribers', path) _, res_json = client.get(url, headers=self.headers) if as_json or not res_json: return res_json res_json['fields'] = [Field(**res) for res in res_json['fields']] return Subscriber(**res_json)
def subscribers(self, group_id, limit=100, offset=0, stype=None, as_json=False): """Get all subscribers in a specified group. https://developers.mailerlite.com/v2/reference#subscribers-in-a-group Parameters ---------- group_id : int group id limit : int How many subscribers you want offset : int page index stype : str Define subscriber type: Here are the possible values: * None - All subscribers (default) * active * unsubscribed * bounced * junk * unconfirmed as_json : bool return result as json format Returns ------- subscribers: list all desired Subscribers. More informations : https://developers.mailerlite.com/v2/reference#subscribers """ params = {'limit': limit, 'offset': offset} if stype and stype.lower() in [ 'active', 'unsubscribed', 'bounced', 'junk', 'unconfirmed' ]: params.update({'type': stype}) url = client.build_url('groups', group_id, 'subscribers', **params) _, res_json = client.get(url, headers=self.headers) if as_json or not res_json: return res_json for res in res_json: res['fields'] = [Field(**field) for field in res['fields']] all_subscribers = [Subscriber(**res) for res in res_json] return all_subscribers
def search(self, search=None, limit=100, offset=0, minimized=True, as_json=False): """Get paginated details of all Subscribers from your account. look at https://developers.mailerlite.com/v2/reference#subscribers Parameters ---------- search : str query parameter to search limit : int How many subscribers you want offset : int page index minimized : bool return minimized response with: id, email, type default: True as_json : bool return result as json format Returns ------- subscribers: list all desired Subscribers. More informations : https://developers.mailerlite.com/v2/reference#subscribers """ params = {'limit': limit, 'offset': offset, 'minimized': minimized} if search is not None: params.update({'query': search}) url = client.build_url('subscribers', 'search', **params) res_code, res_json = client.get(url, headers=self.headers) if as_json or not res_json: return res_json if not minimized: for res in res_json: res['fields'] = [Field(**field) for field in res['fields']] all_subscribers = [Subscriber(**res) for res in res_json] return all_subscribers
def all(self, as_json=False): """Get list of fields from your account. https://developers.mailerlite.com/v2/reference#all-fields Parameters ---------- as_json : bool return result as json format Returns ------- fields: list all desired Fields. """ url = client.build_url('fields') res_code, res_json = client.get(url, headers=self.headers) if as_json or not res_json: return res_json all_fields = [Field(**res) for res in res_json] return all_fields