Skip to content
This repository was archived by the owner on Jul 17, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pip install -e .

## Method reference

For complete reference, visit the [official MailerLite API reference](https://developers.mailerlite.com/reference).
For the complete reference, visit the [official MailerLite API reference](https://developers.mailerlite.com/reference).

## Examples

Expand All @@ -60,6 +60,14 @@ First, Grab YOUR_API_KEY from your Mailerlite account (Profile > Integrations >
>>> api = MailerLiteApi('YOUR_API_KEY')
```

A second option is to define an environment variable named `MAILERLITE_PYTHON_API_KEY`.
Then, you do not need to precise it in your code:

```python
>>> from mailerlite import MailerLiteApi
>>> api = MailerLiteApi()
```

### Campaigns

#### Get all campaigns or a specific one
Expand Down Expand Up @@ -160,7 +168,7 @@ First, Grab YOUR_API_KEY from your Mailerlite account (Profile > Integrations >

Get the total count of all subscribers in a single call.

Please, be aware that is not documented in the official API.
Please, be aware that this is not a documented feature in the official API.

```python
>>> api.subscribers.count()
Expand Down Expand Up @@ -229,9 +237,9 @@ This method calls the import endpoint https://developers.mailerlite.com/referenc

This method calls the add single subscriber endpoint https://developers.mailerlite.com/reference#add-single-subscriber
```python
>>> api.groups.add_single_subscriber(group_id=12345, subscriber_data={"email": "john@wick.com", "name": "John Wick" ...}, autoresponders=False, resubscribe=False, as_json=False)
>>> api.groups.add_single_subscriber(group_id=12345, subscribers_data={"email": "john@wick.com", "name": "John Wick" ...}, autoresponders=False, resubscribe=False, as_json=False)
```
Unlike the method above, this add only one subscriber to a group. The ```subscriber_data``` argument accepts all subscriber attributes.
Unlike the method above, this adds only one subscriber to a group. The ```subscriber_data``` argument accepts all subscriber attributes.
Check available attributes on https://developers.mailerlite.com/reference#create-a-subscriber

#### Delete one subscriber from a Group
Expand Down
5 changes: 4 additions & 1 deletion mailerlite/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Mailerlite API."""
import os

from mailerlite.campaign import Campaigns
from mailerlite.segment import Segments
Expand All @@ -15,7 +16,7 @@ class MailerLiteApi:

"""

def __init__(self, api_key):
def __init__(self, api_key=None):
"""Initialize a new mailerlite.api object.

Parameters
Expand All @@ -24,6 +25,8 @@ def __init__(self, api_key):
Your mailerlite api_key.

"""
api_key = api_key or os.environ.get("MAILERLITE_PYTHON_API_KEY", None)

if not api_key or not isinstance(api_key, str):
raise ValueError("Empty API_KEY. Please enter a valid API_KEY")

Expand Down
4 changes: 2 additions & 2 deletions mailerlite/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ def add_subscribers(self, group_id, subscribers_data, resubscribe=False,
' dict that contains the following keys: email,'
' name')

errors = [d for d in body['subscribers'] if 'email' not in d.keys()
if 'name' not in d.keys()]
errors = [d for d in body['subscribers']
if not {'email', 'name'}.issubset(d.keys())]
if errors:
raise ValueError('All subscribers_data should contain the'
' following keys: email, name')
Expand Down
4 changes: 4 additions & 0 deletions mailerlite/tests/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ def test_account(header):

optin = acc.double_optin()
assert 'enabled' in optin.keys()

with pytest.raises(OSError):
# double_optin not available with this API keys
acc.set_double_optin(False)
14 changes: 14 additions & 0 deletions mailerlite/tests/test_campaign.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ def test_campaign_error(header):
with pytest.raises(ValueError):
campaign.count(status='inbox')

with pytest.raises(IOError):
campaign.all(order='inbox')

with pytest.raises(ValueError):
campaign.create(data=[("subject", "Regular campaign subject"),
("type", "regular")])

with pytest.raises(ValueError):
campaign.create(data={"random_keys": "test"})

with pytest.raises(ValueError):
campaign.create(data={"random_keys": "test",
"type": "regular"})


def test_crud_campaign(header, campaign_data, campaign_data_ab):
campaign_obj = Campaigns(header)
Expand Down
13 changes: 13 additions & 0 deletions mailerlite/tests/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ def test_groups_subscriber(header):

groups.delete_subscriber(group_1.id, new_subs[0].id)

with pytest.raises(ValueError):
data = {'name': 'John',
'fields': {'company': 'MailerLite'}}
groups.add_subscribers(group_1.id, data)

with pytest.raises(ValueError):
groups.add_subscribers(group_1.id, subscribers_data='hey!')


def test_groups_single_subscriber(header):
groups = Groups(header)
Expand Down Expand Up @@ -142,3 +150,8 @@ def test_groups_single_subscriber(header):
assert new_sub.email == mail

groups.delete_subscriber(group_1.id, new_sub.id)

with pytest.raises(ValueError):
data = {'name': 'John',
'fields': {'company': 'MailerLite'}}
groups.add_single_subscriber(group_1.id, data)
6 changes: 6 additions & 0 deletions mailerlite/tests/test_subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,10 @@ def test_subscribers_crud(header):
activity = subscriber.activity(email=mail)
assert len(activity) in [0, 1]

with pytest.raises(IOError):
subscriber.activity()

with pytest.raises(ValueError):
subscriber.activity(email=mail, atype='test')

assert subscriber.delete(e_res.id) is None