|
| 1 | +import json |
| 2 | +import uvicorn as uvicorn |
| 3 | + |
| 4 | +from fastapi import Depends, FastAPI, HTTPException |
| 5 | +from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm |
| 6 | +from dataclasses import dataclass |
| 7 | +from quali_api import QualiAPIHandler |
| 8 | + |
| 9 | +app = FastAPI() |
| 10 | + |
| 11 | +oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token") |
| 12 | + |
| 13 | + |
| 14 | +@dataclass |
| 15 | +class CloudshellConfig: |
| 16 | + server: str |
| 17 | + domain: str |
| 18 | + port: str |
| 19 | + |
| 20 | + |
| 21 | +def _get_config(): |
| 22 | + with open("config.json") as f: |
| 23 | + data = json.load(f) |
| 24 | + return CloudshellConfig(server=data["CS_SERVER"], |
| 25 | + domain=data["CS_DOMAIN"], |
| 26 | + port=data["QUALI_API_PORT"]) |
| 27 | + |
| 28 | + |
| 29 | +config = _get_config() |
| 30 | +quali_api = QualiAPIHandler(host=config.server, port=config.port) |
| 31 | + |
| 32 | + |
| 33 | +@app.post("/token") |
| 34 | +async def login(form_data: OAuth2PasswordRequestForm = Depends()): |
| 35 | + user_name = form_data.username |
| 36 | + password = form_data.password |
| 37 | + if not user_name and password: |
| 38 | + raise HTTPException(status_code=400, |
| 39 | + detail="Must provide user and password") |
| 40 | + response = quali_api.login(user_name=user_name, password=password, domain=config.domain) |
| 41 | + if not response.ok: |
| 42 | + raise HTTPException(status_code=response.status_code, |
| 43 | + detail=response.reason) |
| 44 | + token = quali_api.get_token_from_login(response) |
| 45 | + return {"access_token": token, "token_type": "bearer"} |
| 46 | + |
| 47 | + |
| 48 | +@app.get("/suites") |
| 49 | +async def read_users_me(token: str = Depends(oauth2_scheme)): |
| 50 | + return quali_api.get_available_suites(token) |
| 51 | + |
| 52 | + |
| 53 | +if __name__ == "__main__": |
| 54 | + uvicorn.run(app, host="0.0.0.0", port=7000) |
0 commit comments