1+ from fastapi import FastAPI , Depends , HTTPException , status
2+ from fastapi .middleware .cors import CORSMiddleware
3+ from fastapi .security import HTTPBasic , HTTPBasicCredentials
4+ from fastapi import Body
5+ from decouple import config
6+ from typing import List
7+ import uvicorn
8+
9+
10+
11+
12+ app = FastAPI ()
13+ app .add_middleware (
14+ CORSMiddleware ,
15+ allow_origins = ["*" ],
16+ allow_credentials = True ,
17+ allow_methods = ["*" ],
18+ allow_headers = ["*" ],
19+ )
20+
21+ username = config ("UpdateUserName" )
22+ password = config ("UpdatePassword" )
23+ security = HTTPBasic ()
24+ async def authorize (credentials : HTTPBasicCredentials ):
25+ if credentials .username == username and credentials .password == password :
26+ return True
27+ else :
28+ return False
29+
30+ versions :List [str ] = ["v3.0.0" ]
31+
32+ # For adding updates
33+ @app .post ("/add_update" )
34+ async def add_update (req :str = Body (...), credentials : HTTPBasicCredentials = Depends (security )):
35+ if await authorize (credentials ):
36+ versions .append (req )
37+ return {"Error" :False , "ErrorMessage" :"" }
38+ else :
39+ raise HTTPException (
40+ status_code = status .HTTP_401_UNAUTHORIZED ,
41+ detail = "You are not authorized to add updates" ,
42+ headers = {"WWW-Authenticate" : "Basic" }
43+ )
44+
45+
46+ # For requesting for updates
47+ @app .get ("/check_update/{req}" )
48+ async def check_update (req :str ):
49+ try :
50+ if versions .index (req ) != versions [- 1 ]:
51+ return {"NewUpdate" :True , "UpdateVersion" :versions [- 1 ]}
52+ else :
53+ return {"NewUpdate" :False , "UpdateVersion" :versions [- 1 ]}
54+ except ValueError as e :
55+ print (e )
56+ return {"NewUpdate" :True , "UpdateVersion" :versions [- 1 ]}
57+
58+
59+
60+ if __name__ == "__main__" :
61+ uvicorn .run ("update_API:app" , port = 7676 , host = '0.0.0.0' , reload = True )
0 commit comments