-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring and adding tests for BulkExport template (states for the …
…async call protocol).
- Loading branch information
Showing
10 changed files
with
870 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
bulkclient/src/main/java/au/csiro/pathling/export/utils/WebUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2023 Commonwealth Scientific and Industrial Research | ||
* Organisation (CSIRO) ABN 41 687 119 230. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package au.csiro.pathling.export.utils; | ||
|
||
import au.csiro.pathling.export.fhir.FhirJsonSupport; | ||
import java.net.URI; | ||
import javax.annotation.Nonnull; | ||
import lombok.experimental.UtilityClass; | ||
import org.apache.http.Consts; | ||
import org.apache.http.HttpEntity; | ||
import org.apache.http.entity.ContentType; | ||
import org.apache.http.entity.StringEntity; | ||
|
||
/** | ||
* Utility methods for working with web resources. | ||
*/ | ||
@UtilityClass | ||
public class WebUtils { | ||
|
||
/** | ||
* The content type for FHIR JSON. | ||
*/ | ||
public static final ContentType APPLICATION_FHIR_JSON = ContentType.create( | ||
"application/fhir+json", | ||
Consts.UTF_8); | ||
/** | ||
* HTTP status code for too many requests. | ||
*/ | ||
public static final int HTTP_TOO_MANY_REQUESTS = 429; | ||
|
||
|
||
/** | ||
* Converts a FHIR resource to an HTTP entity using GSON serialization. | ||
* | ||
* @param fhirResource the FHIR resource to convert. | ||
* @return the HTTP entity. | ||
*/ | ||
@Nonnull | ||
public static HttpEntity toFhirJsonEntity(@Nonnull final Object fhirResource) { | ||
return new StringEntity( | ||
FhirJsonSupport.toJson(fhirResource), APPLICATION_FHIR_JSON); | ||
} | ||
|
||
/** | ||
* Ensures that the URI ends with a slash. | ||
* | ||
* @param uri the URI to ensure ends with a slash. | ||
* @return the URI with a trailing slash. | ||
*/ | ||
@Nonnull | ||
public static URI ensurePathEndsWithSlash(@Nonnull final URI uri) { | ||
return uri.getPath().endsWith("/") | ||
? uri | ||
: URI.create(uri + "/"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
bulkclient/src/main/java/au/csiro/pathling/export/ws/BulkExportAsyncService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright 2023 Commonwealth Scientific and Industrial Research | ||
* Organisation (CSIRO) ABN 41 687 119 230. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package au.csiro.pathling.export.ws; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.http.client.HttpClient; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.client.methods.HttpUriRequest; | ||
import org.apache.http.util.EntityUtils; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
|
||
|
||
/** | ||
* Service for handling asynchronous requests for bulk export. | ||
*/ | ||
@AllArgsConstructor | ||
@Slf4j | ||
public class BulkExportAsyncService { | ||
|
||
@Nonnull | ||
final HttpClient httpClient; | ||
|
||
@Nonnull | ||
final URI fhirEndpointUri; | ||
|
||
/** | ||
* Kicks off a bulk export request. | ||
* | ||
* @param request the request to kick off | ||
* @return the {@link AsyncResponse}response | ||
* @throws IOException if an error occurs during the request | ||
*/ | ||
@Nonnull | ||
AsyncResponse kickOff(@Nonnull final BulkExportRequest request) throws IOException { | ||
final HttpUriRequest httpRequest = request.toHttpRequest(fhirEndpointUri); | ||
log.debug("KickOff: Request: {}", httpRequest); | ||
if (httpRequest instanceof HttpPost) { | ||
log.debug("KickOff: Request body: {}", | ||
EntityUtils.toString(((HttpPost) httpRequest).getEntity())); | ||
} | ||
return httpClient.execute(httpRequest, AsynResponseHandler.of(BulkExportResponse.class)); | ||
} | ||
|
||
/** | ||
* Checks the status of a bulk export request. Returns an {@link AsyncResponse} that may contain a | ||
* {@link AcceptedAsyncResponse} or a {@link BulkExportResponse} on completion. | ||
* | ||
* @param statusUri the status URI | ||
* @return the {@link AsyncResponse} response | ||
* @throws IOException if an error occurs during the request | ||
*/ | ||
@Nonnull | ||
AsyncResponse checkStatus(@Nonnull final URI statusUri) throws IOException { | ||
log.debug("Poolin: Get status from: " + statusUri); | ||
final HttpUriRequest statusRequest = new HttpGet(statusUri); | ||
statusRequest.setHeader("accept", "application/json"); | ||
return httpClient.execute(statusRequest, AsynResponseHandler.of(BulkExportResponse.class)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.