Book Image

Python API Development Fundamentals

By : Jack Chan, Ray Chung, Jack Huang
Book Image

Python API Development Fundamentals

By: Jack Chan, Ray Chung, Jack Huang

Overview of this book

Python is a flexible language that can be used for much more than just script development. By knowing the Python RESTful APIs work, you can build a powerful backend for web applications and mobile applications using Python. You'll take your first steps by building a simple API and learning how the frontend web interface can communicate with the backend. You'll also learn how to serialize and deserialize objects using the marshmallow library. Then, you'll learn how to authenticate and authorize users using Flask-JWT. You'll also learn how to enhance your APIs by adding useful features, such as email, image upload, searching, and pagination. You'll wrap up the whole book by deploying your APIs to the cloud. By the end of this book, you'll have the confidence and skill to leverage the power of RESTful APIs and Python to build efficient web applications.
Table of Contents (12 chapters)

About the Book

Python is a flexible language that can be used for much more than just script development. By knowing how the Python RESTful APIs work, you can build a powerful backend for web applications and mobile applications using Python.

You'll take your first steps by building a simple API and learning how the frontend web interface can communicate with the backend. You'll also learn how to serialize and deserialize objects using the marshmallow library. Then, you'll learn how to authenticate and authorize users using Flask-JWT. Apart from all this, you'll also learn how to enhance your APIs by adding useful features, such as email, image upload, searching, and pagination. You'll wrap up the whole book by deploying the APIs to the cloud.

By the end of this book, you'll have the confidence and skill to leverage the power of RESTful APIs and Python to build efficient web applications.

About the Authors

Jack Chan started programming at the age of 10. He was an active participant in worldwide programming contests at university. Since graduation, he has been working in the finance and IT industries for more than 10 years, building systems that analyze millions of transactions and positions to spot suspicious activity. He has leveraged the powerful analytical Python libraries to perform data analysis and performance optimization for a trading system that works at a microsecond level. He has an in-depth knowledge of the modern software development life cycle, which uses automated testing, continuous integration, and agile methodologies. Among all programming languages, he found Python to be the most expressive and powerful. He has created courses and taught students all over the world, using Python as the teaching language. Inspiring aspiring developers to take on the software engineering career path has always been Jack's goal.

Ray Chung is a developer and an instructor. He loves helping students learn to code and master software development. He is now self-employed and develops web applications, network applications, and chatbots using Python. The first program he sold was a network application that helped clients to configure, maintain and test thousands of multi-vendor network devices. He's experienced with big projects such as a Marathon's online registration system, rental car management systems, and more. He has worked extensively with Google App Engine, PostgreSQL, and advanced system architecture design. He has been a self-taught developer for many years and knows the most efficient ways to learn a new skill.

Jack Huang is a programmer with more than 7 years of experience in developing web applications in Python, Javascript, and .NET. He is skilled in web frameworks such as Flask, Django, and Vue, as well as in PostgreSQL, DynamoDB, MongoDB, RabbitMQ, Redis, Elasticsearch, RESTful API design, payment processing, system architecture design, database design, and Unix systems. He has written applications for an accessories shop platform, an ERP system, a divination web application, a podcast platform, a job search service, a blog system, a salon reservation system, an e-commerce service, and more. He also has experience in handling large amounts of data and optimizing payment processing. He is an expert web application developer who loves coding and is constantly following the newest technology.

Learning Objectives

By the end of this book, you will be able to:

  • Understand the concept of a RESTful API
  • Build a RESTful API using Flask and the Flask-Restful extension
  • Manipulate a database using Flask-SQLAlchemy and Flask-Migrate
  • Send out plaintext and HTML format emails using the Mailgun API
  • Implement a pagination function using Flask-SQLAlchemy
  • Use caching to improve API performance and efficiently obtain the latest information
  • Deploy an application to Heroku and test it using Postman

Audience

This book is ideal for aspiring software developers who have a basic-to-intermediate knowledge of Python programming and who want to develop web applications using Python. Knowledge of how web applications work will be beneficial, but is not essential.

Approach

This book takes the learning-by-doing approach to explain concepts to you. You'll build a real-life web application by implementing each concept that you learn in theory. This way, you'll reinforce your new skill.

Hardware Requirements

For the optimal experience, we recommend the following hardware configuration:

  • Processor: Intel Core i5 or equivalent
  • Memory: 4 GB RAM (8 GB preferred)
  • Storage: 35 GB available space

Software Requirements

We also recommend that you have the following software installed in advance:

  • OS: Windows 7 SP1 64-bit, Windows 8.1 64-bit or Windows 10 64-bit, Ubuntu Linux, or the latest version of OS X
  • Browser: Google Chrome/Mozilla Firefox (the latest version)
  • Python 3.4+ (the latest version is Python 3.8: from https://python.org)
  • Pycharm
  • Postman
  • Postgres Database

Conventions

Code words in the text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows:

"Next, we will work on the create_recipe function, which creates a recipe in memory. Use the /recipes route to trigger the create_recipe function and the methods = [POST] argument to specify that the route decorator will only respond to POST requests."

New terms and important words are shown in bold. Words that you see on screen, for example, in menus or dialog boxes, appear in the text like this: " Then, select Definition and set the password. Click Save".

A block of code is set as follows:

    if not recipe:
        return jsonify({'message': 'recipe not found'}), HTTPStatus.NOT_FOUND

Installation and Setup

Before we can do awesome things with data, we need to be prepared with the most productive environment. In this short section, we will see how to do that.

Installing Python

Go to https://www.python.org/downloads/ and follow the instructions specific to your platform.

Installing Pycharm Community Edition

Go to https://www.jetbrains.com/pycharm/download/ and follow the instructions specific to your platform.

Installing Postman

Go to https://www.getpostman.com/downloads/ and follow the instructions specific to your platform.

Installing Postgres Database

We are going to install Postgres on our local machine:

  1. Go to http://www.postgresql.org and click Download for the download page.
  2. Select macOS or Windows, depending on your operation system.
  3. Under Interactive installer by EnterpriseDB, download the latest version of the installer. The installer contains PostgreSQL as well as pgAdmin, which is a graphical tool for managing and developing your databases.
  4. Install Postgres version 11.4. Follow the on-screen instructions to install Postgres and set the password.
  5. Once you are done with the installation, you will be brought to pgAdmin. Please set up a pgAdmin password.

Additional Resources

The code bundle for this book is also hosted on GitHub at https://github.com/TrainingByPackt/Python-API-Development-Fundamentals. We also have other code bundles from our rich catalog of courses and videos available at https://github.com/PacktPublishing/. Check them out!