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
  • Testing the API using a Swagger UI

In this chapter, we implement the API for the orders service, which is one of the microservices of the CoffeeMesh website, the project we introduced in section 1.5. CoffeeMesh is an application that makes and delivers coffee on demand at any time, wherever you are. The orders service allows customers to place orders with CoffeeMesh. As we implement the orders API, you will get an early look into the concepts and processes that we dissect in more detail throughout this book. The code for this chapter is available under the ch02 folder of the GitHub repository provided with this book.

2.1 Introducing the orders API specification

Let’s begin by analyzing the requirements of the orders API. Using the orders API, we can place orders, update them, retrieve their details, or cancel them. The orders API specification is available in a file named ch02/oas.yaml in the GitHub repository for this book. OAS stands for OpenAPI specification, which is a standard format for documenting REST APIs. In chapter 5, you’ll learn to document your APIs using OpenAPI. As you can see in figure 2.1, the API specification describes a REST API with four main URL paths:

2.2 High-level architecture of the orders application

2.3 Implementing the API endpoints

2.4 Implementing data validation models with pydantic

2.5 Validating request payloads with pydantic

2.6 Marshalling and validating response payloads with pydantic

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

Summary

sitemap