def all(self, limit=100, offset=0, gfilters='', as_json=False): """Get list of groups from your account. look at https://developers.mailerlite.com/v2/reference#groups Parameters ---------- limit : int How many groups you want offset : int page index gfilters : str group filters as_json : bool return result as json format Returns ------- groups: list all desired Groups. More informations : https://developers.mailerlite.com/v2/reference#groups """ params = {'limit': limit, 'offset': offset, 'filters': gfilters} url = client.build_url('groups', **params) _, res_json = client.get(url, headers=self.headers) if as_json or not res_json: return res_json all_groups = [Group(**res) for res in res_json] return all_groups
def groups(self, as_json=False, **identifier): """Get groups subscriber belongs to. More informations: https://developers.mailerlite.com/v2/reference#groups-subscriber-belongs-to Parameters ---------- identifier : str should be subscriber id or email. e.g: id=1343965485 or email='*****@*****.**' as_json : bool return result as json format Returns ------- groups: list all groups that a subscriber belongs to. More informations : https://developers.mailerlite.com/v2/reference#groups """ path = get_id_or_email_identifier(**identifier) if path is None: raise IOError('An identifier must be define') url = client.build_url('subscribers', path, 'groups') res_code, res_json = client.get(url, headers=self.headers) if as_json or not res_json: return res_json all_groups = [Group(**res) for res in res_json] return all_groups
def get(self, id, as_json=False): """Get single field by ID from your account. look at https://developers.mailerlite.com/v2/reference#all-fields Parameters ---------- id : int should be group id. e.g: id=1343965485 as_json : bool return result as json format Returns ------- Field: :class:Field a single field """ url = client.build_url('fields', id) res_code, res_json = client.get(url, headers=self.headers) if as_json or not res_json: return res_json return Field(**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_code, 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 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 get(self, webhook_id, as_json=False): """Get single field by ID from your account. https://developers.mailerlite.com/v2/reference#get-single-webhook Parameters ---------- webhook_id : int ID of a webhook as_json : bool return result as json format Returns ------- webhook: dict the desired webhook. """ url = client.build_url('webhooks', webhook_id) _, res_json = client.get(url, headers=self.headers) if as_json or not res_json: return res_json webhook = Webhook(**res_json) return webhook
def activity(self, as_json=False, atype=None, **identifier): """Get activities (clicks, opens, etc) of selected subscriber. More informations: https://developers.mailerlite.com/v2/reference#activity-of-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 atype : str Define activity type: Here are the possible values: * None - All activities (default) * opens * clicks * bounces * junks * unsubscribes * forwards * sendings Returns ------- activities: list all subscriber activities. More informations : https://developers.mailerlite.com/v2/reference#activity-of-single-subscriber """ path = get_id_or_email_identifier(**identifier) if path is None: raise IOError('An identifier must be define') args = ['subscribers', path, 'activity'] if atype: possible_atype = [ 'opens', 'clicks', 'junks', 'bounces', 'unsubscribes', 'forwards', 'sendings' ] if atype not in possible_atype: raise ValueError('Incorrect value atype. Activity type should' ' be {0}'.format(possible_atype)) args.append(atype) url = client.build_url(*args) res_code, res_json = client.get(url, headers=self.headers) if as_json or not res_json: return res_json all_activities = [Activity(**res) for res in res_json] return all_activities
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 all(self): """Get list of Webhooks. https://developers.mailerlite.com/v2/reference#get-webhooks-list Returns ------- webhooks: list of dict all webhooks. """ url = client.build_url('webhooks') _, res_json = client.get(url, headers=self.headers) return res_json
def double_optin(self): """Retrieve the status double opt-in. https://developers.mailerlite.com/v2/reference#get-double-optin-status Returns ------- success: bool deletion status """ url = client.build_url('settings', 'double_optin') _, res_json = client.get(url, headers=self.headers) return res_json
def count(self): """Return the number of segments. More informations: https://developers.mailerlite.com/reference#section-segments-count Returns ------- count: int number of segments """ url = client.build_url('segments', 'count') _, res_json = client.get(url, headers=self.headers) return res_json['count']
def info(self): """Get account info. https://developers.mailerlite.com/v2/reference#account Returns ------- info: dict all account information. """ url = client.build_url('me') res_code, res_json = client.get(url, headers=self.headers) return res_json
def all(self, status='sent', limit=100, offset=0, order='asc', as_json=False): """Get paginated details of all campaigns from your account. look at https://developers.mailerlite.com/reference#campaigns-by-type Parameters ---------- status : str Define campaigns type: Here are the possible values: * sent - campaigns which are sent already (default) * draft - campaigns which aren't completed or sent to subscribers * outbox - campaigns which are being sent right now or scheduled limit : int How many campaigns you want offset : int page index order : str pick the order. Here are the possible values: ASC (default) or DESC as_json : bool return result as json format Returns ------- campaigns: list all desired campaign. More informations concerning campaign : https://developers.mailerlite.com/reference#section-response-body-parameters """ if order.upper() not in ['ASC', 'DESC']: raise IOError("Incorrect order, please choose between ASC or DESC") params = {'limit': limit, 'offset': offset, 'order': order} url = client.build_url('campaigns', status, **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['opened'] = Stats(**res.pop('opened')) res['clicked'] = Stats(**res.pop('clicked')) all_campaigns = [Campaign(**res) for res in res_json] return all_campaigns
def stats(self): """Get basic stats for of account, such as subscribers, open/click rates and so on. https://developers.mailerlite.com/v2/reference#stats Returns ------- stats: dict account stats """ url = client.build_url('stats') res_code, res_json = client.get(url, headers=self.headers) return res_json
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 count(self, status='sent'): """Return the number of campaigns. Returns ------- status : str Define campaigns type: Here are the possible values: * sent - campaigns which are sent already (default) * draft - campaigns which aren't completed or sent to subscribers * outbox - campaigns which are being sent right now or scheduled count: int number of campaigns """ if status.lower() not in ['sent', 'draft', 'outbox']: raise ValueError('Incorrect status, check documentation') url = client.build_url('campaigns', status.lower(), 'count') _, res_json = client.get(url, headers=self.headers) return res_json['count']
def get(self, webhook_id): """Get single field by ID from your account. https://developers.mailerlite.com/v2/reference#get-single-webhook Parameters ---------- webhook_id : int ID of a webhook Returns ------- webhook: dict the desired webhook. """ url = client.build_url('webhooks', webhook_id) _, res_json = client.get(url, headers=self.headers) return res_json
def all(self, as_json=False): """Get list of Webhooks. https://developers.mailerlite.com/v2/reference#get-webhooks-list Returns ------- webhooks: list of dict all webhooks. as_json : bool return result as json format """ url = client.build_url('webhooks') _, res_json = client.get(url, headers=self.headers) if as_json or not res_json: return res_json all_webhooks = [Webhook(**res) for res in res_json.get('webhooks')] return all_webhooks
def all(self, limit=100, offset=0, order='asc', as_json=False): """Get paginated details of all segments from your account. look at https://developers.mailerlite.com/reference#segments-1 Parameters ---------- limit : int How many segments you want offset : int page index order : str pick the order. Here are the possible values: ASC (default) or DESC as_json : bool return result as json format Returns ------- segments: list all desired segment. More informations: https://developers.mailerlite.com/v2/reference#segments-1 meta: object some meta informations about the segments """ if order.upper() not in ['ASC', 'DESC']: raise IOError("Incorrect order, please choose between ASC or DESC") params = {'limit': limit, 'offset': offset, 'order': order} url = client.build_url('segments', **params) _, res_json = client.get(url, headers=self.headers) if as_json or not res_json: return res_json['data'], res_json['meta'] all_segments = [Segment(**res) for res in res_json['data']] res_json['meta']['pagination'] = Pagination(**res_json['meta'] .pop('pagination')) meta = Meta(**res_json['meta']) return all_segments, meta
def count(self, stype=None, as_json=False): """Get the count of subscribers of a type. Please, be aware that `count` is not documented in the official API https://developers.mailerlite.com/reference#subscribers Parameters ---------- 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 ------- number: Integer the count of subscribers """ warn("Please, be aware that `count` is not in the official API") params = {} if stype and stype.lower() in [ 'active', 'unsubscribed', 'bounced', 'junk', 'unconfirmed' ]: params.update({'type': stype}) url = client.build_url('subscribers', 'count', **params) res_code, res_json = client.get(url, headers=self.headers) if as_json or not res_json: return res_json return res_json['count']
def get(self, group_id, as_json=False): """Get single group by ID from your account. look at https://developers.mailerlite.com/v2/reference#single-group Parameters ---------- group_id : int should be group id. e.g: id=1343965485 as_json : bool return result as json format Returns ------- Group: :class:Group a single group """ url = client.build_url('groups', group_id) _, res_json = client.get(url, headers=self.headers) if as_json or not res_json: return res_json return Group(**res_json)
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