Skip to content

Commit

Permalink
TypeError Handling (#61)
Browse files Browse the repository at this point in the history
Fix TypeError on 3.5.x (RasPi, Stretch)
  • Loading branch information
brentru authored Jul 17, 2018
1 parent 9ef016e commit 80b455a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Adafruit_IO/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.0.0"
__version__ = "2.0.11"
5 changes: 4 additions & 1 deletion Adafruit_IO/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,12 @@ def _compose_url(self, path, is_time=None):


def _handle_error(self, response):
# Handle explicit errors.
# Throttling Error
if response.status_code == 429:
raise ThrottlingError()
# Resource on AdafruitIO not Found Error
elif response.status_code == 400:
raise RequestError(response)
# Handle all other errors (400 & 500 level HTTP responses)
elif response.status_code >= 400:
raise RequestError(response)
Expand Down
6 changes: 3 additions & 3 deletions Adafruit_IO/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import json
import json, requests

# MQTT RC Error Types
MQTT_ERRORS = [ 'Connection successful',
Expand All @@ -42,9 +42,9 @@ def __init__(self, response):
response.status_code, response.reason, error_message))

def _parse_error(self, response):
content = response.json()
try:
content = json.loads(response.content)
return ' - '.join(content['error'])
return content['error']
except ValueError:
return ""

Expand Down
24 changes: 19 additions & 5 deletions examples/api/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Author: Tony DiCola

# Import Adafruit IO REST client.
from Adafruit_IO import Client
from Adafruit_IO import Client, RequestError, Feed

# Set to your Adafruit IO key.
# Remember, your key is a secret,
Expand All @@ -17,20 +17,34 @@
# Create an instance of the REST client.
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

# Assign a foo feed, if one exists already
try:
foo = aio.feeds('foo')
except RequestError: # Doesn't exist, create a new feed
feed = Feed(name="foo")
foo = aio.create_feed(feed)

# Assign a test feed, if one exists already
try:
test = aio.feeds('test')
except RequestError: # Doesn't exist, create a new feed
feed = Feed(name="test")
test = aio.create_feed(feed)

# Send a value to the feed 'Test'.
aio.send_data('Test', 42)
aio.send_data(test.key, 42)

# Send a string value 'bar' to the feed 'Foo'.
aio.send_data('Foo', 'bar')
aio.send_data(foo.key, 'bar')

# Now read the most recent value from the feed 'Test'. Notice that it comes
# back as a string and should be converted to an int if performing calculations
# on it.
data = aio.receive('Test')
data = aio.receive(test.key)
print('Retrieved value from Test has attributes: {0}'.format(data))
print('Latest value from Test: {0}'.format(data.value))

# Finally read the most revent value from feed 'Foo'.
data = aio.receive('Foo')
data = aio.receive(foo.key)
print('Retrieved value from Foo has attributes: {0}'.format(data))
print('Latest value from Foo: {0}'.format(data.value))
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
'License :: OSI Approved :: MIT License',
'Intended Audience :: Developers',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Topic :: Home Automation',
'Topic :: Software Development']
Expand All @@ -59,8 +61,6 @@

version = verstr,
install_requires = ["requests", "paho-mqtt"],
python_requires = ">=3.6.0",



packages = ['Adafruit_IO'],
Expand Down

0 comments on commit 80b455a

Please sign in to comment.