-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge12.py
More file actions
executable file
·115 lines (102 loc) · 4.62 KB
/
challenge12.py
File metadata and controls
executable file
·115 lines (102 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Challenge 12: Write an application that will create a route in mailgun so
# that when an email is sent to <YourSSO>@apichallenges.mailgun.org it calls
# your Challenge 1 script that builds 3 servers.
# Copyright 2013 Scott Gilbert
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
# Required Parameters:
# none
#
# Optional Parameters:
# -h, --help show help message and exit
# --email EMAILADDR email address for which to create route
# (default is
# scott.gilbert@apichallenges.mailgun.org)
# --dest STRING Either a email address or URL to which to route
# the email. (default is
# http://cldsrvr.com/challenge1)
# --list List routes. No new routes are created.
# --delete ID Delete route identified by ID.
# --priority PRIORITY Priority of new route.
# --description DESCRIPTION Description of new route.
import os
import argparse
import requests
import json
def mg_list_routes(apiKey):
"""List MailGun routes for account identified by apiKey. """
routes = requests.get("https://api.mailgun.net/v2/routes",
auth=("api", apiKey))
#print routes.text
routedict = json.loads(routes.text)
print "Priority ID Description ",
print "Expression/Action"
print "-------- ------------------------- ------------------- ",
print "------------------------------"
for r in routedict['items']:
print "%7s %-25s %-20s %s" % (r['priority'], r['id'], r['description'],
r['expression'])
for a in r['actions']:
print "%7s %-25s %-20s %s" % ('', '', '', a)
return routes
def mg_delete_route(apiKey, routeID):
"""Delete MailGun route identified by routeID for account identified by
apiKey.
"""
print "Deleting route %s" % routeID
return requests.delete("https://api.mailgun.net/v2/routes/%s" % routeID,
auth=("api", apiKey))
def mg_create_route(apiKey, email, dest, priority, desc):
"""Create MailGun route."""
print "Creating route for %s, with priority %s," % (email, priority),
print "to %s with description '%s'" % (dest, desc)
return requests.post("https://api.mailgun.net/v2/routes",
auth=("api", apiKey),
data={"priority": priority,
"description": desc,
"expression": "match_recipient('%s')" % email,
"action": "forward('%s')" % dest})
if __name__ == "__main__":
print "\nChallenge 12: Write an application that will create a route in"
print "mailgun so that when an email is sent to",
print "<YourSSO>@apichallenges.mailgun.org\nit calls your Challenge 1",
print "script that builds 3 servers\n\n"
parser = argparse.ArgumentParser()
parser.add_argument("--email",
default="scott.gilbert@apichallenges.mailgun.org",
help="Email address for which to create route")
parser.add_argument("--dest", default="http://cldsrvr.com/challenge1",
help="Either a email address or URL to which to route")
parser.add_argument("--priority", default=50, type=int,
help="Priority of new route")
parser.add_argument("--description", default="Challenge12 route",
help="Description of new route")
group = parser.add_mutually_exclusive_group()
group.add_argument("--list", action="store_true",
help="List current routes. No routes are created.")
group.add_argument("--delete",
help="Delete route identified by ID.")
args = parser.parse_args()
credential_file = os.path.expanduser("~/.mailgunapi")
apiKey = open(credential_file, 'r').read().strip()
if args.list:
mg_list_routes(apiKey)
elif args.delete:
mg_delete_route(apiKey, args.delete)
else:
mg_create_route(apiKey, args.email, args.dest, args.priority,
args.description)
# vim: ts=2 sw=2 tw=78 expandtab