Reader small image

You're reading from  Building RESTful Python Web Services

Product typeBook
Published inOct 2016
Reading LevelIntermediate
PublisherPackt
ISBN-139781786462251
Edition1st Edition
Languages
Concepts
Right arrow
Author (1)
Gaston C. Hillar
Gaston C. Hillar
author image
Gaston C. Hillar

Gaston C. Hillar is Italian and has been working with computers since he was 8 years old. Gaston has a Bachelor's degree in computer science (graduated with honors) and an MBA. Currently, Gaston is an independent IT consultant and a freelance author who is always looking for new adventures anywhere in the world. He was a senior contributing editor at Dr. Dobb's, and has written more than a hundred articles on software development topics. He has received the prestigious Intel Black Belt Software Developer award eight times. He has written many articles about Java for Oracle Java Magazine. Gaston was also a former Microsoft MVP in technical computing. He lives with his wife, Vanesa, and his two sons, Kevin and Brandon.
Read more about Gaston C. Hillar

Right arrow

Preface

REST (Representational State Transfer) is the architectural style that is driving modern web development and mobile apps. In fact, developing and interacting with RESTful Web Services is a required skill in any modern software development job. Sometimes, you have to interact with an existing API and in other cases, you have to design a RESTful API from scratch and make it work with JSON (JavaScript Object Notation).

Python is one of the most popular programming languages. Python 3.5 is the most modern version of Python. It is open source, multiplatform, and you can use it to develop any kind of application, from websites to extremely complex scientific computing applications. There is always a Python package that makes things easier for you to avoid reinventing the wheel and solve the problems faster. The most important and popular Cloud computing providers make it easy to work with Python and its related Web frameworks. Thus, Python is an ideal choice for developing RESTful Web Services. The book covers all the things you need to know to select the most appropriate Python Web framework and develop a RESTful API from scratch.

You will work with the three most popular Python web frameworks that make it easy to develop RESTful Web Services: Django, Flask, and Tornado. Each web framework has its advantages and tradeoffs. You will work with examples that represent appropriate cases for each of these Web frameworks, in combination with additional Python packages that will simplify the most common tasks. You will learn to use different tools to test and develop high-quality, consistent and scalable RESTful Web Services. You will also take advantage of object-oriented programming, also known as OOP, to maximize code reuse and minimize maintenance costs.

You will always write unit tests and improve test coverage for all of the RESTful Web Services that you will develop throughout the book. You won’t just run the sample code but you will also make sure that you write tests for your RESTful API.

This book will allow you to learn how to take advantage of many packages that will simplify the most common tasks related to RESTful Web Services. You will be able to start creating your own RESTful APIs for any domain in any of the covered Web frameworks in Python 3.5 or greater.

What this book covers

Chapter 1, Developing RESTful APIs with Django, in this chapter we will start working with Django and Django REST Framework, and we will create a RESTful Web API that performs CRUD (Create, Read, Update and Delete) operations on a simple SQLite database.

Chapter 2, Working with Class-Based Views and Hyperlinked APIs in Django, in this chapter we will expand the capabilities of the RESTful API that we started in the previous chapter. We will change the ORM settings to work with a more powerful PostgreSQL database and we will take advantage of advanced features included in Django REST Framework that allow us to reduce boilerplate code for complex APIs, such as class based views.

Chapter 3, Improving and Adding Authentication to an API with Django, in this chapter we will improve the RESTful API that we started in the previous chapter. We will add unique constraints to the model and update the database. We will make it easy to update single fields with the PATCH method and we will take advantage of pagination. We will start working with authentication, permissions and throttling.

Chapter 4, Throttling, Filtering, Testing and Deploying an API with Django, in this chapter we will take advantage of many features included in Django REST Framework to define throttling policies. We will use filtering, searching and ordering classes to make it easy to configure filters, search queries and desired order for the results in HTTP requests. We will use the browsable API feature to test these new features included in our API. We will write a first round of unit tests, measure test coverage and then write additional unit tests to improve test coverage. Finally, we will learn many considerations for deployment and scalability.

Chapter 5 , Developing RESTful APIs with Flask, in this chapter we will start working with Flask and its Flask-RESTful extension. We will create a RESTful Web API that performs CRUD operations on a simple list.

Chapter 6, Working with Models, SQLAlchemy, and Hyperlinked APIs in Flask, in this chapter we will expand the capabilities of the RESTful API that we started in the previous chapter. We will use SQLAlchemy as our ORM to work with a PostgreSQL database and we will take advantage of advanced features included in Flask and Flask-RESTful that will allow us to easily organize code for complex APIs, such as models and blueprints.

Chapter 7, Improving and Adding Authentication to an API with Flask, in this chapter we will improve the RESTful API in many ways. We will add user friendly error messages when resources aren’t unique. We will test how to update single or multiple fields with the PATCH method and we will create our own generic pagination class. Then, we will start working with authentication and permissions. We will added a user model and we will update the database. We will make many changes in the different pieces of code to achieve a specific security goal and we will take advantage of Flask-HTTPAuth and passlib to use HTTP authentication in our API.

Chapter 8, Testing and Deploying an API with Flask, in this chapter we will set up a testing environment. We will install nose2 to make it easy to discover and execute unit tests and we will create a new database to be used for testing. We will write a first round of unit tests, measure test coverage and then write additional unit tests to improve test coverage. Finally, we will learn many considerations for deployment and scalability.

Chapter 9,Developing RESTful APIs with Tornado, we will work with Tornado to create a RESTful Web API. We will design a RESTful API to interact with slow sensors and actuators. We will defined the requirements for our API and we will understand the tasks performed by each HTTP method. We will create the classes that represent a drone and write code to simulate slow I/O operations that are called for each HTTP request method. We will write classes that represent request handlers and process the different HTTP requests and configure the URL patterns to route URLs to request handlers and their methods.

Chapter 10, Working with Asynchronous Code, Testing, and Deploying an API with Tornado, in this chapter we will understand the difference between synchronous and asynchronous execution. We will create a new version of the RESTful API that takes advantage of the non-blocking features in Tornado combined with asynchronous execution. We will improve scalability for our existing API and we will make it possible to start executing other requests while waiting for the slow I/O operations with sensors and actuators. Then, we will set up a testing environment. We will install nose2 to make it easy to discover and execute unit tests. We will wrote a first round of unit tests, measure test coverage and then write additional unit tests to improve test coverage. We will create all the necessary tests to have a complete coverage of all the lines of code.

What you need for this book

This book will guide you through the installation of all the tools that you need to follow the examples. You will need to install Webstorm version 10 to effectively run the code samples present in this book.

Who this book is for

This book is intended for web developers with no knowledge of WebStorm yet but who are experienced in JavaScript, Node.js, HTML, and CSS, and are reasonably familiar with frameworks such as AngularJS and Meteor.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "If no game matches the specified id or primary key, the server will return just a 404 Not Found status."

A block of code is set as follows:

from django.apps import AppConfig
class GamesConfig(AppConfig):
    name = 'games'

Any command-line input or output is written as follows:

python3 -m venv ~/PythonREST/Django01

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply e-mail feedback@packtpub.com, and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Building-RESTful-Python-Web-Services. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at copyright@packtpub.com with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at questions@packtpub.com, and we will do our best to address the problem.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Building RESTful Python Web Services
Published in: Oct 2016Publisher: PacktISBN-13: 9781786462251
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Gaston C. Hillar

Gaston C. Hillar is Italian and has been working with computers since he was 8 years old. Gaston has a Bachelor's degree in computer science (graduated with honors) and an MBA. Currently, Gaston is an independent IT consultant and a freelance author who is always looking for new adventures anywhere in the world. He was a senior contributing editor at Dr. Dobb's, and has written more than a hundred articles on software development topics. He has received the prestigious Intel Black Belt Software Developer award eight times. He has written many articles about Java for Oracle Java Magazine. Gaston was also a former Microsoft MVP in technical computing. He lives with his wife, Vanesa, and his two sons, Kevin and Brandon.
Read more about Gaston C. Hillar