fastApi Flashcards

1
Q

virtual environments

A

py -m venv venv
venv/scripts/activate

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

pip install fastapi
pip install uvicorn

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

correr el servidor

A

reload: recargar la pagina

uvicorn main:app –reload –port 5000

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

open since other desvices

A

uvicorn main:app –reload –port 5000 –host 0.0.0.0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

documentation Swagger

A

http://127.0.0.1:5000/docs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

example basic

A

from fastapi import FastAPI

app = FastAPI()

@app.get(“/”)
def home():
return {“message”: “¡Hola Mundo!”}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

change the name & vesion => of documentation in Swagger

A

app = FastAPI()

app.title = “My First FastAPI App”
app.version = “0.0.1”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

TAGS => in documentation

A

@app.get(“/uno”, tags=[“home”])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

get receive parameters :

localhost/movies/1

@app.get(‘/movies/{id}’, tags=[‘movies’])

A

@app.get(‘/movies/{id}’, tags=[‘movies’])
def get_movie22(id: int):
for movie in movies:
if id == movie[“id”]:
return movie
return

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

get receive parameters

localhost/movies/?category=Acccion&year=2000

A

@app.get(‘/movies/’, tags=[‘movies’])
def get_movies_by_category(category: str, year: int):
return [ item for item in movies if item[‘category’] == category ]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

reply HTTP (replai)
HTMLResponse

A

from fastapi.responses import HTMLResponse

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

SCHEMA (eskima) => fill like a CLASS

from pydantic import BaseModel, Field

class Movie(BaseModel):

@app.post(‘/movies’, tags=[‘movies’])
def create_movie(movie: Movie):
movies.append(movie)
return movies

A

class Movie(BaseModel):
id: Optional[int] = None
title: str = Field(min_length=5, max_length=15)
overview: str = Field(min_length=15, max_length=50)
year: int = Field(le=2022)
rating:float = Field(ge=1, le=10)
category:str = Field(min_length=5, max_length=15)

class Config:
    schema_extra = {
        "example": {
            "id": 1,
            "title": "Mi película",
            "overview": "Descripción de la película",
            "year": 2022,
            "rating": 9.8,
            "category" : "Acción"
        }
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Query

A

from fastapi import FastAPI, Body, Path, Query

@app.get(‘/movies/’, tags=[‘movies’])

def get_movies_by_category(category: str = Query(min_length=5, max_length=15)):

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Path

A

from fastapi import FastAPI, Body, Path, Query

@app.get(‘/movies/{id}’, tags=[‘movies’])

def get_movie(id: int = Path(ge=1, le=2000)):

for item in movies:
    if item["id"] == id:
        return item
return []
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

status code

A

200 => Ok
201 => Created
400 => Bad request (request // solicitud)
401 => No authorized
Not authenticated
authorized (a-to-raizd)

403 => forbidden // token

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

token
the tokens dont change

A

pip install pyjwt

17
Q
A