Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
175 lines (148 loc) · 5.27 KB
/
Copy pathmain.py
File metadata and controls
175 lines (148 loc) · 5.27 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import asyncio
import logging
from datetime import datetime
import coloredlogs
from aiohttp import web
from fhirpy.base.exceptions import OperationOutcome
from sqlalchemy.sql.expression import select
from aidbox_python_sdk.db import DBProxy
from aidbox_python_sdk.handlers import routes
from aidbox_python_sdk.main import create_app as _create_app
from aidbox_python_sdk.sdk import SDK
from aidbox_python_sdk.settings import Settings
logger = logging.getLogger()
coloredlogs.install(level="DEBUG", fmt="%(asctime)s %(levelname)s %(message)s")
settings = Settings(**{})
resources = {
"Client": {"SPA": {"secret": "123456", "grant_types": ["password"]}},
"User": {
"superadmin": {
"email": "superadmin@example.com",
"password": "12345678",
}
},
"AccessPolicy": {"superadmin": {"engine": "json-schema", "schema": {"required": ["user"]}}},
}
seeds = {
"Patient": {
"dfaa8925-f32e-4687-8fd0-272844cff544": {
"name": [
{
"use": "official",
"family": "Hauck852",
"given": ["Alayna598"],
"prefix": ["Ms."],
}
],
"gender": "female",
},
"dfaa8925-f32e-4687-8fd0-272844cff545": {
"name": [
{
"use": "official",
"family": "Doe12",
"given": ["John46"],
"prefix": ["Ms."],
}
]
},
},
"Contract": {
"e9a3ce1d-745d-4fe1-8e97-807a820e6151": {},
"e9a3ce1d-745d-4fe1-8e97-807a820e6152": {},
"e9a3ce1d-745d-4fe1-8e97-807a820e6153": {},
},
}
sdk = SDK(settings, resources=resources, seeds=seeds)
def create_app():
return _create_app(sdk)
@sdk.subscription("Appointment")
async def appointment_sub(event, request):
"""
POST /Appointment
"""
await asyncio.sleep(5)
return await _appointment_sub(event, request)
async def _appointment_sub(event, request):
participants = event["resource"]["participant"]
patient_id = next(
p["actor"]["id"] for p in participants if p["actor"]["resourceType"] == "Patient"
)
patient = await request.app["client"].resources("Patient").get(id=patient_id)
patient_name = "{} {}".format(patient["name"][0]["given"][0], patient["name"][0]["family"])
appointment_dates = "Start: {:%Y-%m-%d %H:%M}, end: {:%Y-%m-%d %H:%M}".format(
datetime.fromisoformat(event["resource"]["start"]),
datetime.fromisoformat(event["resource"]["end"]),
)
logging.info("*" * 40)
if event["action"] == "create":
logging.info("%s, new appointment was created.", patient_name)
logging.info(appointment_dates)
elif event["action"] == "update":
if event["resource"]["status"] == "booked":
logging.info("%s, appointment was updated.", patient_name)
logging.info(appointment_dates)
elif event["resource"]["status"] == "cancelled":
logging.info("%s, appointment was cancelled.", patient_name)
logging.debug("`Appointment` subscription handler")
logging.debug("Event: %s", event)
@sdk.operation(
methods=["POST", "PATCH"],
path=["signup", "register", {"name": "date"}, {"name": "test"}],
)
def signup_register_op(operation, request):
"""
POST /signup/register/21.02.19/testvalue
PATCH /signup/register/22.02.19/patchtestvalue
"""
logging.debug("`signup_register_op` operation handler")
logging.debug("Operation data: %s", operation)
logging.debug("Request: %s", request)
return web.json_response({"success": "Ok", "request": request["route-params"]})
@sdk.operation(methods=["GET"], path=["Patient", "$weekly-report"], public=True)
@sdk.operation(methods=["GET"], path=["Patient", "$daily-report"])
async def daily_patient_report(operation, request):
"""
GET /Patient/$weekly-report
GET /Patient/$daily-report
"""
patients = request["app"]["client"].resources("Patient")
async for p in patients:
logging.debug(p.serialize())
logging.debug("`daily_patient_report` operation handler")
logging.debug("Operation data: %s", operation)
logging.debug("Request: %s", request)
return web.json_response({"type": "report", "success": "Ok", "msg": "Response from APP"})
async def get_app_ids(db: DBProxy):
app = db.App
return await db.alchemy(select(app.c.id))
@routes.get("/db-tests")
async def db_tests(request):
db = request.app["db"]
return web.json_response(await get_app_ids(db))
@sdk.operation(
["POST"],
["Observation", "observation-custom-op"],
compliance={
"fhirCode": "observation-custom-op",
"fhirUrl": "http://test.com",
"fhirResource": ["Observation"],
},
)
async def observation_custom_op(operation, request):
return {"message": "Observation custom operation response"}
@sdk.operation(
["POST"],
["$operation-outcome-test"],
)
async def operation_outcome_test_op(operation, request):
raise OperationOutcome(reason="test reason")
@sdk.operation(
["POST"],
["$dispatch-test"],
)
async def dispatch_test_op(operation, request):
return web.json_response({"status": "ok"})
@sdk.subscription("Location")
async def dispatch_test_sub(event, request):
pass