From d2286d002b9f03ef8de8c1035a216fa60ddb068d Mon Sep 17 00:00:00 2001 From: Oto Brglez Date: Thu, 5 Mar 2020 13:37:28 +0100 Subject: [PATCH] Minor enhancements. --- .gitignore | 4 ++ .travis.yml | 1 + databox/__init__.py | 71 +++++++++++++--------------- example.py | 14 ++---- {databox test => tests}/__init__.py | 0 {databox test => tests}/test_push.py | 42 +++++++--------- 6 files changed, 60 insertions(+), 72 deletions(-) rename {databox test => tests}/__init__.py (100%) rename {databox test => tests}/test_push.py (57%) diff --git a/.gitignore b/.gitignore index 2f3c113..f669718 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ +# Idea +*.idea +*.iml + # Source: https://github.com/github/gitignore/blob/master/Python.gitignore *.xml *.pid diff --git a/.travis.yml b/.travis.yml index f50d641..3ad7559 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,7 @@ python: - "3.4" - "3.5" - "3.6" + - "3.7" install: - "pip install -r requirements.txt" diff --git a/databox/__init__.py b/databox/__init__.py index eeb9d9e..682c79c 100644 --- a/databox/__init__.py +++ b/databox/__init__.py @@ -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' @@ -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) @@ -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) diff --git a/example.py b/example.py index 83d6b0a..5b6719d 100755 --- a/example.py +++ b/example.py @@ -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 """ @@ -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([ @@ -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()) diff --git a/databox test/__init__.py b/tests/__init__.py similarity index 100% rename from databox test/__init__.py rename to tests/__init__.py diff --git a/databox test/test_push.py b/tests/test_push.py similarity index 57% rename from databox test/test_push.py rename to tests/test_push.py index b39580a..e5ec536 100644 --- a/databox test/test_push.py +++ b/tests/test_push.py @@ -1,33 +1,30 @@ 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( @@ -35,33 +32,26 @@ def test_push_validation(self): 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'