Skip to content

Commit eb45e03

Browse files
author
Josh Mervine
committed
Adding examples and updating readme.
1 parent e255109 commit eb45e03

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ to send the OAuth header in the body or URLEncoded.
8787

8888
```
8989
git clone https://github.com/jmervine/python-maxcdn.git
90+
cd python-maxcdn
9091
9192
make # setup and test
9293
make setup # installation w/ deps
@@ -98,3 +99,19 @@ make int/all # integration w/ python2 python3.2 python3.3 python3.4
9899
make nose # verbose test output w/ nosetests
99100
```
100101

102+
# Examples
103+
104+
Running examples:
105+
106+
```
107+
git clone https://github.com/jmervine/python-maxcdn.git
108+
cd python-maxcdn
109+
make setup
110+
111+
export PYTHONPATH=./build:./maxcdn:$PYTHONPATH
112+
113+
./examples/simple.py
114+
./examples/report.py # [hourly|daily|monthly]
115+
./examples/purge.py # [zoneid]
116+
```
117+

examples/purge.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python
2+
import pprint as pp
3+
from os import environ as env
4+
from sys import exit, argv
5+
from textwrap import dedent
6+
from maxcdn import MaxCDN
7+
8+
try:
9+
zoneid = argv[1]
10+
except:
11+
zoneid = None
12+
13+
if not "ALIAS" in env or not "KEY" in env or not "SECRET" in env:
14+
print(dedent("""\
15+
Usage: purge.py zoneid
16+
17+
Add credentials to your environment like so:
18+
19+
$ export ALIAS=<alias>
20+
$ export KEY=<key>
21+
$ export SECRET=<secret>
22+
"""))
23+
exit(1)
24+
25+
maxcdn = MaxCDN(env["ALIAS"], env["KEY"], env["SECRET"])
26+
27+
if zoneid is None:
28+
zones = maxcdn.get("/zones/pull.json")
29+
for zone in zones["data"]["pullzones"]:
30+
print("Purging zone: %s (%s)" % (
31+
zone["name"], zone["id"]))
32+
33+
pp.pprint(maxcdn.purge(zone["id"]))
34+
else:
35+
print("Purging zone: %s" % (zoneid))
36+
res = maxcdn.purge(zoneid)
37+
try:
38+
if res["code"] == 200:
39+
print("SUCCESS!")
40+
else:
41+
print("Failed with code: " + res["code"])
42+
exit(res["code"])
43+
except KeyError:
44+
print("Something went terribly wrong!")
45+
pp.pprint(res)
46+
exit(1)
47+

examples/report.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python
2+
import pprint as pp
3+
from os import environ as env
4+
from maxcdn import MaxCDN
5+
6+
try:
7+
report = "/"+argv[1]
8+
except:
9+
report = ""
10+
11+
if not "ALIAS" in env or not "KEY" in env or not "SECRET" in env:
12+
print(dedent("""\
13+
Usage: report.py [monthly|daily|hourly]
14+
15+
Add credentials to your environment like so:
16+
17+
$ export ALIAS=<alias>
18+
$ export KEY=<key>
19+
$ export SECRET=<secret>
20+
"""))
21+
exit(1)
22+
23+
24+
maxcdn = MaxCDN(env["ALIAS"], env["KEY"], env["SECRET"])
25+
26+
zones = maxcdn.get("/zones/pull.json")
27+
for zone in zones["data"]["pullzones"]:
28+
print("Zone report for: %s (%s)" % (
29+
zone["name"], zone["url"]))
30+
31+
# summary
32+
fetch = maxcdn.get("/reports/%s/stats.json%s" % (zone["id"], report))
33+
for key, val in fetch["data"]["summary"].iteritems():
34+
print(" - %s: %s" % (key, val))
35+
36+
# popularfiles
37+
print(" ")
38+
print("Popular Files:")
39+
40+
fetch = maxcdn.get("/reports/%s/popularfiles.json?page_size=10" % (zone["id"]))
41+
for file in fetch["data"]["popularfiles"]:
42+
print(" - url: " + file["uri"])
43+
print(" - hits: " + file["hit"])
44+
print(" - size: " + file["size"])
45+
46+
print(" ")
47+

examples/simple.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python
2+
import pprint as pp
3+
from os import environ as env
4+
from maxcdn import MaxCDN
5+
6+
if not "ALIAS" in env or not "KEY" in env or not "SECRET" in env:
7+
print(dedent("""\
8+
Usage: simple.py
9+
10+
Add credentials to your environment like so:
11+
12+
$ export ALIAS=<alias>
13+
$ export KEY=<key>
14+
$ export SECRET=<secret>
15+
"""))
16+
exit(1)
17+
18+
maxcdn = MaxCDN(env["ALIAS"], env["KEY"], env["SECRET"])
19+
20+
print("GET '/account.json'")
21+
pp.pprint(maxcdn.get("/account.json"))
22+
23+
print("GET '/account.json/address'")
24+
pp.pprint(maxcdn.get("/account.json/address"))
25+
26+
print("GET '/reports/stats.json/hourly'")
27+
pp.pprint(maxcdn.get("/reports/stats.json/hourly"))
28+

0 commit comments

Comments
 (0)