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(ClientType theClientType);
/**
* 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.

View File

@ -73,4 +73,12 @@ public interface IHttpClient {
IHttpRequest createRequest(HttpClientRequestParameters theParameters);
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) {
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);
}
@ -128,6 +130,7 @@ public class ApacheHttpClient extends BaseHttpClient implements IHttpClient {
@Override
public IHttpRequest createRequest(HttpClientRequestParameters theParameters) {
myUrl = new StringBuilder(theParameters.getUrl());
HttpRequestBase request = constructRequestBase(theParameters, getEntityFromParameters(theParameters));
return new ApacheHttpRequest(myClient, request);
}

View File

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

View File

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

View File

@ -109,7 +109,9 @@ public abstract class BaseHttpClientInvocation {
HttpClientRequestParameters clientRequestParameters =
new HttpClientRequestParameters(theParameters.getUrl(), theParameters.getRequestTypeEnum());
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());
}

View File

@ -400,7 +400,15 @@ public class GenericClient extends BaseClient implements IGenericClient {
@Override
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
@ -711,7 +719,15 @@ public class GenericClient extends BaseClient implements IGenericClient {
IClientResponseHandler<Z> theHandler,
BaseHttpClientInvocation theInvocation) {
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>()
@ -1060,13 +1076,13 @@ public class GenericClient extends BaseClient implements IGenericClient {
private class GetPageInternal extends BaseClientExecutable<IGetPageTyped<Object>, Object>
implements IGetPageTyped<Object> {
private Class<? extends IBaseBundle> myBundleType;
private String myUrl;
private final Class<? extends IBaseBundle> myBundleType;
private final String myUrl;
private PagingHttpMethodEnum myPagingHttpMethod = PagingHttpMethodEnum.GET;
public GetPageInternal(String theUrl, Class<? extends IBaseBundle> theBundleType) {
super(null);
public GetPageInternal(IHttpClient theClient, String theUrl, Class<? extends IBaseBundle> theBundleType) {
super(theClient);
myUrl = theUrl;
myBundleType = theBundleType;
}
@ -1209,10 +1225,16 @@ public class GenericClient extends BaseClient implements IGenericClient {
private static final String PREVIOUS = "previous";
private String myPageUrl;
private IHttpClient myClient;
public LoadPageInternal(IHttpClient theClient) {
myClient = theClient;
}
@Override
public <T extends IBaseBundle> IGetPageTyped andReturnBundle(Class<T> theBundleType) {
Validate.notNull(theBundleType, "theBundleType must not be null");
return new GetPageInternal(myPageUrl, theBundleType);
return new GetPageInternal(myClient, myPageUrl, theBundleType);
}
@Override
@ -2267,7 +2289,6 @@ public class GenericClient extends BaseClient implements IGenericClient {
@Override
public OUTPUT execute() {
Map<String, List<String>> params = getParamMap();
for (TokenParam next : myTags) {

View File

@ -145,6 +145,9 @@ abstract class BaseHttpClientInvocationWithContents extends BaseHttpClientInvoca
if (theParams.getClient() != null) {
// use the provided one
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 {
// make a new one
httpClient = getRestfulClientFactory()
@ -167,6 +170,7 @@ abstract class BaseHttpClientInvocationWithContents extends BaseHttpClientInvoca
}
if (myParams != null) {
IHttpRequest request = httpClient.createParamRequest(getContext(), myParams, encoding);
return request;
}

View File

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