-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_server.py
More file actions
executable file
·93 lines (80 loc) · 3.14 KB
/
api_server.py
File metadata and controls
executable file
·93 lines (80 loc) · 3.14 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3
# coding: utf-8
import textwrap
from typing import Annotated
from fastapi import FastAPI, Depends, HTTPException, Query
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from starlette.status import HTTP_401_UNAUTHORIZED, HTTP_500_INTERNAL_SERVER_ERROR
from logic import HeatNeededIndicator, Event
import caldav
import datetime
import os
import json
import dotenv
dotenv.load_dotenv()
app = FastAPI()
security = HTTPBasic(realm=os.getenv("api_realm"))
users_db = json.loads(os.getenv("api_users"))
indicator = HeatNeededIndicator(
preheat_minutes=0, # Placeholder, will be set dynamically
cooloff_minutes=0, # Placeholder, will be set dynamically
no_heat_tag=os.getenv("no_heat_tag")
)
wrapper = textwrap.TextWrapper(initial_indent=' ' * 4, width=80, subsequent_indent=' ' * 8)
indicator.set_wrapper(wrapper)
def create_caldav_client():
return caldav.DAVClient(
url=os.getenv("caldav_url"),
username=os.getenv("caldav_user"),
password=os.getenv("caldav_password"),
timeout=float(os.getenv("caldav_timeout", 0)) or None
)
client = create_caldav_client()
principal = client.principal()
calendar_id = os.getenv("calendar_id")
calendar = principal.calendar(cal_id=calendar_id)
def authenticate_user(username: str, password: str):
if username in users_db and users_db[username] == password:
return True
return False
def get_calendar_events(now: datetime.datetime):
global client, principal, calendar
try:
return indicator.get_next_events(calendar, now.astimezone())
except caldav.error.CaldavError:
# Reset client on timeout or other CalDAV errors
client = create_caldav_client()
principal = client.principal()
calendar = principal.calendar(cal_id=calendar_id)
raise HTTPException(
status_code=HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to retrieve events from CalDAV server. Please try again later."
)
@app.get("/next-events")
def read_next_events(
credentials: Annotated[HTTPBasicCredentials, Depends(security)],
preheat_minutes: Annotated[int, Query(..., ge=0, description="Preheat duration in minutes")],
cooloff_minutes: Annotated[int, Query(..., ge=0, description="Cooloff duration in minutes")],
now: datetime.datetime = Query(default_factory=datetime.datetime.now, description="Current date-time")
) -> list[Event]:
username = credentials.username
password = credentials.password
if not authenticate_user(username, password):
raise HTTPException(
status_code=HTTP_401_UNAUTHORIZED,
detail="Invalid authentication credentials",
headers={"WWW-Authenticate": f'Basic realm="{security.realm}"'},
)
indicator.preheat_minutes = preheat_minutes
indicator.cooloff_minutes = cooloff_minutes
events = get_calendar_events(now)
return events
if __name__ == "__main__":
import uvicorn
uvicorn.run(
app,
host=os.getenv("api_server_host", "127.0.0.1"),
port=int(os.getenv("api_server_port", 8000)),
root_path=os.getenv("api_server_root_path", "/"),
log_level="info"
)