eshan6704 commited on
Commit
8c6ae93
·
verified ·
1 Parent(s): 74b0969

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -0
app.py CHANGED
@@ -12,6 +12,100 @@ import bhavcopy_html
12
  import nsepython
13
  import yahooinfo
14
  import build_nse_fno
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
 
17
  # ======================================================
 
12
  import nsepython
13
  import yahooinfo
14
  import build_nse_fno
15
+ from fastapi import FastAPI, HTTPException
16
+ from fastapi.middleware.cors import CORSMiddleware
17
+ from pydantic import BaseModel
18
+ from typing import Optional, Callable, Dict
19
+
20
+ app = FastAPI(title="Stock Backend")
21
+
22
+ # ---------- CORS ----------
23
+ app.add_middleware(
24
+ CORSMiddleware,
25
+ allow_origins=["*"],
26
+ allow_methods=["*"],
27
+ allow_headers=["*"],
28
+ )
29
+
30
+ # ---------- REQUEST MODEL (ONLY 4 FIELDS) ----------
31
+ class FetchRequest(BaseModel):
32
+ req_type: str # stock_info | stock_intraday | stock_daily | ...
33
+ name: str
34
+ date_end: str # dd-mm-yyyy (ALWAYS REQUIRED)
35
+ date_start: Optional[str] = None # dd-mm-yyyy (optional)
36
+
37
+ # ---------- HEALTH ----------
38
+ @app.get("/")
39
+ def health():
40
+ return {"status": "ok", "service": "backend alive"}
41
+
42
+ # ---------- HANDLERS ----------
43
+ def stock_info(req: FetchRequest):
44
+ return {
45
+ "success": True,
46
+ "meta": {
47
+ "req_type": req.req_type,
48
+ "name": req.name,
49
+ "as_on": req.date_end
50
+ },
51
+ "data": {
52
+ "sector": "FMCG",
53
+ "market_cap": "5.2L Cr",
54
+ "pe": 28.4
55
+ }
56
+ }
57
+
58
+ def stock_intraday(req: FetchRequest):
59
+ return {
60
+ "success": True,
61
+ "meta": {
62
+ "req_type": req.req_type,
63
+ "name": req.name,
64
+ "as_on": req.date_end,
65
+ "interval": "5min"
66
+ },
67
+ "data": [
68
+ {"time": "09:15", "close": 412.5, "volume": 120345},
69
+ {"time": "09:20", "close": 413.8, "volume": 98234},
70
+ {"time": "09:25", "close": 413.5, "volume": 75621}
71
+ ]
72
+ }
73
+
74
+ def stock_daily(req: FetchRequest):
75
+ return {
76
+ "success": True,
77
+ "meta": {
78
+ "req_type": req.req_type,
79
+ "name": req.name,
80
+ "to": req.date_end
81
+ },
82
+ "data": [
83
+ {"date": "23-12-2025", "close": 410.2},
84
+ {"date": "24-12-2025", "close": 412.9},
85
+ {"date": "25-12-2025", "close": 413.5}
86
+ ]
87
+ }
88
+
89
+ # ---------- ROUTER ----------
90
+ ROUTER: Dict[str, Callable[[FetchRequest], dict]] = {
91
+ "stock_info": stock_info,
92
+ "stock_intraday": stock_intraday,
93
+ "stock_daily": stock_daily,
94
+ }
95
+
96
+ # ---------- MAIN ENDPOINT ----------
97
+ @app.post("/api/fetch")
98
+ def fetch_data(req: FetchRequest):
99
+
100
+ handler = ROUTER.get(req.req_type)
101
+
102
+ if not handler:
103
+ raise HTTPException(
104
+ status_code=400,
105
+ detail=f"Unsupported req_type: {req.req_type}"
106
+ )
107
+
108
+ return handler(req)
109
 
110
 
111
  # ======================================================