-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmain.py
58 lines (42 loc) · 1.38 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
from typing import List
from fastapi import FastAPI
from cassandra.cqlengine.management import sync_table
from . import (
db,
config,
crud,
models,
schema,
)
settings = config.get_settings()
Product = models.Product
ProductScrapeEvent = models.ProductScrapeEvent
app = FastAPI()
session = None
@app.on_event("startup")
def on_startup():
global session
session = db.get_session()
sync_table(Product)
sync_table(ProductScrapeEvent)
@app.get("/")
def read_index():
return {"hello": "world", "name": settings.name}
@app.get("/products", response_model=List[schema.ProductListSchema])
def products_list_view():
return list(Product.objects.all())
@app.post("/events/scrape")
def events_scrape_create_view(data: schema.ProductListSchema):
product, _ = crud.add_scrape_event(data.dict())
return product
@app.get("/products/{asin}",)
def products_detail_view(asin):
data = dict(Product.objects.get(asin=asin))
events = list(ProductScrapeEvent.objects().filter(asin=asin).limit(5))
events = [schema.ProductScrapeEventDetailSchema(**x) for x in events]
data['events'] = events
data['events_url'] = f"/products/{asin}/events"
return data
@app.get("/products/{asin}/events", response_model=List[schema.ProductScrapeEventDetailSchema])
def products_scrapes_list_view(asin):
return list(ProductScrapeEvent.objects().filter(asin=asin))