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:
consuming-apis/: therequestsexamples from REST and Python: Consuming APIs. The tutorial shows these in the REPL, so here they're runnable scripts thatprint()the results, one script per HTTP method section.flask-api/: the Flask countries API from Tools of the Trade → Flask.django-api/: thecountryapiDjango project with Django REST framework from Tools of the Trade → Django REST Framework.fastapi-api/: the FastAPI countries API from Tools of the Trade → FastAPI.
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.