Chapter 8. Web Layer

Preview

Chapter 3 was a quick look at how to define FastAPI web endpoints, pass simple string inputs to them, and get responses. This chapter goes further into the top layer of a FastAPI application—which could also be called an Interface or Router layer—and its integration with the Service and Data layers.

As before, I’ll start with small examples. Then I’ll introduce some structure, dividing layers into subsections to allow for cleaner development and growth. The less code we write, the less we’ll need to remember and fix later.

The basic sample data in this book concerns imaginary creatures, or cryptids, and their explorers. You may find parallels with other domains of information.

What do we do with information, in general? Like most websites, ours will provide ways to do the following:

  • Retrieve

  • Create

  • Modify

  • Replace

  • Delete

Starting from the top, we’ll create web endpoints that perform these functions on our data. At first, we’ll provide fake data to make the endpoints work with any web client. In the following chapters, we’ll move the fake data code down into the lower layers. At each step, we’ll ensure that the site still works and passes data through correctly. Finally, in Chapter 10, we’ll drop the faking and store real data in real databases, for a full end-to-end (Web → Service → Data) website.

Note

Allowing any anonymous visitor to perform all these actions will be an object lesson in “why we can’t have nice things.” Chapter 11 discusses ...

Get FastAPI now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.