Skip to content

Instantly share code, notes, and snippets.

@lukasklein
Last active March 6, 2020 10:18
Show Gist options
  • Save lukasklein/ced155a49d9cfe8ca9ac658f4c15a295 to your computer and use it in GitHub Desktop.
Save lukasklein/ced155a49d9cfe8ca9ac658f4c15a295 to your computer and use it in GitHub Desktop.
import requests
class DatacakeApi:
api_token = None
api_base = 'https://api.datacake.co/v1'
def __init__(self, api_token):
self.api_token = api_token
def post(self, path, data):
return requests.post(f'{self.api_base}{path}', json=data, headers={
'Authorization': f'Token {self.api_token}'
})
def record_measurement(self, device_id, field, value):
"""
Records a single measurement.
Arguments:
device_id -- the uuid of the device to record to
field -- the field name
value -- the value to record
"""
return self.post(f'/devices/{device_id}/record/', {
"field": field,
"value": value,
})
def record_measurements(self, device_id, values):
"""
Records multiple measurements.
Arguments:
device_id -- the uuid of the device to record to
values -- a list of dicts of
{
field -- the field name
value -- the value to record
}
"""
return self.post(f'/devices/{device_id}/record/?batch=True', values)
"""
api = DatacakeApi(api_token='asdf123')
api.record_measurement('63e27b2b-8cd1-4503-be02-1fbb2a8dae4e', 'FLOAT_FELD', 12.34)
api.record_measurements('63e27b2b-8cd1-4503-be02-1fbb2a8dae4e', [
{
'field': 'FLOAT_FELD',
'value': 12.34,
}
])
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment