Тёмный

Build APIs with Flask (the right way) 

microapis
Подписаться 1,3 тыс.
Просмотров 6 тыс.
50% 1

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 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" (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 (github.com/apiflask/apiflask).
Links:
- Flask: github.com/pallets/flask
- Flask-smorest's GitHub: github.com/marshmallow-code/f...
- Flask-smorest's docs: flask-smorest.readthedocs.io/...
- Marshmallow: github.com/marshmallow-code/m...
- OpenAPI: www.openapis.org/
- JSON Schema: json-schema.org/
- The concept of collection and signleton endpoints is introduced by Prakash Subramaniam in "REST API Design - Resource Modeling" (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: 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: microapis.io/resources/micros...
Check out my newsletter for advanced content on APIs and distributed systems: microapis.substack.com/
Let me know also what other kinds of topics you'd like me to address in future videos!

Наука

Опубликовано:

 

6 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 15   
@venil82
@venil82 3 месяца назад
13 mintes in to video, and I love it already! thanks for sharing proper knowledge,
@microapis
@microapis Месяц назад
Thank you for your supportive comment @venil82 🙌!
@polatkaya418
@polatkaya418 5 месяцев назад
Came from your comment on my Post in Reddit, absolute teacher!
@microapis
@microapis 5 месяцев назад
Thank you for your kind words and for visiting 🙌!
@jbjc3924
@jbjc3924 5 месяцев назад
very exited to get started thank you for putting this together
@microapis
@microapis 5 месяцев назад
Thank you! Glad to help if something isn't clear or you need any help!
@chidobg84
@chidobg84 5 месяцев назад
Excellent work sir, thank you for your work. Do you know of anyway to avoid using the CDN for the OPEAPI_SWAGGER_UI_URL and other external urls? Assuming policy does not allow use of CDNs.
@microapis
@microapis 23 дня назад
Thank you for your question! A workaround would be serving the Swagger UI assets from your own domain. For example, you can download the assets here: cdn.jsdelivr.net/npm/swagger-ui-dist/. You could serve them from Flask, from an S3 bucket if you use AWS, from Cloudflare if you use that, or any other alternative.
@mlsandreas
@mlsandreas 5 месяцев назад
Thank you for this video!! waiting for more content. So My question flask or fastapi , what is your choice?
@microapis
@microapis 5 месяцев назад
Thank you @mlsandreas! If I'm going to build just an API, FastAPI is my choice. If I'm going to render HTML and such, I go with Flask. You can render HTML with FastAPI via Starlette too, but I just find Flask more convenient. Also I like the concept of middleware in Flask (aka plugins) slightly more. However, for the most part I find both frameworks very similar. Do you have a preference?x
@mlsandreas
@mlsandreas 5 месяцев назад
@@microapis i prefer flask because i want to use it not for apis but for web apps too
@microapis
@microapis 5 месяцев назад
That makes perfect sense! Only recommendation I make to organisations making this move is to use to use a proper Flask framework (aka plugin) for the API implementation 🚀
@jasonhall6526
@jasonhall6526 4 месяца назад
Such a ROBUST video!!!!!!
@microapis
@microapis Месяц назад
Thank you @jasonhall6526 🙌🚀🚀!!
Далее
Why You NEED To Learn FastAPI | Hands On Project
21:15
Просмотров 151 тыс.
API Security Fundamentals [2023]
50:35
Просмотров 1,5 тыс.
Conquering fears and slippery slops on two wheels!
00:18
Build your first REST API with Flask and PostgreSQL
27:42
How to Use Flask-SQLAlchemy With Flask Blueprints
20:10
15 Python Libraries You Should Know About
14:54
Просмотров 370 тыс.
FastAPI, Flask or Django - Which Should You Use?
9:49
PA-RISC рабочая станция HP Visualize
41:27