Created
September 26, 2018 20:27
-
-
Save matkoniecz/29a2a83c99ad738b1f59d89af5b2adf7 to your computer and use it in GitHub Desktop.
Python wrapper for Beeminder API requests (goal datapoints ONLY)
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
""" | |
beeminder_client.py ~ Python wrapper for Beeminder API requests (goal datapoints ONLY) | |
Usage: | |
from beeminder_client import BeeminderClient | |
beeminder = BeeminderClient() | |
timestamp = get_epoch_time_in_seconds() | |
params = { | |
'timestamp': timestamp, | |
'comment': '1 gizmos completed (Cron entry on %s)' % (get_date_string(), ), | |
'value': 1, | |
'requestid': 'cron-goalName-%s' % (timestamp, ) | |
} | |
response = beeminder.post('goalName', params=params) | |
print response.body | |
License: | |
http://www.wtfpl.net/ | |
""" | |
import unirest | |
beeminder_username = 'USERNAME HERE' | |
beeminder_token = 'TOKEN HERE' | |
class BeeminderClient(object): | |
_params = { | |
'auth_token': beeminder_token, | |
} | |
def _build_params(self, new_params): | |
for k, v in self._params.items(): | |
new_params[k] = v | |
return new_params | |
def _build_url(self, goal_str): | |
return "https://www.beeminder.com/api/v1/users/%s/goals/%s/datapoints.json" % (beeminder_username, goal_str, ) | |
def get(self, goal_str, params={}): | |
url = self._build_url(goal_str) | |
params = self._build_params(params) | |
return unirest.get(url, params=params) | |
def put(self, goal_str, params={}): | |
url = self._build_url(goal_str) | |
params = self._build_params(params) | |
return unirest.put(url, params=params) | |
def patch(self, goal_str, params={}): | |
url = self._build_url(goal_str) | |
params = self._build_params(params) | |
return unirest.patch(url, params=params) | |
def post(self, goal_str, params={}): | |
url = self._build_url(goal_str) | |
params = self._build_params(params) | |
return unirest.post(url, params=params) | |
def delete(self, goal_str, params={}): | |
url = self._build_url(goal_str) | |
params = self._build_params(params) | |
return unirest.delete(url, params=params) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment