Skip to content

Instantly share code, notes, and snippets.

@tmarkiewicz
Created December 9, 2010 21:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmarkiewicz/735347 to your computer and use it in GitHub Desktop.
Save tmarkiewicz/735347 to your computer and use it in GitHub Desktop.
Python code to access the StatsMix API
# 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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment