Build APIs with Flask (the right way)

Описание к видео 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:

Check out https://learn.microapis.io/ for more educational content about APIs and web development. Use the code "HALFOFF-YT" to obtain a 50% discount on my course "Build APIs with Python: FastAPI Edition" (https://sso.teachable.com/secure/1592...)

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.

Another good library I recommend you check out is APIFlask (https://github.com/apiflask/apiflask).

Links:
- Flask: https://github.com/pallets/flask
- Flask-smorest's GitHub: https://github.com/marshmallow-code/f...
- Flask-smorest's docs: https://flask-smorest.readthedocs.io/...
- Marshmallow: https://github.com/marshmallow-code/m...
- OpenAPI: https://www.openapis.org/
- JSON Schema: https://json-schema.org/
- The concept of collection and signleton endpoints is introduced by Prakash Subramaniam in "REST API Design - Resource Modeling" (https://www.thoughtworks.com/en-gb/in...)

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.

I'm also the author of "Microservice APIs". Get a copy through this link: http://mng.bz/jy4x and use the following code to get a 40% discount: slperalta (reach out to me if you have issues using the code).

You can also download two chapters of the book for free from this link: https://microapis.io/resources/micros...

Check out my newsletter for advanced content on APIs and distributed systems: https://microapis.substack.com/

Let me know also what other kinds of topics you'd like me to address in future videos!

Комментарии

Информация по комментариям в разработке