Skip to content

Commit 1a824d0

Browse files
Merge pull request #81 from mailjet/dnefodov/feature/add-dash-contained-urls
Add possibility to use dash contained URLs
2 parents 9fde1c2 + 9077e40 commit 1a824d0

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Check out all the resources and Python code examples in the official [Mailjet Do
2626
- [Client / Call configuration specifics](#client--call-configuration-specifics)
2727
- [API versioning](#api-versioning)
2828
- [Base URL](#base-url)
29+
- [URL path](#url-path)
2930
- [Request examples](#request-examples)
3031
- [POST request](#post-request)
3132
- [Simple POST request](#simple-post-request)
@@ -148,6 +149,22 @@ mailjet = Client(auth=(api_key, api_secret),api_url="https://api.us.mailjet.com/
148149

149150
If your account has been moved to Mailjet's **US architecture**, the URL value you need to set is `https://api.us.mailjet.com`.
150151

152+
### URL path
153+
154+
According to python special characters limitations we can't use slashes `/` and dashes `-` which is acceptable for URL path building. Instead python client uses another way for path building. You should replase slashes `/` by underscore `_` and dashes `-` by capitalizing next letter in path.
155+
For example, to reach `statistics/link-click` path you should call `statistics_linkClick` attribute of python client.
156+
157+
```python
158+
# GET `statistics/link-click`
159+
mailjet = Client(auth=(api_key, api_secret))
160+
filters = {
161+
'CampaignId': 'xxxxxxx'
162+
}
163+
result = mailjet.statistics_linkClick.get(filters=filters)
164+
print result.status_code
165+
print result.json()
166+
```
167+
151168
## Request examples
152169

153170
### POST request

mailjet_rest/client.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import json
55
import logging
6+
import re
67

78
import requests
89
from requests.compat import urljoin
@@ -11,6 +12,13 @@
1112
requests.packages.urllib3.disable_warnings()
1213

1314

15+
def prepare_url(key: str):
16+
"""Replaces capital letters to lower one with dash prefix."""
17+
char_elem = key.group(0)
18+
if char_elem.isupper():
19+
return '-' + char_elem.lower()
20+
21+
1422
class Config(object):
1523
DEFAULT_API_URL = 'https://api.mailjet.com/'
1624
API_REF = 'http://dev.mailjet.com/email-api/v3/'
@@ -79,6 +87,7 @@ def __init__(self, auth=None, **kwargs):
7987
self.config = Config(version=version, api_url=api_url)
8088

8189
def __getattr__(self, name):
90+
name = re.sub(r"[A-Z]", prepare_url, name)
8291
split = name.split('_')
8392
#identify the resource
8493
fname = split[0]

0 commit comments

Comments
 (0)