-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.py
67 lines (52 loc) · 2.17 KB
/
api.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from typing import List
from fastapi import APIRouter, HTTPException
from fastapi.responses import HTMLResponse
from schema import Balance, CreateTransaction, CreateUser
from service import *
router = APIRouter()
@router.get("/", response_class=HTMLResponse)
def root_endpoint():
return """
<!DOCTYPE html>
<html>
<head><title>Splitwise API</title></head>
<body style="margin:0; height:100vh; display:flex; flex-direction:column; align-items:center; justify-content:center; text-align:center;">
<div>
<p>
<a href="https://subh.me/">website</a> ·
<a href="https://www.linkedin.com/in/subhrashisdas/">linkedin</a> ·
<a href="https://github.com/subhrashisdas/">github</a>
</p>
<p>© 2024 Subhrashis Das. All rights reserved.</p>
</div>
</body>
</html>
"""
@router.post("/users/", response_model=User)
def create_user_endpoint_endpoint(user: CreateUser):
try:
return create_user(user.email)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
@router.post("/transactions/", response_model=TransactionResponse)
def create_transaction_endpoint(transaction: CreateTransaction):
try:
return create_transaction(transaction)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
@router.put("/transactions/{transaction_id}/", response_model=TransactionResponse)
def update_transaction_endpoint(transaction_id: int, transaction: CreateTransaction):
try:
return update_transaction(transaction_id, transaction)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
@router.get("/users/{user_id}/balance/", response_model=List[Balance])
def get_user_balance_endpoint(user_id: int) -> List[Balance]:
return get_user_balance(user_id)
@router.post("/users/{user_id}/clear/")
def clear_user_balance_endpoint(user_id: int):
try:
clear_user_balance(user_id)
return {}
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))