Skip to content

Commit 51814e8

Browse files
committed
Replacing POST with GET on API
1 parent 80065f4 commit 51814e8

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

.travis.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
language: python
2+
sudo: false
3+
24
python:
35
- "2.7"
6+
47
install:
5-
- "pip install ."
68
- "pip install -r requirements.txt"
9+
- "pip install ."
10+
711
script:
8-
- python -m unittest discover -v
12+
- python -m unittest discover -v

databox test/test_push.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import unittest
22
from databox import *
33
from pprint import pprint as pp
4+
from os import getenv
45

56

67
def mock_push_json(data=None, path='/'):
@@ -9,7 +10,7 @@ def mock_push_json(data=None, path='/'):
910

1011
class TestPush(unittest.TestCase):
1112
def setUp(self):
12-
self.databox_push_token = "adxg1kq5a4g04k0wk0s4wkssow8osw84"
13+
self.databox_push_token = getenv("DATABOX_PUSH_TOKEN") or "adxg1kq5a4g04k0wk0s4wkssow8osw84"
1314
self.client = Client(self.databox_push_token)
1415

1516
self.original_push_json = self.client._push_json
@@ -57,7 +58,7 @@ def test_insert_all(self):
5758

5859

5960
def test_last_push(self):
60-
self.client._push_json = lambda data=None, path='/': {
61+
self.client._get_json = lambda path='/': {
6162
'err': [],
6263
'no_err': 0
6364
}
@@ -66,7 +67,7 @@ def test_last_push(self):
6667

6768

6869
def test_last_push_with_number(self):
69-
self.client._push_json = lambda data=None, path='/': path
70+
self.client._get_json = lambda data=None, path='/': path
7071
assert self.client.last_push(3) == '/lastpushes/3'
7172

7273

@@ -82,7 +83,7 @@ def test_short(self):
8283
},
8384
], token=self.databox_push_token) is True
8485

85-
Client._push_json = lambda data=None, path='/': {
86+
Client._get_json = lambda number=None, path='/': {
8687
'err': [],
8788
'no_err': 0
8889
}

databox/__init__.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ def _push_json(self, data=None, path="/"):
6464

6565
return response.json()
6666

67+
def _get_json(self, path):
68+
response = requests.get(
69+
self.push_host + path,
70+
auth=HTTPBasicAuth(self.push_token, ''),
71+
headers={
72+
'Content-Type': 'application/json',
73+
'User-Agent': "Databox/" + __version__ + " (Python)"
74+
}
75+
)
76+
77+
return response.json()
78+
6779
def push(self, key, value, date=None, attributes=None):
6880
self.last_push_content = self._push_json({
6981
'data': [self.process_kpi(
@@ -84,7 +96,7 @@ def insert_all(self, rows):
8496
return self.last_push_content['status'] == 'ok'
8597

8698
def last_push(self, number=1):
87-
return self._push_json(path='/lastpushes/{n}'.format(**{'n': number}))
99+
return self._get_json(path='/lastpushes/{n}'.format(**{'n': number}))
88100

89101

90102
def push(key, value, date=None, token=None):

example.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
from datetime import date, timedelta
33
from random import randint
44
from databox import insert_all
5+
from os import getenv
56

67
"""
78
This example generates fake temperature KPIs for last 14 days and inserts them.
89
"""
910

10-
TOKEN = "adxg1kq5a4g04k0wk0s4wkssow8osw84"
11+
TOKEN = getenv("DATABOX_PUSH_TOKEN") or "adxg1kq5a4g04k0wk0s4wkssow8osw84"
1112

1213
from databox import Client
1314

@@ -27,7 +28,6 @@
2728
#
2829
#
2930

30-
3131
print client.insert_all([
3232
{'key': 'temp.boston', 'value': 51},
3333
{'key': 'temp.boston', 'value': 49, 'date': '2015-01-01 17:00:00'},
@@ -37,5 +37,7 @@
3737
}},
3838
])
3939

40+
print "--------"
41+
4042
print client.last_push(3)
4143

0 commit comments

Comments
 (0)