-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
63 lines (53 loc) · 2.95 KB
/
server.py
File metadata and controls
63 lines (53 loc) · 2.95 KB
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
from fastapi import FastAPI, Request, Response, Body
from fastapi.responses import RedirectResponse
from fastapi.middleware.cors import CORSMiddleware
from wrapper import Product, ProductReview, Shop
app = FastAPI(title='shopee-wrapper', openapi_url=f'/shopee-wrapper/openapi.json')
app.add_middleware(
CORSMiddleware,
allow_origins=['*'],
allow_credentials=True,
allow_methods=['*'],
allow_headers=['*'],
)
@app.get("/")
def read_root():
return RedirectResponse(url='/docs', status_code=302)
@app.get("/shops/")
async def get_shop(request: Request, response: Response, payload: dict = Body(default={})):
url = payload['url'] if 'url' in payload else request.query_params.get('url')
username = payload['username'] if 'username' in payload else request.query_params.get('username')
shop = Shop(link=url, username=username)
response.status_code = shop.status_code
response.body = shop.serialize if not shop.error else {'error': shop.error}
return response.body
@app.get("/shops/products/")
async def get_products(request: Request, response: Response, payload: dict = Body(default={})):
url = payload['url'] if 'url' in payload else request.query_params.get('url')
username = payload['username'] if 'username' in payload else request.query_params.get('username')
shop = Shop(link=url, username=username)
response.status_code = shop.status_code
response.body = {'products': len(shop.products)} if not shop.error else {'error': shop.error}
return response.body
@app.get("/products/")
async def get_product(request: Request, response: Response, payload: dict = Body(default={})):
url = payload['url'] if 'url' in payload else request.query_params.get('url')
shop_id = payload['shop_id'] if 'shop_id' in payload else request.query_params.get('shop_id')
item_id = payload['item_id'] if 'item_id' in payload else request.query_params.get('item_id')
product = Product(link=url, shop_id=shop_id, item_id=item_id)
response.status_code = product.status_code
response.body = product.serialize if not product.error else {'error': product.error}
return response.body
@app.get("/products/reviews/")
async def get_review(request: Request, response: Response, payload: dict = Body(default={})):
url = payload['url'] if 'url' in payload else request.query_params.get('url')
shop_id = payload['shop_id'] if 'shop_id' in payload else request.query_params.get('shop_id')
item_id = payload['item_id'] if 'item_id' in payload else request.query_params.get('item_id')
filter = request.query_params.get('filter') or 0
limit = request.query_params.get('limit') or 5
offset = request.query_params.get('offset') or 0
product = Product(link=url, shop_id=shop_id, item_id=item_id, filter=filter, limit=limit, offset=offset)
review = product.reviews
response.status_code = review.status_code
response.body = review.serialize if not review.error else {'error': review.error}
return response.body