ebook img

Hands-On RESTful Python Web Services: Develop RESTful web services or APIs with modern Python 3.7, 2nd Edition PDF

490 Pages·2018·11.803 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Hands-On RESTful Python Web Services: Develop RESTful web services or APIs with modern Python 3.7, 2nd Edition

Hands-On RESTful Python Web Services Second Edition Develop RESTful web services or APIs with modern Python 3.7 Gaston C. Hillar BIRMINGHAM - MUMBAI Hands-On RESTful Python Web Services Second Edition Copyright © 2018 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author(s), nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Commissioning Editor: Aaron Lazar Acquisition Editor: Reshma Raman Content Development Editor: Rohit Singh Technical Editor: Gaurav Gala Copy Editor: Safis Editing Project Coordinator: Vaidehi Sawant Proofreader: Safis Editing Indexer: Priyanka Dhadke Graphics: Alishon Mendonsa Production Coordinator: Shraddha Falebhai First published: October 2016 Second edition: December 2018 Production reference: 1221218 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78953-222-7 www.packtpub.com To my sons, Kevin and Brandon, and my wife, Vanesa. – Gaston C. Hillar mapt.io Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. Why subscribe? Spend less time learning and more time coding with practical eBooks and videos from over 4,000 industry professionals Improve your learning with Skill Plans built especially for you Get a free eBook or video every month Mapt is fully searchable Copy and paste, print, and bookmark content Packt.com Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and, as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks. Contributors About the author 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. While writing this book, I was fortunate enough to work with an excellent team at Packt Publishing Ltd, whose contributions vastly improved its presentation. Reshma Raman allowed me to provide her with ideas for developing the second edition of this book and I jumped at the exciting prospect of teaching how to use many popular web frameworks for developing RESTful web services with modern Python 3.6 and 3.7. Rohit Singh helped me realize my vision for this new edition of the book and provided many sensible suggestions regarding the text, the format, and the flow. I would like to thank my technical reviewers and proofreaders for their thorough reviews and insightful comments. I was able to incorporate some of the knowledge and wisdom they have gained during their many years in the software development industry. This book was possible because they provided valuable feedback. I usually start writing notes about ideas for a book when I spend time at software development conferences and events. I wrote the initial idea for the first edition of this book in San Francisco, California, at Intel Developer Forum 2015. One year later, at Intel Developer Forum 2016, I had the opportunity to discuss the book I was completing with a number of software engineers and incorporate their suggestions in the final drafts of the first edition. I started writing notes for the second edition in San Jose, California, at Sensors Expo 2018. The entire process of writing a book requires a huge number of lonely hours. I wouldn't be able to write an entire book without dedicating some time to play soccer against my sons, Kevin and Brandon, and my nephew, Nicolas. Of course, I never won a match. However, I did score a few goals. About the reviewers Norbert Máté is a web developer who started his career back in 2008. His first programming language as a professional web developer was PHP, before moving on to JavaScript/Node.js and Python/Django/Django Rest Framework. He is passionate about software architecture, design patterns, and clean code. Norbert has reviewed other Django books, including Django RESTful Web Services. Sanjeev Jaiswal is a Computer Graduate from CUSAT with 9 years of industrial experience. He uses Perl, Python, AWS, and GNU/Linux for his day-to-day activities. He is currently working on projects involving penetration testing, source code review, and security design and implementations in AWS and cloud security projects. He is currently learning about DevSecOps and security automation as well. Sanjeev loves teaching engineering students and IT professionals. He has been teaching in his leisure time for the last 8 years. He has written Instant PageSpeed Optimization and co-authored Learning Django Web Development for Packt Publishing. Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea. Table of Contents Preface 1 Chapter 1: Developing RESTful APIs and Microservices with Flask 1.0.2 9 Designing a RESTful API to interact with a simple data source 10 Understanding the tasks performed by each HTTP method 12 Understanding microservices 13 Working with lightweight virtual environments 14 Setting up a virtual environment with Flask and Flask-RESTful 20 Declaring status codes for the responses with an enumerable 21 Creating the model 24 Using a dictionary as a repository 25 Configuring output fields 26 Working with resourceful routing on top of Flask pluggable views 28 Configuring resource routing and endpoints 34 Making HTTP requests to the Flask API 35 Working with the curl and httpie command-line tools 37 Working with GUI tools – Postman and others 46 Consuming the API with other programming languages 55 Test your knowledge 55 Summary 57 Chapter 2: Working with Models, SQLAlchemy, and Hyperlinked APIs in Flask 58 Designing a RESTful API to interact with a PostgreSQL 10.5 database 59 Understanding the tasks performed by each HTTP method 60 Installing packages with the requirements.txt file to simplify our common tasks 62 Creating the database 64 Configuring the database 66 Creating models with their relationships 67 Creating schemas to validate, serialize, and deserialize models 70 Combining blueprints with resourceful routing 75 Understanding and configuring resourceful routing 85 Registering the blueprint and running migrations 86 Verifying the contents of the PostgreSQL database 90 Creating and retrieving related resources 92 Test your knowledge 100 Table of Contents Summary 102 Chapter 3: Improving Our API and Adding Authentication to it with Flask 103 Improving unique constraints in the models 104 Understanding the differences between the PUT and the PATCH methods 111 Updating fields for a resource with the PATCH method 112 Coding a generic pagination class 114 Adding pagination features 119 Understanding the steps to add authentication and permissions 126 Adding a user model 128 Creating schemas to validate, serialize, and deserialize users 131 Adding authentication to resources 131 Creating resource classes to handle users 134 Running migrations to generate the user table 137 Composing requests with the necessary authentication 140 Test your knowledge 144 Summary 145 Chapter 4: Testing and Deploying an API in a Microservice with Flask 146 Setting up unit tests with pytest 146 Creating a database for testing 148 Creating fixtures to perform setup and teardown tasks for running clean tests 151 Writing the first round of unit tests 152 Running unit tests with pytest and checking testing coverage 159 Improving testing coverage 164 Understanding strategies for deployments and scalability 171 Test your knowledge 174 Summary 175 Chapter 5: Developing RESTful APIs with Django 2.1 176 Designing a RESTful API to interact with a simple SQLite database 176 Understanding the tasks performed by each HTTP method 178 Setting up the virtual environment with Django REST framework 179 Creating the models 186 Managing serialization and deserialization 191 Understanding status codes for the responses 197 Writing API views 198 Making HTTP requests to the Django API 202 Working with command-line tools - curl and httpie 204 Working with GUI tools - Postman and others 213 Test your knowledge 217 [ ii ]

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.