Skip to content

Commit

Permalink
Merge pull request #1926 from njorocs/cr-integration
Browse files Browse the repository at this point in the history
Cr integration
  • Loading branch information
PatrickWaweru authored Jun 20, 2024
2 parents 4e8193d + 3b344d9 commit 5c71116
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public class CommonMetadata extends AbstractMetadataBundle {
public static final String GP_CLIENT_VERIFICATION_USE_EMR_PROXY = "kenyaemr.client.registry.use.emr.proxy";
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";
public static final String GP_CLIENT_VERIFICATION_API_TOKEN = "kenyaemr.client.registry.api.token";
public static final String GP_CLIENT_VERIFICATION_TOKEN_URL = "kenyaemr.client.registry.token.url";
Expand Down Expand Up @@ -249,6 +253,19 @@ public void install() {
if(Context.getAdministrationService().getGlobalPropertyObject(CommonMetadata.GP_CLIENT_VERIFICATION_GET_END_POINT) == null) {
install(globalProperty(GP_CLIENT_VERIFICATION_GET_END_POINT, "A GET API for getting client information at the client registry", "https://afyakenyaapi.health.go.ke/partners/registry/search"));
}
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", ""));
}
if(Context.getAdministrationService().getGlobalPropertyObject(CommonMetadata.GP_SHA_CLIENT_VERIFICATION_GET_API_SECRET) == null) {
install(globalProperty(GP_SHA_CLIENT_VERIFICATION_GET_API_SECRET, "API secret token for for connecting to the SHA client registry", ""));
}
if(Context.getAdministrationService().getGlobalPropertyObject(CommonMetadata.GP_CLIENT_VERIFICATION_POST_END_POINT) == null) {
install(globalProperty(GP_CLIENT_VERIFICATION_POST_END_POINT, "A POST API for posting client information to the client registry", "https://afyakenyaapi.health.go.ke/partners/registry"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
import java.util.Comparator;

import org.springframework.http.MediaType;
import java.util.Base64;

/**
* The rest controller for exposing resources through kenyacore and kenyaemr modules
Expand Down Expand Up @@ -3264,4 +3265,124 @@ public Object getLatestObs(@RequestParam("patientUuid") String patientUuid, @Req
return ret;

}

/**
* End Point for getting Patient resource from SHA client registry
* @param payload
* @param identifier
* @param identifierType
* @return
*/
@CrossOrigin(origins = "*", methods = {RequestMethod.GET, RequestMethod.OPTIONS})
@RequestMapping(method = RequestMethod.GET, value = "/getSHAPatient/{payload}/{identifier}/{identifierType}")
public ResponseEntity<String> getSHAPatient(@PathVariable String payload, @PathVariable String identifier, @PathVariable String identifierType) {
String strUserName = "";
String strPassword = "";

String errorResponse = "{\"status\": \"Error\"}";
try {
// Retrieve base URL from global properties
GlobalProperty globalGetUrl = Context.getAdministrationService().getGlobalPropertyObject(CommonMetadata.GP_SHA_CLIENT_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/shaPatientResource";
}

// 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 + "/" + payload + "/" + identifier + "/" + identifierType;
System.out.println("SHA Client registry verification: 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 client registry verification: ERROR: {} "+ ex.getMessage(), ex);
}
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 5c71116

Please sign in to comment.