Search in sources :

Example 1 with FatalRequestIncompleteException

use of org.hl7.davinci.FatalRequestIncompleteException in project CRD by HL7-DaVinci.

the class FhirRequestProcessor method executeFhirQuery.

/**
 * Execute a Fhir query with the given query body and query url.
 * @param queryBody
 * @param queryUrl
 * @param cdsRequest
 * @param fhirComponents
 * @param httpMethod
 * @return
 */
public static IBaseResource executeFhirQuery(String queryBody, String queryUrl, CdsRequest<?, ?> cdsRequest, FhirComponentsT fhirComponents, HttpMethod httpMethod) {
    if (cdsRequest.getFhirServer() == null) {
        throw new FatalRequestIncompleteException("Attempted to perform a Query Batch Request, but no fhir " + "server provided.");
    }
    // Remove the trailing '/' if there is one.
    String fhirBase = cdsRequest.getFhirServer();
    if (fhirBase != null && fhirBase.endsWith("/")) {
        fhirBase = fhirBase.substring(0, fhirBase.length() - 1);
    }
    String fullUrl = fhirBase + "/" + queryUrl;
    // TODO: Once our provider fhir server is up, switch the fetch to use the hapi client instead
    // cdsRequest.getOauth();
    // FhirContext ctx = FhirContext.forR4();
    // BearerTokenAuthInterceptor authInterceptor = new BearerTokenAuthInterceptor(oauth.get("access_token"));
    // IGenericClient client = ctx.newRestfulGenericClient(server);
    // client.registerInterceptor(authInterceptor);
    // return client;
    // IGenericClient client = ctx.newRestfulGenericClient(serverBase);
    // return client.search().byUrl(query).encodedJson().returnBundle(Bundle.class).execute();
    String token = null;
    if (cdsRequest.getFhirAuthorization() != null) {
        token = cdsRequest.getFhirAuthorization().getAccessToken();
    }
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
    if (!queryBody.isEmpty()) {
        headers.setContentType(MediaType.APPLICATION_JSON);
    }
    if (token != null) {
        headers.set("Authorization", "Bearer " + token);
    }
    HttpEntity<String> entity = new HttpEntity<>(queryBody, headers);
    try {
        logger.info("Fetching: " + fullUrl);
        // Request source: https://www.hl7.org/fhir/http.html#transaction
        ResponseEntity<String> response = restTemplate.exchange(fullUrl, httpMethod, entity, String.class);
        logger.info("Fetched: " + response.getBody());
        return fhirComponents.getJsonParser().parseResource(response.getBody());
    } catch (RestClientException e) {
        logger.warn("Unable to make the fetch request", e);
        return null;
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) FatalRequestIncompleteException(org.hl7.davinci.FatalRequestIncompleteException) HttpEntity(org.springframework.http.HttpEntity) RestTemplate(org.springframework.web.client.RestTemplate) RestClientException(org.springframework.web.client.RestClientException)

Example 2 with FatalRequestIncompleteException

use of org.hl7.davinci.FatalRequestIncompleteException in project CRD by HL7-DaVinci.

the class PrefetchHydrator method hydrate.

/**
 * Attempt to hydrate missing prefetch elements, note that this modifies the request object.
 */
public void hydrate() {
    Object crdResponse = cdsRequest.getPrefetch();
    for (PrefetchTemplateElement prefetchElement : cdsService.getPrefetchElements()) {
        String prefetchKey = prefetchElement.getKey();
        // check if the prefetch has already been populated with that key
        Boolean alreadyIncluded = false;
        try {
            alreadyIncluded = (PropertyUtils.getProperty(crdResponse, prefetchKey) != null);
        } catch (Exception e) {
            throw new RuntimeException("System error: Mismatch in prefetch keys between the " + "CrdPrefetch and the key templates set in the service.", e);
        }
        if (!alreadyIncluded) {
            // check if the bundle actually has element
            String prefetchQuery = cdsService.prefetch.get(prefetchKey);
            String hydratedPrefetchQuery = hydratePrefetchQuery(prefetchQuery);
            // e.g. this could be a query template for a medication order but we have a device request
            if (hydratedPrefetchQuery != null) {
                if (cdsRequest.getFhirServer() == null) {
                    throw new FatalRequestIncompleteException("Attempting to fill the prefetch, but no fhir " + "server provided. Either provide a full prefetch or provide a fhir server.");
                }
                try {
                    PropertyUtils.setProperty(crdResponse, prefetchKey, prefetchElement.getReturnType().cast(FhirRequestProcessor.executeFhirQueryUrl(hydratedPrefetchQuery, cdsRequest, fhirComponents, HttpMethod.GET)));
                } catch (Exception e) {
                    logger.warn("Failed to fill prefetch for key: " + prefetchKey, e);
                }
            }
        }
    }
}
Also used : FatalRequestIncompleteException(org.hl7.davinci.FatalRequestIncompleteException) PrefetchTemplateElement(org.hl7.davinci.PrefetchTemplateElement) FatalRequestIncompleteException(org.hl7.davinci.FatalRequestIncompleteException)

Aggregations

FatalRequestIncompleteException (org.hl7.davinci.FatalRequestIncompleteException)2 PrefetchTemplateElement (org.hl7.davinci.PrefetchTemplateElement)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1 RestClientException (org.springframework.web.client.RestClientException)1 RestTemplate (org.springframework.web.client.RestTemplate)1