| @@ -1,5 +1,5 @@ | |||
| # Tutorial 1: Your first Python API server | |||
| # Your first Python REST API server | |||
| In this tutorial you will learn how to make a simple HTTP/REST API in Python 2.7 (or 3.5). When you're done you will know about: | |||
| @@ -14,8 +14,11 @@ In this tutorial you will learn how to make a simple HTTP/REST API in Python 2.7 | |||
| |What | Where | Why | | |||
| |:-------------------:|:----------------------:|:------------------:| | |||
| |A nice primer on Python decorators | [http://bit.ly/25ij4gY](http://bit.ly/25ij4gY) | You might want to get a little under the hood on how the routes (and other) decorators work. | | |||
| |:-------------------:|:----------------------:|:------------------| | |||
| |A nice primer on Python decorators | [https://realpython.com/blog/python/primer-on-python-decorators/](https://realpython.com/blog/python/primer-on-python-decorators/) | You might want to get a little under the hood on how the routes (and other) decorators work. | | |||
| |Resource modeling - the RESTful way | [https://www.thoughtworks.com/insights/blog/rest-api-design-resource-modeling](https://www.thoughtworks.com/insights/blog/rest-api-design-resource-modeling) | Can't get enough of REST? | | |||
| |A thorough intro to RESTful APIs | [https://codeplanet.io/principles-good-restful-api-design/](https://codeplanet.io/principles-good-restful-api-design/) | This really nails the full spectrum of concerns in a nice way, but get some coffee before you start. | | |||
| ## STEP 1: Environment check | |||
| @@ -29,9 +32,9 @@ You will minimally need Python 2.7.1x for this tutorial. Most if not all of the | |||
| ``` | |||
| 2. execute `pip install flask` | |||
| * if you need to know more about `pip`, please [read more on it](http://www.pythonforbeginners.com/basics/python-pip-usage/) | |||
| * if you need to know more about `pip`, please [read more on it](http://www.pythonforbeginners.com/basics/python-pip-usage/). | |||
| 3. check to see if you have json installed | |||
| 3. check to see if you have the `json` library installed | |||
| ``` | |||
| $ python | |||
| @@ -118,10 +121,10 @@ Let's put your fixed/assigned/DHCP address in first (remember this _may_ change | |||
| ``` | |||
| and make the change accordingly: | |||
| ```python | |||
| if __name__ == "__main__": | |||
| app.run(_your_ip_address_, port=5200) | |||
| ``` | |||
| ```python | |||
| if __name__ == "__main__": | |||
| app.run(_your_ip_address_, port=5200) | |||
| ``` | |||
| ### Set DEBUG mode ON | |||
| @@ -158,7 +161,7 @@ data = { | |||
| } | |||
| ``` | |||
| To convert this Python dictionary to JSON so we can return it over the wire, we use the `json` library. The two methods you should just remember are `json.dumps(a_python_object)` and `json.loads(a_json_string)` ... these two methods allow you to go back and forth from Python a JSON string (`dumps`) and from a JSON object to a Python object (`loads`) so | |||
| To convert this Python dictionary to JSON so we can return it over the wire, we use the `json` library. The two methods you should just remember are `json.dumps(a_python_object)` and `json.loads(a_json_string)` ... these two methods allow you to go back and forth from Python to a JSON string (`dumps`) and from a JSON object (as a string) back to a Python object (`loads`). | |||
| ### INGREDIENT B: A `/status` route and handler | |||
| @@ -172,7 +175,7 @@ def the_handler_for_the_route(): | |||
| return | |||
| ``` | |||
| This pattern will be used over and over in your code, study it and remember it. To learn how to use this pattern to specify things like your `GET` or `POST` handlers, read the docs further here: http://flask.pocoo.org/docs/0.11/api/#flask.Flask.route and here: http://flask.pocoo.org/docs/0.11/api/#url-route-registrations. Everything you'd want to know will be there (including handling variable parts of your route, defaults, method handling, etc.). | |||
| This pattern will be used over and over in your code, study it and remember it. To learn how to use this pattern to specify things like your `GET` or `POST` handlers, read the docs further here: [http://flask.pocoo.org/docs/0.11/api/#flask.Flask.route](http://flask.pocoo.org/docs/0.11/api/#flask.Flask.route) and here: [http://flask.pocoo.org/docs/0.11/api/#url-route-registrations](http://flask.pocoo.org/docs/0.11/api/#url-route-registrations). Everything you'd want to know will be there (including handling variable parts of your route, defaults, method handling, etc.). | |||
| Let's finish off our `/status` route ... make sure you place the route somewhere before the `if __name__ ...` line of code: | |||
| @@ -189,15 +192,3 @@ def status_endpoint(): | |||
| ``` | |||
| Your final code should look like something like the sample file provided in [app.py](./app.py). | |||
| ```python | |||
| %pastebin -d "v0.1 gSchool Talk" gSchoolWinter16_PythonFlaskApp.md | |||
| ``` | |||
| u'https://gist.github.com/8c90a3673f6e44ac30f2891155f4f74c' | |||