FastAPI Data Validation(추가) - 16
·
Develop/FastAPI
1. 새로운 Field를 추가 class Book(BaseModel): id: UUID title: str = Field(min_length=1) author: str = Field(min_length=1, max_length=100) #최소길이는 1, 최대길이는 100 description: Optional[str] = Field(title="Description of the book", max_length=100, min_length=1) rating: int = Field(gt=-1, lt=101) #-1보다는 크고 101보다는 작다 (0~100사이) 2. Swagger 확인 Schemas에 추가 된 것을 볼 수 있다. 3. rating의 Field의 유효성 검사 rating에 101과 102를 넣..
FastAPI Fields / Data Validation - 15
·
Develop/FastAPI
1. pydantic 라이브러리의 Field를 import from pydantic import BaseModel, Field 2. BaseModel을 정의한 클래스에 Field를 추가 class Book(BaseModel): id: UUID title: str = Field(min_length=1) #최소길이가 1 author: str description: str rating: int ​ 3. Swagger 확인 최소길이 1인 title에 빈 값을 return해주니 유효성 검사에서 에러발생 4. BaseModel을 정의한 클래스에 Field를 추가 class Book(BaseModel): id: UUID title: str = Field(min_length=1) author: str descripti..
FastAPI Post Request BaseModel - 14
·
Develop/FastAPI
1. BaseModel 생성 from fastapi import FastAPI from pydantic import BaseModel from uuid import UUID app = FastAPI() class Book(BaseModel): id: UUID title: str author: str description: str rating: int BOOKS = [] @app.get("/") async def read_all_books(): return BOOKS 2. Post BaseModel 생성 매개변수 book은 BaseModel이 정의되어있는 Book 클래스를 받음 BOOKS 빈 list에 추가되는 book값을 list에 append 추가되는 book값을 return @app.post("/")..
FastAPI BaseModel 생성 - 13
·
Develop/FastAPI
1. FastAPI import from fastapi import FastAPI app = FastAPI() 2. BOOKS에 빈 List 생성 from fastapi import FastAPI app = FastAPI() BOOKS = [] 3. 전체 리스트 return 하는 API 정의 from fastapi import FastAPI app = FastAPI() BOOKS = [] @app.get("/") async def read_all_books(): return BOOKS 4. Book class 생성(BaseModel을 상속) 스키마 모델로 pydantic을 사용 Books 클래스 내부 필드들이 각각의 Type Hints가 달림 id 필드에 UUID from fastapi import Fa..
FastAPI Path / Query Parameters 정리 - 12
·
Develop/FastAPI
1. Path Parameters url 경로에 들어가는 변수 book_name이 경로 매개변수 @app.delete("/{book_name}") async def delete_book(book_name): del BOOKS[book_name] return f"Book {book_name} deleted." 2. Query Parameters url 구문 제일 뒤에 "/" 붙혀도 되고 안붙혀도 됨 ?(매개변수 값)와 &(추가 값)로 구분 @app.delete("/assignment/") async def delete_book_assignment(book_name: str): del BOOKS[book_name] return BOOKS 3. Path / Query Parameter 종합 @app.get("..
FastAPI Delete Request - 11
·
Develop/FastAPI
1. Delete 데이터 삭제 ​ 2. dictionary 정의 BOOKS = { 'book_1': {'title':'Title One','author':'Author One'}, 'book_2': {'title':'Title Two','author':'Author Two'}, 'book_3': {'title': 'Title Three', 'author': 'Author Three'}, 'book_4': {'title': 'Title Four', 'author': 'Author Four'}, 'book_5': {'title': 'Title Five', 'author': 'Author Five'}, } 3. 함수 정의 @app.delete book_name을 받아서 BOOKS의 키 값으로 제거 @app.d..
FastAPI Put Request - 10
·
Develop/FastAPI
1. PUT 데이터 업데이트 ​ 2. dictionary 정의 BOOKS = { 'book_1': {'title':'Title One','author':'Author One'}, 'book_2': {'title':'Title Two','author':'Author Two'}, 'book_3': {'title': 'Title Three', 'author': 'Author Three'}, 'book_4': {'title': 'Title Four', 'author': 'Author Four'}, 'book_5': {'title': 'Title Five', 'author': 'Author Five'}, } 3. 함수 정의 @app.put("/{book_name}") async def update_book(boo..
FastAPI Post Request - 09
·
Develop/FastAPI
1. POST 데이터 생성 DB 저장 ​ 2. dictionary 정의 BOOKS = { 'book_1': {'title':'Title One','author':'Author One'}, 'book_2': {'title':'Title Two','author':'Author Two'}, 'book_3': {'title': 'Title Three', 'author': 'Author Three'}, 'book_4': {'title': 'Title Four', 'author': 'Author Four'}, 'book_5': {'title': 'Title Five', 'author': 'Author Five'}, } 3. 함수 정의 @app.post("/") async def create_book(book_tit..
FastAPI Query Parameters - 08
·
Develop/FastAPI
1. dictionary 정의 BOOKS = { 'book_1': {'title':'Title One','author':'Author One'}, 'book_2': {'title':'Title Two','author':'Author Two'}, 'book_3': {'title': 'Title Three', 'author': 'Author Three'}, 'book_4': {'title': 'Title Four', 'author': 'Author Four'}, 'book_5': {'title': 'Title Five', 'author': 'Author Five'}, } 2. 함수안에 매개변수 정의 skip_book은 string을 받고 Default 값으로 book_3으로 지정한다. new_books라는 ..
FastAPI Path Parameters(추가) - 07
·
Develop/FastAPI
1. dictionary 정의 BOOKS = { 'book_1': {'title':'Title One','author':'Author One'}, 'book_2': {'title':'Title Two','author':'Author Two'}, 'book_3': {'title': 'Title Three', 'author': 'Author Three'}, 'book_4': {'title': 'Title Four', 'author': 'Author Four'}, 'book_5': {'title': 'Title Five', 'author': 'Author Five'}, } 2. 함수 정의 전체 BOOKS의 list를 볼 수 있음 @app.get("/") async def read_all_books(): ret..