Skip to content

Commit

Permalink
Added functionality for getting practitioner resource from remote server
Browse files Browse the repository at this point in the history
  • Loading branch information
njorocs committed Jun 20, 2024
1 parent f16a405 commit 3b344d9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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", ""));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3325,4 +3325,64 @@ public ResponseEntity<String> 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<String> 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);
}

}

0 comments on commit 3b344d9

Please sign in to comment.