| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
parent directory.. | ||||
This folder provides the code examples for the Real Python tutorial Python and REST APIs: Interacting With Web Services.
The examples are grouped into one subfolder per section of the tutorial, because the Flask and FastAPI examples both use a file called app.py, and because the tutorial itself advises you to keep each example in its own folder:
Create and activate a virtual environment:
$ python -m venv venv
$ source venv/bin/activateInstall the pinned dependencies:
(venv) $ python -m pip install -r requirements.txtThe single requirements.txt covers all four examples. If you'd rather isolate them, then create one virtual environment per subfolder and install only the packages that example needs.
Each script sends one kind of request to JSONPlaceholder and prints the response, so you need an internet connection to run them:
(venv) $ cd consuming-apis/
(venv) $ python get_request.py
{'userId': 1, 'id': 1, 'title': 'delectus aut autem', 'completed': False}
200
application/json; charset=utf-8The other scripts are post_request.py, put_request.py, patch_request.py, and delete_request.py.
(venv) $ cd flask-api/
(venv) $ export FLASK_APP=app.py
(venv) $ export FLASK_DEBUG=1
(venv) $ flask runThen request the endpoint at http://127.0.0.1:5000/countries.
The django-api/ folder is the countryapi project that the tutorial creates with django-admin startproject countryapi and python manage.py startapp countries. Set up its database and load the fixture before you start the server:
(venv) $ cd django-api/
(venv) $ python manage.py migrate
(venv) $ python manage.py loaddata countries.json
Installed 3 object(s) from 1 fixture(s)
(venv) $ python manage.py runserverThen request the endpoint at http://127.0.0.1:8000/countries/.
(venv) $ cd fastapi-api/
(venv) $ uvicorn app:app --reloadThen request the endpoint at http://127.0.0.1:8000/countries.
| Back | FazBrowse Home | New Git URL |