-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
52 lines (45 loc) · 1.43 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from fastapi import FastAPI, HTTPException
import mysql.connector
from pydantic import BaseModel
import os
import dotenv
dotenv.load_dotenv()
app = FastAPI()
db = mysql.connector.connect(
host=os.environ.get("MYSQL_HOST"),
user=os.environ.get("MYSQL_USER"),
password=os.environ.get("MYSQL_PASSWORD"),
database=os.environ.get("MYSQL_DATABASE")
)
print(db, flush=True)
cursor = db.cursor()
db.commit()
class CallRecord(BaseModel):
username: str
call_duration: int
@app.post('/mobile/{call_record.username}/call')
async def record_call(call_record: CallRecord):
try:
cursor.execute("""
INSERT INTO calls (username, call_duration)
VALUES (%s, %s)
""", (call_record.username, call_record.call_duration))
db.commit()
return {'message': 'Call recorded successfully'}
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
@app.get('/mobile/{username}/billing/')
async def get_billing_info(username: str):
try:
cursor.execute("""
SELECT COUNT(*) as call_count, SUM(CEIL(call_duration / 30)) as block_count
FROM calls
WHERE username = %s
""", (username,))
result = cursor.fetchone()
return {
'call_count': result[0],
'block_count': result[1]
}
except Exception as e:
raise HTTPException(status_code=500, detail="Internal Server Error")