Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

README.md

CH14 - Readme

This chapter is about APIs. You will find three main folders:

  1. api_code contains the FastAPI project about Railways
  2. apic or API Consumer. This is a Django project to show an example on how to talk to an API from another application.
  3. samples. This folder contains the examples that went in the chapter, so you can ignore it.

Setup

Install requirements with pip from their folder:

$ pip install -r requirements.txt

If you want to create your own dummy data, please also install the dev requirements:

$ pip install -r dev.txt

To generate a new database with random data:

$ cd api_code
$ python dummy_data.py

This will generate a new train.db file for you.

Running the API

Open a terminal window, change into the api_code folder and type this command:

$ uvicorn main:app --reload

The --reload flag makes sure uvicorn is reloaded if you make changes to the source while the app is running.

Running the Django consumer

While the API is running in a terminal, open another terminal window, change into the apic folder, and type the following:

$ python manage.py migrate  # only the first time, to generate the django db

Once migrations have been applied, run this to start the app:

$ python manage.py runserver 8080

We need to specify a port for Django that is not 8000 (its normal default), since the API is running on that one.

When the Django app is running, open a browser and go to: http://localhost:8080

You should see the welcome page of the Railways app. From there you can navigate using links.

In the authentication section, you can use these credentials to authenticate:

username: fabrizio.romano@example.com
password: f4bPassword