1+ from fastapi import APIRouter , Depends , HTTPException , Path
2+ from uptime_kuma_api import UptimeKumaApi , UptimeKumaException
3+ from typing import List
4+
5+ from schemas .maintenance import Maintenance , MaintenanceUpdate , MonitorMaintenance
6+ from schemas .api import API
7+ from utils .deps import get_current_user
8+ from config import logger as logging
9+
10+
11+
12+ router = APIRouter (redirect_slashes = True )
13+
14+ @router .get ("" , description = "Get all Maintenances" )
15+ async def get_maintenances (cur_user : API = Depends (get_current_user )):
16+ api : UptimeKumaApi = cur_user ['api' ]
17+ try :
18+ return {"maintenances" : api .get_maintenances ()}
19+ except Exception as e :
20+ logging .fatal (e )
21+ raise HTTPException (500 , str (e ))
22+
23+
24+ @router .get ("/{maintenance_id}" , description = "Get Maintenance By ID" )
25+ async def get_maintenance (maintenance_id :int = Path (...) , cur_user : API = Depends (get_current_user )):
26+ api : UptimeKumaApi = cur_user ['api' ]
27+
28+ if maintenance_id :
29+ try :
30+ maintenance = api .get_maintenance (maintenance_id )
31+ except UptimeKumaException as e :
32+ logging .info (e )
33+ raise HTTPException (404 , {"message" : "Maintenance not found !" })
34+ except Exception as e :
35+ logging .fatal (e )
36+ raise HTTPException (500 , str (e ))
37+
38+ return {"maintenance" : maintenance }
39+
40+ raise HTTPException (404 , {"message" : "Maintenance not found !" })
41+
42+
43+
44+ @router .post ("" , description = "Create a Maintenance" )
45+ async def create_maintenance (maintenance : Maintenance ,cur_user : API = Depends (get_current_user )):
46+ api : UptimeKumaApi = cur_user ['api' ]
47+ try :
48+ resp = api .add_maintenance (** maintenance .dict ())
49+ except TypeError as e :
50+ logging .error (e )
51+ raise HTTPException (422 , str (e ))
52+ except Exception as e :
53+ logging .fatal (e )
54+ raise HTTPException (500 , str (e ))
55+
56+ return resp
57+
58+
59+
60+ @router .patch ("/{maintenance_id}" , description = "Update a specific Maintenance" )
61+ async def update_maintenance (maintenance : MaintenanceUpdate ,maintenance_id :int = Path (...) ,cur_user : API = Depends (get_current_user )):
62+ api : UptimeKumaApi = cur_user ['api' ]
63+ try :
64+ resp = api .edit_maintenance (id_ = maintenance_id , ** maintenance .dict (exclude_unset = True ))
65+ except UptimeKumaException as e :
66+ logging .info (e )
67+ raise HTTPException (404 , {"message" : "Maintenance not found !" })
68+ except TypeError as e :
69+ logging .error (e )
70+ raise HTTPException (422 , str (e ) )
71+ except Exception as e :
72+ logging .fatal (e )
73+ raise HTTPException (500 , str (e ))
74+
75+ return {** resp , "maintenance_data" :maintenance .dict (exclude_unset = True )}
76+
77+
78+
79+ @router .delete ("/{maintenance_id}" , description = "Delete a specific Maintenance" )
80+ async def delete_maintenance (maintenance_id :int = Path (...) ,cur_user : API = Depends (get_current_user )):
81+ api : UptimeKumaApi = cur_user ['api' ]
82+ try :
83+ # kinda dumb the api doesnt check if th id exists he just sends an event
84+ resp = api .delete_maintenance (maintenance_id )
85+ except UptimeKumaException as e :
86+ logging .info (e )
87+ raise HTTPException (404 , {"message" : "Maintenance not found !" })
88+ except Exception as e :
89+ logging .fatal (e )
90+ raise HTTPException (500 , str (e ))
91+
92+ return resp
93+
94+
95+
96+ @router .post ("/{maintenance_id}/pause" , description = "Pause a specific maintenance" )
97+ async def pause_maintenance (maintenance_id :int = Path (...) ,cur_user : API = Depends (get_current_user )):
98+ api : UptimeKumaApi = cur_user ['api' ]
99+ try :
100+ resp = api .pause_maintenance (maintenance_id )
101+ except UptimeKumaException as e :
102+ logging .info (e )
103+ raise HTTPException (404 , {"message" : "maintenance not found !" })
104+ except Exception as e :
105+ logging .fatal (e )
106+ raise HTTPException (500 , str (e ))
107+
108+ return resp
109+
110+
111+
112+ @router .post ("/{maintenance_id}/resume" , description = "Resume a specific maintenance" )
113+ async def resume_maintenance (maintenance_id :int = Path (...) ,cur_user : API = Depends (get_current_user )):
114+ api : UptimeKumaApi = cur_user ['api' ]
115+ try :
116+ resp = api .resume_maintenance (maintenance_id )
117+ except UptimeKumaException as e :
118+ logging .info (e )
119+ raise HTTPException (404 , {"message" : "maintenance not found !" })
120+ except Exception as e :
121+ logging .fatal (e )
122+ raise HTTPException (500 , str (e ))
123+
124+ return resp
125+
126+ @router .get ("/{maintenance_id}/monitors" , description = "Get monitors to a maintenance." )
127+ async def add_monitor_maintenance (maintenance_id :int = Path (...),cur_user : API = Depends (get_current_user ))-> List [MonitorMaintenance ]:
128+ api : UptimeKumaApi = cur_user ['api' ]
129+ try :
130+ monitors = api .get_monitor_maintenance (maintenance_id )
131+ except UptimeKumaException as e :
132+ logging .info (e )
133+ raise HTTPException (404 , {"message" : f"maintenance not found ! " })
134+ except Exception as e :
135+ logging .fatal (e )
136+ raise HTTPException (500 , str (e ))
137+
138+ return monitors
139+
140+ @router .post ("/{maintenance_id}/monitors" , description = "Adds monitors to a maintenance." )
141+ async def add_monitor_maintenance (monitors : List [MonitorMaintenance ], maintenance_id :int = Path (...),cur_user : API = Depends (get_current_user )):
142+ api : UptimeKumaApi = cur_user ['api' ]
143+ try :
144+ mns = [m .dict () for m in monitors ]
145+ resp = api .add_monitor_maintenance (maintenance_id , mns )
146+ except UptimeKumaException as e :
147+ logging .info (e )
148+ raise HTTPException (404 , {"message" : f"maintenance or monitor not found !" })
149+ except Exception as e :
150+ logging .fatal (e )
151+ raise HTTPException (500 , str (e ))
152+
153+ return resp
0 commit comments