-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
69 lines (57 loc) · 2.23 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import os
import aioredis
from dotenv import load_dotenv
from fastapi import FastAPI
from fastapi import status
from fastapi_cache import FastAPICache
from fastapi_cache.backends.redis import RedisBackend
from fastapi_cache.decorator import cache
from starlette.responses import Response
from urllib.parse import urlparse
from webscraping.get_data import get_cardapio, prepare_data, get_cardapio_dia
load_dotenv() # take environment variables from .env.
app = FastAPI(
title='Api R.U UFFS',
version='0.0.1',
contact={
"name": "Diogo Baltazar do Nascimento",
"url": "https://github.com/mascDriver",
"email": "[email protected]",
},
redoc_url='/'
)
@app.on_event("startup")
async def startup():
url = urlparse(os.environ.get("REDIS_TLS_URL"))
redis = aioredis.StrictRedis(host=url.hostname, port=url.port, username=url.username, password=url.password)
FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
# @app.get("/")
# @cache(expire=86400)
# async def home():
# return dict(message=f"Bem vindo a api ru uffs, acesse {app.docs_url} para mais informações")
@app.get("/campus/{campus}")
@cache(expire=86400)
async def ver_cardapio_campus(campus: str, response: Response):
if campus == 'erechim':
response.status_code = status.HTTP_302_FOUND
return dict(message=f"Campus {campus} está em desenvolvimento.")
bs = get_cardapio(campus)
if not bs:
response.status_code = status.HTTP_404_NOT_FOUND
return dict(message=f"Campus {campus} não encontrado.")
cardapio = prepare_data(bs)
response.status_code = status.HTTP_200_OK
return {'cardapios': cardapio}
@app.get("/campus/{campus}/dia/{dia}")
@cache(expire=86400)
async def ver_cardapio_campus_dia(campus: str, dia: int, response: Response):
bs = get_cardapio(campus)
if not bs:
response.status_code = status.HTTP_404_NOT_FOUND
return dict(message=f"Campus '{campus}' não encontrado.")
cardapios = prepare_data(bs)
if dia not in range(0, 5):
response.status_code = status.HTTP_400_BAD_REQUEST
return dict(message=f"Informe um dia entre 0 - 4.")
cardapio = get_cardapio_dia(dia, cardapios)
return dict(cardapios=cardapio)