Yazılım Geliştirme
1 dakika okuma
Python FastAPI ile REST API
FastAPI ile type-safe, yuksek performansli REST API gelistirme.
E
Emre Cihan Kurulum
pip install fastapi uvicorn[standard] pydantic
Temel Uygulama
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
app = FastAPI(title='Ornek API')
class Kullanici(BaseModel):
id: int
email: str
ad: str
aktif: bool = True
db: dict[int, Kullanici] = {}
@app.get('/kullanicilar', response_model=list[Kullanici])
async def liste():
return list(db.values())
@app.get('/kullanicilar/{id}', response_model=Kullanici)
async def getir(id: int):
if id not in db:
raise HTTPException(404, 'Bulunamadi')
return db[id]
@app.post('/kullanicilar', status_code=201)
async def olustur(k: Kullanici):
db[k.id] = k
return k
@app.delete('/kullanicilar/{id}', status_code=204)
async def sil(id: int):
if id not in db: raise HTTPException(404)
del db[id]
Calistirma
uvicorn main:app --reload
# Docs: http://localhost:8000/docs
FastAPI otomatik OpenAPI dokumantasyonu sunar.
Paylaş:
Twitter / X
E
Emre Cihan
Sistem Yöneticisi · Maker · Elektronik Meraklısı
İlgili yazılar