test api
This commit is contained in:
17
api/route/default.py
Normal file
17
api/route/default.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from fastapi import APIRouter
|
||||
import toml
|
||||
from pathlib import Path
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/")
|
||||
async def root():
|
||||
version = "dev"
|
||||
pyproject_path = Path(__file__).parent.parent.parent / "pyproject.toml"
|
||||
if pyproject_path.exists():
|
||||
try:
|
||||
data = toml.load(pyproject_path)
|
||||
version = data.get("project", {}).get("version", "dev")
|
||||
except Exception:
|
||||
pass
|
||||
return {"status": "ok", "msg": "VPN Bot API", "version": version}
|
||||
39
api/route/subscription.py
Normal file
39
api/route/subscription.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional
|
||||
from api.db.subscription import get_subscription, set_subscription, extend_subscription
|
||||
import uuid
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
router = APIRouter(prefix="/subscription", tags=["subscription"])
|
||||
|
||||
class SubscriptionInfo(BaseModel):
|
||||
user_id: int
|
||||
uuid: str
|
||||
until: datetime
|
||||
|
||||
class SubscriptionCreate(BaseModel):
|
||||
user_id: int
|
||||
months: int
|
||||
|
||||
@router.get("/{user_id}", response_model=Optional[SubscriptionInfo])
|
||||
async def get_user_subscription(user_id: int):
|
||||
sub = await get_subscription(user_id)
|
||||
if not sub:
|
||||
return None
|
||||
return SubscriptionInfo(user_id=user_id, uuid=sub["uuid"], until=sub["until"])
|
||||
|
||||
@router.post("/new", response_model=SubscriptionInfo)
|
||||
async def create_subscription(data: SubscriptionCreate):
|
||||
sub_id = str(uuid.uuid4())
|
||||
until = datetime.now() + timedelta(days=30*data.months)
|
||||
await set_subscription(data.user_id, sub_id, until)
|
||||
return SubscriptionInfo(user_id=data.user_id, uuid=sub_id, until=until)
|
||||
|
||||
@router.post("/extend", response_model=SubscriptionInfo)
|
||||
async def extend_user_subscription(data: SubscriptionCreate):
|
||||
new_until = await extend_subscription(data.user_id, data.months)
|
||||
if not new_until:
|
||||
raise HTTPException(status_code=404, detail="Subscription not found")
|
||||
sub = await get_subscription(data.user_id)
|
||||
return SubscriptionInfo(user_id=data.user_id, uuid=sub["uuid"], until=new_until)
|
||||
Reference in New Issue
Block a user