Skip to content

HTTP Responses

hitch-b24 edited this page Mar 6, 2018 · 1 revision

To provide simplicity, the HTTP request functions in the picosapi module will return a Response object depending on whether the request was successful or not.

Successful Requests

When the response indicates success, a SuccessResponse object will be returned from the HTTP request functions. This object has two members:

  • code - the status code of the response
  • content - a Python dictionary representing the JSON returned from the resource

Printing the object or using the str() function will produce a pretty-printed string formatted as follows:

[SUCCESS] :: Response Code: {code}
{content}

Failed Requests

When the response indicates failure or error, an ErrorResponse object will be returned from the HTTP request functions. This object has three members:

  • code - the status code of the response
  • content - a Python dictionary representing the JSON returned from the resource, or the JSON constructed by the HTTP request function.
  • message - a string summarizing the reason for the error

Printing the object or using the str() function will produce a pretty-printed string formatted as follows:

[ERROR] {message} :: Response Code: {code}
{content}

Examples

Below is an example of using the picosapi response objects with a pico that manages several other picos each representing a person (NOTE: here we use Python 3, but picosapi is compatible with Python 2.7 and up):

import picosapi as picos
...
url = picos.api_url(my_picos_eci, "people_manager", "people")
# This should return a JSON array of people
ok, response = picos.get(url)
if ok:
  name = response.content[0]["name"]
  age = response.content[0]["age"]
  ...
else:
  print(response)

Picos API Documentation

A Note About Picos

Picos are useful in building Internet of Things (IoT) technology that preserves personal freedom. They are currently being developed under the direction of Dr. Phil Windley by Pico Labs at Brigham Young University. Picos is an actor-based programming system that supports people-centric, reactive programming on the Internet of Things.

For more information about Picos, visit Pico Labs or see Dr. Windley's article on Reactive Programming with Picos.

Clone this wiki locally