ebook img

Python API Development Fundamentals PDF

372 Pages·2019·21.945 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Python API Development Fundamentals

Python API Development Fundamentals Develop a full-stack web application with Python and Flask Jack Chan Ray Chung Jack Huang Python API Development Fundamentals Copyright © 2019 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Authors: Jack Chan, Ray Chung, and Jack Huang Technical Reviewer: Amritansh Managing Editor: Aditya Shah Acquisitions Editors: Kunal Sawant and Anindya Sil Production Editor: Salma Patel Editorial Board: Shubhopriya Banerjee, Bharat Botle, Ewan Buckingham, Megan Carlisle, Mahesh Dhyani, Manasa Kumar, Alex Mazonowicz, Bridget Neale, Dominic Pereira, Shiny Poojary, Abhisekh Rane, Erol Staveley, Ankita Thakur, Nitesh Thakur, and Jonathan Wray. First Published: November 2019 Production Reference: 1211119 ISBN: 978-1-83898-399-4 Published by Packt Publishing Ltd. Livery Place, 35 Livery Street Birmingham B3 2PB, UK Table of Contents Preface i Chapter 1: Your First Step 1 Introduction .................................................................................................... 2 Understanding API ......................................................................................... 2 RESTful API ...................................................................................................... 4 REST Constraints/Principles ................................................................................ 4 HTTP Protocol ................................................................................................. 5 HTTP Methods and CRUD .............................................................................. 5 The JSON Format ............................................................................................ 7 HTTP Status Codes ......................................................................................... 8 Commonly used HTTP Status Codes .................................................................. 8 Open API ......................................................................................................... 8 The Flask Web Framework .......................................................................... 10 Building a Simple Recipe Management Application ................................ 10 Virtual Environment .......................................................................................... 11 Exercise 1: Building Our First Flask Application ............................................. 12 Exercise 2: Managing Recipes with Flask ........................................................ 15 Using curl or httpie to Test All the Endpoints .......................................... 20 Exercise 3: Testing Our API Endpoints with httpie and curl ......................... 21 Postman ........................................................................................................ 27 The Postman GUI ............................................................................................... 27 Sending a GET Request ...................................................................................... 28 Sending a POST Request ................................................................................... 29 Saving a Request ................................................................................................ 29 Activity 1: Sending Requests to Our APIs Using Postman ............................. 30 Exercise 4: Automated Testing Using Postman .............................................. 30 Activity 2: Implement and Test the delete_recipe Function ......................... 32 Summary ....................................................................................................... 33 Chapter 2: Starting to Build Our Project 35 Introduction .................................................................................................. 36 What is Flask-RESTful? ................................................................................. 36 Using Flask-RESTful to Develop Our Recipe-Sharing Platform, "Smilecook" ..................................................... 36 Virtual Environment .................................................................................... 37 Exercise 5: Creating a Development Project in PyCharm ............................. 38 Creating a Recipe Model ............................................................................. 40 Exercise 6: Creating the Recipe Model ............................................................ 41 Resourceful Routing .......................................................................................... 42 Exercise 7: Defining an API Endpoint for the Recipe Model ......................... 43 Exercise 8: Defining the Recipe Resource ....................................................... 45 Exercise 9: Publishing and Unpublishing the Recipes ................................... 46 Configuring Endpoints ................................................................................. 48 Exercise 10: Creating the Main Application File ............................................. 48 Making HTTP Requests to the Flask API using curl and httpie ............... 50 Exercise 11: Testing the Endpoints Using curl and httpie ............................. 50 Exercise 12: Testing the Auto-Incremented Recipe ID .................................. 51 Exercise 13: Getting All the Recipes Back ....................................................... 52 Exercise 14: Testing the Recipe Resources ..................................................... 53 Exercise 15: Negative Testing ........................................................................... 55 Exercise 16: Modifying the Recipes .................................................................. 56 Exercise 17: Getting Back Specific Recipes with a Certain ID ....................... 57 Activity 3: Testing the APIs Using Postman .................................................... 58 Activity 4: Implementing the Delete Recipe Function ................................... 58 Summary ....................................................................................................... 59 Chapter 3: Manipulating a Database with SQLAlchemy 61 Introduction .................................................................................................. 62 Databases ..................................................................................................... 62 Database Management System ....................................................................... 62 SQL ................................................................................................................. 63 ORM ............................................................................................................... 63 Exercise 18: Setting Up a Smilecook Database ............................................... 64 Defining Our Models ................................................................................... 67 Exercise 19: Installing Packages and Defining Models ................................. 69 Exercise 20: Using Flask-Migrate to Build a Database Upgrade Script ........ 74 Exercise 21: Applying Database Insertion ....................................................... 79 Activity 5: Creating a User and a Recipe ......................................................... 81 Password Hashing ....................................................................................... 82 Exercise 22: Implement the User Registration Feature and Hash the User's Password ......................................................................... 82 Exercise 23: Testing the Application in Postman ........................................... 86 Activity 6: Upgrading and Downgrading a Database ..................................... 89 Summary ....................................................................................................... 90 Chapter 4: Authentication Services and Security with JWT 93 Introduction .................................................................................................. 94 JWT ................................................................................................................. 94 Flask-JWT-Extended ..................................................................................... 96 Exercise 24: Implementing a User Login Function ......................................... 97 Exercise 25: Testing the User Login Function .............................................. 101 Exercise 26: Creating the me Endpoint ........................................................ 105 Designing the Methods in the Recipe Model .......................................... 107 Exercise 27: Implementing Access-Controlled Recipe Management Functions ..................................................................... 107 Exercise 28: Testing the Recipe Management Functions ........................... 111 Refresh Tokens ........................................................................................... 114 Exercise 29: Adding a Refresh Token Function ........................................... 115 Exercise 30: Obtaining a New Access Token Using a Refresh Token ........ 117 The User Logout Mechanism .................................................................... 118 Exercise 31: Implementing the Logout Function ........................................ 119 Exercise 32: Testing the Logout Function .................................................... 121 Activity 7: Implementing Access Control on the publish/unpublish Recipe Function .................................................. 123 Summary ..................................................................................................... 123 Chapter 5: Object Serialization with marshmallow 125 Introduction ................................................................................................ 126 Serialization versus Deserialization ........................................................ 126 marshmallow .............................................................................................. 127 A Simple Schema ........................................................................................ 127 Field Validation ................................................................................................ 128 Customizing Deserialization Methods .......................................................... 128 UserSchema Design ................................................................................... 129 Exercise 33: Using marshmallow to Validate the User Data ..................... 130 Exercise 34: Testing the User Endpoint before and after Authentication ............................................................................... 133 RecipeSchema Design ............................................................................... 135 Exercise 35: Implementing RecipeSchema .................................................. 136 Exercise 36: Testing the Recipe API .............................................................. 142 The PATCH Method .................................................................................... 146 Exercise 37: Using the PATCH Method to Update the Recipe .................... 146 Searching for Authors and Unpublished Recipes ....................................... 149 Using the webargs Package to Parse the Request Arguments ................. 150 Exercise 38: Implementing Access Control on Recipes .............................. 150 Exercise 39: Retrieving Recipes from a Specific Author ............................. 154 Activity 8: Serializing the recipe Object Using marshmallow .................... 158 Summary ..................................................................................................... 159 Chapter 6: Email Confirmation 161 Introduction ................................................................................................ 162 Mailgun ....................................................................................................... 163 Exercise 40: Get Started with Using Mailgun ............................................... 163 Exercise 41: Using the Mailgun API to Send Out Emails ............................. 166 User Account Activation Workflow .......................................................... 168 Exercise 42: Generating the Account Activation Token ............................. 169 Exercise 43: Sending Out the User Account Activation Email ................... 170 Activity 9: Testing the Complete User Registration and Activation Workflow ................................................................................ 174 Setting Up Environment Variables ................................................................ 174 Exercise 44: Setting Up Environment Variables in PyCharm ..................... 174 HTML Format Email ................................................................................... 176 Activity 10: Creating the HTML Format User Account Activation Email ... 178 Summary ..................................................................................................... 178 Chapter 7: Working with Images 181 Introduction ................................................................................................ 182 Building the User Avatar Function ........................................................... 182 Exercise 45: Adding the avatar_image Attribute to the User Model ........ 183 Flask-Uploads ............................................................................................. 185 Upload Sets ...................................................................................................... 186 Exercise 46: Implementing the User Avatar Upload Function .................. 186 Exercise 47: Testing the User Avatar Upload Function Using Postman ... 191 Image Resizing and Compression ............................................................ 195 Introduction to Pillow ................................................................................ 195 Exercise 48: Implementing Image Compression in Our Smilecook Application ........................................................................ 196 Exercise 49: Testing the Image Compression Function .............................. 199 Activity 11: Implementing the Recipe Cover Image Upload Function ...... 200 Activity 12: Testing the Image Upload Function .......................................... 200 Summary ..................................................................................................... 201 Chapter 8: Pagination, Searching, and Ordering 203 Introduction ................................................................................................ 204 Pagination ................................................................................................... 204 Paginated APIs ............................................................................................ 205 Exercise 50: Implementing Pagination on the Published Recipes Retrieval Function ............................................................................ 206 Exercise 51: Testing the Pagination Functions ............................................ 211 Activity 13: Implementing Pagination on the User-Specific Recipe Retrieval API ........................................................................................ 215 Activity 14: Testing Pagination on the User-Specific Recipe Retrieval API ........................................................................................ 215 Recipe Searching ........................................................................................ 216 Exercise 52: Implementing the Search Function ......................................... 217 Exercise 53: Testing the Search Function ..................................................... 218 Sorting and Ordering ................................................................................. 219 Exercise 54: Implementing Sorting and Ordering ....................................... 221 Exercise 55: Testing the Sorting and Ordering Feature ............................. 222 Activity 15: Searching for Recipes with Specific Ingredients ..................... 224 Summary ..................................................................................................... 225 Chapter 9: Building More Features 227 Introduction ................................................................................................ 228 Caching ........................................................................................................ 228 Benefit of Caching ........................................................................................... 229 Flask-Caching .............................................................................................. 229 Exercise 56: Implementing Caching Functionality Using Flask-Caching ........................................................................................ 230 Exercise 57: Testing the Caching Function with Postman ......................... 233 Clearing the Cache when Data Updates ...................................................... 235 Activity 16: Getting Cache Data after Updating Recipe Details ................. 235 Exercise 58: Implementing Cache-Clearing Functionality .......................... 236 Exercise 59: Verifying the Cache-Clearing Function ................................... 237 API Rate Limiting ........................................................................................ 240 HTTP Headers and Response Codes ............................................................. 241 Flask-Limiter ............................................................................................... 241 Exercise 60: Implementing API Rate-Limiting Functionality ...................... 242 Exercise 61: Verifying the Rate-Limit Function ............................................ 244 Exercise 62: Adding a Whitelist ..................................................................... 245 Activity 17: Adding Multiple Rate-Limit Restrictions .................................. 247 Summary ..................................................................................................... 247 Chapter 10: Deployment 249 Introduction ................................................................................................ 250 Deployment ................................................................................................ 250 Comparing SaaS, PaaS, and IaaS .............................................................. 251

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.