Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Building REST APIs

with Flask
Create Python Web Services
with MySQL

Kunal Relan
Building REST APIs with Flask: Create Python Web Services with MySQL
Kunal Relan
New Delhi, Delhi, India

ISBN-13 (pbk): 978-1-4842-5021-1 ISBN-13 (electronic): 978-1-4842-5022-8


https://doi.org/10.1007/978-1-4842-5022-8

Copyright © 2019 by Kunal Relan


This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or
part of the material is concerned, specifically the rights of translation, reprinting, reuse of
illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way,
and transmission or information storage and retrieval, electronic adaptation, computer software,
or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark
symbol with every occurrence of a trademarked name, logo, or image we use the names, logos,
and images only in an editorial fashion and to the benefit of the trademark owner, with no
intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if
they are not identified as such, is not to be taken as an expression of opinion as to whether or not
they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of
publication, neither the authors nor the editors nor the publisher can accept any legal
responsibility for any errors or omissions that may be made. The publisher makes no warranty,
express or implied, with respect to the material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Nikhil Karkal
Development Editor: Laura Berendson
Coordinating Editor: Divya Modi
Cover designed by eStudioCalamar
Cover image designed by Freepik (www.freepik.com)
Distributed to the book trade worldwide by Springer Science+Business Media New York,
233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505,
e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a
California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc
(SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail rights@apress.com, or visit http://www.apress.
com/rights-permissions.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook
versions and licenses are also available for most titles. For more information, reference our Print
and eBook Bulk Sales web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available
to readers on GitHub via the book’s product page, located at www.apress.com/9781484250211.
For more detailed information, please visit http://www.apress.com/source-code.
Printed on acid-free paper
Dedicated to caffeine and sugar, my companions
through many long night of writing, and
extra credits to my mom.
Table of Contents
About the Author���������������������������������������������������������������������������������ix
About the Technical Reviewer�������������������������������������������������������������xi
Acknowledgments�����������������������������������������������������������������������������xiii
Introduction����������������������������������������������������������������������������������������xv

Chapter 1: Beginning with Flask����������������������������������������������������������1


Introduction to Flask���������������������������������������������������������������������������������������������1
Starting Flask��������������������������������������������������������������������������������������������������2
Flask Components Covered in This Book���������������������������������������������������������3
Introduction to RESTful Services��������������������������������������������������������������������������4
Uniform Interface���������������������������������������������������������������������������������������������7
Representations����������������������������������������������������������������������������������������������8
Messages��������������������������������������������������������������������������������������������������������9
Links Between Resources�����������������������������������������������������������������������������12
Caching����������������������������������������������������������������������������������������������������������13
Stateless��������������������������������������������������������������������������������������������������������13
Planning REST API�����������������������������������������������������������������������������������������14
API Design�����������������������������������������������������������������������������������������������������15
Setting Up Development Environment����������������������������������������������������������������16
Working with PIP�������������������������������������������������������������������������������������������17
Choosing the IDE�������������������������������������������������������������������������������������������18
Understanding Python Virtual Environments�������������������������������������������������19

v
Table of Contents

Setting Up Flask��������������������������������������������������������������������������������������������������24
Installing Flask����������������������������������������������������������������������������������������������25
Conclusion����������������������������������������������������������������������������������������������������������26

Chapter 2: Database Modeling in Flask����������������������������������������������27


Introduction���������������������������������������������������������������������������������������������������������27
SQL Databases����������������������������������������������������������������������������������������������28
NoSQL Databases������������������������������������������������������������������������������������������28
Key Differences: MySQL vs. MongoDB����������������������������������������������������������29
Creating a Flask Application with SQLAlchemy��������������������������������������������������30
Creating an Author Database�������������������������������������������������������������������������33
Sample Flask MongoEngine Application�������������������������������������������������������������46
Conclusion����������������������������������������������������������������������������������������������������������58

Chapter 3: CRUD Application with Flask (Part 1)��������������������������������59


User Authentication���������������������������������������������������������������������������������������������88
Conclusion����������������������������������������������������������������������������������������������������������96

Chapter 4: CRUD Application with Flask (Part 2)��������������������������������97


Introduction���������������������������������������������������������������������������������������������������������97
Email Verification������������������������������������������������������������������������������������������������98
File Upload��������������������������������������������������������������������������������������������������������109
API Documentation��������������������������������������������������������������������������������������������114
Building Blocks of API Documentation��������������������������������������������������������115
OpenAPI Specification���������������������������������������������������������������������������������116
Conclusion��������������������������������������������������������������������������������������������������������134

vi
Table of Contents

Chapter 5: Testing in Flask���������������������������������������������������������������135


Introduction�������������������������������������������������������������������������������������������������������135
Setting Up Unit Tests�����������������������������������������������������������������������������������136
Unit Testing User Endpoints�������������������������������������������������������������������������139
Test Coverage���������������������������������������������������������������������������������������������������155
Conclusion��������������������������������������������������������������������������������������������������������157

Chapter 6: Deploying Flask Applications������������������������������������������159


Deploying Flask with uWSGI and Nginx on Alibaba Cloud ECS�������������������������160
Deploying Flask on Gunicorn with Apache on Alibaba Cloud ECS���������������������167
Deploying Flask on AWS Elastic Beanstalk�������������������������������������������������������172
Deploying Flask App on Heroku������������������������������������������������������������������������176
Adding a Procfile�����������������������������������������������������������������������������������������177
Deploying Flask App on Google App Engine������������������������������������������������������180
Conclusion��������������������������������������������������������������������������������������������������������182

Chapter 7: Monitoring Flask Applications����������������������������������������183


Application Monitoring��������������������������������������������������������������������������������������183
Sentry����������������������������������������������������������������������������������������������������������185
Flask Monitoring Dashboard������������������������������������������������������������������������187
New Relic����������������������������������������������������������������������������������������������������189
Bonus Services��������������������������������������������������������������������������������������������192
Conclusion��������������������������������������������������������������������������������������������������������194

Index�������������������������������������������������������������������������������������������������195

vii
About the Author
Kunal Relan is an iOS security researcher
and a full stack developer with more than
four years of experience in various fields
of technology, including network security,
DevOps, cloud infrastructure, and application
development, working as a consultant with
start-ups around the globe. He is an Alibaba
Cloud MVP and author of iOS Penetration
Testing (Apress) and a variety of white papers.
Kunal is a technology enthusiast and an active
speaker. He regularly contributes to open source communities and writes
articles for Digital Ocean and Alibaba Techshare.  

ix
About the Technical Reviewer
Saurabh Badhwar is a software engineer
with a passion to build scalable distributed
systems. He is mostly working to solve
challenges related to performance of software
at a large scale and has been involved in
building solutions that help other developers
quickly analyze and compare performance
of their systems when running at scale.
He is also passionate about working with
open source communities and has been
actively participating as a contributor in various domains, which involve
development, testing, and community engagement. Saurabh has also been
an active speaker at various conferences where he has been talking about
performance of large-scale systems.  

xi
Acknowledgments
I would like to thank Apress for providing me this platform, without which
this would have been a lot harder. I would also like to thank Mr. Nikhil
Karkal for his help and Miss Divya Modi for her perseverance, without
whom this would have been a farsighted project.
I’d like to mention about the strong Python community which helped
me understand the core concepts in my early years of programming, which
inspired me to contribute back to the community with this book.
Last but certainly not the least, I would like to acknowledge all the
people who constantly reminded me about the deadlines and helped me
write this book, especially my family and Aparna Abhijit for helping me out
with editing.

xiii
Introduction
Flask is a lightweight microframework for web applications built on top
of Python, which provides an efficient framework for building web-based
applications using the flexibility of Python and strong community support
with the capability of scaling to serve millions of users.
Flask has excellent community support, documentation, and
supporting libraries; it was developed to provide a barebone framework for
developers, giving them the freedom to build their applications using their
preferred set of libraries and tools.
This book takes you through different stages of a REST API–based
application development process using flask which explains the basics of
the Flask framework assuming the readers understand Python. We’ll cover
database integration, understanding REST services, REST APIs performing
CRUD operations, user authentication, third-party library integrations,
testing, deployment, and application monitoring.
At the end of this book, you’ll have a fair understanding of Flask
framework, REST, testing, deploying, and managing Flask applications,
which will open doors to understanding REST API development.

xv

You might also like