From 534de22b04d3369684995bca42394fb02c866820 Mon Sep 17 00:00:00 2001 From: leif stawnyczy Date: Fri, 9 Aug 2024 17:17:39 -0400 Subject: [PATCH] test fixes --- .../okhttp/client/OkHttpRestfulClient.java | 46 +++++++++++++++---- .../client/impl/BaseHttpClientInvocation.java | 1 + 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/hapi-fhir-client-okhttp/src/main/java/ca/uhn/fhir/okhttp/client/OkHttpRestfulClient.java b/hapi-fhir-client-okhttp/src/main/java/ca/uhn/fhir/okhttp/client/OkHttpRestfulClient.java index 57e1c823562..20e1a99c13b 100644 --- a/hapi-fhir-client-okhttp/src/main/java/ca/uhn/fhir/okhttp/client/OkHttpRestfulClient.java +++ b/hapi-fhir-client-okhttp/src/main/java/ca/uhn/fhir/okhttp/client/OkHttpRestfulClient.java @@ -28,6 +28,7 @@ import ca.uhn.fhir.rest.client.api.HttpClientUtil; import ca.uhn.fhir.rest.client.api.IHttpClient; import ca.uhn.fhir.rest.client.api.IHttpRequest; import ca.uhn.fhir.rest.client.impl.BaseHttpClientInvocation; +import ca.uhn.fhir.rest.client.impl.RestfulClientFactory; import ca.uhn.fhir.rest.client.method.MethodUtil; import ca.uhn.fhir.rest.param.HttpClientRequestParameters; import okhttp3.Call; @@ -45,6 +46,7 @@ import static ca.uhn.fhir.okhttp.utils.UrlStringUtils.endsWith; import static ca.uhn.fhir.okhttp.utils.UrlStringUtils.everythingAfterFirstQuestionMark; import static ca.uhn.fhir.okhttp.utils.UrlStringUtils.hasQuestionMark; import static ca.uhn.fhir.okhttp.utils.UrlStringUtils.withTrailingQuestionMarkRemoved; +import static org.apache.commons.lang3.StringUtils.isNotBlank; /** * A Http Request based on OkHttp. This is an adapter around the class @@ -53,6 +55,7 @@ import static ca.uhn.fhir.okhttp.utils.UrlStringUtils.withTrailingQuestionMarkRe * @author Matthew Clarke | matthew.clarke@orionhealth.com | Orion Health */ public class OkHttpRestfulClient implements IHttpClient { + private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(OkHttpRestfulClient.class); private Call.Factory myClient; private StringBuilder myUrl; @@ -80,13 +83,19 @@ public class OkHttpRestfulClient implements IHttpClient { @Override public IHttpRequest createByteRequest( FhirContext theContext, String theContents, String theContentType, EncodingEnum theEncoding) { - initBaseRequest(theContext, theEncoding, createPostBody(theContents, theContentType)); + initBaseRequest(theContext, theEncoding, createPostBody(theContents, theContentType), RequestTypeEnum.POST); return myRequest; } - private void initBaseRequest(FhirContext theContext, EncodingEnum theEncoding, RequestBody body) { + private void initBaseRequest( + FhirContext theContext, + EncodingEnum theEncoding, + RequestBody body, + RequestTypeEnum theRequestType + ) { + RequestTypeEnum requestType = theRequestType != null ? theRequestType : myRequestType; String sanitisedUrl = withTrailingQuestionMarkRemoved(myUrl.toString()); - myRequest = new OkHttpRestfulRequest(myClient, sanitisedUrl, myRequestType, body); + myRequest = new OkHttpRestfulRequest(myClient, sanitisedUrl, requestType, body); addHeadersToRequest(myRequest, theEncoding, theContext); } @@ -97,7 +106,7 @@ public class OkHttpRestfulClient implements IHttpClient { @Override public IHttpRequest createParamRequest( FhirContext theContext, Map> theParams, EncodingEnum theEncoding) { - initBaseRequest(theContext, theEncoding, getFormBodyFromParams(theParams)); + initBaseRequest(theContext, theEncoding, getFormBodyFromParams(theParams), RequestTypeEnum.POST); return myRequest; } @@ -114,7 +123,7 @@ public class OkHttpRestfulClient implements IHttpClient { @Override public IHttpRequest createBinaryRequest(FhirContext theContext, IBaseBinary theBinary) { - initBaseRequest(theContext, null, createPostBody(theBinary.getContent(), theBinary.getContentType())); + initBaseRequest(theContext, null, createPostBody(theBinary.getContent(), theBinary.getContentType()), RequestTypeEnum.POST); return myRequest; } @@ -124,16 +133,35 @@ public class OkHttpRestfulClient implements IHttpClient { @Override public IHttpRequest createGetRequest(FhirContext theContext, EncodingEnum theEncoding) { - initBaseRequest(theContext, theEncoding, null); + initBaseRequest(theContext, theEncoding, null, RequestTypeEnum.GET); return myRequest; } @Override public IHttpRequest createRequest(HttpClientRequestParameters theParameters) { + RequestBody requestBody = null; + switch (theParameters.getRequestTypeEnum()) { + case POST: + case PUT: + if (theParameters.getFormParams() != null && !theParameters.getFormParams().isEmpty()) { + requestBody = getFormBodyFromParams(theParameters.getFormParams()); + } else if (theParameters.getByteContents() != null) { + requestBody = createPostBody(theParameters.getByteContents(), theParameters.getContentType()); + } else if (isNotBlank(theParameters.getContents())) { + requestBody = createPostBody(theParameters.getContents(), theParameters.getContentType()); + } else if (theParameters.getBaseBinary() != null) { + requestBody = createPostBody(theParameters.getBaseBinary().getContent(), theParameters.getContentType()); + } else { + ourLog.debug("No body contents found for HTTP-{}", theParameters.getRequestTypeEnum().name()); + } + + break; + } initBaseRequest( - theParameters.getFhirContext(), - theParameters.getEncodingEnum(), - createPostBody(theParameters.getByteContents(), theParameters.getContents())); + theParameters.getFhirContext(), + theParameters.getEncodingEnum(), + requestBody, + theParameters.getRequestTypeEnum()); return myRequest; } diff --git a/hapi-fhir-client/src/main/java/ca/uhn/fhir/rest/client/impl/BaseHttpClientInvocation.java b/hapi-fhir-client/src/main/java/ca/uhn/fhir/rest/client/impl/BaseHttpClientInvocation.java index 2eccca8821e..61aa6365a3d 100644 --- a/hapi-fhir-client/src/main/java/ca/uhn/fhir/rest/client/impl/BaseHttpClientInvocation.java +++ b/hapi-fhir-client/src/main/java/ca/uhn/fhir/rest/client/impl/BaseHttpClientInvocation.java @@ -114,6 +114,7 @@ public abstract class BaseHttpClientInvocation { HttpClientRequestParameters clientRequestParameters = new HttpClientRequestParameters(theParameters.getUrl(), theParameters.getRequestTypeEnum()); clientRequestParameters.setEncodingEnum(theParameters.getEncodingEnum()); + clientRequestParameters.setFhirContext(getContext()); IHttpRequest request = httpClient.createRequest(clientRequestParameters); for (Header h : getHeaders()) { request.addHeader(h.getName(), h.getValue());