-
Notifications
You must be signed in to change notification settings - Fork 0
HTTP Responses
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.
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}
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}
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)The picosapi module was originally developed by Braden Hitchcock while taking a distributed system design class at Brigham Young University in 2018. It is free to use and distribute under the MIT License.
If you wish to contribute to the project, please fork the repository and create pull requests that can be merged into later versions.
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.