File size: 1,574 Bytes
409e584
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86c49fc
409e584
 
 
 
 
 
 
 
 
 
5ff89f7
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
from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel

from price_analysis import market_price_estimation

app = FastAPI(
    title="Market Prices Estimation API",
    description="API for estimating market prices based on analysis.",
    version="1.0",
    docs_url="/docs",  # Customize this URL to match your requirements
    redoc_url="/redoc",  # Optional: ReDoc documentation endpoint
)

# Add CORS middleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


class MarketEstimation(BaseModel):
    product_name: str
    cost_price: int
    user_price: int


@app.get("/")
async def root():
    return {
        "message": "Welcome to the Market Prices Estimation API!",
        "version": "1.0",
        "endpoints": {
            "/": "This welcome message",
            "/market-prices-estimation/": "POST endpoint for price analysis"
        }
    }


@app.post("/market-prices-estimation")
async def market_prices_estimation_endpoint(request: MarketEstimation):
    try:
        response = market_price_estimation(request.product_name, request.cost_price, request.user_price)

        if not isinstance(response, dict):
            raise ValueError("market_price_estimation must return a dictionary")

        return JSONResponse(status_code=200, content=response)

    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))