Building RESTful Python Web Services

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

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."

Author(s): Gaston C. Hillar
Year: 2016

Language: English

Cover
Copyright
Credits
About the Author
Acknowledgments
About the Reviewer
www.PacktPub.com
Table of Contents
Preface
Chapter 1: Developing RESTful APIs with Django
Designing a RESTful API to interact with a simple SQLite database
Understanding the tasks performed by each HTTP method
Working with lightweight virtual environments
Setting up the virtual environment with Django REST framework
Creating the models
Managing serialization and deserialization
Writing API views
Making HTTP requests to the API
Working with command-line tools – curl and httpie
Working with GUI tools – Postman and others
Test your knowledge
Summary
Chapter 2: Working with Class-Based Views and Hyperlinked APIs in Django
Using model serializers to eliminate duplicate code
Working with wrappers to write API views
Using the default parsing and rendering options and move beyond JSON
Browsing the API
Designing a RESTful API to interact with a complex PostgreSQL database
Understanding the tasks performed by each HTTP method
Declaring relationships with the models
Managing serialization and deserialization with relationships and hyperlinks
Creating class-based views and using generic classes
Taking advantage of generic class based views
Working with endpoints for the API
Creating and retrieving related resources
Test your knowledge
Summary
Chapter 3: Improving and Adding Authentication to an API With Django
Adding unique constraints to the models
Updating a single field for a resource with the PATCH method
Taking advantage of pagination
Customizing pagination classes
Understanding authentication, permissions and throttling
Adding security-related data to the models
Creating a customized permission class for object-level permissions
Persisting the user that makes a request
Configuring permission policies
Setting a default value for a new required field in migrations
Composing requests with the necessary authentication
Browsing the API with authentication credentials
Test your knowledge
Summary
Chapter 4: Throttling, Filtering, Testing, and Deploying an API with Django
Understanding throttling classes
Configuring throttling policies
Testing throttling policies
Understanding filtering, searching, and ordering classes
Configuring filtering, searching, and ordering for views
Testing filtering, searching, and ordering
Filtering, searching, and ordering in the Browsable API
Setting up unit tests
Writing a first round of unit tests
Running unit tests and checking testing coverage
Improving testing coverage
Understanding strategies for deployments and scalability
Test your knowledge
Summary
Chapter 5: Developing RESTful APIs with Flask
Designing a RESTful API to interact with a simple data source
Understanding the tasks performed by each HTTP method
Setting up a virtual environment with Flask and Flask-RESTful
Declaring status codes for the responses
Creating the model
Using a dictionary as a repository
Configuring output fields
Working with resourceful routing on top of Flask pluggable views
Configuring resource routing and endpoints
Making HTTP requests to the Flask API
Working with command-line tools – curl and httpie
Working with GUI tools – Postman and others
Test your knowledge
Summary
Chapter 6: Working with Models, SQLAlchemy, and Hyperlinked APIs in Flask
Designing a RESTful API to interact with a PostgreSQL database
Understanding the tasks performed by each HTTP method
Installing packages to simplify our common tasks
Creating and configuring the database
Creating models with their relationships
Creating schemas to validate, serialize, and deserialize models
Combining blueprints with resourceful routing
Registering the blueprint and running migrations
Creating and retrieving related resources
Test your knowledge
Summary
Chapter 7: Improving and Adding Authentication to an API with Flask
Improving unique constraints in the models
Updating fields for a resource with the PATCH method
Coding a generic pagination class
Adding pagination features
Understanding the steps to add authentication and permissions
Adding a user model
Creating a schemas to validate, serialize, and deserialize users
Adding authentication to resources
Creating resource classes to handle users
Running migrations to generate the user table
Composing requests with the necessary authentication
Test your knowledge
Summary
Chapter 8: Testing and Deploying an API with Flask
Setting up unit tests
Writing a first round of unit tests
Running unit tests with nose2 and checking testing coverage
Improving testing coverage
Understanding strategies for deployments and scalability
Test your knowledge
Summary
Chapter 9: Developing RESTful APIs with Tornado
Designing a RESTful API to interact with slow sensors and actuators
Understanding the tasks performed by each HTTP method
Setting up a virtual environment with Tornado
Declaring status codes for the responses
Creating the classes that represent a drone
Writing request handlers
Mapping URL patterns to request handlers
Making HTTP requests to the Tornado API
Working with command-line tools – curl and httpie
Working with GUI tools – Postman and others
Test your knowledge
Summary
Chapter 10: Working with Asynchronous Code, Testing, and Deploying an API with Tornado
Understanding synchronous and asynchronous execution
Refactoring code to take advantage of asynchronous decorators
Mapping URL patterns to asynchronous request handlers
Making HTTP requests to the Tornado non-blocking API
Setting up unit tests
Writing a first round of unit tests
Running unit tests with nose2 and checking testing coverage
Improving testing coverage
Other Python Web frameworks for building RESTful APIs
Test your knowledge
Summary
Appendix: Exercise Answers
Chapter 1, Developing RESTful APIs with Django
Chapter 2, Working with Class-Based Views and Hyperlinked APIs in Django
Chapter 3, Improving and Adding Authentication to an API With Django
Chapter 4, Throttling, Filtering, Testing, and Deploying an API with Django
Chapter 5, Developing RESTful APIs with Flask
Chapter 6, Working with Models, SQLAlchemy, and Hyperlinked APIs in Flask
Chapter 7, Improving and Adding Authentication to an API with Flask
Chapter 8, Testing and Deploying an API with Flask
Chapter 9, Developing RESTful APIs with Tornado
Chapter 10, Working with Asynchronous Code, Testing, and Deploying an API with Tornado
Index