Skip to content

Commit 2e35caf

Browse files
committed
Initial commit
1 parent 6020e79 commit 2e35caf

File tree

9 files changed

+164
-1
lines changed

9 files changed

+164
-1
lines changed

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) <year> <copyright holders>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,35 @@
1-
# push-api-python
1+
Databox bindings for Python
2+
===========================
3+
4+
Setup
5+
-----
6+
7+
You can install this package by cloning this directory and running:
8+
9+
```$ python setup.py install```
10+
11+
Getting Databox access tokens
12+
-----------------------------
13+
14+
When in Databox Designer go to Account > Access tokens, then either create a new token or get already existing one.
15+
16+
17+
Using the Databox Python library
18+
--------------------------------
19+
20+
from databox import PushClient
21+
22+
# instantiate PushClient
23+
client = PushClient('<access token>')
24+
25+
# push metric "answer1" with value 42 for today
26+
client.add(42, "answer1")
27+
28+
# push metric "answer1" with value 42 for any other day
29+
client.add(42, "answer1", "2015-05-01 10:10:10")
30+
31+
# send to Databox
32+
print client.send()
33+
34+
# get last push
35+
print client.lastPush()

databox/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Databox Python bindings
2+
# Authors:
3+
# Vlada Petrovic <[email protected]>
4+
5+
__package_name__ = 'databox'
6+
__version__ = '0.1'
7+
__author__ = 'Vlada Petrovic'
8+
__author_email__ = '[email protected]'
9+
__description__ = (
10+
'''Push metrics to Databox. ''')
11+
__url__ = 'https://github.com/databox/databox-python'

databox/__init__.pyc

437 Bytes
Binary file not shown.

databox/client.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import json
2+
import requests
3+
from requests.auth import HTTPBasicAuth
4+
5+
class PushClient(object):
6+
metrics = {"data":[]}
7+
8+
def __init__(self, api_key):
9+
self.api_key = api_key
10+
self.host = "https://push2new.databox.com"
11+
12+
def add(self, value, metric_key, date=None):
13+
item = {}
14+
15+
# Add date if provided
16+
if date is not None:
17+
item["date"] = date
18+
19+
# Add value
20+
item["$" + metric_key] = value
21+
22+
# Add to metrics
23+
self.metrics["data"].append(item)
24+
25+
def addWithAttribute(self, value, metric_key, attribute_value, attribute_name, date=None):
26+
item = {}
27+
28+
# Add date if provided
29+
if date is not None:
30+
item["date"] = date
31+
32+
# Add value
33+
item["$" + metric_key] = value
34+
35+
# Add attribute
36+
item[attribute_name] = attribute_value
37+
38+
# Add to metrics
39+
self.metrics["data"].append(item)
40+
41+
def send(self):
42+
payload = json.dumps(self.metrics)
43+
response = requests.post(
44+
self.host,
45+
auth = HTTPBasicAuth(self.api_key, ""),
46+
headers = {"Content-Type": "application/json"},
47+
data = payload
48+
)
49+
50+
return response.json()
51+
52+
def lastPush(self):
53+
response = requests.post(
54+
self.host + "/lastpushes/1",
55+
auth = HTTPBasicAuth(self.api_key, ""),
56+
headers = {"Content-Type": "application/json"}
57+
)
58+
59+
return response.json()

databox/client.pyc

2.09 KB
Binary file not shown.

sample1.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import json
2+
from databox.client import PushClient
3+
4+
# Create Push object
5+
pc = PushClient("339fb7fcxssg0sk48wocogo8wsck8408")
6+
7+
# Add metrics
8+
pc.add(576.45, "sales.total", "2015-05-10 09:00:00")
9+
pc.add(42, "orders.total", "2015-05-10 09:00:00")
10+
11+
# Send to Databox
12+
response = pc.send()
13+
14+
if response["status"] == "ok":
15+
print "Successfully pushed metrics to Databox!"
16+
else:
17+
# get error info
18+
lastPush = pc.lastPush()
19+
print json.dumps(lastPush, indent=4)

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[metadata]
2+
description-file = README.md

setup.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import databox
2+
import setuptools
3+
4+
install_requires = []
5+
install_requires.append('requests >= 2.7')
6+
7+
setuptools.setup(
8+
name=databox.__package_name__,
9+
version=databox.__version__,
10+
author=databox.__author__,
11+
author_email=databox.__author_email__,
12+
description=databox.__description__,
13+
url=databox.__url__,
14+
license='MIT',
15+
packages=setuptools.find_packages(exclude=('tests',)),
16+
install_requires=install_requires
17+
)

0 commit comments

Comments
 (0)