Book Image

Hands-On RESTful Python Web Services - Second Edition

By : Gaston C. Hillar
1 (1)
Book Image

Hands-On RESTful Python Web Services - Second Edition

1 (1)
By: Gaston C. Hillar

Overview of this book

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.
Table of Contents (19 chapters)
Title Page
Dedication
About Packt
Contributors
Preface
Index

Chapter 1. Developing RESTful APIs and Microservices with Flask 1.0.2

In this chapter, we will start our journey toward RESTful Web APIs with Python 3.7 and four different web frameworks. Python is one of the most popular and versatile programming languages. There are thousands of Python packages, and these allow you to extend Python capabilities to any kind of domain you can imagine, such as web development, Internet of Things (IoT), artificial intelligence, machine learning, and scientific computing. We can work with many different web frameworks and packages to easily build simple and complex RESTful Web APIs with Python, and we can combine these frameworks with other Python packages.

We can leverage our existing knowledge of Python and all of its packages to code the different pieces of our RESTful Web APIs and their ecosystem. We can use the object-oriented features to create code that is easier to maintain, understand, and reuse. We can use all the packages that we are already comfortable with to interact with databases, web services, and different APIs. Python makes it easy for us to create RESTful Web APIs. In addition, lightweight frameworks, such as Flask, are ideal candidates for creating microservices that provide RESTful APIs. We don't need to learn another programming language; we can use the one we already know and love.

In this chapter, we will start working with Flask 1.0.2 and its Flask-RESTful extension, and we will create a RESTful Web API that performs CRUD (short for Create, Read, Update, and Delete) operations on a simple list. We will establish the baseline to develop microservices that provide a RESTful API with Flask. We will look at the following topics:

  • Design a RESTful API that performs CRUD operations in Flask with the Flask-RESTful extension
  • Understand the tasks performed by each HTTP method
  • Understand microservices
  • Work with lightweight virtual environments
  • Set up the virtual environment with Flask and its Flask-RESTful extension
  • Declare status codes for the responses with an enumerable
  • Create the model
  • Use a dictionary as a repository
  • Configure output fields
  • Work with resourceful routing on top of Flask pluggable views
  • Configure resource routing and endpoints
  • Make HTTP requests to the Flask API
  • Work with command-line tools to interact with the Flask API
  • Work with GUI tools to interact with the Flask API
  • Consume the API with other programming languages