Skip to content

Commit 2ec4c71

Browse files
committed
wip2
1 parent 37cebe3 commit 2ec4c71

File tree

4 files changed

+47
-39
lines changed

4 files changed

+47
-39
lines changed

app/routers/statuspage.py

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from config import logger as logging
44
from schemas.api import API
55
from utils.deps import get_current_user
6-
from schemas.statuspage import StatusPageList, StatusPage, StatusPageList, AddStatusPageResponse, SaveStatusPageRequest
6+
from schemas.statuspage import StatusPageList, StatusPage, AddStatusPageResponse, AddStatusPageRequest,SaveStatusPageRequest,SaveStatusPageResponse, DeleteStatusPageResponse
77

88
router = APIRouter(redirect_slashes=True)
99

@@ -30,17 +30,12 @@ async def get_status_page(slug: str, cur_user: API = Depends(get_current_user)):
3030
logging.fatal(e)
3131
raise HTTPException(500, str(e))
3232

33-
from fastapi import Query
3433

3534
@router.post("", response_model=AddStatusPageResponse, description="Add a status page")
36-
async def add_status_page(
37-
slug: str = Query(...),
38-
title: str = Query(...),
39-
cur_user: API = Depends(get_current_user),
40-
):
35+
async def add_status_page(status_page_data: AddStatusPageRequest, cur_user: API = Depends(get_current_user)):
4136
api: UptimeKumaApi = cur_user['api']
4237
try:
43-
return api.add_status_page(slug, title)
38+
return api.add_status_page(status_page_data.slug, status_page_data.title)
4439
except UptimeKumaException as e:
4540
logging.error(e)
4641
raise HTTPException(400, str(e))
@@ -49,18 +44,15 @@ async def add_status_page(
4944
raise HTTPException(500, str(e))
5045

5146

52-
53-
@router.put("/{slug}", description="Save a status page")
47+
@router.post("/{slug}",response_model=SaveStatusPageResponse, description="Save a status page")
5448
async def save_status_page(
5549
slug: str = Path(...),
5650
status_page_data: SaveStatusPageRequest = Body(...),
5751
cur_user: API = Depends(get_current_user),
58-
):
59-
print("--------DEBUG----------")
60-
print(slug)
61-
print("--------DEBUG----------")
52+
):
6253
api: UptimeKumaApi = cur_user['api']
6354
try:
55+
print (status_page_data.id)
6456
return api.save_status_page(
6557
slug,
6658
id=status_page_data.id,
@@ -84,18 +76,19 @@ async def save_status_page(
8476
logging.fatal(e)
8577
raise HTTPException(500, str(e))
8678

87-
@router.delete("/{slug}", description="Delete a status page")
88-
async def delete_status_page(slug:str=Path(...), cur_user: API = Depends(get_current_user)):
79+
@router.delete("/{slug}", response_model=DeleteStatusPageResponse, description="Delete a status page")
80+
async def delete_status_page(slug: str = Path(...), cur_user: API = Depends(get_current_user)):
8981
api: UptimeKumaApi = cur_user['api']
90-
print("--------DEBUG----------")
91-
print(slug)
92-
print("--------DEBUG----------")
9382
try:
94-
api.delete_status_page(slug)
95-
return {"detail": f"Status page '{slug}' successfully deleted."}
83+
return api.delete_status_page(slug)
84+
#Catch type error...which is actually a success, go figgure. {"detail":"'NoneType' object has no attribute 'values'"}
85+
except TypeError as e:
86+
if "NoneType" in str(e):
87+
logging.info("Status page deleted successfully")
88+
return {"status": "success"}
9689
except UptimeKumaException as e:
9790
logging.error(e)
9891
raise HTTPException(404, str(e))
9992
except Exception as e:
10093
logging.fatal(e)
101-
raise HTTPException(500, str(e))
94+
raise HTTPException(500, str(e))

app/schemas/statuspage.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import List, Optional
2-
from pydantic import BaseModel
2+
from pydantic import BaseModel, Field, HttpUrl, constr
33

44
class Incident(BaseModel):
55
content: str
@@ -23,9 +23,9 @@ class PublicGroup(BaseModel):
2323
weight: int
2424

2525
class StatusPage(BaseModel):
26-
customCSS: Optional[str] = None # Make customCSS field optional
26+
customCSS: Optional[str] = None
2727
description: Optional[str]
28-
domainNameList: List
28+
domainNameList: List[HttpUrl]
2929
footerText: Optional[str]
3030
googleAnalyticsId: Optional[str]
3131
icon: str
@@ -35,29 +35,39 @@ class StatusPage(BaseModel):
3535
published: bool
3636
showPoweredBy: bool
3737
showTags: bool
38-
slug: str
38+
slug: constr(min_length=1)
3939
theme: str
4040
title: str
4141
publicGroupList: Optional[List[PublicGroup]]
4242

4343
class StatusPageList(BaseModel):
4444
statuspages: List[StatusPage]
4545

46+
class AddStatusPageRequest(BaseModel):
47+
slug: Optional[str] = None
48+
title: Optional[str] = None
49+
msg: Optional[str] = None
4650

4751
class AddStatusPageResponse(BaseModel):
48-
msg: str
49-
52+
msg: Optional[str] = None
5053
class SaveStatusPageRequest(BaseModel):
5154
id: int
5255
title: str
56+
slug: constr(min_length=1)
5357
description: Optional[str] = None
5458
theme: Optional[str] = "light"
5559
published: Optional[bool] = True
5660
showTags: Optional[bool] = False
57-
domainNameList: Optional[List[str]] = None
61+
domainNameList: Optional[List[HttpUrl]] = None
5862
googleAnalyticsId: Optional[str] = None
5963
customCSS: Optional[str] = ""
6064
footerText: Optional[str] = None
6165
showPoweredBy: Optional[bool] = True
6266
icon: Optional[str] = "/icon.svg"
6367
publicGroupList: Optional[List] = None
68+
69+
class SaveStatusPageResponse(BaseModel):
70+
detail: str
71+
72+
class DeleteStatusPageResponse(BaseModel):
73+
detail: str = "Status page deleted"

db/test.sqlite3

0 Bytes
Binary file not shown.

tests/statuspage.sh

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,32 @@ echo -e "\nToken: ${TOKEN}"
99

1010
echo -e "\nGet all status pages:"
1111
curl -L -H 'Accept: application/json' -H "Authorization: Bearer ${TOKEN}" http://127.0.0.1:8000/statuspages
12-
echo -e "\nGet a specific status page:"
13-
curl -L -H 'Accept: application/json' -H "Authorization: Bearer ${TOKEN}" http://127.0.0.1:8000/statuspages/hsl-dev
1412

1513
echo -e "\nAdd a status page:"
1614
curl -X 'POST' \
1715
-H "Authorization: Bearer ${TOKEN}" \
18-
'http://127.0.0.1:8000/statuspages?slug=slugToDelete&title=slugToDelete'
16+
-H 'Content-Type: application/json' \
17+
-d '{"title": "New Page", "slug": "new-page", "msg": "Initial message"}' \
18+
'http://127.0.0.1:8000/statuspages'
19+
20+
echo -e "\nGet a specific status page:"
21+
curl -L -H 'Accept: application/json' -H "Authorization: Bearer ${TOKEN}" http://127.0.0.1:8000/statuspages/new-page
22+
STATUS_PAGE_ID=$(curl -L -H 'Accept: application/json' -H "Authorization: Bearer ${TOKEN}" http://127.0.0.1:8000/statuspages/new-page | jq -r ".id")
23+
echo -e "\nStatus Page ID: ${STATUS_PAGE_ID}"
1924

2025
echo -e "\nSave a status page:"
21-
curl -X 'PUT' \
26+
curl -X 'POST' \
2227
-H "Authorization: Bearer ${TOKEN}" \
2328
-H 'Content-Type: application/json' \
24-
--data-raw '{
25-
"id": 2,
29+
-d '{
30+
"id": '${STATUS_PAGE_ID}',
31+
"slug": "new-page",
2632
"title": "Updated Title",
2733
"description": "Updated Description",
2834
"theme": "dark",
2935
"published": true,
3036
"showTags": true,
31-
"domainNameList": ["example1.com", "example2.com"],
37+
"domainNameList": ["https://example1.com", "https://example2.com"],
3238
"googleAnalyticsId": "UA-123456789-1",
3339
"customCSS": "body { background-color: red; }",
3440
"footerText": "Custom Footer Text",
@@ -49,10 +55,9 @@ curl -X 'PUT' \
4955
}
5056
]
5157
}' \
52-
'http://127.0.0.1:8000/statuspages/slugToDelete'
58+
'http://127.0.0.1:8000/statuspages/new-page'
5359

5460
echo -e "\nDelete a status page:"
5561
curl -X 'DELETE' \
5662
-H "Authorization: Bearer ${TOKEN}" \
57-
'http://127.0.0.1:8000/statuspages/slugToDelete'
58-
63+
'http://127.0.0.1:8000/statuspages/new-page'

0 commit comments

Comments
 (0)