-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp-example.py
88 lines (65 loc) · 1.92 KB
/
http-example.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!./venv/bin/python
from typing import Any, Optional
from fastapi import FastAPI, Query, Request
from pydantic import BaseModel
import base64
version = "v1.2.3"
app = FastAPI()
#
# RECEIVE AND DO SOMETHIND REQUIRED WITH NOTIFICATIONS DATA
# MAIN
#
class NotifyData(BaseModel):
recipients: list[str] # recipients list
subject: str # notification subject
text: str # text of notification
image: str # base64 encoded image
# POST / receives prepared notification data to send
# required
@app.post("/")
async def notify(req: Request, data: NotifyData):
print("data:", data.recipients, data.subject, data.text, len(data.image))
comp = int(req.query_params['compression'])
save = req.query_params['save'].lower() == 'true'
print({"comp":comp,"save":save})
if save == "true":
f = open("image.jpg", "wb")
f.write(base64.b64decode(data.image))
f.close()
return {}
#
# STATUS
# OPTIONAL
#
# the list of contacts will be available for selection in the recipients line
class Contact(BaseModel):
name: str
value: str
# list of variables based on which custom fields will be added to the RTMIP
class Variable(BaseModel):
type: str
name: str
label: str
desc: Optional[str] = None
readonly: Optional[bool] = None
value: Optional[Any] = None
default: Optional[Any] = None
width: int = 12
# status response
class StatusResp(BaseModel):
version: Optional[str]
contacts: Optional[list[Contact]] = None
variables: Optional[list[Variable]] = None
# GET /status can be useful for transmitting any custom parameters from the
# rtmip to the service
# optional
@app.get("/status", response_model=StatusResp)
async def root():
return StatusResp(
version=version,
contacts=[Contact(name="test",value="[email protected]")],
variables=[
Variable(type="number", name="compression", label="Compression Level"),
Variable(type="checkbox", name="save", label="Save Images"),
],
)