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
520
Fast API を用いた Web API の開発
初心者にも向ける勉強会 20 WEB バックエンドのお話し Python, PostgreSQL等 でお話しした内容です。
ryounasso
January 19, 2024
Tweet
Share
More Decks by ryounasso
See All by ryounasso
React inside basics: learn from “build own react"
ryounasso
0
93
抽象データ型について学んだ
ryounasso
0
220
開発効率向上のためのリファクタリングの一歩目の選択肢 ~コード分割~ / JJUG CCC 2024 Fall
ryounasso
0
2.8k
Clean Architecture by TypeScript & NestJS
ryounasso
0
830
テストゼロの個人開発プロジェクトにテストを導入した話
ryounasso
0
400
簡易 DI コンテナを作って DI コンテナを知る
ryounasso
1
1.2k
TypeScript_コンパイラの内側に片足を入れる
ryounasso
3
760
kintone新機能開発のお仕事
ryounasso
2
120
Other Decks in Programming
See All in Programming
なぜ「共通化」を考え、失敗を繰り返すのか
rinchoku
1
640
PostgreSQLのRow Level SecurityをPHPのORMで扱う Eloquent vs Doctrine #phpcon #track2
77web
2
510
Porting a visionOS App to Android XR
akkeylab
0
440
Is Xcode slowly dying out in 2025?
uetyo
1
260
イベントストーミング図からコードへの変換手順 / Procedure for Converting Event Storming Diagrams to Code
nrslib
2
650
Google Agent Development Kit でLINE Botを作ってみた
ymd65536
2
240
AI駆動のマルチエージェントによる業務フロー自動化の設計と実践
h_okkah
0
100
ソフトウェア品質を数字で捉える技術。事業成長を支えるシステム品質の マネジメント
takuya542
1
12k
システム成長を止めない!本番無停止テーブル移行の全貌
sakawe_ee
1
160
Node-RED を(HTTP で)つなげる MCP サーバーを作ってみた
highu
0
120
20250628_非エンジニアがバイブコーディングしてみた
ponponmikankan
0
670
VS Code Update for GitHub Copilot
74th
2
630
Featured
See All Featured
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
How to Think Like a Performance Engineer
csswizardry
24
1.7k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.4k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
730
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Large-scale JavaScript Application Architecture
addyosmani
512
110k
Measuring & Analyzing Core Web Vitals
bluesmoon
7
510
The Illustrated Children's Guide to Kubernetes
chrisshort
48
50k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
35
2.4k
Agile that works and the tools we love
rasmusluckow
329
21k
The Pragmatic Product Professional
lauravandoore
35
6.7k
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