-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
80 lines (55 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
70
71
72
73
74
75
76
77
78
79
80
import os
from dotenv import load_dotenv
from fastapi import FastAPI
from starlette import status
from starlette.responses import Response
import db.database
import scraper
from db.item import Item
from db.record import Record
from infra import sentry
app = FastAPI(
title='Lego Price Tracker API',
description='REST API for tracking Lego prices',
version='1.0.0',
license_info={
'name': 'MIT',
'url': 'https://github.com/Loupeznik/lego-price-tracker/blob/master/LICENSE',
},
)
@app.get("/items", tags=["items"])
async def get_items() -> list[Item]:
data = await db.database.get_items()
return data
@app.post("/items", status_code=status.HTTP_201_CREATED, tags=["items"], responses={201: {"description": "Item created"}})
async def add_item(item: Item):
await db.database.add_item(item)
return Response(status_code=status.HTTP_201_CREATED)
@app.delete("/items/{id}", tags=["items"], status_code=status.HTTP_204_NO_CONTENT,
responses={204: {"description": "Item deleted"}, 404: {"description": "Item not found"}})
async def delete_item(item_id: str):
result = await db.database.delete_item(item_id)
return Response(status_code=status.HTTP_204_NO_CONTENT if result else status.HTTP_404_NOT_FOUND)
@app.get("/records/{set_id}", tags=["records"], status_code=status.HTTP_200_OK, responses={404: {"description": "Set not found"}})
async def get_records_by_set_id(set_id: int = None) -> list[Record] or Response:
data = await db.database.get_records_by_set_id(set_id)
if data is None:
return Response(status_code=status.HTTP_404_NOT_FOUND)
return data
@app.get("/records", tags=["records"])
async def get_records() -> list[Record]:
data = await db.database.get_records()
return data
@app.get("/scrape", status_code=status.HTTP_204_NO_CONTENT, tags=["scrape"])
async def scrape():
driver = scraper.get_driver()
items = await db.database.get_items()
for item in items:
await scraper.scrape(item, driver)
return Response(status_code=status.HTTP_204_NO_CONTENT)
@app.on_event("startup")
async def start():
load_dotenv()
sentry.add_sentry(
bool(os.environ["SENTRY_ENABLED"]), os.environ["SENTRY_DSN"])
await db.database.init()