Hands-On RESTful Python Web Services - Second Edition

Book Description

Explore the best tools and techniques to create lightweight, maintainable, and scalable Python web services

Key Features

  • Combine Python with different data sources to build complex RESTful APIs from scratch
  • Configure and fine-tune your APIs using the best tools and techniques available
  • Use command-line and GUI tools to test CRUD operations performed by RESTful Web Services or APIs

Book Description

Python is the language of choice for millions of developers worldwide that builds great web services in RESTful architecture. This second edition of Hands-On RESTful Python Web Services will cover the best tools you can use to build engaging web services.

This book shows you how to develop RESTful APIs using the most popular Python frameworks and all the necessary stacks with Python, combined with related libraries and tools. You'll learn to incorporate all new features of Python 3.7, Flask 1.0.2, Django 2.1, Tornado 5.1, and also a new framework, Pyramid. As you advance through the chapters, you will get to grips with each of these frameworks to build various web services, and be shown use cases and best practices covering when to use a particular framework.

You'll then successfully develop RESTful APIs with all frameworks and understand how each framework processes HTTP requests and routes URLs. You'll also discover best practices for validation, serialization, and deserialization. In the concluding chapters, you will take advantage of specific features available in certain frameworks such as integrated ORMs, built-in authorization and authentication, and work with asynchronous code. At the end of each framework, you will write tests for RESTful APIs and improve code coverage.

By the end of the book, you will have gained a deep understanding of the stacks needed to build RESTful web services.

What you will learn

  • Select the most appropriate framework based on requirements
  • Develop complex RESTful APIs from scratch using Python
  • Use requests handlers, URL patterns, serialization, and validations
  • Add authentication, authorization, and interaction with ORMs and databases
  • Debug, test, and improve RESTful APIs with four frameworks
  • Design RESTful APIs with frameworks and create automated tests

Who this book is for

This book is for web developers who have a working knowledge of Python and would like to build amazing web services by taking advantage of the various frameworks of Python. You should have some knowledge of RESTful APIs.

Publisher Resources

Download Example Code

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Hands-On RESTful Python Web Services Second Edition
  3. Dedication
  4. About Packt
    1. Why subscribe?
    2. Packt.com
  5. Contributors
    1. About the author
    2. About the reviewers
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Reader feedback
    5. Customer support
    6. Get in touch
      1. Errata
      2. Reviews
      3. Piracy
      4. Questions
  7. Developing RESTful APIs and Microservices with Flask 1.0.2
    1. Designing a RESTful API to interact with a simple data source
    2. Understanding the tasks performed by each HTTP method
    3. Understanding microservices
    4. Working with lightweight virtual environments
    5. Setting up a virtual environment with Flask and Flask-RESTful
    6. Declaring status codes for the responses with an enumerable
    7. Creating the model
    8. Using a dictionary as a repository
    9. Configuring output fields
    10. Working with resourceful routing on top of Flask pluggable views
    11. Configuring resource routing and endpoints
    12. Making HTTP requests to the Flask API
      1. Working with the curl and httpie command-line tools
      2. Working with GUI tools – Postman and others
      3. Consuming the API with other programming languages
    13. Test your knowledge
    14. Summary
  8. Working with Models, SQLAlchemy, and Hyperlinked APIs in Flask
    1. Designing a RESTful API to interact with a PostgreSQL 10.5 database
    2. Understanding the tasks performed by each HTTP method
    3. Installing packages with the requirements.txt file to simplify our common tasks
    4. Creating the database
    5. Configuring the database
    6. Creating models with their relationships
    7. Creating schemas to validate, serialize, and deserialize models
    8. Combining blueprints with resourceful routing
    9. Understanding and configuring resourceful routing
    10. Registering the blueprint and running migrations
    11. Verifying the contents of the PostgreSQL database
    12. Creating and retrieving related resources
    13. Test your knowledge
    14. Summary
  9. Improving Our API and Adding Authentication to it with Flask
    1. Improving unique constraints in the models
    2. Understanding the differences between the PUT and the PATCH methods
    3. Updating fields for a resource with the PATCH method
    4. Coding a generic pagination class
    5. Adding pagination features
    6. Understanding the steps to add authentication and permissions
    7. Adding a user model
    8. Creating schemas to validate, serialize, and deserialize users
    9. Adding authentication to resources
    10. Creating resource classes to handle users
    11. Running migrations to generate the user table
    12. Composing requests with the necessary authentication
    13. Test your knowledge
    14. Summary
  10. Testing and Deploying an API in a Microservice with Flask
    1. Setting up unit tests with pytest
    2. Creating a database for testing
    3. Creating fixtures to perform setup and teardown tasks for running clean tests
    4. Writing the first round of unit tests
    5. Running unit tests with pytest and checking testing coverage
    6. Improving testing coverage
    7. Understanding strategies for deployments and scalability
    8. Test your knowledge
    9. Summary
  11. Developing RESTful APIs with Django 2.1
    1. Designing a RESTful API to interact with a simple SQLite database
    2. Understanding the tasks performed by each HTTP method
    3. Setting up the virtual environment with Django REST framework
    4. Creating the models
    5. Managing serialization and deserialization
    6. Understanding status codes for the responses
    7. Writing API views
    8. Making HTTP requests to the Django API
      1. Working with command-line tools - curl and httpie
      2. Working with GUI tools - Postman and others
    9. Test your knowledge
    10. Summary
  12. Working with Class-Based Views and Hyperlinked APIs in Django 2.1
    1. Using model serializers to eliminate duplicate code
    2. Working with wrappers to write API views
    3. Using the default parsing and rendering options and moving beyond JSON
    4. Browsing the API
    5. Designing a RESTful API to interact with a complex PostgreSQL 10.5 database
    6. Understanding the tasks performed by each HTTP method
    7. Declaring relationships with the models
    8. Installing packages with the requirements.txt file to work with PostgreSQL
    9. Configuring the database
    10. Running migrations
    11. Verifying the contents of the PostgreSQL database
    12. Managing serialization and deserialization with relationships and hyperlinks
    13. Creating class-based views and using generic classes
    14. Taking advantage of generic class-based views
    15. Working with endpoints for the API
    16. Browsing an API with relationships
    17. Creating and retrieving related resources
    18. Test your knowledge
    19. Summary
  13. Improving Our API and Adding Authentication to it with Django
    1. Adding unique constraints to the models
    2. Updating a single field for a resource with the PATCH method
    3. Taking advantage of pagination
    4. Customizing pagination classes
    5. Understanding authentication, permissions, and throttling
    6. Adding security-related data to the models
    7. Creating a customized permission class for object-level permissions
    8. Persisting the user that makes a request and configuring permission policies
    9. Setting a default value for a new required field in migrations
    10. Composing requests with the necessary authentication
    11. Browsing the API with authentication credentials
    12. Test your knowledge
    13. Summary
  14. Throttling, Filtering, Testing, and Deploying an API with Django 2.1
    1. Installing packages with the requirements.txt file to work with filters, throttling, and tests
    2. Understanding filtering, searching, and ordering classes
    3. Configuring filtering, searching, and ordering for views
    4. Executing HTTP requests to test filtering, searching, and ordering
    5. Filtering, searching and ordering in the Browsable API
    6. Understanding throttling classes and goals
    7. Configuring throttling policies
    8. Executing HTTP requests to test throttling policies
    9. Setting up unit tests with pytest
    10. Writing the first round of unit tests
    11. Running unit tests with pytest
    12. Improving testing coverage
    13. Running Django RESTful APIs on the cloud
    14. Test your knowledge
    15. Summary
  15. Developing RESTful APIs with Pyramid 1.10
    1. Designing a RESTful API to interact with a simple data source
    2. Setting up the virtual environment with Pyramid 1.10
    3. Creating a new Pyramid project based on a template
    4. Creating the model
    5. Using a dictionary as a repository
    6. Creating a Marshmallow schema to validate, serialize, and deserialize the model
    7. Working with view callables and view configurations
    8. Understanding and configuring view handlers
    9. Making HTTP requests to the API with command-line tools
    10. Test your knowledge
    11. Summary
  16. Developing RESTful APIs with Tornado 5.1.1
    1. Designing a RESTful API to interact with slow sensors and actuators
    2. Understanding the tasks performed by each HTTP method
    3. Setting up a virtual environment with Tornado 5.1.1
    4. Creating classes that represent a drone
    5. Writing request handlers
    6. Mapping URL patterns to request handlers
    7. Making HTTP requests to the Tornado API
      1. Working with command-line tools - curl and httpie
      2. Working with GUI tools - Postman and others
    8. Test your knowledge
    9. Summary
  17. Working with Asynchronous Code, Testing, and Deploying an API with Tornado
    1. Understanding synchronous and asynchronous execution
    2. Refactoring code to take advantage of asynchronous decorators
    3. Mapping URL patterns to asynchronous request handlers
    4. Making HTTP requests to the Tornado non-blocking API
    5. Setting up unit tests with pytest
    6. Writing the first round of unit tests
    7. Running unit tests with pytest and checking testing coverage
    8. Improving testing coverage
    9. Understanding strategies for deploying Tornado APIs to the cloud
    10. Test your knowledge
    11. Summary
  18. Assessment
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 5
    6. Chapter 6
    7. Chapter 7
    8. Chapter 8
    9. Chapter 9
    10. Chapter 10
    11. Chapter 11
  19. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product Information

  • Title: Hands-On RESTful Python Web Services - Second Edition
  • Author(s): Gaston C. Hillar
  • Release date: December 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781789532227