filmov
tv
Build APIs with Flask (the right way)

Показать описание
Flask is a hugely popular Python web development framework. Tons of websites are built with Flask, and tons of them contain APIs. I've had the privilege to work with many companies building APIs with Flask, I've seen some common errors and anti-patterns, and I've put together a bunch of best practices. This video teaches you how to avoid some common mistakes when building APIs with Flask and how to do it the rigth way. We'll build a simple TODO API.
The code repository for this tutorial is here:
0:00 Introduction
00:51 Introducing Flask
03:04 How to choose a good API library
04:25 Introducing Flask-smorest
05:36 Setting up with environment with Poetry
08:25 Configuring the Python interpreter with Pycharm
09:00 Creating an instance of the Flask application object
09:39 Creating a "hello world" application with Flask
11:00 How to run Flask's web development server
13:41 Setting up Flask-smorest
14:46 Working with blueprints
17:27 Adding configuration for Flask-smorest
21:38 Adding an in-memory list of tasks
23:25 Function-based vs class-based routes/views
24:30 Collection vs singleton endpoints
25:07 Implementing the /todo/tasks URL path
27:26 Visualising Flask-smorest's auto generated API docs
28:39 Defining API validation schemas with Marshmallow
29:51 Hooking Marshmallow models with Flask endpoints
30:19 Implementing the UpdateTask and Task schemas
31:13 Defining response payloads with Marshmallow models
31:29 Defining URL query parameters with Marshmallow
34:01 Returning data from the API
35:46 Testing the /todo/tasks endpoints with the Swagger UI
36:55 Handling URL query parameters for sorting tasks
40:40 Implementing the singleton endpoints
43:03 Implementing GET /todo/tasks/{task_id}
43:24 Using Flask's abort() function to return API errors
43:49 Implementing PUT /todo/tasks/{task_id}
44:45 Implementing DELETE /todo/tasks/{task_id}
45:38 Testing and fixing the singleton endpoints
40:05 Wrapping up
If you're not familiar with it, Flask is one of Python's top web development frameworks. People love Flask because it's lightweight, non-opinionated, very intuitive and easy to use. Flask gives us a lot of flexibility on how to structure our applications and how to implement each layer. However, with great flexibility also comes great responsibility, and this is where things often go astray.
That's specially true for APIs. I've seen it myself. You can get going building APIs directly with Flask, returning JSON, parsing and validating request payloads yourself. And before you realise it, you've built your own API framework, just not a very good one.
APIs are deceptively simple, and so it's very tempting to build them without the right tools. Don't do that. In this video, you'll learn to build Flask APIs using Flask-smorest, a great Flask plugin for building REST APIs.
A good API library has the following ingredients:
- Handles data validation and serialization (for both requests and responses)
- Handles and validates URL query and path parameters
- Has an understanding of how OpenAPI works (OpenAPI is the gold standard for documenting REST APIs)
- Plays well with JSON Schema semantics (OpenAPI is based on JSON Schema)
Flask-smorest meets all these criteria.
Links:
If you liked this video, please like it and share it with your network! You can also subscribe to my channel! All this goes a long way to supporting me to continue creating this kind of content.
Let me know also what other kinds of topics you'd like me to address in future videos!
The code repository for this tutorial is here:
0:00 Introduction
00:51 Introducing Flask
03:04 How to choose a good API library
04:25 Introducing Flask-smorest
05:36 Setting up with environment with Poetry
08:25 Configuring the Python interpreter with Pycharm
09:00 Creating an instance of the Flask application object
09:39 Creating a "hello world" application with Flask
11:00 How to run Flask's web development server
13:41 Setting up Flask-smorest
14:46 Working with blueprints
17:27 Adding configuration for Flask-smorest
21:38 Adding an in-memory list of tasks
23:25 Function-based vs class-based routes/views
24:30 Collection vs singleton endpoints
25:07 Implementing the /todo/tasks URL path
27:26 Visualising Flask-smorest's auto generated API docs
28:39 Defining API validation schemas with Marshmallow
29:51 Hooking Marshmallow models with Flask endpoints
30:19 Implementing the UpdateTask and Task schemas
31:13 Defining response payloads with Marshmallow models
31:29 Defining URL query parameters with Marshmallow
34:01 Returning data from the API
35:46 Testing the /todo/tasks endpoints with the Swagger UI
36:55 Handling URL query parameters for sorting tasks
40:40 Implementing the singleton endpoints
43:03 Implementing GET /todo/tasks/{task_id}
43:24 Using Flask's abort() function to return API errors
43:49 Implementing PUT /todo/tasks/{task_id}
44:45 Implementing DELETE /todo/tasks/{task_id}
45:38 Testing and fixing the singleton endpoints
40:05 Wrapping up
If you're not familiar with it, Flask is one of Python's top web development frameworks. People love Flask because it's lightweight, non-opinionated, very intuitive and easy to use. Flask gives us a lot of flexibility on how to structure our applications and how to implement each layer. However, with great flexibility also comes great responsibility, and this is where things often go astray.
That's specially true for APIs. I've seen it myself. You can get going building APIs directly with Flask, returning JSON, parsing and validating request payloads yourself. And before you realise it, you've built your own API framework, just not a very good one.
APIs are deceptively simple, and so it's very tempting to build them without the right tools. Don't do that. In this video, you'll learn to build Flask APIs using Flask-smorest, a great Flask plugin for building REST APIs.
A good API library has the following ingredients:
- Handles data validation and serialization (for both requests and responses)
- Handles and validates URL query and path parameters
- Has an understanding of how OpenAPI works (OpenAPI is the gold standard for documenting REST APIs)
- Plays well with JSON Schema semantics (OpenAPI is based on JSON Schema)
Flask-smorest meets all these criteria.
Links:
If you liked this video, please like it and share it with your network! You can also subscribe to my channel! All this goes a long way to supporting me to continue creating this kind of content.
Let me know also what other kinds of topics you'd like me to address in future videos!
Комментарии