# Labs

## RESTful Labs

## Flask-RESTful

> Flask-RESTful is an extension for Flask that adds support for quickly building REST APIs. It is a lightweight abstraction that works with your existing ORM/libraries. Flask-RESTful encourages best practices with minimal setup. If you are familiar with Flask, Flask-RESTful should be easy to pick up. [Flask-RESTful Documentation](http://flask-restful.readthedocs.org/en/latest/index.html)
>
> It’s time to write your first REST API. This guide assumes you have a working understanding of Flask, and that you have already installed both Flask and Flask-RESTful. [Here](https://github.com/theiotlearninginitiative/internetofthings101/tree/82a57e2c2b485b1fd6e594db2c7d0ccb8bf398bb/documentation/\[http:/flask-restful-cn.readthedocs.org/en/0.3.4/quickstart.html)

```bash
root@board:~# pip install flask-restful
Downloading/unpacking Flask
  Downloading Flask-0.10.1.tar.gz (544kB): 544kB downloaded
Downloading/unpacking flask-restful
  Downloading Flask-RESTful-0.3.5.tar.gz (102kB): 102kB downloaded
Downloading/unpacking aniso8601>=0.82 (from flask-restful)
  Downloading aniso8601-1.1.0.tar.gz (49kB): 49kB downloaded
Downloading/unpacking python-dateutil (from aniso8601>=0.82->flask-restful)
  Downloading python-dateutil-2.5.1.tar.gz (235kB): 235kB downloaded
Installing collected packages: Flask flask-restful, aniso8601, python-dateutil
Successfully installed Flask flask-restful aniso8601 python-dateutil
Cleaning up...
root@board:~#
```

```bash
root@board:~# vi mainflask.py
```

```python
#!/usr/bin/python

from flask import Flask
from flask_restful import Api, Resource

app = Flask(__name__)
api = Api(app)

class DataSensorRestApi(Resource):
    def get(self):
        data = 'This is data from a sensor'
        return data

api.add_resource(DataSensorRestApi, '/datasensor')

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)
```

```bash
    root@board:~# python mainflask.py
     * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
     * Restarting with stat
     * Debugger is active!
     * Debugger pin code: 331-202-890
```

Connect to your boardipaddress:5000/datasensor in a web browser...

Once connected your board will display under the terminal:

```bash
    127.0.0.1 - - [28/Dec/2015 15:07:38] "GET /datasensor HTTP/1.1" 200 -
    127.0.0.1 - - [28/Dec/2015 15:07:40] "GET /datasensor HTTP/1.1" 200 -
```

Once connected your browser will display:

```bash
"This is data from a sensor"
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://theiotlearninginitiative.gitbook.io/internetofthings101/architecture/communications/protocols/restful/labs.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
