Skip to content

Commit

Permalink
Merge pull request #81 from mailjet/dnefodov/feature/add-dash-contain…
Browse files Browse the repository at this point in the history
…ed-urls

Add possibility to use dash contained URLs
  • Loading branch information
DanyilNefodov committed Apr 29, 2022
2 parents 9fde1c2 + 9077e40 commit 1a824d0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Check out all the resources and Python code examples in the official [Mailjet Do
- [Client / Call configuration specifics](#client--call-configuration-specifics)
- [API versioning](#api-versioning)
- [Base URL](#base-url)
- [URL path](#url-path)
- [Request examples](#request-examples)
- [POST request](#post-request)
- [Simple POST request](#simple-post-request)
Expand Down Expand Up @@ -148,6 +149,22 @@ mailjet = Client(auth=(api_key, api_secret),api_url="https://api.us.mailjet.com/

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

### URL path

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.
For example, to reach `statistics/link-click` path you should call `statistics_linkClick` attribute of python client.

```python
# GET `statistics/link-click`
mailjet = Client(auth=(api_key, api_secret))
filters = {
'CampaignId': 'xxxxxxx'
}
result = mailjet.statistics_linkClick.get(filters=filters)
print result.status_code
print result.json()
```

## Request examples

### POST request
Expand Down
9 changes: 9 additions & 0 deletions mailjet_rest/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import json
import logging
import re

import requests
from requests.compat import urljoin
Expand All @@ -11,6 +12,13 @@
requests.packages.urllib3.disable_warnings()


def prepare_url(key: str):
"""Replaces capital letters to lower one with dash prefix."""
char_elem = key.group(0)
if char_elem.isupper():
return '-' + char_elem.lower()


class Config(object):
DEFAULT_API_URL = 'https://api.mailjet.com/'
API_REF = 'http://dev.mailjet.com/email-api/v3/'
Expand Down Expand Up @@ -79,6 +87,7 @@ def __init__(self, auth=None, **kwargs):
self.config = Config(version=version, api_url=api_url)

def __getattr__(self, name):
name = re.sub(r"[A-Z]", prepare_url, name)
split = name.split('_')
#identify the resource
fname = split[0]
Expand Down

0 comments on commit 1a824d0

Please sign in to comment.