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..
FastAPI Path Parameters(열거형) - 06
·
Develop/FastAPI
1. Enum 모듈을 삽입 사전에 정해진 범위에 값이 있을 때 사용 from enum import Enum ​ 2. 새로운 클래스를 생성 Enums 상속 str 상속 -> 내부 클래스 변수들은 string 형식이라고 명 class DirectionName(str, Enum): north = "North" south = "South" east = "East" west = "West" 3. Path Parameters(경로 매개변수) url에서 값을 받음 @app.get("/directions/{direction_name}") async def get_direction(direction_name: DirectionName): if direction_name == DirectionName.north: retu..
FastAPI 요청 메서드 로직 - 05
·
Develop/FastAPI
1. CRUD Operations Create - 생성 Read - 호출 Update - 업데이트 Delete - 삭제 2. Use HTTP Request Method GET - 호출 PUT - 업데이트 POST - 생성 DELETE - 삭제 ex) book GET - get all the books PUT - Update book POST - Create new book DELETE - Delete book 3. 딕셔너리 만들어서 GET 호출하기 from fastapi import FastAPI app = FastAPI() BOOKS = { 'book_1': {'title':'Title One','author':'Author One'}, 'book_2': {'title':'Title Two','auth..
FastAPI Swagger / HTTP 데코레이터 - 04
·
Develop/FastAPI
1. FastAPI 실행 uvicorn {파일이름.py}:app --reload ​ 2. FastAPI OpenAPI http://127.0.0.1:8000/openapi.json Swagger UI http://127.0.0.1:8000/docs CURL이란? = Client URL - 클라이언트에서 커맨드 라인이나 소스코드로 손 쉽게 웹 브라우저 처럼 활동할 수 있도록 해주는 기술 -i: 응답 헤더 출력 (옵션 없으면 응답 본문만 출력함) -v: 중간 처리 과정, 오류 메시지, 요청 메시지와 응답 메시지를 헤더와 본문을 포함해 전체 출력 -X: 요청 메소드를 지정 (옵션 없으면 기본값은 GET) -H: 요청 헤더를 지정 -d: 요청 본문을 지정 (옵션 없으면 요청 본문 없음) 출처 - https://..
FastAPI - Swagger, HTTP 요청 관련 - 03
·
Develop/FastAPI
실무에서 FastAPI를 채택 하는 이유(문서 자동화) FastAPI는 OpenAPI를 채택하고 있어, 이를 기반으로 자신의 코드를 읽어 OpenAPI 규격에 맞는 json 파일을 만들어주기 때문에 백엔드 엔지니어가 문서 작업에 할애하는 시간을 줄일 수 있다. 1. OpenAPI란? OpenAPI 또는 OpenAPI Specification(OAS)라고 부름 RESTful API를 기 정의된 규칙에 맞게 API spec을 json이나 yaml로 표현하는 방식 2. OpenAPI Document Defines Schema Data Format Data Type Path Object And much more 2. View OpenAPI Schema FastAPI는 OpenAPI Schema를 생성 애플리케이..