Skip to content

Commit 300b268

Browse files
committed
initial working library
1 parent b3e145d commit 300b268

File tree

11 files changed

+222
-2
lines changed

11 files changed

+222
-2
lines changed

.gitignore

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#Useful gitignore for python: https://github.com/github/gitignore/blob/master/Python.gitignore
2+
*.py[cod]
3+
4+
# C extensions
5+
*.so
6+
7+
# Packages
8+
*.egg
9+
*.egg-info
10+
dist
11+
build
12+
eggs
13+
parts
14+
bin
15+
var
16+
sdist
17+
develop-eggs
18+
.installed.cfg
19+
lib
20+
lib64
21+
__pycache__
22+
distribute-0.*
23+
24+
# Unit test / coverage reports
25+
.coverage
26+
.tox
27+
nosetests.xml
28+
29+
# Translations
30+
*.mo

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
0.0.1
2+
----
3+
Initial Changelog

LICENSE.md

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

LICENSE.txt

Whitespace-only changes.

Makefile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
publish: clean
2+
python setup.py sdist upload
3+
4+
clean:
5+
#rm -rf Adafruit_BBIO.* build dist
6+
rm -f *.pyo
7+
rm -f *.egg
8+
tests:
9+
py.test
10+
11+
build:
12+
python setup.py build --force
13+
14+
install: build
15+
python setup.py install --force

README.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Adafruit_IO
2+
3+
TODO: Write a description
4+
5+
## Installation
6+
7+
pip install Adafruit_IO
8+
9+
## Usage
10+
11+
Easiest
12+
13+
from Adafruit_IO import Client
14+
aio = Client('unique_key_id')
15+
#data can be of any type, string, number, hash, json
16+
aio.send("Feed Name", data)
17+
18+
#You can also receive data easily:
19+
value = aio.receive("Feed Name")
20+
21+
#It will ge the next input available, and mark it as read.
22+
23+
24+
Advanced
25+
26+
from Adafruit_IO import Client
27+
aio = Client('unique_key_id')
28+
29+
#get all of your feeds for the key
30+
aio.feeds
31+
32+
#get a specific feed using ID or Name
33+
aio.feeds(3)
34+
aio.feeds("feed name")
35+
36+
#create a feed
37+
aio.create_feed({:name => "New Feed Name", ...})
38+
39+
## Contributing
40+
41+
1. Fork it ( http://github.com/adafruit/io-client-python/fork )
42+
2. Create your feature branch (`git checkout -b my-new-feature`)
43+
3. Commit your changes (`git commit -am 'Add some feature'`)
44+
4. Push to the branch (`git push origin my-new-feature`)
45+
5. Create new Pull Request

adafruit_io/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .client import Client

adafruit_io/client.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import json
2+
3+
from urllib3 import connection_from_url
4+
from urllib import urlencode, quote
5+
6+
#fork of ApiClient Class: https://github.com/shazow/apiclient
7+
class Client(object):
8+
BASE_URL = 'http://localhost:3002/'
9+
10+
def __init__(self, key, rate_limit_lock=None):
11+
self.key = key
12+
self.rate_limit_lock = rate_limit_lock
13+
self.connection_pool = self._make_connection_pool(self.BASE_URL)
14+
15+
def _make_connection_pool(self, url):
16+
return connection_from_url(url)
17+
18+
def _compose_url(self, path):
19+
return self.BASE_URL + path
20+
21+
def _compose_get_url(self, path, params=None):
22+
return self.BASE_URL + path + '?' + urlencode(params)
23+
24+
def _handle_response(self, response):
25+
return json.loads(response.data)
26+
27+
def _request(self, method, path, params=None):
28+
if (method.lower() == "get"):
29+
url = self._compose_get_url(path, params)
30+
else:
31+
url = self._compose_url(path)
32+
33+
self.rate_limit_lock and self.rate_limit_lock.acquire()
34+
headers = {"X-Api-Key": self.key, 'Content-Type':'application/json'}
35+
if (method.upper() == "GET"):
36+
r = self.connection_pool.urlopen(method.upper(), url, headers=headers)
37+
else:
38+
r = self.connection_pool.urlopen(method.upper(), url, headers=headers, body=json.dumps(params))
39+
40+
return self._handle_response(r)
41+
42+
def _get(self, path, **params):
43+
return self._request('GET', path, params=params)
44+
45+
def _post(self, path, params):
46+
return self._request('POST', path, params=params)
47+
48+
def send(self, feed_name, data):
49+
feed_name = quote(feed_name)
50+
path = "api/feeds/{}/streams/send".format(feed_name)
51+
return self._post(path, {'value': data})
52+
53+
def receive(self, feed_name):
54+
feed_name = quote(feed_name)
55+
path = "api/feeds/{}/streams/last".format(feed_name)
56+
return self._get(path)
57+
58+
def receive_next(self, feed_name):
59+
feed_name = quote(feed_name)
60+
path = "api/feeds/{}/streams/next".format(feed_name)
61+
return self._get(path)
62+
63+
def receive_previous(self, feed_name):
64+
feed_name = quote(feed_name)
65+
path = "api/feeds/{}/streams/last".format(feed_name)
66+
return self._get(path)
67+
68+
def streams(self, feed_id_or_key, stream_id=None):
69+
if stream_id is None:
70+
path = "api/feeds/{}/streams".format(feed_id_or_key)
71+
else:
72+
path = "api/feeds/{}/streams/{}".format(feed_id_or_key, stream_id)
73+
return self._get(path)
74+
75+
def create_stream(self, feed_id_or_key, data):
76+
path = "api/feeds/{}/streams".format(feed_id_or_key)
77+
return self._post(path, data)
78+

setup.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
version='0.0.1',
66
author='Justin Cooper',
77
author_email='justin@adafruit.com',
8-
packages=['adafruit_io'],
8+
packages=['Adafruit_IO'],
99
url='http://pypi.python.org/pypi/adafruit_io/',
1010
license='LICENSE.txt',
1111
description='IO Client library for io.adafruit.com',
1212
long_description=open('README.md').read(),
13-
install_requires=[],
13+
install_requires=[
14+
"apiclient >= 1.0.2"
15+
],
1416
)

test.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from Adafruit_IO import Client
2+
io = Client('e2b0fac48ae32f324df4aa05247c16e991494b08')
3+
4+
r = io.send("Test Python", 12)
5+
print r
6+
7+
r = io.receive("Test Python")
8+
print r
9+
10+
r = io.receive_next("Test Python")
11+
print r

tests/test_setup.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pytest
2+
3+
from Adafruit_IO import Client
4+
5+
def teardown_module(module):
6+
pass
7+
8+
class TestSetup:
9+
def test_set_key(self):
10+
key = "unique_key_id"
11+
io = Client(key)
12+
assert key == io.key

0 commit comments

Comments
 (0)