Reader small image

You're reading from  Practical Python Programming for IoT

Product typeBook
Published inNov 2020
Reading LevelIntermediate
PublisherPackt
ISBN-139781838982461
Edition1st Edition
Languages
Right arrow

The get() class method

The get() class method handles HTTP GET requests for our LEDControl resource. It's what handled our URL request when we tested the API previously with the following command:

$ curl -X GET http://localhost:5000/led

get() simply returns, on line (13), the global state variable:

    def get(self):
""" Handles HTTP GET requests to return current LED state."""
return state # (13)

Flask-RESTful returns JSON responses to clients, and that's why we return the state variable. In Python, state is a dictionary structure that can be mapped directly into a JSON format. We saw the following JSON example previously when we make a GET request using curl:

{ "level": 50 }

This class-as-a-resource (for example, LEDControl) and method-to-HTTP-method mapping (for example, LEDControl.get()) is an example of how the Flask-RESTful extension makes RESTful API development...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Practical Python Programming for IoT
Published in: Nov 2020Publisher: PacktISBN-13: 9781838982461