Created
May 23, 2012 03:42
-
-
Save jdorfman/2773142 to your computer and use it in GitHub Desktop.
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
#NetDNA API Sample Code - Python | |
#Version 1.0a | |
# Thanks to @sajal from TurboBytes.com for getting this script started | |
# Contributed: @jdorfman & the @netdna family | |
import oauth.oauth as oauth | |
import httplib2, json | |
import pprint | |
class NetDNAREST(): | |
def __init__(self, key, secret, alias, server="rws.netdna.com"): | |
self.alias = alias | |
self.server = server | |
self.token = None | |
self.http = httplib2.Http() | |
self.signature_method_hmac_sha1 = oauth.OAuthSignatureMethod_HMAC_SHA1() | |
self.consumer = oauth.OAuthConsumer(key, secret) | |
def request(self, method, endpoint, params=None): | |
oauth_request = oauth.OAuthRequest.from_consumer_and_token(self.consumer, token=self.token, http_url=endpoint, http_method=method, parameters=params) | |
oauth_request.sign_request(self.signature_method_hmac_sha1, self.consumer, self.token) | |
r, c = self.http.request(oauth_request.to_url(), method=oauth_request.http_method,body=None, headers=oauth_request.to_header()) | |
if r["status"] != "200": | |
raise Exception(r["status"], r, c) | |
return json.loads(c) | |
def get(self, uri): | |
endpoint = "https://%s/%s%s" %(self.server, self.alias, uri) | |
print "\n" | |
print endpoint | |
return self.request("GET", endpoint) | |
def get_day_stats_per_node(self, zoneid, date): | |
return api.get("/zones/%s/nodes.json/stats/daily?date_from=%s&date_to=%s" %(zoneid, date, date)) | |
def get_zone_stats(self, zoneid, reporttype, date): | |
ds = api.get("/reports/%s/stats.json/%s?date_from=%s&date_to=%s" %(zoneid, reporttype, date, date)) | |
for stat in ds["data"]["stats"]: | |
if stat["timestamp"] == date: | |
return stat | |
return None | |
if __name__ in "__main__": | |
api = NetDNAREST("xxxxxxxxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxxxxxxx", "companyalias") | |
# Accounts API | |
pprint.pprint(api.get("/account.json")) | |
pprint.pprint(api.get("/account.json/address")) | |
# Users API | |
pprint.pprint(api.get("/users.json")) | |
pprint.pprint(api.get("/users.json/7385")) | |
# Zones API | |
pprint.pprint(api.get("/zones.json")) | |
pprint.pprint(api.get("/zones.json/summary")) | |
pprint.pprint(api.get("/zones.json/count")) | |
# Pull Zone API | |
pprint.pprint(api.get("/zones/pull.json")) | |
pprint.pprint(api.get("/zones/pull.json/count")) | |
pprint.pprint(api.get("/zones/pull.json/15603")) | |
# Pull Zone Custom Domains API | |
pprint.pprint(api.get("/zones/pull/15603/customdomains.json")) | |
# Push Zone API | |
pprint.pprint(api.get("/zones/push.json")) | |
pprint.pprint(api.get("/zones/push.json/count")) | |
pprint.pprint(api.get("/zones/push.json/17260")) | |
# Push Zone Custom Domains API | |
pprint.pprint(api.get("/zones/push/17260/customdomains.json")) | |
# VOD Zone API | |
pprint.pprint(api.get("/zones/vod.json")) | |
pprint.pprint(api.get("/zones/vod.json/count")) | |
pprint.pprint(api.get("/zones/vod.json/17264")) | |
# Live Zone API | |
pprint.pprint(api.get("/zones/live.json")) | |
pprint.pprint(api.get("/zones/live.json/count")) | |
pprint.pprint(api.get("/zones/live.json/26876")) | |
# Zones SSL API | |
pprint.pprint(api.get("/zones/pull/21826/ssl.json")) | |
# Zone Stats API | |
pprint.pprint(api.get_zone_stats(15603, "hourly", "2012-05-19")) | |
pprint.pprint(api.get_zone_stats(15603, "daily", "2012-05-19")) | |
pprint.pprint(api.get_zone_stats(15603, "monthly", "2012-04-19")) | |
# Zone Nodes API | |
pprint.pprint(api.get("/zones/nodes.json")) | |
## todo - http://developer.netdna.com/api/console#!/reports/stats | |
# Zone Popular Files API | |
pprint.pprint(api.get("/reports/popularfiles.json")) | |
pprint.pprint(api.get("/reports/pull/popularfiles.json")) | |
pprint.pprint(api.get("/reports/push/popularfiles.json")) | |
# Zone Status Codes API | |
pprint.pprint(api.get("/reports/statuscodes.json")) | |
pprint.pprint(api.get("/reports/21826/statuscodes.json")) | |
pprint.pprint(api.get("/reports/pull/statuscodes.json")) | |
pprint.pprint(api.get("/reports/push/statuscodes.json")) | |
pprint.pprint(api.get("/reports/pull/21826/statuscodes.json")) | |
pprint.pprint(api.get("/reports/push/27296/statuscodes.json")) | |
# Zone File Types API | |
pprint.pprint(api.get("/reports/filetypes.json")) | |
pprint.pprint(api.get("/reports/21826/filetypes.json")) | |
pprint.pprint(api.get("/reports/pull/filetypes.json")) | |
pprint.pprint(api.get("/reports/push/filetypes.json")) | |
pprint.pprint(api.get("/reports/pull/21826/filetypes.json")) | |
pprint.pprint(api.get("/reports/push/27296/filetypes.json")) | |
# Zone File Size Ranges API | |
pprint.pprint(api.get("/reports/filesizes.json")) | |
pprint.pprint(api.get("/reports/21826/filesizes.json")) | |
pprint.pprint(api.get("/reports/pull/filesizes.json")) | |
pprint.pprint(api.get("/reports/push/filesizes.json")) | |
pprint.pprint(api.get("/reports/pull/21826/filesizes.json")) | |
pprint.pprint(api.get("/reports/push/27296/filesizes.json")) | |
# Zone Stats by Directory API | |
pprint.pprint(api.get("/reports/statsbydir.json")) | |
pprint.pprint(api.get("/reports/pull/statsbydir.json")) | |
pprint.pprint(api.get("/reports/push/statsbydir.json")) | |
# Live Zone Stats API | |
pprint.pprint(api.get("/zones/live/connectionstats.json/hourly")) | |
pprint.pprint(api.get("/zones/live/connectionstats.json/daily")) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment