Using Python, Flask, FastAPI, OpenAPI and more José Haro Peralta M A N N I N G 1. API design and documentation 2. Build the client and the server API server developers against the API API client developers documentation. 3. Test the implementation against the specification. API specification Documentation-driven development is an API-first development method in which you design and document the API first; then, you build the API server and the API client against the documentation; and finally, you use the API documentation to validate the server and client implementations. Documentation-driven development helps you reduce the chances of API integration failure, and it gives you more control and visibility of integration errors. Collection endpoints Singleton endpoints Get a collection Place an order Pay for an order Cancel an order of orders POST/orders POST/orders/8/pay POST/orders/8/cancel GET/orders {payload} {payload} {payload} Status code: 200 Status code: 201 Status code: 200 Status code: 200 {payload} {payload} {payload} {payload} APIclient Singleton endpoints Orders Retrieve an order Update an order Delete an order service PUT/orders/8 GET/orders/8 DELETE /orders/8 {payload} HTTPrequests Status code: 200 Status code: 200 Status code: 204 HTTPresponses {payload} {payload} REST APIs are structured around endpoints. We distinguish between singleton endpoints, such as GET /orders/8, and collection endpoints, such as GET /orders. REST APIs leverage the semantics of HTTP methods to indicate actions (such as POST to create resources), and they use HTTP status codes that signal the result of processing the request (such as 200 for successful responses). Microservice APIs Microservice APIs USING PYTHON, FLASK, FASTAPI, OPENAPI AND MORE JOSÉ HARO PERALTA MANNING SHELTER ISLAND For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: [email protected] ©2023 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. The author and publisher have made every effort to ensure that the information in this book was correct at press time. The author and publisher do not assume and hereby disclaim any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from negligence, accident, or any other cause, or from any usage of the information herein. Manning Publications Co. Development editor: Marina Michaels 20 Baldwin Road Technical development editor: Nick Watts PO Box 761 Review editor: Mihaela Batinic´ Shelter Island, NY 11964 Production editor: Andy Marinkovich Copy editor: Michele Mitchell Proofreader: Katie Tennant Technical proofreader: Al Krinker Typesetter: Dennis Dalinnik Cover designer: Marija Tudor ISBN: 9781617298417 Printed in the United States of America To Jiwon, without whose constant support and encouragement I wouldn’t have been able to write this book, and to Ivy, that boundless spark of joy that makes everything I do worth it. brief contents PART 1 INTRODUCING MICROSERVICE APIS...............................1 1 ■ What are microservice APIs? 3 2 ■ A basic API implementation 20 3 ■ Designing microservices 45 PART 2 DESIGNING AND BUILDING REST APIS ........................59 4 ■ Principles of REST API design 61 5 ■ Documenting REST APIs with OpenAPI 90 6 ■ Building REST APIs with Python 110 7 ■ Service implementation patterns for microservices 144 PART 3 DESIGNING AND BUILDING GRAPHQL APIS ...............183 8 ■ Designing GraphQL APIs 185 9 ■ Consuming GraphQL APIs 210 10 ■ Building GraphQL APIs with Python 233 vii viii BRIEF CONTENTS PART 4 SECURING, TESTING, AND DEPLOYING MICROSERVICE APIS ..................................................267 11 ■ API authorization and authentication 269 12 ■ Testing and validating APIs 302 13 ■ Dockerizing microservice APIs 331 14 ■ Deploying microservice APIs with Kubernetes 342