class MaxCDNIntegration(unittest.TestCase): def setUp(self): self.alias = os.environ["ALIAS"] self.key = os.environ["KEY"] self.secret = os.environ["SECRET"] self.time = str(int(time.mktime(time.gmtime()))) self.max = MaxCDN(self.alias, self.key, self.secret) def test_get(self): for end_point in ["account.json", "account.json/address", "users.json", "zones.json"]: if "/" in end_point: key = end_point.split("/")[1] else: key = end_point.replace(".json", "") rsp = self.max.get(end_point) self.assertTrue(rsp["data"][key], "get " + key + " with data") def test_get_logs(self): rsp = self.max.get("v3/reporting/logs.json") self.assertTrue(rsp["next_page_key"], "get v3/reporting/logs.json with data") def test_post_and_delete(self): data = {"name": self.time, "url": "http://www.example.com/"} res = self.max.post("/zones/pull.json", data=data) zid = str(res["data"]["pullzone"]["id"]) rsp = self.max.delete("/zones/pull.json/" + zid) self.assertTrue(zid, "post") self.assertEqual(200, rsp["code"], "delete") def test_put(self): street = self.time + "_put" rsp = self.max.put("/account.json/address", {"street1": street}) self.assertEqual(street, str(rsp["data"]["address"]["street1"])) def test_purge(self): rsp = self.max.get("zones/pull.json") zones = rsp["data"]["pullzones"] zone = zones[len(zones) - 1]["id"] rsp = self.max.purge(zone) self.assertEqual(200, rsp["code"]) rsp = self.max.get("reports/popularfiles.json") popularfiles = rsp["data"]["popularfiles"] rsp = self.max.purge(zone, popularfiles[0]["uri"]) self.assertEqual(200, rsp["code"]) files = [popularfiles[0]["uri"], popularfiles[1]["uri"]] rsp = self.max.purge(zone, files) self.assertEqual(200, rsp["code"])
class MaxCDNIntegration(unittest.TestCase): def setUp(self): self.alias = os.environ["ALIAS"] self.key = os.environ["KEY"] self.secret = os.environ["SECRET"] self.time = str(int(time.mktime(time.gmtime()))) self.max = MaxCDN(self.alias, self.key, self.secret) def test_get(self): for end_point in [ "account.json", "account.json/address", "users.json", "zones.json" ]: if "/" in end_point: key = end_point.split("/")[1] else: key = end_point.replace(".json", "") rsp = self.max.get(end_point) self.assertTrue(rsp["data"][key], "get " + key + " with data") def test_get_logs(self): rsp = self.max.get("v3/reporting/logs.json") self.assertTrue(rsp["next_page_key"], "get v3/reporting/logs.json with data") def test_post_and_delete(self): data = {"name": self.time, "url": "http://www.example.com/"} res = self.max.post("/zones/pull.json", data=data) zid = str(res["data"]["pullzone"]["id"]) rsp = self.max.delete("/zones/pull.json/" + zid) self.assertTrue(zid, "post") self.assertEqual(200, rsp["code"], "delete") def test_put(self): street = self.time + "_put" rsp = self.max.put("/account.json/address", {"street1": street}) self.assertEqual(street, str(rsp["data"]["address"]["street1"])) def test_purge(self): rsp = self.max.get("zones/pull.json") zones = rsp["data"]["pullzones"] zone = zones[len(zones) - 1]["id"] rsp = self.max.purge(zone) self.assertEqual(200, rsp["code"]) rsp = self.max.get("reports/popularfiles.json") popularfiles = rsp["data"]["popularfiles"] rsp = self.max.purge(zone, popularfiles[0]["uri"]) self.assertEqual(200, rsp["code"]) files = [popularfiles[0]["uri"], popularfiles[1]["uri"]] rsp = self.max.purge(zone, files) self.assertEqual(200, rsp["code"])
def fetch_maxcdn_zones(): data = dict() data['maxcdn'] = dict() for account in CREDENTIALS['maxcdn']: data['maxcdn'][account] = dict() api = MaxCDN(CREDENTIALS['maxcdn'][account]['alias'], CREDENTIALS['maxcdn'][account]['consumer_key'], CREDENTIALS['maxcdn'][account]['consumer_secret']) # page_size=100 because default value is 50 zones = api.get("/zones/pull.json?page_size=100") for zone in zones["data"]["pullzones"]: zone_name = zone['name'] zone_id = zone['id'] data['maxcdn'][account][zone_name] = zone_id return data
#!/usr/bin/env python import pprint as pp from os import environ as env from maxcdn import MaxCDN from textwrap import dedent if not "ALIAS" in env or not "KEY" in env or not "SECRET" in env: print(dedent("""\ Usage: simple.py Add credentials to your environment like so: $ export ALIAS=<alias> $ export KEY=<key> $ export SECRET=<secret> """)) exit(1) maxcdn = MaxCDN(env["ALIAS"], env["KEY"], env["SECRET"]) print("GET '/account.json'") pp.pprint(maxcdn.get("/account.json")) print("GET '/account.json/address'") pp.pprint(maxcdn.get("/account.json/address")) print("GET '/reports/stats.json/hourly'") pp.pprint(maxcdn.get("/reports/stats.json/hourly"))
if not "ALIAS" in env or not "KEY" in env or not "SECRET" in env: print(dedent("""\ Usage: purge.py zoneid Add credentials to your environment like so: $ export ALIAS=<alias> $ export KEY=<key> $ export SECRET=<secret> """)) exit(1) maxcdn = MaxCDN(env["ALIAS"], env["KEY"], env["SECRET"]) if zoneid is None: zones = maxcdn.get("/zones/pull.json") for zone in zones["data"]["pullzones"]: print("Purging zone: %s (%s)" % ( zone["name"], zone["id"])) pp.pprint(maxcdn.purge(zone["id"])) else: print("Purging zone: %s" % (zoneid)) res = maxcdn.purge(zoneid) try: if res["code"] == 200: print("SUCCESS!") else: print("Failed with code: " + res["code"]) exit(res["code"]) except KeyError:
#!/usr/bin/env python import pprint as pp from os import environ as env from maxcdn import MaxCDN from textwrap import dedent if not "ALIAS" in env or not "KEY" in env or not "SECRET" in env: print( dedent("""\ Usage: simple.py Add credentials to your environment like so: $ export ALIAS=<alias> $ export KEY=<key> $ export SECRET=<secret> """)) exit(1) maxcdn = MaxCDN(env["ALIAS"], env["KEY"], env["SECRET"]) print("GET '/account.json'") pp.pprint(maxcdn.get("/account.json")) print("GET '/account.json/address'") pp.pprint(maxcdn.get("/account.json/address")) print("GET '/reports/stats.json/hourly'") pp.pprint(maxcdn.get("/reports/stats.json/hourly"))
class MaxCDNTests(unittest.TestCase): def setUp(self): self.alias = "test_alias" self.key = "test_key" self.secret = "test_secret" self.server = "rws.example.com" self.maxcdn = MaxCDN(self.alias, self.key, self.secret, server=self.server) def test_init(self): self.assertTrue(self.maxcdn) self.assertEqual(self.maxcdn.url, "https://rws.example.com/test_alias") def test_get_url(self): self.assertEqual(self.maxcdn._get_url("/foo.json"), "https://rws.example.com/test_alias/foo.json") self.assertEqual(self.maxcdn._get_url("foo.json"), "https://rws.example.com/test_alias/foo.json") def test_data_request(self): for meth in ["post", "put", "delete"]: requests.Session.request = mock.create_autospec( mock_request, return_value=response(meth)) data = {"foo": "bar"} rsp = self.maxcdn._data_request(meth, meth + ".json", data=data) expected = {"code": 200, "method": meth, "data": {"foo": "bar"}} self.assertEqual(rsp, expected) requests.Session.request = mock.create_autospec( mock_request, return_value=error_response()) with self.assertRaises(MaxCDN.ServerError, None): self.maxcdn._data_request(meth, meth + ".json", data={"foo": "bar"}) def test_get(self): requests.Session.request = mock.create_autospec( mock_request, return_value=response("get")) expected = {"code": 200, "method": "get", "data": {"foo": "bar"}} self.assertEqual(self.maxcdn.get("/get.json"), expected) requests.Session.request = mock.create_autospec( mock_request, return_value=error_response()) with self.assertRaises(MaxCDN.ServerError, None): self.maxcdn.get("/get.json") def test_post(self): requests.Session.request = mock.create_autospec( mock_request, return_value=response("post")) rsp = self.maxcdn.post("/post.json", data={"foo": "bar"}) expected = {"code": 200, "method": "post", "data": {"foo": "bar"}} self.assertEqual(rsp, expected) rsp = self.maxcdn.post("/post.json", params={"foo": "bar"}) self.assertEqual(rsp, expected) rsp = self.maxcdn.post("/post.json", params="foo=bar") self.assertEqual(rsp, expected) def test_put(self): requests.Session.request = mock.create_autospec( mock_request, return_value=response("put")) expected = {"code": 200, "method": "put", "data": {"foo": "bar"}} self.assertEqual(self.maxcdn.put("/put.json"), expected) def test_delete(self): requests.Session.request = mock.create_autospec( mock_request, return_value=response("delete")) expected = {"code": 200, "method": "delete", "data": {"foo": "bar"}} self.assertEqual(self.maxcdn.delete("/delete.json"), expected) rsp = self.maxcdn.patch("/delete.json", file_or_files="/foo.css") self.assertEqual(rsp, expected) files = ["/foo.css", "/bar.css"] expected = {"code": 200, "method": "delete", "data": {"foo": "bar"}} rsp = self.maxcdn.patch("/delete.json", file_or_files=files) self.assertEqual(rsp, expected) def test_purge(self): requests.Session.request = mock.create_autospec( mock_request, return_value=response("delete")) expected = {"code": 200, "method": "delete", "data": {"foo": "bar"}} self.assertEqual(self.maxcdn.purge(12345), expected) self.assertEqual(self.maxcdn.purge(12345, "/master.css"), expected) files = ["/master.css", "/other.css"] self.assertEqual(self.maxcdn.purge(12345, files), expected)
class MaxCDNTests(unittest.TestCase): def setUp(self): self.alias = "test_alias" self.key = "test_key" self.secret = "test_secret" self.server = "rws.example.com" self.maxcdn = MaxCDN(self.alias, self.key, self.secret, server=self.server) def test_init(self): self.assertTrue(self.maxcdn) self.assertEqual(self.maxcdn.url, "https://rws.example.com/test_alias") def test_get_url(self): self.assertEqual(self.maxcdn._get_url("/foo.json"), "https://rws.example.com/test_alias/foo.json") self.assertEqual(self.maxcdn._get_url("foo.json"), "https://rws.example.com/test_alias/foo.json") def test_data_request(self): for meth in [ "post", "put", "delete" ]: requests.Session.request = mock.create_autospec(mock_request, return_value=response(meth)) self.assertEqual(self.maxcdn._data_request(meth, meth+".json", data={"foo":"bar"}), { "code": 200, "method": meth, "data": { "foo":"bar" } }) def test_get(self): requests.Session.request = mock.create_autospec(mock_request, return_value=response("get")) self.assertEqual(self.maxcdn.get("/get.json"), { "code": 200, "method": "get", "data": { "foo":"bar" } }) def test_post(self): requests.Session.request = mock.create_autospec(mock_request, return_value=response("post")) self.assertEqual(self.maxcdn.post("/post.json", data={ "foo": "bar" }), { "code": 200, "method": "post", "data": { "foo":"bar" } }) self.assertEqual(self.maxcdn.post("/post.json", params={ "foo": "bar" }), { "code": 200, "method": "post", "data": { "foo":"bar" } }) self.assertEqual(self.maxcdn.post("/post.json", params="foo=bar"), { "code": 200, "method": "post", "data": { "foo":"bar" } }) def test_put(self): requests.Session.request = mock.create_autospec(mock_request, return_value=response("put")) self.assertEqual(self.maxcdn.put("/put.json"), { "code": 200, "method": "put", "data": { "foo":"bar" } }) def test_purge(self): requests.Session.request = mock.create_autospec(mock_request, return_value=response("purge")) self.assertEqual(self.maxcdn.purge("/purge.json"), { "code": 200, "method": "purge", "data": { "foo":"bar" } }) def test_delete(self): requests.Session.request = mock.create_autospec(mock_request, return_value=response("delete")) self.assertEqual(self.maxcdn.delete("/delete.json"), { "code": 200, "method": "delete", "data": { "foo":"bar" } }) self.assertEqual(self.maxcdn.patch("/delete.json", file_or_files="/foo.css"), { "code": 200, "method": "delete", "data": { "foo":"bar" } }) self.assertEqual(self.maxcdn.patch("/delete.json", file_or_files=["/foo.css", "/bar.css"]), { "code": 200, "method": "delete", "data": { "foo":"bar" } }) def test_purge(self): requests.Session.request = mock.create_autospec(mock_request, return_value=response("delete")) self.assertEqual(self.maxcdn.purge(12345), { "code": 200, "method": "delete", "data": { "foo":"bar" } }) self.assertEqual(self.maxcdn.purge(12345, "/master.css"), { "code": 200, "method": "delete", "data": { "foo":"bar" } }) self.assertEqual(self.maxcdn.purge(12345, ["/master.css", "/other.css"]), { "code": 200, "method": "delete", "data": { "foo":"bar" } })
if not "ALIAS" in env or not "KEY" in env or not "SECRET" in env: print(dedent("""\ Usage: report.py [monthly|daily|hourly] Add credentials to your environment like so: $ export ALIAS=<alias> $ export KEY=<key> $ export SECRET=<secret> """)) exit(1) maxcdn = MaxCDN(env["ALIAS"], env["KEY"], env["SECRET"]) zones = maxcdn.get("/zones/pull.json") for zone in zones["data"]["pullzones"]: print("Zone report for: %s (%s)" % ( zone["name"], zone["url"])) # summary fetch = maxcdn.get("/reports/%s/stats.json%s" % (zone["id"], report)) for key, val in fetch["data"]["summary"].items(): print(" - %s: %s" % (key, val)) # popularfiles print(" ") print("Popular Files:") fetch = maxcdn.get("/reports/%s/popularfiles.json?page_size=10" % (zone["id"])) for file in fetch["data"]["popularfiles"]: