public
Last active

Python code to access the StatsMix API

  • Download Gist
statsmix.py
Python
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
# send a new stat to a metric
import httplib
import urllib
params = urllib.urlencode({'metric_id': XXXXX, 'value': 1})
# generated_at is set to now by default, to explicitly set use the following:
# params = urllib.urlencode({'metric_id': 4877, 'value': 1, 'generated_at':'2012-07-08 12:33:53'})
headers = {'X-StatsMix-Token': 'STATSMIX_API_KEY'}
conn = httplib.HTTPConnection('api.statsmix.com:80')
conn.request('POST', '/api/v2/stats', params, headers)
response = conn.getresponse()
print response.status, response.reason
print response.read()
conn.close()
 
# send a new stat to a metric using the track method
# unles a profile_id is explicity set, the metric will be added to the first profile in the account
import httplib
import urllib
params = urllib.urlencode({'name': 'Python Test Metric', 'value': 1})
headers = {'X-StatsMix-Token': 'STATSMIX_API_KEY'}
conn = httplib.HTTPConnection('api.statsmix.com:80')
conn.request('POST', '/api/v2/track', params, headers)
response = conn.getresponse()
print response.status, response.reason
print response.read()
conn.close()
 
# delete a stat from a metric
params = None
headers = {'X-StatsMix-Token': 'STATSMIX_API_KEY'}
stat_id = XXXXX
conn = httplib.HTTPConnection('api.statsmix.com:80')
conn.request('DELETE', '/api/v2/stats/%d/' % stat_id, params, headers)
response = conn.getresponse()
print response.status, response.reason
conn.close()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.