Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor enhancements. #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Idea
*.idea
*.iml

# Source: https://github.com/github/gitignore/blob/master/Python.gitignore
*.xml
*.pid
Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ python:
- "3.4"
- "3.5"
- "3.6"
- "3.7"

install:
- "pip install -r requirements.txt"
Expand Down
71 changes: 34 additions & 37 deletions databox/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import requests
from requests.auth import HTTPBasicAuth
from os import getenv
from json import dumps as json_dumps

from .__version__ import __version__


class Client(object):
push_token = None
push_host = 'https://push.databox.com'
Expand Down Expand Up @@ -54,62 +53,57 @@ def _push_json(self, data=None, path="/"):
data = json_dumps(data)

response = requests.post(
self.push_host + path,
auth=HTTPBasicAuth(self.push_token, ''),
headers={
'Content-Type': 'application/json',
'User-Agent': 'databox-python/' + __version__,
'Accept': 'application/vnd.databox.v' + __version__.split('.')[0] + '+json'
},
data=data
self.push_host + path,
auth=requests.HTTPBasicAuth(self.push_token, ''),
headers={
'Content-Type': 'application/json',
'User-Agent': 'databox-python/' + __version__,
'Accept': 'application/vnd.databox.v' + __version__.split('.')[0] + '+json'
},
data=data
)

return response.json()

def _get_json(self, path):
response = requests.get(
self.push_host + path,
auth=HTTPBasicAuth(self.push_token, ''),
headers={
'Content-Type': 'application/json',
'User-Agent': 'databox-python/' + __version__,
'Accept': 'application/vnd.databox.v' + __version__.split('.')[0] + '+json'
}
self.push_host + path,
auth=requests.HTTPBasicAuth(self.push_token, ''),
headers={
'Content-Type': 'application/json',
'User-Agent': 'databox-python/' + __version__,
'Accept': 'application/vnd.databox.v' + __version__.split('.')[0] + '+json'
}
)

return response.json()

def _delete_json(self, path):
response = requests.delete(
self.push_host + path,
auth=HTTPBasicAuth(self.push_token, ''),
headers={
'Content-Type': 'application/json',
'User-Agent': 'databox-python/' + __version__,
'Accept': 'application/vnd.databox.v' + __version__.split('.')[0] + '+json'
}
self.push_host + path,
auth=requests.HTTPBasicAuth(self.push_token, ''),
headers={
'Content-Type': 'application/json',
'User-Agent': 'databox-python/' + __version__,
'Accept': 'application/vnd.databox.v' + __version__.split('.')[0] + '+json'
}
)

return response.json()

def push(self, key, value, date=None, attributes=None, unit=None):
self.last_push_content = self._push_json({
'data': [self.process_kpi(
key=key,
value=value,
date=date,
unit=unit,
attributes=attributes
)]
})
'data': [self.process_kpi(key=key,
value=value,
date=date,
unit=unit,
attributes=attributes
)]})

return self.last_push_content['id']

def insert_all(self, rows, forcePush=None):
payload = {
'data': [self.process_kpi(**row) for row in rows]
}

payload = {'data': [self.process_kpi(**row) for row in rows]}
if isinstance(forcePush, bool) and forcePush:
payload['meta'] = {'ensure_unique': True}
self.last_push_content = self._push_json(payload)
Expand All @@ -133,7 +127,10 @@ def push(key, value, date=None, attributes=None, unit=None, token=None):
return Client(token).push(key, value, date, attributes, unit)


def insert_all(rows=[], token=None):
def insert_all(rows=None, token=None):
if rows is None:
rows = []

return Client(token).insert_all(rows)


Expand Down
14 changes: 5 additions & 9 deletions example.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python
from datetime import date, timedelta
from random import randint
from databox import Client
from os import getenv

"""
Expand All @@ -9,10 +8,7 @@

TOKEN = getenv("DATABOX_PUSH_TOKEN") or "your_token_1234321"

from databox import Client

client = Client(TOKEN)

push = client.push('transaction', 1447.4)

pushId = client.insert_all([
Expand All @@ -25,11 +21,11 @@
{'key': 'transaction', 'value': 45.6, 'unit': 'USD'}
])

print ("Push id: ", pushId)
print("Push id: ", pushId)

# lastPushes = client.last_push(3)
# print lastPushes

print (client.get_push(pushId))
print (client.metrics())
print (client.purge())
print(client.get_push(pushId))
print(client.metrics())
print(client.purge())
File renamed without changes.
42 changes: 16 additions & 26 deletions databox test/test_push.py → tests/test_push.py
Original file line number Diff line number Diff line change
@@ -1,67 +1,57 @@
import unittest
from databox import *
from pprint import pprint as pp
from databox import Client
from os import getenv

RESPONSE_ID = '2837643'


def mock_push_json(data=None, path='/'):
return {'id': RESPONSE_ID}


class TestPush(unittest.TestCase):
def setUp(self):
self.databox_push_token = getenv("DATABOX_PUSH_TOKEN") or "your_token_1234321"
# Configuration of tokens in real world - use your own!
self.databox_push_token = getenv("DATABOX_PUSH_TOKEN") or "your_token"
self.client = Client(self.databox_push_token)

# Mocking the original methods with fake ones
self.original_push_json = self.client._push_json
self.client._push_json = mock_push_json


def test_push(self):
assert self.client.push("templj", 10.0) is RESPONSE_ID
assert self.client.push("templj", 12.0, date="2015-01-01 09:00:00") is RESPONSE_ID

assert self.client.push("temp", 10.0) is RESPONSE_ID
assert self.client.push("temp", 12.0, date="2015-01-01 09:00:00") is RESPONSE_ID

def test_push_with_attributes(self):
assert self.client.push("meta", 100, attributes={
'n': 100
}) is RESPONSE_ID

assert self.client.push("meta", 100, attributes={'n': 100}) is RESPONSE_ID

def test_push_validation(self):
self.assertRaises(
Client.KPIValidationException,
lambda: self.client.push(None, None)
)


def test_insert_all(self):
assert self.client.insert_all([
{'key': 'templj', 'value': 83.3},
{'key': 'templj', 'value': 83.3, 'date': "2015-01-01 09:00:00"},
{'key': 'templj', 'value': 12.3},
{'key': 'temp', 'value': 83.3},
{'key': 'temp', 'value': 83.3, 'date': "2015-01-01 09:00:00"},
{'key': 'temp', 'value': 12.3},
]) is RESPONSE_ID

self.assertRaises(
Client.KPIValidationException,
lambda: self.client.insert_all([
{'value': 83.3},
{'key': 'templj', 'value': 83.3, 'date': "2015-01-01 09:00:00"},
{'key': 'templj', 'value': 12.3},
])
)
values = [{'value': 83.3},
{'key': 'temp', 'value': 83.3, 'date': "2015-01-01 09:00:00"},
{'key': 'temp', 'value': 12.3}]

self.assertRaises(Client.KPIValidationException, lambda: self.client.insert_all(values))

def test_last_push(self):
self.client._get_json = lambda path='/': {
'err': [],
'no_err': 0
}
'no_err': 0}

assert self.client.last_push()['err'] == []


def test_last_push_with_number(self):
self.client._get_json = lambda data=None, path='/': path
assert self.client.last_push(3) == '/lastpushes?limit=3'