Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Fast API を用いた Web API の開発
Search
ryounasso
January 19, 2024
Programming
0
100
Fast API を用いた Web API の開発
初心者にも向ける勉強会 20 WEB バックエンドのお話し Python, PostgreSQL等 でお話しした内容です。
ryounasso
January 19, 2024
Tweet
Share
More Decks by ryounasso
See All by ryounasso
テストゼロの個人開発プロジェクトにテストを導入した話
ryounasso
0
110
簡易 DI コンテナを作って DI コンテナを知る
ryounasso
1
580
TypeScript_コンパイラの内側に片足を入れる
ryounasso
3
470
kintone新機能開発のお仕事
ryounasso
2
60
Other Decks in Programming
See All in Programming
“Seeing Like a Programmer”—Resiliency, Limits, and Moral Hazards in Software Engineering (LambdaConf 2024)
chriskrycho
0
420
TypeScriptから始める VR生活
tamagokakeg
2
110
Prepare for Jakarta EE 11 - Performance and Developer Productivity
ivargrimstad
0
100
RailsConf 2024: Riffing on Rails: sketch your way to better designed code
kaspth
0
200
JavaScript Closure
asoluka
0
1.9k
WinActorの勉強を継続する方法
tamai_63
0
130
Timeline エディター拡張入門
yucchiy
0
440
Open standards for building event-driven applications in the cloud
meteatamel
0
210
dbtのドメイン分割による データ基盤の改善とDigdagとの連携
sakama
0
500
Namespace, What and Why
tagomoris
3
590
Jetpack Composeとデザインシステム
rmakiyama
0
210
Exploring the Implementation of “t.Run”, “t.Parallel”, and “t.Cleanup”
akarin
1
150
Featured
See All Featured
Become a Pro
speakerdeck
PRO
13
4.6k
How to Ace a Technical Interview
jacobian
273
22k
Navigating Team Friction
lara
179
13k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
245
20k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
188
16k
How GitHub Uses GitHub to Build GitHub
holman
468
290k
Practical Orchestrator
shlominoach
183
9.8k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
126
32k
Debugging Ruby Performance
tmm1
70
11k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
352
28k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
8
3.5k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
0
110
Transcript
Fast API を用いた Web API の開発 ryounasso
API とは Application Programming Interface の略 開発者が複雑な機能をより簡単に作成できるように、プログラミング言語から提供 される構造。 複雑なコードを抽象化し、それに変わる簡潔な構文を提供する。 Web
API の紹介 | mdn web docs https://developer.mozilla.org/ja/docs/Learn/JavaScript/Cl ient-side_web_APIs/Introduction クライアント サーバ Request page Request data Response
Fast API とは (公式ドキュメントより) FastAPI : https://fastapi.tiangolo.com/ Python の型ヒントに基づいて、API を構築するための、モダンで、高速な、
Web フレームワーク 主な特徴 - 高速 - 少ないバグ - 直感的 - 簡単 - 短い - 堅牢性 - Standards-based 個人的に - 簡単 - API ドキュメントの自動生成 - 型定義 (Pydantic)
環境構築はドキュメントがおすすめ fastapi pydantic uvicorn requests sqlalchemy aiomysql 公式ドキュメント https://fastapi.tiangolo.com/ja/tutorial/ Docker
を使う場合 https://fastapi.tiangolo.com/ja/deployment/docker/ 使用するライブラリ
Hello World!! main.py
例) ToDo アプリ 今回は例として、タスクの追加、登録の流れを実装 データ構造は id, タスク名, DONE を持つ +-------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra | +-------+---------------+------+-----+---------+----------------+ | id | int | NO | PRI | NULL | auto_increment | | name | varchar(1024) | YES | | NULL | | | done | tinyint(1) | YES | | NULL | | +-------+---------------+------+-----+---------+----------------+ app | – cruds/ | – models/ | – routers/ | – schemas/ | – main.py | – migrate_db.py
リクエストパスとパラメータを考える - タスクの追加 /tasks POST - タスクの取得 /tasks GET - URL はケバブケース - パラメータはキャメルケース
- リソースの操作は HTTP メソッドで表現 - リソースに動詞を用いない - 非リソースには動詞を用いる リソース: データの一部 コレクション: リソースのグループ URL: リソースやコレクションの場所
レスポンスを考える - 理解しやすい命名にする - 深さ・プロパティ数に注意する
API 設計ツールを使用する “Open API Initiative” という団体が “OAS” という REST API
記述フォーマット を推進している メリット - API の開発効率の向上 - 広く受け入れられているため、様々なツールを使用できる - チーム内などで、共有しやすい Swagger : https://swagger.io/ api blueprint : https://apiblueprint.org/
FastAPI だと Swagger UI や ReDoc を用いてドキュメントを自動生成してくれる
FastAPI だと Swagger UI や ReDoc を用いてドキュメントを自動生成してくれる
タスクの追加を実装 - スキーマの作成 - DB の model を作成 - エンドポイントの作成
- DB への値の追加処理を作成
スキーマの作成
DB の model を作成 マイグレートも必要
エンドポイントの作成
DB への値の追加処理を作成 - DB モデルの TaskModel に変換 - DB にコミット
- DB 上のデータをもとに task を更新
router で呼び出す
実装を確認 Swagger UI で簡単に確認が可能!
タスクの取得を実装 - スキーマの作成 - エンドポイントの作成 - DB への値の取得処理を作成
スキーマ / エンドポイントの作成
DB への値の取得処理を実装
実装を確認 Swagger UI で確認!
まとめ FastAPI を用いて Web API 開発の全体像をみた 簡単に早く Web API を実装することが可能
More - タスクを完了にするエンドポイントを作成 - エラーハンドリングを実装 - テストを実装
参考資料 - FastAPI https://fastapi.tiangolo.com/ - FastAPI 入門 | Zenn https://zenn.dev/sh0nk/books/537bb028709ab9
None