2 A basic API implementation

 

This chapter covers

  • Reading and understanding the requirements of an API specification
  • Structuring our application into a data layer, an application layer, and an interface layer
  • Implementing API endpoints using FastAPI
  • Implementing data validation models (schemas) using Pydantic

In this chapter, we implement the API for the orders service, which is one of the microservices of the CoffeeMesh website, the project that we introduced in chapter 1. CoffeeMesh is an application that makes and delivers coffee on demand at any time, wherever you are. In this chapter, we’ll implement the API for one of the services in the CoffeeMesh platform: the orders service, which allows customers to place orders. In doing so, you will get an early peek into the concepts and processes that we dissect in more detail throughout this book. In this chapter, I go quickly through the steps and concepts involved in the implementation of a microservice, and build an intuition of the main ideas that we will cover in more detail in later chapters.

The code for this chapter is available under the folder ch02 of the GitHub repository provided with this book.

2.1   Introducing the API specification

2.2   High-level architecture of the orders application

2.3   Implementing the API endpoints

2.4   Validating API requests payloads and marshalling responses

2.4.1   Defining validation schemas with Pydantic

2.4.2   Validating request payloads with Pydantic

2.4.3   Marshalling and validating response payloads with Pydantic

2.5   Adding an in-memory list of orders to the API

2.6   Summary