Skip to content

Commit 7a75488

Browse files
committed
Backend: Día 2
1 parent f9d222c commit 7a75488

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed
0 Bytes
Binary file not shown.
Binary file not shown.

Backend/FastAPI/users.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Clase en vídeo (01/12/2022): https://www.twitch.tv/videos/1667582141
2+
3+
### Users API ###
4+
5+
from fastapi import FastAPI
6+
from pydantic import BaseModel
7+
8+
# Inicia el server: uvicorn users:app --reload
9+
10+
app = FastAPI()
11+
12+
13+
class User(BaseModel):
14+
id: int
15+
name: str
16+
surname: str
17+
url: str
18+
age: int
19+
20+
21+
users_list = [User(id=1, name="Brais", surname="Moure", url="https://moure.dev", age=35),
22+
User(id=2, name="Moure", surname="Dev",
23+
url="https://mouredev.com", age=35),
24+
User(id=3, name="Brais", surname="Dahlberg", url="https://haakon.com", age=33)]
25+
26+
27+
@app.get("/usersjson")
28+
async def usersjson(): # Creamos un JSON a mano
29+
return [{"name": "Brais", "surname": "Moure", "url": "https://moure.dev", "age": 35},
30+
{"name": "Moure", "surname": "Dev",
31+
"url": "https://mouredev.com", "age": 35},
32+
{"name": "Haakon", "surname": "Dahlberg", "url": "https://haakon.com", "age": 33}]
33+
34+
35+
@app.get("/users")
36+
async def users():
37+
return users_list
38+
39+
40+
@app.get("/user/{id}") # Path
41+
async def user(id: int):
42+
return search_user(id)
43+
44+
45+
@app.get("/user/") # Query
46+
async def user(id: int):
47+
return search_user(id)
48+
49+
50+
def search_user(id: int):
51+
users = filter(lambda user: user.id == id, users_list)
52+
try:
53+
return list(users)[0]
54+
except:
55+
return {"error": "No se ha encontrado el usuario"}

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
> * Base de datos
2121
> * Despliegue en servidor
2222
>
23-
> **🔴 SIGUIENTE CLASE: Jueves 1 de Diciembre a las 20:00 (hora España)**
23+
> **🔴 SIGUIENTE CLASE: Jueves 8 de Diciembre a las 20:00 (hora España)**
2424
25-
> 🗓 En [Discord](https://discord.gg/mouredev) tienes creado un [evento](https://discord.gg/mouredev?event=1045647021337497600) para que consultes la hora de tu país y añadas un recordatorio.
25+
> 🗓 En [Discord](https://discord.gg/mouredev) tienes creado un [evento](https://discord.gg/mouredev?event=1048225560637214790) para que consultes la hora de tu país y añadas un recordatorio.
2626
>
2727
> Mientras, aprovecha para practicar unos [retos de programación](https://retosdeprogramacion.com/semanales2022) y así ir mejorando poco a poco.
2828
>
@@ -39,6 +39,7 @@ Curso en el que aprenderemos a utilizar Python para backend e implementaremos un
3939
> Código: Directorio "Backend" en el proyecto
4040
4141
* [Clase 1 - 24/11/2022 - Hola Mundo en FastAPI](https://www.twitch.tv/videos/1661716599)
42+
* [Clase 2 - 01/12/2022 - Operaciones con GET y peticiones HTTP](https://www.twitch.tv/videos/1667582141)
4243

4344
### Curso de fundamentos desde cero
4445

0 commit comments

Comments
 (0)