-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontroller.py
More file actions
228 lines (192 loc) · 8.07 KB
/
controller.py
File metadata and controls
228 lines (192 loc) · 8.07 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"""
Controller layer for orchestrating calls to external services
"""
from fhir.r4 import Device, Organization, Practitioner
from requests import Response
from gateway_api.clinical_jwt import JWT
from gateway_api.common.error import (
NoAsidFoundError,
NoCurrentEndpointError,
NoCurrentProviderError,
NoOrganisationFoundError,
)
from gateway_api.get_structured_record.request import GetStructuredRecordRequest
from gateway_api.pds import PdsClient
from gateway_api.provider import GpProviderClient
from gateway_api.sds import SdsClient, SdsSearchResults
class Controller:
"""
Orchestrates calls to PDS -> SDS -> GP provider.
"""
gp_provider_client: GpProviderClient | None
def __init__(
self,
pds_base_url: str,
sds_base_url: str,
sds_api_key: str,
timeout: int = 10,
) -> None:
"""
Create a controller instance.
"""
self.pds_base_url = pds_base_url
self.sds_base_url = sds_base_url
self.sds_api_key = sds_api_key
self.timeout = timeout
self.gp_provider_client = None
def run(self, request: GetStructuredRecordRequest) -> Response:
"""
Controller entry point
Expects a GetStructuredRecordRequest instance that contains the header and body
details of the HTTP request received
Orchestration steps:
1) Call PDS to obtain the patient's GP (provider) ODS code.
2) Call SDS using provider ODS to obtain provider ASID + provider endpoint.
3) Call SDS using consumer ODS to obtain consumer ASID.
4) Call GP provider to obtain patient records.
"""
auth_token = self.get_auth_token()
provider_ods = self._get_pds_details(auth_token, request.nhs_number)
consumer_asid, provider_asid, provider_endpoint = self._get_sds_details(
request.ods_from.strip(), provider_ods
)
token = self.get_jwt_for_provider(provider_endpoint, request.ods_from.strip())
# Call GP provider with correct parameters
self.gp_provider_client = GpProviderClient(
provider_endpoint=provider_endpoint,
provider_asid=provider_asid,
consumer_asid=consumer_asid,
token=token,
)
provider_response = self.gp_provider_client.access_structured_record(
trace_id=request.trace_id,
body=request.request_body,
)
return provider_response
def get_auth_token(self) -> str:
"""
Retrieve the authorization token.
This is a placeholder implementation. Replace with actual logic to obtain
the auth token as needed.
"""
return "AUTH_TOKEN123"
def get_jwt_for_provider(self, provider_endpoint: str, consumer_ods: str) -> JWT:
# For requesting device details, see:
# https://webarchive.nationalarchives.gov.uk/ukgwa/20250307092533/https://developer.nhs.uk/apis/gpconnect/integration_cross_organisation_audit_and_provenance.html#requesting_device-claim
# For requesting practitioner details, see:
# https://webarchive.nationalarchives.gov.uk/ukgwa/20250307092533/https://developer.nhs.uk/apis/gpconnect/integration_cross_organisation_audit_and_provenance.html#requesting_practitioner-claim
# TODO [GPCAPIM-362]: Get requesting device details
# requesting_device = Device(
# system="https://consumersupplier.com/Id/device-identifier",
# value="CONS-APP-4",
# model="Consumer product name",
# version="5.3.0",
# )
requesting_device = Device.model_validate(
{
"resourceType": "Device",
"identifier": [
{
"system": "https://orange.testlab.nhs.uk/gpconnect-demonstrator/Id/local-system-instance-id",
"value": "gpcdemonstrator-1-orange",
}
],
"model": "GP Connect Demonstrator",
"version": "1.5.0",
}
)
# TODO [GPCAPIM-309]: Get practitioner details
requesting_practitioner = Practitioner.model_validate(
{
"resourceType": "Practitioner",
"id": "10019",
"name": [
{
"family": "Doe",
"given": ["John"],
"prefix": ["Mr"],
}
],
"identifier": [
{
"system": "https://fhir.nhs.uk/Id/sds-user-id",
"value": "111222333444",
},
{
"system": "https://fhir.nhs.uk/Id/sds-role-profile-id",
"value": "444555666777",
},
{
"system": "https://orange.testlab.nhs.uk/gpconnect-demonstrator/Id/local-user-id",
"value": "98ed4f78-814d-4266-8d5b-cde742f3093c",
},
],
}
)
# TODO [GPCAPIM-363]: Get the consumer org name
requesting_organization = Organization.from_ods_code(
name="Consumer organisation name", ods_code=consumer_ods
)
# TODO [GPCAPIM-364]: Get consumer URL for issuer. Use CDG API URL for now.
issuer = "https://clinical-data-gateway-api.sandbox.nhs.uk"
audience = provider_endpoint
token = JWT(
issuer=issuer,
subject=requesting_practitioner.id,
audience=audience,
requesting_device=requesting_device.model_dump(),
requesting_organization=requesting_organization.model_dump(),
requesting_practitioner=requesting_practitioner.model_dump(),
)
return token
def _get_pds_details(self, auth_token: str, nhs_number: str) -> str:
"""
Call PDS to find the provider ODS code (GP ODS code) for a patient.
"""
# PDS: find patient and extract GP ODS code (provider ODS)
pds = PdsClient(
auth_token=auth_token,
base_url=self.pds_base_url,
timeout=self.timeout,
ignore_dates=True,
)
patient = pds.search_patient_by_nhs_number(nhs_number)
if not patient.gp_ods_code:
raise NoCurrentProviderError(nhs_number=nhs_number)
return patient.gp_ods_code
def _get_sds_details(
self, consumer_ods: str, provider_ods: str
) -> tuple[str, str, str]:
"""
Call SDS to obtain consumer ASID, provider ASID, and provider endpoint.
This method performs two SDS lookups:
- provider details (ASID + endpoint)
- consumer details (ASID)
"""
# SDS: Get provider details (ASID + endpoint) for provider ODS
sds = SdsClient(
base_url=self.sds_base_url,
api_key=self.sds_api_key,
timeout=self.timeout,
)
provider_details: SdsSearchResults = sds.get_org_details(
provider_ods, get_endpoint=True
)
if provider_details.is_not_found:
raise NoOrganisationFoundError(org_type="provider", ods_code=provider_ods)
provider_asid = (provider_details.asid or "").strip()
if not provider_asid:
raise NoAsidFoundError(org_type="provider", ods_code=provider_ods)
provider_endpoint = (provider_details.endpoint or "").strip()
if not provider_endpoint:
raise NoCurrentEndpointError(provider_ods=provider_ods)
# SDS: Get consumer details (ASID) for consumer ODS
consumer_details: SdsSearchResults = sds.get_org_details(
consumer_ods, get_endpoint=False
)
if consumer_details.is_not_found:
raise NoOrganisationFoundError(org_type="consumer", ods_code=consumer_ods)
consumer_asid = (consumer_details.asid or "").strip()
if not consumer_asid:
raise NoAsidFoundError(org_type="consumer", ods_code=consumer_ods)
return consumer_asid, provider_asid, provider_endpoint