Python API Development Fundamentals

Book description

Learn all that's needed to build a fully functional web application from scratch.

Key Features

  • Delve deep into the principle behind RESTful API
  • Learn how to build a scalable web application with the RESTful API architecture and Flask framework
  • Know what are the exact tools and methodology to test your applications and how to use them

Book Description

Python is a flexible language that can be used for much more than just script development. By knowing the Python RESTful APIs work, you can build a powerful backend for web applications and mobile applications using Python.

You'll take your first steps by building a simple API and learning how the frontend web interface can communicate with the backend. You'll also learn how to serialize and deserialize objects using the marshmallow library. Then, you'll learn how to authenticate and authorize users using Flask-JWT. You'll also learn how to enhance your APIs by adding useful features, such as email, image upload, searching, and pagination. You'll wrap up the whole book by deploying your APIs to the cloud.

By the end of this book, you'll have the confidence and skill to leverage the power of RESTful APIs and Python to build efficient web applications.

What you will learn

  • Understand the concept of a RESTful API
  • Build a RESTful API using Flask and the Flask-Restful extension
  • Manipulate a database using Flask-SQLAlchemy and Flask-Migrate
  • Send out plaintext and HTML format emails using the Mailgun API
  • Implement a pagination function using Flask-SQLAlchemy
  • Use caching to improve API performance and efficiently obtain the latest information
  • Deploy an application to Heroku and test it using Postman

Who this book is for

This book is ideal for aspiring software developers who have a basic-to-intermediate knowledge of Python programming and who want to develop web applications using Python. Knowledge of how web applications work will be beneficial but is not essential.

Table of contents

  1. Preface
    1. About the Book
      1. About the Authors
      2. Learning Objectives
      3. Audience
      4. Approach
      5. Hardware Requirements
      6. Software Requirements
      7. Conventions
      8. Installation and Setup
      9. Additional Resources
  2. 1. Your First Step
    1. Introduction
    2. Understanding API
    3. RESTful API
      1. REST Constraints/Principles
    4. HTTP Protocol
    5. HTTP Methods and CRUD
    6. The JSON Format
    7. HTTP Status Codes
      1. Commonly used HTTP Status Codes
    8. Open API
    9. The Flask Web Framework
    10. Building a Simple Recipe Management Application
      1. Virtual Environment
      2. Exercise 1: Building Our First Flask Application
      3. Exercise 2: Managing Recipes with Flask
    11. Using curl or httpie to Test All the Endpoints
      1. Exercise 3: Testing Our API Endpoints with httpie and curl
    12. Postman
      1. The Postman GUI
      2. Sending a GET Request
      3. Sending a POST Request
      4. Saving a Request
      5. Activity 1: Sending Requests to Our APIs Using Postman
      6. Exercise 4: Automated Testing Using Postman
      7. Activity 2: Implement and Test the delete_recipe Function
    13. Summary
  3. 2. Starting to Build Our Project
    1. Introduction
    2. What is Flask-RESTful?
      1. Using Flask-RESTful to Develop Our Recipe-Sharing Platform, "Smilecook"
    3. Virtual Environment
      1. Exercise 5: Creating a Development Project in PyCharm
    4. Creating a Recipe Model
      1. Exercise 6: Creating the Recipe Model
      2. Resourceful Routing
      3. Exercise 7: Defining an API Endpoint for the Recipe Model
      4. Exercise 8: Defining the Recipe Resource
      5. Exercise 9: Publishing and Unpublishing the Recipes
    5. Configuring Endpoints
      1. Exercise 10: Creating the Main Application File
    6. Making HTTP Requests to the Flask API using curl and httpie
      1. Exercise 11: Testing the Endpoints Using curl and httpie
      2. Exercise 12: Testing the Auto-Incremented Recipe ID
      3. Exercise 13: Getting All the Recipes Back
      4. Exercise 14: Testing the Recipe Resources
      5. Exercise 15: Negative Testing
      6. Exercise 16: Modifying the Recipes
      7. Exercise 17: Getting Back Specific Recipes with a Certain ID
      8. Activity 3: Testing the APIs Using Postman
      9. Activity 4: Implementing the Delete Recipe Function
    7. Summary
  4. 3. Manipulating a Database with SQLAlchemy
    1. Introduction
    2. Databases
      1. Database Management System
    3. SQL
    4. ORM
      1. Exercise 18: Setting Up a Smilecook Database
    5. Defining Our Models
      1. Exercise 19: Installing Packages and Defining Models
      2. Exercise 20: Using Flask-Migrate to Build a Database Upgrade Script
      3. Exercise 21: Applying Database Insertion
      4. Activity 5: Creating a User and a Recipe
    6. Password Hashing
      1. Exercise 22: Implement the User Registration Feature and Hash the User's Password
      2. Exercise 23: Testing the Application in Postman
      3. Activity 6: Upgrading and Downgrading a Database
    7. Summary
  5. 4. Authentication Services and Security with JWT
    1. Introduction
    2. JWT
    3. Flask-JWT-Extended
      1. Exercise 24: Implementing a User Login Function
      2. Exercise 25: Testing the User Login Function
      3. Exercise 26: Creating the me Endpoint
    4. Designing the Methods in the Recipe Model
      1. Exercise 27: Implementing Access-Controlled Recipe Management Functions
      2. Exercise 28: Testing the Recipe Management Functions
    5. Refresh Tokens
      1. Exercise 29: Adding a Refresh Token Function
      2. Exercise 30: Obtaining a New Access Token Using a Refresh Token
    6. The User Logout Mechanism
      1. Exercise 31: Implementing the Logout Function
      2. Exercise 32: Testing the Logout Function
      3. Activity 7: Implementing Access Control on the publish/unpublish Recipe Function
    7. Summary
  6. 5. Object Serialization with marshmallow
    1. Introduction
    2. Serialization versus Deserialization
    3. marshmallow
    4. A Simple Schema
      1. Field Validation
      2. Customizing Deserialization Methods
    5. UserSchema Design
      1. Exercise 33: Using marshmallow to Validate the User Data
      2. Exercise 34: Testing the User Endpoint before and after Authentication
    6. RecipeSchema Design
      1. Exercise 35: Implementing RecipeSchema
      2. Exercise 36: Testing the Recipe API
    7. The PATCH Method
      1. Exercise 37: Using the PATCH Method to Update the Recipe
      2. Searching for Authors and Unpublished Recipes
      3. Using the webargs Package to Parse the Request Arguments
      4. Exercise 38: Implementing Access Control on Recipes
      5. Exercise 39: Retrieving Recipes from a Specific Author
      6. Activity 8: Serializing the recipe Object Using marshmallow
    8. Summary
  7. 6. Email Confirmation
    1. Introduction
    2. Mailgun
      1. Exercise 40: Get Started with Using Mailgun
      2. Exercise 41: Using the Mailgun API to Send Out Emails
    3. User Account Activation Workflow
      1. Exercise 42: Generating the Account Activation Token
      2. Exercise 43: Sending Out the User Account Activation Email
      3. Activity 9: Testing the Complete User Registration and Activation Workflow
      4. Setting Up Environment Variables
      5. Exercise 44: Setting Up Environment Variables in PyCharm
    4. HTML Format Email
      1. Activity 10: Creating the HTML Format User Account Activation Email
    5. Summary
  8. 7. Working with Images
    1. Introduction
    2. Building the User Avatar Function
      1. Exercise 45: Adding the avatar_image Attribute to the User Model
    3. Flask-Uploads
      1. Upload Sets
      2. Exercise 46: Implementing the User Avatar Upload Function
      3. Exercise 47: Testing the User Avatar Upload Function Using Postman
    4. Image Resizing and Compression
    5. Introduction to Pillow
      1. Exercise 48: Implementing Image Compression in Our Smilecook Application
      2. Exercise 49: Testing the Image Compression Function
      3. Activity 11: Implementing the Recipe Cover Image Upload Function
      4. Activity 12: Testing the Image Upload Function
    6. Summary
  9. 8. Pagination, Searching, and Ordering
    1. Introduction
    2. Pagination
    3. Paginated APIs
      1. Exercise 50: Implementing Pagination on the Published Recipes Retrieval Function
      2. Exercise 51: Testing the Pagination Functions
      3. Activity 13: Implementing Pagination on the User-Specific Recipe Retrieval API
      4. Activity 14: Testing Pagination on the User-Specific Recipe Retrieval API
    4. Recipe Searching
      1. Exercise 52: Implementing the Search Function
      2. Exercise 53: Testing the Search Function
    5. Sorting and Ordering
      1. Exercise 54: Implementing Sorting and Ordering
      2. Exercise 55: Testing the Sorting and Ordering Feature
      3. Activity 15: Searching for Recipes with Specific Ingredients
    6. Summary
  10. 9. Building More Features
    1. Introduction
    2. Caching
      1. Benefit of Caching
    3. Flask-Caching
      1. Exercise 56: Implementing Caching Functionality Using Flask-Caching
      2. Exercise 57: Testing the Caching Function with Postman
      3. Clearing the Cache when Data Updates
      4. Activity 16: Getting Cache Data after Updating Recipe Details
      5. Exercise 58: Implementing Cache-Clearing Functionality
      6. Exercise 59: Verifying the Cache-Clearing Function
    4. API Rate Limiting
      1. HTTP Headers and Response Codes
    5. Flask-Limiter
      1. Exercise 60: Implementing API Rate-Limiting Functionality
      2. Exercise 61: Verifying the Rate-Limit Function
      3. Exercise 62: Adding a Whitelist
      4. Activity 17: Adding Multiple Rate-Limit Restrictions
    6. Summary
  11. 10. Deployment
    1. Introduction
    2. Deployment
    3. Comparing SaaS, PaaS, and IaaS
    4. The Heroku Platform
    5. Configuration Handling in Smilecook
      1. Exercise 63: Configuration Handling for the Production and Development Environments
      2. Exercise 64: Adding a Staging Configuration Class
    6. Heroku Application
      1. Exercise 65: Creating a New Application in Heroku
    7. Heroku Add-Ons
      1. Exercise 66: Installing Heroku Postgres
    8. Setting Up Environment Variables for the Heroku App
      1. Exercise 67: Setting Up the App Environment Variables
      2. Deployment Using Heroku Git
      3. What is Git?
      4. What is gitignore?
      5. What is Procfile?
      6. What is Gunicorn?
      7. Exercise 68: Setting Up the Git and the Heroku CLI
      8. Exercise 69: Checking the Heroku Postgres Tables in pgAdmin
    9. Setting Up Variables in Postman
      1. Exercise 70: Setting Up Variables in Postman
      2. Activity 18: Changing access_token to a Variable in Postman
    10. Setting up the Front-end Interface to Work with the Smilecook API
    11. Summary
  12. Appendix
    1. 1: Your First Step
      1. Activity 1: Sending Requests to Our APIs Using Postman
      2. Activity 2: Implement and Test the delete_recipe Function
    2. 2: Starting to Build Our Project
      1. Activity 3: Testing the APIs Using Postman
      2. Activity 4: Implementing the Delete Recipe Function
    3. 3: Manipulating a Database with SQLAlchemy
      1. Activity 5: Creating a User and a Recipe
      2. Activity 6: Upgrading and Downgrading a Database
    4. 4: Authenticated Services and Security with JWTs
      1. Activity 7: Implementing Access Control on the publish/unpublish Recipe Function
    5. 5: Validating APIs Using marshmallow
      1. Activity 8: Serializing the recipe Object Using marshmallow
    6. 6: Email Confirmations
      1. Activity 9: Testing the Complete User Registration and Activation Workflow
      2. Activity 10: Creating the HTML Format User Account Activation Email
    7. 7: Working with Images
      1. Activity 11: Implementing the Recipe Cover Image Upload Function
      2. Activity 12: Testing the Image Upload Function
    8. 8: Pagination, Searching, and Ordering
      1. Activity 13: Implementing Pagination on the User-Specific Recipe Retrieval API
      2. Activity 14: Testing Pagination on the User-Specific Recipe Retrieval API
      3. Activity 15: Searching for Recipes with Specific Ingredients
    9. 9: Building More Features
      1. Activity 16: Getting Cache Data after Updating Recipe Details
      2. Activity 17: Adding Multiple Rate-Limit Restrictions
    10. 10: Deployment
      1. Activity 18: Changing access_token to a Variable in Postman

Product information

  • Title: Python API Development Fundamentals
  • Author(s): Jack Chan, Ray Chung, Jack Huang
  • Release date: November 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781838983994