Created
November 21, 2014 06:05
-
-
Save kpx-dev/94495515230701a7f105 to your computer and use it in GitHub Desktop.
SendGrid Multiple Credentials
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This Python code snippet will show you how to work with SendGrid Multiple | |
Credentials | |
install the amazing requests library | |
$ pip install requests==2.4.3 | |
export your SendGrid credential from command line | |
$ export SG_USERNAME=your_sg_username | |
$ export SG_PASSWORD=your_sg_password | |
save this file as python example.py and run it | |
$ python example.py | |
gist link: | |
""" | |
import requests | |
import os | |
SG_URL = "https://api.sendgrid.com/api/credentials" | |
def build_url(endpoint, params=None): | |
url = "{}/{}.json?api_user={}&api_key={}".format( | |
SG_URL, | |
endpoint, | |
os.environ['SG_USERNAME'], | |
os.environ['SG_PASSWORD']) | |
if params: | |
for k, v in params.iteritems(): | |
url += "&{}={}".format(k, v) | |
return url | |
if __name__ == '__main__': | |
print 'get all multiple credentials...' | |
url = build_url("get") | |
res = requests.get(url) | |
print res.json() | |
print 'add new multiple credentials...' | |
url = build_url("add") | |
data = {"username": "_example", "password": "!example", | |
"permissions": '{"email": 1, "web": 1, "api": 1}'} | |
res = requests.post(url, data) | |
print res.json() | |
print 'edit multiple credentials...' | |
url = build_url("edit") | |
data = {"username": "example", "password": "!example", | |
"permissions": '{"email": 1, "web": 1, "api": 0}'} | |
res = requests.post(url, data) | |
print res.json() | |
print 'remove credential...' | |
url = build_url("remove") | |
data = {"username": "_example"} | |
res = requests.post(url, data) | |
print res.json() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment