Skip to content

Commit e89eb18

Browse files
authored
Merge branch 'master' into master
2 parents 114907a + b2fdd50 commit e89eb18

File tree

3 files changed

+59
-23
lines changed

3 files changed

+59
-23
lines changed

README.md

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
#Simple Mailjet APIv3 wrapper
2-
1+
[api_credential]: https://app.mailjet.com/account/api_keys
32
[doc]: http://dev.mailjet.com/guides/?python#
43
[api_doc]: https://github.com/mailjet/api-documentation
4+
[smsDashboard]: https://app.mailjet.com/sms?_ga=2.81581655.1972348350.1522654521-1279766791.1506937572
5+
[smsInfo]: https://app.mailjet.com/docs/transactional-sms?_ga=2.183303910.1972348350.1522654521-1279766791.1506937572#trans-sms-token
6+
7+
![alt text](https://www.mailjet.com/images/email/transac/logo_header.png "Mailjet")
8+
9+
# Official Mailjet Python Wrapper
510

611
[![Build Status](https://travis-ci.org/mailjet/mailjet-apiv3-python.svg?branch=master)](https://travis-ci.org/mailjet/mailjet-apiv3-python)
712

813
### API documentation
914

10-
Every code examples can be find on the [Mailjet Documentation][doc]
15+
All code examples can be found on the [Mailjet Documentation][doc].
1116

1217
(Please refer to the [Mailjet Documentation Repository][api_doc] to contribute to the documentation examples)
1318

@@ -19,14 +24,23 @@ Every code examples can be find on the [Mailjet Documentation][doc]
1924

2025
## Getting Started
2126

22-
First, make sure you have an API key, and an API secret.
23-
Once you got them, save them in your environment:
27+
Grab your API and Secret Keys [here][api_credential]. You need them for authentication when using the Email API:
2428

25-
```
29+
```bash
2630
export MJ_APIKEY_PUBLIC='your api key'
2731
export MJ_APIKEY_PRIVATE='your api secret'
2832
```
2933

34+
## API Versioning
35+
36+
The Mailjet API is spread among three distinct versions:
37+
38+
- `v3` - The Email API
39+
- `v3.1` - Email Send API v3.1, which is the latest version of our Send API
40+
- `v4` - SMS API
41+
42+
Since most Email API endpoints are located under `v3`, it is set as the default one and does not need to be specified when making your request. For the others you need to specify the version using `version`. For example, if using Send API `v3.1`:
43+
3044
``` python
3145
# import the mailjet wrapper
3246
from mailjet_rest import Client
@@ -36,21 +50,21 @@ import os
3650
API_KEY = os.environ['MJ_APIKEY_PUBLIC']
3751
API_SECRET = os.environ['MJ_APIKEY_PRIVATE']
3852

39-
mailjet = Client(auth=(API_KEY, API_SECRET), version='v3')
53+
mailjet = Client(auth=(API_KEY, API_SECRET), version='v3.1')
4054

4155
```
4256

43-
**NOTE**: `version` reflects the api version in the url (`https://api.mailjet.com/{{ version }}/REST/`). It is `'v3'` by default and can be used to select another api version (for example `v3.1` for the new send API).
57+
For additional information refer to our [API Reference](https://dev.preprod.mailjet.com/reference/overview/versioning/).
4458

4559
## Make a `GET` request:
4660
``` python
47-
# get every contacts
61+
# get all contacts
4862
result = mailjet.contact.get()
4963
```
5064

5165
## `GET` request with filters:
5266
``` python
53-
# get the 2 first contacts
67+
# get the first 2 contacts
5468
result = mailjet.contact.get(filters={'limit': 2})
5569
```
5670
## `POST` request
@@ -61,25 +75,45 @@ result = mailjet.sender.create(data={'email': '[email protected]'})
6175

6276
## Combine a resource with an action
6377
``` python
64-
# Get the contact lists of contact #2
78+
# Get the contacts lists of contact #2
6579
result = mailjet.contact_getcontactslists.get(id=2)
6680
```
6781

6882
## Send an Email
6983
``` python
7084

71-
email = {
72-
'FromName': 'Mr Smith',
73-
'FromEmail': '[email protected]',
74-
'Subject': 'Test Email',
75-
'Text-Part': 'Hey there !',
76-
'Recipients': [{'Email': 'your email here'}]
85+
from mailjet_rest import Client
86+
import os
87+
api_key = os.environ['MJ_APIKEY_PUBLIC']
88+
api_secret = os.environ['MJ_APIKEY_PRIVATE']
89+
mailjet = Client(auth=(api_key, api_secret), version='v3.1')
90+
data = {
91+
'Messages': [
92+
{
93+
"From": {
94+
"Email": "[email protected]",
95+
"Name": "Mailjet Pilot"
96+
},
97+
"To": [
98+
{
99+
"Email": "[email protected]",
100+
"Name": "passenger 1"
101+
}
102+
],
103+
"Subject": "Your email flight plan!",
104+
"TextPart": "Dear passenger 1, welcome to Mailjet! May the delivery force be with you!",
105+
"HTMLPart": "<h3>Dear passenger 1, welcome to Mailjet!</h3><br />May the delivery force be with you!"
106+
}
107+
]
77108
}
78-
79-
mailjet.send.create(email)
109+
result = mailjet.send.create(data=data)
110+
print result.status_code
111+
print result.json()
80112

81113
```
82114

115+
You can also use the previous version of Mailjet's Send API (v3). You can find the documentation explaining the overall differences and code samples [here](https://dev.mailjet.com/guides/?python#sending-a-basic-email-v3).
116+
83117
## Create a new Contact
84118
``` python
85119

mailjet_rest/client.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ def _get(self, filters=None, action_id=None, id=None, **kwargs):
5252
return api_call(self._auth, 'get', self._url, headers=self.headers, action=self.action, action_id=action_id, filters=filters, resource_id=id, **kwargs)
5353

5454
def get_many(self, filters=None, action_id=None, **kwargs):
55-
return self._get(filters=filters, **kwargs)
55+
return self._get(filters=filters, action_id=action_id **kwargs)
5656

5757
def get(self, id=None, filters=None, action_id=None, **kwargs):
58-
return self._get(id=id, filters=filters, **kwargs)
58+
return self._get(id=id, filters=filters, action_id=action_id, **kwargs)
5959

6060
def create(self, data=None, filters=None, id=None, action_id=None, **kwargs):
6161
if self.headers['Content-type'] == 'application/json':
@@ -81,9 +81,11 @@ def __init__(self, auth=None, **kwargs):
8181

8282
def __getattr__(self, name):
8383
split = name.split('_')
84+
#identify the resource
8485
fname = split[0]
8586
action = None
8687
if (len(split) > 1):
88+
#identify the sub resource (action)
8789
action = split[1]
8890
if action == 'csvdata':
8991
action = 'csvdata/text:plain'
@@ -131,7 +133,7 @@ def build_url(url, method, action=None, resource_id=None, action_id=None):
131133
if action:
132134
url += '/%s' % action
133135
if action_id:
134-
url += '/%d' % action_id
136+
url += '/{}'.format(action_id)
135137

136138
return url
137139

test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def test_user_agent(self):
9090
auth=self.auth,
9191
version='v3.1'
9292
)
93-
self.assertEqual(self.client.config.user_agent, 'mailjet-apiv3-python/v1.3.0')
93+
self.assertEqual(self.client.config.user_agent, 'mailjet-apiv3-python/v1.3.2')
9494

9595

9696
if __name__ == '__main__':

0 commit comments

Comments
 (0)