fixing the client

This commit is contained in:
leif stawnyczy 2024-08-01 16:20:26 -04:00
parent a4b731d809
commit cf2f55afbb
9 changed files with 60 additions and 14 deletions

View File

@ -99,6 +99,8 @@ public interface IGenericClient extends IRestfulClient {
*/ */
IGetPage loadPage(); IGetPage loadPage();
IGetPage loadPage(ClientType theClientType);
/** /**
* Fluent method for the "meta" operations, which can be used to get, add and remove tags and other * Fluent method for the "meta" operations, which can be used to get, add and remove tags and other
* Meta elements from a resource or across the server. * Meta elements from a resource or across the server.

View File

@ -73,4 +73,12 @@ public interface IHttpClient {
IHttpRequest createRequest(HttpClientRequestParameters theParameters); IHttpRequest createRequest(HttpClientRequestParameters theParameters);
void addHeadersToRequest(IHttpRequest theRequest, EncodingEnum theEncodingEnum, FhirContext theContext); void addHeadersToRequest(IHttpRequest theRequest, EncodingEnum theEncodingEnum, FhirContext theContext);
/**
* Updates the client's url;
* This is used when we reuse a client for multiple different requests
* (ex, searches, or fetching the /metadata endpoint followed by whatever
* the actual endpoint is, etc)
*/
void setNewUrl(StringBuilder theUrl);
} }

View File

@ -114,7 +114,9 @@ public class ApacheHttpClient extends BaseHttpClient implements IHttpClient {
} }
private HttpRequestBase constructRequestBase(HttpEntity theEntity) { private HttpRequestBase constructRequestBase(HttpEntity theEntity) {
HttpClientRequestParameters parameters = new HttpClientRequestParameters(myUrl.toString(), myRequestType); // request type for requests with bodies is POST
RequestTypeEnum requestType = myRequestType == null ? RequestTypeEnum.POST : myRequestType;
HttpClientRequestParameters parameters = new HttpClientRequestParameters(myUrl.toString(), requestType);
return constructRequestBase(parameters, theEntity); return constructRequestBase(parameters, theEntity);
} }
@ -128,6 +130,7 @@ public class ApacheHttpClient extends BaseHttpClient implements IHttpClient {
@Override @Override
public IHttpRequest createRequest(HttpClientRequestParameters theParameters) { public IHttpRequest createRequest(HttpClientRequestParameters theParameters) {
myUrl = new StringBuilder(theParameters.getUrl());
HttpRequestBase request = constructRequestBase(theParameters, getEntityFromParameters(theParameters)); HttpRequestBase request = constructRequestBase(theParameters, getEntityFromParameters(theParameters));
return new ApacheHttpRequest(myClient, request); return new ApacheHttpRequest(myClient, request);
} }

View File

@ -50,8 +50,8 @@ import java.util.Map;
*/ */
public class ApacheHttpRequest extends BaseHttpRequest implements IHttpRequest { public class ApacheHttpRequest extends BaseHttpRequest implements IHttpRequest {
private HttpClient myClient; private final HttpClient myClient;
private HttpRequestBase myRequest; private final HttpRequestBase myRequest;
public ApacheHttpRequest(HttpClient theClient, HttpRequestBase theApacheRequest) { public ApacheHttpRequest(HttpClient theClient, HttpRequestBase theApacheRequest) {
this.myClient = theClient; this.myClient = theClient;

View File

@ -41,7 +41,7 @@ public abstract class BaseHttpClient implements IHttpClient {
private final Map<String, List<String>> myIfNoneExistParams; private final Map<String, List<String>> myIfNoneExistParams;
private final String myIfNoneExistString; private final String myIfNoneExistString;
protected RequestTypeEnum myRequestType; protected RequestTypeEnum myRequestType;
protected final StringBuilder myUrl; protected StringBuilder myUrl;
/** /**
* Constructor * Constructor
@ -59,6 +59,11 @@ public abstract class BaseHttpClient implements IHttpClient {
this.myHeaders = theHeaders; this.myHeaders = theHeaders;
} }
@Override
public void setNewUrl(StringBuilder theUrl) {
myUrl = theUrl;
}
private void addHeaderIfNoneExist(IHttpRequest result) { private void addHeaderIfNoneExist(IHttpRequest result) {
if (myIfNoneExistParams != null) { if (myIfNoneExistParams != null) {
StringBuilder b = newHeaderBuilder(myUrl); StringBuilder b = newHeaderBuilder(myUrl);

View File

@ -109,7 +109,9 @@ public abstract class BaseHttpClientInvocation {
HttpClientRequestParameters clientRequestParameters = HttpClientRequestParameters clientRequestParameters =
new HttpClientRequestParameters(theParameters.getUrl(), theParameters.getRequestTypeEnum()); new HttpClientRequestParameters(theParameters.getUrl(), theParameters.getRequestTypeEnum());
clientRequestParameters.setEncodingEnum(theParameters.getEncodingEnum()); clientRequestParameters.setEncodingEnum(theParameters.getEncodingEnum());
return httpClient.createRequest(clientRequestParameters); IHttpRequest request = httpClient.createRequest(clientRequestParameters);
httpClient.addHeadersToRequest(request, theParameters.getEncodingEnum(), getContext());
return request;
// return httpClient.createGetRequest(getContext(), theParameters.getEncodingEnum()); // return httpClient.createGetRequest(getContext(), theParameters.getEncodingEnum());
} }

View File

@ -400,7 +400,15 @@ public class GenericClient extends BaseClient implements IGenericClient {
@Override @Override
public IGetPage loadPage() { public IGetPage loadPage() {
return new LoadPageInternal(); return loadPage(ClientType.DEPRECATED);
}
@Override
public IGetPage loadPage(ClientType theClientType) {
if (theClientType == ClientType.PRESERVED) {
return new LoadPageInternal(myClient);
}
return new LoadPageInternal(null);
} }
@Override @Override
@ -711,7 +719,15 @@ public class GenericClient extends BaseClient implements IGenericClient {
IClientResponseHandler<Z> theHandler, IClientResponseHandler<Z> theHandler,
BaseHttpClientInvocation theInvocation) { BaseHttpClientInvocation theInvocation) {
if (isKeepResponses()) { if (isKeepResponses()) {
myLastRequest = theInvocation.asHttpRequest(getServerBase(), theParams, getEncoding(), myPrettyPrint); // myLastRequest = theInvocation.asHttpRequest(getServerBase(), theParams, getEncoding(), myPrettyPrint);
myLastRequest = theInvocation.asHttpRequest(
new AsHttpRequestParams()
.setUrlBase(getServerBase())
.setClient(myClient)
.setExtraParams(theParams)
.setEncodingEnum(getParamEncoding())
.setPrettyPrint(myPrettyPrint)
);
} }
InvokeClientParameters<Z> params = new InvokeClientParameters<Z>() InvokeClientParameters<Z> params = new InvokeClientParameters<Z>()
@ -1060,13 +1076,13 @@ public class GenericClient extends BaseClient implements IGenericClient {
private class GetPageInternal extends BaseClientExecutable<IGetPageTyped<Object>, Object> private class GetPageInternal extends BaseClientExecutable<IGetPageTyped<Object>, Object>
implements IGetPageTyped<Object> { implements IGetPageTyped<Object> {
private Class<? extends IBaseBundle> myBundleType; private final Class<? extends IBaseBundle> myBundleType;
private String myUrl; private final String myUrl;
private PagingHttpMethodEnum myPagingHttpMethod = PagingHttpMethodEnum.GET; private PagingHttpMethodEnum myPagingHttpMethod = PagingHttpMethodEnum.GET;
public GetPageInternal(String theUrl, Class<? extends IBaseBundle> theBundleType) { public GetPageInternal(IHttpClient theClient, String theUrl, Class<? extends IBaseBundle> theBundleType) {
super(null); super(theClient);
myUrl = theUrl; myUrl = theUrl;
myBundleType = theBundleType; myBundleType = theBundleType;
} }
@ -1209,10 +1225,16 @@ public class GenericClient extends BaseClient implements IGenericClient {
private static final String PREVIOUS = "previous"; private static final String PREVIOUS = "previous";
private String myPageUrl; private String myPageUrl;
private IHttpClient myClient;
public LoadPageInternal(IHttpClient theClient) {
myClient = theClient;
}
@Override @Override
public <T extends IBaseBundle> IGetPageTyped andReturnBundle(Class<T> theBundleType) { public <T extends IBaseBundle> IGetPageTyped andReturnBundle(Class<T> theBundleType) {
Validate.notNull(theBundleType, "theBundleType must not be null"); Validate.notNull(theBundleType, "theBundleType must not be null");
return new GetPageInternal(myPageUrl, theBundleType); return new GetPageInternal(myClient, myPageUrl, theBundleType);
} }
@Override @Override
@ -2267,7 +2289,6 @@ public class GenericClient extends BaseClient implements IGenericClient {
@Override @Override
public OUTPUT execute() { public OUTPUT execute() {
Map<String, List<String>> params = getParamMap(); Map<String, List<String>> params = getParamMap();
for (TokenParam next : myTags) { for (TokenParam next : myTags) {

View File

@ -145,6 +145,9 @@ abstract class BaseHttpClientInvocationWithContents extends BaseHttpClientInvoca
if (theParams.getClient() != null) { if (theParams.getClient() != null) {
// use the provided one // use the provided one
httpClient = theParams.getClient(); httpClient = theParams.getClient();
// update the url to the one we want (in case the
// previous client did not have the correct one
httpClient.setNewUrl(url);
} else { } else {
// make a new one // make a new one
httpClient = getRestfulClientFactory() httpClient = getRestfulClientFactory()
@ -167,6 +170,7 @@ abstract class BaseHttpClientInvocationWithContents extends BaseHttpClientInvoca
} }
if (myParams != null) { if (myParams != null) {
IHttpRequest request = httpClient.createParamRequest(getContext(), myParams, encoding); IHttpRequest request = httpClient.createParamRequest(getContext(), myParams, encoding);
return request; return request;
} }

View File

@ -63,7 +63,8 @@ public class HttpSimpleClientInvocation extends BaseHttpClientInvocation {
@Override @Override
public IHttpRequest asHttpRequest(AsHttpRequestParams theParams) { public IHttpRequest asHttpRequest(AsHttpRequestParams theParams) {
CreateRequestParameters parameters = new CreateRequestParameters(); CreateRequestParameters parameters = new CreateRequestParameters();
parameters.setUrl(theParams.getUrlBase()); // parameters.setUrl(myUrl == null ? theParams.getUrlBase() : myUrl);
parameters.setUrl(myUrl);
parameters.setEncodingEnum(theParams.getEncodingEnum()); parameters.setEncodingEnum(theParams.getEncodingEnum());
parameters.setRequestTypeEnum(myPagingHttpMethod.getRequestType()); parameters.setRequestTypeEnum(myPagingHttpMethod.getRequestType());
parameters.setClient(theParams.getClient()); parameters.setClient(theParams.getClient());