Skip to content

Commit

Permalink
typeerror handling fix
Browse files Browse the repository at this point in the history
  • Loading branch information
brentru committed Jul 16, 2018
1 parent 9ef016e commit 21c49b1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
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))

0 comments on commit 21c49b1

Please sign in to comment.