Базовая реализация UserAPI
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
from fastapi import Depends, FastAPI, HTTPException, Query, Body
|
||||
from contextlib import asynccontextmanager
|
||||
import asyncio
|
||||
from statistics import median
|
||||
|
||||
from db import *
|
||||
from call2api import check_user_token, user_in_db, transfer_coins, get_stats
|
||||
|
||||
|
||||
#------------------------------------------------------------
|
||||
# INIT
|
||||
#------------------------------------------------------------
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
init_engine()
|
||||
await create_db_and_tables()
|
||||
yield
|
||||
|
||||
app = FastAPI(lifespan=lifespan)
|
||||
|
||||
CONFIG = asyncio.run(read())
|
||||
SYSTEM_API_TOKEN = CONFIG['system_api_token']
|
||||
#------------------------- END ------------------------------
|
||||
|
||||
|
||||
#------------------------------------------------------------
|
||||
# APIs (main code)
|
||||
#------------------------------------------------------------
|
||||
async def token_check(username, user_token):
|
||||
return await check_user_token(SYSTEM_API_TOKEN, username, user_token)
|
||||
|
||||
@app.post('/api/register_user_token/')
|
||||
async def register_user_token_api(
|
||||
token: str = Body(),
|
||||
user_token: str = Body(),
|
||||
session: AsyncSession = Depends(get_session)
|
||||
):
|
||||
if token != SYSTEM_API_TOKEN:
|
||||
raise HTTPException(status_code=401, detail='Invalid token')
|
||||
user_token_db = await user_token_in_db_func(session, user_token)
|
||||
if user_token_db:
|
||||
raise HTTPException(status_code=409, detail='Token already exist')
|
||||
new_user_token = UserToken(token=user_token)
|
||||
try:
|
||||
session.add(new_user_token)
|
||||
await session.commit()
|
||||
await session.refresh(new_user_token)
|
||||
return 'OK'
|
||||
except Exception as e:
|
||||
await session.rollback()
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
@app.post('/api/unregister_user_token/')
|
||||
async def unregister_user_token_api(
|
||||
token: str = Body(),
|
||||
user_token: str = Body(),
|
||||
session: AsyncSession = Depends(get_session)
|
||||
):
|
||||
if token != SYSTEM_API_TOKEN:
|
||||
raise HTTPException(status_code=401, detail='Invalid token')
|
||||
user_token_db = await user_token_in_db_func(session, user_token)
|
||||
if not user_token_db:
|
||||
raise HTTPException(status_code=409, detail='Token not exist')
|
||||
try:
|
||||
await session.delete(user_token_db)
|
||||
await session.commit()
|
||||
return 'OK'
|
||||
except Exception as e:
|
||||
await session.rollback()
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
@app.post('/api/get_user_token_info/')
|
||||
async def get_user_token_info_api(
|
||||
username: str | None = Body(None),
|
||||
user_token: str = Body(),
|
||||
session: AsyncSession = Depends(get_session)
|
||||
):
|
||||
if not token_check(username, user_token):
|
||||
raise HTTPException(status_code=401, detail='Invalid username or token')
|
||||
user_token_db = await user_token_in_db_func(session, user_token)
|
||||
if not user_token_db:
|
||||
raise HTTPException(status_code=409, detail='Token not exist')
|
||||
# Force data retrieval (DB can not load it properly, because it "lazy")
|
||||
issue_date = await session.run_sync(lambda sess: user_token_db.issue_date)
|
||||
logs = await session.run_sync(lambda sess: user_token_db.logs)
|
||||
return {'issue_date': issue_date, 'logs': logs}
|
||||
|
||||
@app.post('/api/user_in_db/')
|
||||
async def user_in_db_api(
|
||||
username: str | None = Body(None),
|
||||
user_token: str = Body(),
|
||||
session: AsyncSession = Depends(get_session)
|
||||
):
|
||||
if not token_check(username, user_token):
|
||||
raise HTTPException(status_code=401, detail='Invalid username or token')
|
||||
return await user_in_db(token=SYSTEM_API_TOKEN, username=username)
|
||||
|
||||
@app.post('/api/transfer_coins/')
|
||||
async def transfer_coins_api(
|
||||
username: str = Body(),
|
||||
user_token: str = Body(),
|
||||
dst_username: str = Body(),
|
||||
amount: float = Body(),
|
||||
session: AsyncSession = Depends(get_session)
|
||||
):
|
||||
if not token_check(username, user_token):
|
||||
raise HTTPException(status_code=401, detail='Invalid username or token')
|
||||
return await transfer_coins(token=SYSTEM_API_TOKEN, src_username=username
|
||||
, dst_username=dst_username, amount=amount)
|
||||
|
||||
@app.post('/api/get_stats/')
|
||||
async def get_stats_api(
|
||||
username: str = Body(),
|
||||
user_token: str = Body()
|
||||
):
|
||||
if not token_check(username, user_token):
|
||||
raise HTTPException(status_code=401, detail='Invalid username or token')
|
||||
return await get_stats(token=SYSTEM_API_TOKEN)
|
||||
|
||||
#------------------------- END ------------------------------
|
||||
|
||||
|
||||
#------------------------------------------------------------
|
||||
# START
|
||||
#------------------------------------------------------------
|
||||
if __name__ == '__main__':
|
||||
import uvicorn
|
||||
uvicorn.run(app, host='0.0.0.0', port=8010)
|
||||
#------------------------- END ------------------------------
|
||||
Reference in New Issue
Block a user