Building RESTful Python Web Services

Book description

Create web services that are lightweight, maintainable, scalable, and secure using the best tools and techniques designed for Python

About This Book

  • Develop RESTful Web Services using the most popular frameworks in Python

  • Configure and fine-tune your APIs using the best tools and techniques available

  • This practical guide will help you to implement complete REST-based APIs from scratch

  • Who This Book Is For

    This book is for web developers who have 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.

    What You Will Learn

  • Develop complex RESTful APIs from scratch with Python combined with and without data sources

  • Choose the most appropriate (micro) framework based on the specific requirements of a RESTful API / web service

  • Debug, test, and profile RESTful APIs with each of the frameworks

  • Develop a complex RESTful API that interacts with a PostgreSQL database

  • Add authentication and permissions to a RESTful API built in each of the frameworks

  • Map URL patterns to request handlers and check how the API works

  • Profile an existing API and refactor it to take advantage of asynchronous code

  • In Detail

    Python is the language of choice for millions of developers worldwide, due to its gentle learning curve as well as its vast applications in day-to-day programming. It serves the purpose of building great web services in the RESTful architecture. This book will show you the best tools you can use to build your own web services.

    Learn how to develop RESTful APIs using the popular Python frameworks and all the necessary stacks with Python, Django, Flask, and Tornado, combined with related libraries and tools. We will dive deep into each of these frameworks to build various web services, and will provide use cases and best practices on when to use a particular framework to get the best results.

    We will show you everything required to successfully develop RESTful APIs with the four frameworks such as request handling, URL mapping, serialization, validation, authentication, authorization, versioning, ORMs, databases, custom code for models and views, and asynchronous callbacks. At the end of each framework, we will add authentication and security to the RESTful APIs and prepare tests for it.

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

    Style and approach

    The book takes a straightforward approach, not spending time getting you started with RESTful APIs and web services. It will give you the best use cases for each framework to build great web services in Python.

    Table of contents

    1. Building RESTful Python Web Services
      1. Building RESTful Python Web Services
      2. Credits
      3. About the Author
      4. Acknowledgments
      5. About the Reviewer
      6. www.PacktPub.com
        1. Why subscribe?
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Developing RESTful APIs with Django
        1. Designing a RESTful API to interact with a simple SQLite database
        2. Understanding the tasks performed by each HTTP method
        3. Working with lightweight virtual environments
        4. Setting up the virtual environment with Django REST framework
        5. Creating the models
        6. Managing serialization and deserialization
        7. Writing API views
        8. Making HTTP requests to the API
          1. Working with command-line tools - curl and httpie
          2. Working with GUI tools - Postman and others
        9. Test your knowledge
        10. Summary
      9. 2. Working with Class-Based Views and Hyperlinked APIs in Django
        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 move beyond JSON
        4. Browsing the API
        5. Designing a RESTful API to interact with a complex PostgreSQL database
        6. Understanding the tasks performed by each HTTP method
        7. Declaring relationships with the models
        8. Managing serialization and deserialization with relationships and hyperlinks
        9. Creating class-based views and using generic classes
        10. Taking advantage of generic class based views
        11. Working with endpoints for the API
        12. Creating and retrieving related resources
        13. Test your knowledge
        14. Summary
      10. 3. Improving and Adding Authentication to an API 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
        9. Configuring permission policies
        10. Setting a default value for a new required field in migrations
        11. Composing requests with the necessary authentication
        12. Browsing the API with authentication credentials
        13. Test your knowledge
        14. Summary
      11. 4. Throttling, Filtering, Testing, and Deploying an API with Django
        1. Understanding throttling classes
        2. Configuring throttling policies
        3. Testing throttling policies
        4. Understanding filtering, searching, and ordering classes
        5. Configuring filtering, searching, and ordering for views
        6. Testing filtering, searching, and ordering
        7. Filtering, searching, and ordering in the Browsable API
        8. Setting up unit tests
        9. Writing a first round of unit tests
        10. Running unit tests and checking testing coverage
        11. Improving testing coverage
        12. Understanding strategies for deployments and scalability
        13. Test your knowledge
        14. Summary
      12. 5. Developing RESTful APIs with Flask
        1. Designing a RESTful API to interact with a simple data source
        2. Understanding the tasks performed by each HTTP method
        3. Setting up a virtual environment with Flask and Flask-RESTful
        4. Declaring status codes for the responses
        5. Creating the model
        6. Using a dictionary as a repository
        7. Configuring output fields
        8. Working with resourceful routing on top of Flask pluggable views
        9. Configuring resource routing and endpoints
        10. Making HTTP requests to the Flask API
          1. Working with command-line tools – curl and httpie
          2. Working with GUI tools - Postman and others
        11. Test your knowledge
        12. Summary
      13. 6. Working with Models, SQLAlchemy, and Hyperlinked APIs in Flask
        1. Designing a RESTful API to interact with a PostgreSQL database
        2. Understanding the tasks performed by each HTTP method
        3. Installing packages to simplify our common tasks
        4. Creating and configuring the database
        5. Creating models with their relationships
        6. Creating schemas to validate, serialize, and deserialize models
        7. Combining blueprints with resourceful routing
        8. Registering the blueprint and running migrations
        9. Creating and retrieving related resources
        10. Test your knowledge
        11. Summary
      14. 7. Improving and Adding Authentication to an API with Flask
        1. Improving unique constraints in the models
        2. Updating fields for a resource with the PATCH method
        3. Coding a generic pagination class
        4. Adding pagination features
        5. Understanding the steps to add authentication and permissions
        6. Adding a user model
        7. Creating a schemas to validate, serialize, and deserialize users
        8. Adding authentication to resources
        9. Creating resource classes to handle users
        10. Running migrations to generate the user table
        11. Composing requests with the necessary authentication
        12. Test your knowledge
        13. Summary
      15. 8. Testing and Deploying an API with Flask
        1. Setting up unit tests
        2. Writing a first round of unit tests
        3. Running unit tests with nose2 and checking testing coverage
        4. Improving testing coverage
        5. Understanding strategies for deployments and scalability
        6. Test your knowledge
        7. Summary
      16. 9. Developing RESTful APIs with Tornado
        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
        4. Declaring status codes for the responses
        5. Creating the classes that represent a drone
        6. Writing request handlers
        7. Mapping URL patterns to request handlers
        8. Making HTTP requests to the Tornado API
          1. Working with command-line tools – curl and httpie
          2. Working with GUI tools - Postman and others
        9. Test your knowledge
        10. Summary
      17. 10. 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
        6. Writing a first round of unit tests
        7. Running unit tests with nose2 and checking testing coverage
        8. Improving testing coverage
        9. Other Python Web frameworks for building RESTful APIs
        10. Test your knowledge
        11. Summary
      18. 11. Exercise Answers
        1. Chapter 1, Developing RESTful APIs with Django
        2. Chapter 2, Working with Class-Based Views and Hyperlinked APIs in Django
        3. Chapter 3, Improving and Adding Authentication to an API With Django
        4. Chapter 4, Throttling, Filtering, Testing, and Deploying an API with Django
        5. Chapter 5, Developing RESTful APIs with Flask
        6. Chapter 6, Working with Models, SQLAlchemy, and Hyperlinked APIs in Flask
        7. Chapter 7, Improving and Adding Authentication to an API with Flask
        8. Chapter 8, Testing and Deploying an API with Flask
        9. Chapter 9, Developing RESTful APIs with Tornado
        10. Chapter 10, Working with Asynchronous Code, Testing, and Deploying an API with Tornado

    Product information

    • Title: Building RESTful Python Web Services
    • Author(s): Gastón C. Hillar
    • Release date: October 2016
    • Publisher(s): Packt Publishing
    • ISBN: 9781786462251