Skip to content

Commit

Permalink
added find method
Browse files Browse the repository at this point in the history
  • Loading branch information
felipefdl committed Aug 10, 2015
1 parent 822160c commit 2009507
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 4 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ else:

```

##### Find Data
**.find(OBJECT)**
``` python
result = my_device.find({'query': 'last_value'})
if result['status']:
print result['result'] # Array with data
else:
print result['result'] # Error (if status is False)

```

##### Delete Data
**.delete(/ID/)**
``` python
Expand Down
12 changes: 9 additions & 3 deletions tago/device/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
from socket import TagoRealTime

API_TAGO = os.environ.get('TAGO_SERVER') or 'https://api.tago.io/'
API_TAGO = os.environ.get('TAGO_SERVER') or 'https://api.tago.io'
REALTIME = os.environ.get('TAGO_REALTIME') or 'realtime.tago.io'
REALTIME_PORT = int(os.environ.get('TAGO_REALTIME_PORT') or 80)

Expand All @@ -14,14 +14,17 @@ def __init__(self, token):

def handle_url(self, id):
if id:
url = API_TAGO + 'data/' + id
url = '{api_endpoint}/data/{id}'.format(api_endpoint=API_TAGO,id=id)
else:
url = API_TAGO + 'data/'
url = '{api_endpoint}/data'.format(api_endpoint=API_TAGO,id=id)
return url

def api_data_post(self, data):
return requests.post(self.handle_url(id=False), data=json.dumps(data), headers=self.default_headers).json()

def api_data_get(self, query):
return requests.get(self.handle_url(id=False), params=query, headers=self.default_headers).json()

def api_data_update(self, data, id):
return requests.put(self.handle_url(id=id), data=json.dumps(data), headers=self.default_headers).json()

Expand All @@ -31,6 +34,9 @@ def api_data_delete(self, id):
def insert(self, data):
return self.api_data_post(data)

def find(self, query):
return self.api_data_get(query)

def update(self, data, id=False):
return self.api_data_update(data, id)

Expand Down
12 changes: 12 additions & 0 deletions tests/test_delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from tago import Tago
import os

TOKEN = os.environ.get('TAGO_TOKEN_DEVICE') or 'TOKEN'

def test_insert():
result = Tago(TOKEN).device.delete()
print result
if result['status']:
assert True
else:
assert False
12 changes: 12 additions & 0 deletions tests/test_get.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from tago import Tago
import os

TOKEN = os.environ.get('TAGO_TOKEN_DEVICE') or 'TOKEN'

def test_insert():
result = Tago(TOKEN).device.find({'query': 'last_value'})
print result
if result['status']:
assert True
else:
assert False
2 changes: 1 addition & 1 deletion tests/test_insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
TOKEN = os.environ.get('TAGO_TOKEN_DEVICE') or 'TOKEN'

def test_insert():
result = Tago(TOKEN).device.insert({'variable': 'py', 'value': 2})
result = Tago(TOKEN).device.insert({'variable': 'test', 'value': 2})
if result['status']:
assert True
else:
Expand Down
12 changes: 12 additions & 0 deletions tests/test_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from tago import Tago
import os

TOKEN = os.environ.get('TAGO_TOKEN_DEVICE') or 'TOKEN'

def test_insert():
result = Tago(TOKEN).device.update({'variable': 'test', 'value': 3})
print result
if result['status']:
assert True
else:
assert False

0 comments on commit 2009507

Please sign in to comment.