Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22875

Real Python: Python REST APIs With Flask, Connexion, and SQLAlchemy – Part 1

$
0
0

Most modern web applications are powered by a REST API under the hood. That way, developers can separate the front-end code from the back-end logic, and users can interact with the interface dynamically. In this three-part tutorial series, you’ll build a REST API with the Flask web framework.

You’ll create a foundation with a basic Flask project then add endpoints and connect them to a SQLite database. You’ll test your API with Swagger UI API documentation that you’ll build along the way.

In the first part of this tutorial series, you’ll learn how to:

  • Build a base Flask project with a REST API
  • Handle HTTP requests with Connexion
  • Define API endpoints using the OpenAPI specification
  • Interact with your API to manage data
  • Build API documentation with Swagger UI

After finishing the first part of this series, you’ll move on to the second part, where you’ll learn to use a proper database to store your data permanently instead of relying on in-memory storage.

This tutorial series is a hand-on guide on how to create a REST API with Flask and interact with it using CRUD operations. If you want to refresh your knowledge on working with APIs, then you can give Python and REST APIs: Interacting With Web Services a read.

You can download the code for the first part of this project by clicking the link below:

Source Code:Click here to download the free source code that you’ll use to build a REST API with the Flask web framework.

Demo

In this three-part tutorial series, you’ll build a REST API to keep track of notes for people that may visit you throughout the year. In this tutorial, you’ll create people like the Tooth Fairy, the Easter Bunny, and Knecht Ruprecht.

Ideally, you want to be on good terms with all three of them. That’s why you’ll send them notes, to increase the chance of getting valuable gifts from them.

You can interact with your application by leveraging the API documentation. Along the way, you’ll build a basic front end that reflects the contents of your database:

In the first part of this series, you’ll create a base Flask project and plug in your first API endpoints. At the end of this part, you’ll be able to see a list of people in the front end and manage each person in the back end:

By leveraging Swagger UI, you’ll create handy documentation for your API along the way. That way, you’ll have the opportunity to test how your API works at each stage of this tutorial and get a useful overview of all your endpoints.

Planning Part One

Besides building the Flask project foundation, you’re going to create a REST API that provides access to a collection of people and to the individuals within that collection. Here’s the API design for the people collection:

ActionHTTP VerbURL PathDescription
ReadGET/api/peopleRead a collection of people.
CreatePOST/api/peopleCreate a new person.
ReadGET/api/people/<lname>Read a particular person.
UpdatePUT/api/people/<lname>Update an existing person.
DeleteDELETE/api/people/<lname>Delete an existing person.

The REST API that you’ll be building will serve a simple people data structure where the people are keyed to the last name, and any updates are marked with a new timestamp.

The dataset that you’ll be working with looks like this:

PEOPLE={"Fairy":{"fname":"Tooth","lname":"Fairy","timestamp":"2022-10-08 09:15:10",},"Ruprecht":{"fname":"Knecht","lname":"Ruprecht","timestamp":"2022-10-08 09:15:13",},"Bunny":{"fname":"Easter","lname":"Bunny","timestamp":"2022-10-08 09:15:27",}}

One of the purposes of an API is to decouple the data from the application that uses it, thereby hiding the data implementation details. Later in this tutorial series, you’ll save your data in a database. But for the start, an in-memory data structure works fine.

Getting Started

In this section, you’ll prepare the development environment for your Flask REST API project. First, you’ll create a virtual environment and install all the dependencies that you need for your project.

Read the full article at https://realpython.com/flask-connexion-rest-api/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]


Viewing all articles
Browse latest Browse all 22875

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>