FastAPI Delete Request - 22
·
Develop/FastAPI
1. DELETE 데이터 삭제 2. 함수 생성 @app.delete("/{book_id}") async def delete_book(book_id: UUID): counter = 0 for BOOK in BOOKS: counter += 1 if BOOK.id == book_id: del BOOKS[counter - 1] return f'ID:{book_id} deleted' 경로 매개변수 book_id로 UUID를 받음 counter라는 변수에 0으로 초기화 BOOKS list를 for문을 돌면서 dict를 하나씩 꺼내서 확인 dict의 id키의 value 값과 book_id로 받은 UUID의 값의 일치 여부를 확인 일치하다면 BOOKS의 인덱스로 접근해 삭제한 후 삭제 멘트 return 3. Swagg..
FastAPI Put Request - 21
·
Develop/FastAPI
1. PUT 데이터 업데이트 2. 함수 생성 @app.put("/{book_id}") async def update_book(book_id: UUID, book: Book): counter = 0 for BOOK in BOOKS: counter += 1 if BOOK.id == book_id: BOOKS[counter - 1] = book return BOOKS[counter - 1] book_id를 경로 매개변수로 UUID로 받고 book을 Book 클래스로 받음.(필수적 매개변수 같이 보이지만 Book 클래스가 정의 되어있음으로 아님) counter라는 변수에 0을 넣으며 초기화 BOOKS list를 for문을 돌며 BOOK dict를 한개씩 꺼내서 확인 for문이 한번 돌 때마다 counter에 1..
FastApi Get By UUID - 20
·
Develop/FastAPI
1. 새로운 API 생성 book_id로 UUID를 받는다. BOOKS list을 돌면서 한개의 dict를 뺀다. dict의 id에 접근해서 value인 UUID를 값을 url로 들어온 book_id와 같은지 확인한다. 조건에 부합한다면 해당 dict의 값을 return 한다. @app.get("/book/{book_id}") async def read_book(book_id:UUID): for x in BOOKS: if x.id == book_id: return x 2. Swagger 확인 ex) db6d5c1f-0460-4bab-8aa1-a801bf843273 id로 확인한다. 딕셔너리의 id에 접근해서 value가 같은지 확인한다. book_1 = Book(id="db6d5c1f-0460-4bab-..
FastAPI Get Request - 19
·
Develop/FastAPI
1. 함수 생성(쿼리 매개변수) 선택적 매개변수로(int 형을 받음) 매개변수에 값을 넣지 않으면 default로 None을 반환 @app.get("/") async def read_all_books(books_to_return: Optional[int] = None): if len(BOOKS) > new_books[0] >> book_1 return ex) books_to_return = 2 >> new_books[0] and new_books[1] >> book_1 and book_2 return @..
FastAPI BaseModel 구성 - 18
·
Develop/FastAPI
1. BaseModel의 구성 정의 Book 클래스에 값을 넣을 때 구성이 어떻게 되는지, 어떤 값들이 들어가는지 예시를 보여줄 수 있다. class Book(BaseModel): id: UUID title: str = Field(min_length=1) author: str = Field(min_length=1, max_length=100) description: Optional[str] = Field(title="Description of the book", max_length=100, min_length=1) rating: int = Field(gt=-1, lt=101) #Config가 Book 클래스의 Example Values class Config: schema_extra = { "exampl..
FastAPI API 없이 값 추가하기 - 17
·
Develop/FastAPI
1. 비동기 없이 함수 생성 4개의 책에 Book클래스를 정의 def create_books_no_api(): book_1 = Book(id="637d1b93-0174-48e7-8959-e17530b6c690", title="Title 1", author="Author 1", description="Description 1", rating=60) book_2 = Book(id="637d1b93-0174-48e7-8959-e17530b6c690", title="Title 2", author="Author 2", description="Description 2", rating=60) book_3 = Book(id="637d1b93-0174-48e7-8959-e17530b6c690", title="Title ..
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..