From 3b344d9fecdbbab22f5607b243d02d69465f6bb7 Mon Sep 17 00:00:00 2001 From: njorocs Date: Thu, 20 Jun 2024 17:41:09 +0300 Subject: [PATCH] Added functionality for getting practitioner resource from remote server --- .../kenyaemr/metadata/CommonMetadata.java | 5 ++ .../KenyaemrCoreRestController.java | 60 +++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/api/src/main/java/org/openmrs/module/kenyaemr/metadata/CommonMetadata.java b/api/src/main/java/org/openmrs/module/kenyaemr/metadata/CommonMetadata.java index 084449ce0a..5a3758f735 100755 --- a/api/src/main/java/org/openmrs/module/kenyaemr/metadata/CommonMetadata.java +++ b/api/src/main/java/org/openmrs/module/kenyaemr/metadata/CommonMetadata.java @@ -43,6 +43,7 @@ public class CommonMetadata extends AbstractMetadataBundle { public static final String GP_CLIENT_VERIFICATION_EMR_VERIFICATION_PROXY_URL = "kenyaemr.client.registry.emr.verification.proxy.url"; public static final String GP_CLIENT_VERIFICATION_GET_END_POINT = "kenyaemr.client.registry.get.api"; public static final String GP_SHA_CLIENT_VERIFICATION_GET_END_POINT = "kenyaemr.sha.registry.get.api"; + public static final String GP_SHA_HEALTH_WORKER_VERIFICATION_GET_END_POINT = "kenyaemr.sha.healthworker.registry.get.api"; public static final String GP_SHA_CLIENT_VERIFICATION_GET_API_USER = "kenyaemr.sha.registry.get.api.user"; public static final String GP_SHA_CLIENT_VERIFICATION_GET_API_SECRET = "kenyaemr.sha.registry.get.api.secret"; public static final String GP_CLIENT_VERIFICATION_POST_END_POINT = "kenyaemr.client.registry.post.api"; @@ -255,6 +256,10 @@ public void install() { if(Context.getAdministrationService().getGlobalPropertyObject(CommonMetadata.GP_SHA_CLIENT_VERIFICATION_GET_END_POINT) == null) { install(globalProperty(GP_SHA_CLIENT_VERIFICATION_GET_END_POINT, "A GET API for getting SHA client information from the client registry", "http://127.0.0.1:9342/api/shaPatientResource")); } + if(Context.getAdministrationService().getGlobalPropertyObject(CommonMetadata.GP_SHA_HEALTH_WORKER_VERIFICATION_GET_END_POINT) == null) { + install(globalProperty(GP_SHA_HEALTH_WORKER_VERIFICATION_GET_END_POINT, "A GET API for getting SHA Health Worker information from Healthcare Worker registry", "http://127.0.0.1:9342/api/practitioner-search")); + } + if(Context.getAdministrationService().getGlobalPropertyObject(CommonMetadata.GP_SHA_CLIENT_VERIFICATION_GET_API_USER) == null) { install(globalProperty(GP_SHA_CLIENT_VERIFICATION_GET_API_USER, "API user for for connecting to the SHA client registry", "")); } diff --git a/omod/src/main/java/org/openmrs/module/kenyaemr/web/controller/KenyaemrCoreRestController.java b/omod/src/main/java/org/openmrs/module/kenyaemr/web/controller/KenyaemrCoreRestController.java index ae600e8b4a..9cd84f2ef4 100644 --- a/omod/src/main/java/org/openmrs/module/kenyaemr/web/controller/KenyaemrCoreRestController.java +++ b/omod/src/main/java/org/openmrs/module/kenyaemr/web/controller/KenyaemrCoreRestController.java @@ -3325,4 +3325,64 @@ public ResponseEntity getSHAPatient(@PathVariable String payload, @PathV } return ResponseEntity.badRequest().contentType(MediaType.APPLICATION_JSON).body(errorResponse); } + + /** + * End Point for getting Practitioner resource from SHA client registry + * @param registrationNumber + * @return + */ + @CrossOrigin(origins = "*", methods = {RequestMethod.GET, RequestMethod.OPTIONS}) + // @RequestMapping(method = RequestMethod.GET, value = "/practitioner-search/{registrationNumber}") + @RequestMapping(method = RequestMethod.GET, value = "/practitioner-search") + public ResponseEntity getSHAPractitioner(@PathVariable String registrationNumber) { + String strUserName = ""; + String strPassword = ""; + + String errorResponse = "{\"status\": \"Error\"}"; + try { + // Retrieve base URL from global properties + GlobalProperty globalGetUrl = Context.getAdministrationService().getGlobalPropertyObject(CommonMetadata.GP_SHA_HEALTH_WORKER_VERIFICATION_GET_END_POINT); + String baseURL = globalGetUrl.getPropertyValue(); + if (baseURL == null || baseURL.trim().isEmpty()) { + // TODO replace base URL with the actual external default url + baseURL = "http://127.0.0.1:9342/api/practitioner-search"; + } + + // Get Auth credentials + GlobalProperty globalGetUsername = Context.getAdministrationService().getGlobalPropertyObject(CommonMetadata.GP_SHA_CLIENT_VERIFICATION_GET_API_USER); + strUserName = globalGetUsername.getPropertyValue(); + + GlobalProperty globalGetPassword = Context.getAdministrationService().getGlobalPropertyObject(CommonMetadata.GP_SHA_CLIENT_VERIFICATION_GET_API_SECRET); + strPassword = globalGetPassword.getPropertyValue(); + // Prepare URL + String completeURL = baseURL + "/" + registrationNumber; + System.out.println("SHA practitioner search: Using SHA GET URL: " + completeURL); + URL url = new URL(completeURL); + + // Set up connection + String auth = strUserName + ":" + strPassword; + String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes()); + + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + connection.setDoOutput(true); + connection.setRequestProperty("Authorization", "Basic " + encodedAuth); + connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); + connection.setRequestProperty("Accept", "application/json"); + connection.setConnectTimeout(10000); // set timeout to 10 seconds + + StringBuilder response = new StringBuilder(); + try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { + String line; + while ((line = reader.readLine()) != null) { + response.append(line); + } + } + return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(response.toString()); + } catch (Exception ex) { + log.error("SHA practitioner search: ERROR: {} "+ ex.getMessage(), ex); + } + return ResponseEntity.badRequest().contentType(MediaType.APPLICATION_JSON).body(errorResponse); + } + }