fix missing logging of requests

This commit is contained in:
Grahame Grieve 2021-11-04 12:41:27 +11:00
parent 52cbe69651
commit 27852389d6
3 changed files with 31 additions and 5 deletions

View File

@ -44,6 +44,7 @@ public class HTMLClientLogger extends BaseLogger implements ToolingClientLogger
private static final boolean DEBUG = false;
private boolean req = false;
private PrintStream file;
public HTMLClientLogger(String log) {
@ -80,6 +81,7 @@ public class HTMLClientLogger extends BaseLogger implements ToolingClientLogger
}
}
file.println("</pre>");
req = true;
}
@Override
@ -87,6 +89,10 @@ public class HTMLClientLogger extends BaseLogger implements ToolingClientLogger
if (DEBUG) {
System.out.println(" txlog resp: " +outcome+" "+present(body));
}
if (!req) {
System.out.println("Record Response without request");
}
req = false;
if (file == null)
return;
file.println("<pre>");

View File

@ -21,6 +21,7 @@ public class Client {
private ToolingClientLogger logger;
private int retryCount;
private long timeout = DEFAULT_TIMEOUT;
private byte[] payload;
public ToolingClientLogger getLogger() {
return logger;
@ -50,6 +51,7 @@ public class Client {
String resourceFormat,
String message,
long timeout) throws IOException {
this.payload = null;
Request.Builder request = new Request.Builder()
.method("OPTIONS", null)
.url(optionsUri.toURL());
@ -62,6 +64,7 @@ public class Client {
Headers headers,
String message,
long timeout) throws IOException {
this.payload = null;
Request.Builder request = new Request.Builder()
.url(resourceUri.toURL());
@ -86,6 +89,7 @@ public class Client {
String message,
long timeout) throws IOException {
if (payload == null) throw new EFhirClientException("PUT requests require a non-null payload");
this.payload = payload;
RequestBody body = RequestBody.create(payload);
Request.Builder request = new Request.Builder()
.url(resourceUri.toURL())
@ -109,6 +113,7 @@ public class Client {
String message,
long timeout) throws IOException {
if (payload == null) throw new EFhirClientException("POST requests require a non-null payload");
this.payload = payload;
RequestBody body = RequestBody.create(MediaType.parse(resourceFormat + ";charset=" + DEFAULT_CHARSET), payload);
Request.Builder request = new Request.Builder()
.url(resourceUri.toURL())
@ -174,7 +179,7 @@ public class Client {
.withMessage(message)
.withHeaders(headers == null ? new Headers.Builder().build() : headers)
.withTimeout(timeout, TimeUnit.MILLISECONDS)
.executeAsBatch();
.executeAsBatch(payload);
}
public <T extends Resource> ResourceRequest<T> executeFhirRequest(Request.Builder request,
@ -190,6 +195,6 @@ public class Client {
.withMessage(message)
.withHeaders(headers == null ? new Headers.Builder().build() : headers)
.withTimeout(timeout, TimeUnit.MILLISECONDS)
.execute();
.execute(payload);
}
}

View File

@ -1,6 +1,8 @@
package org.hl7.fhir.r5.utils.client.network;
import okhttp3.*;
import okio.RealBufferedSink;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.r5.formats.IParser;
import org.hl7.fhir.r5.formats.JsonParser;
@ -201,18 +203,30 @@ public class FhirRequestBuilder {
}
protected Request buildRequest() {
return httpRequest.build();
Request req = httpRequest.build();
return req;
}
public <T extends Resource> ResourceRequest<T> execute() throws IOException {
public <T extends Resource> ResourceRequest<T> execute(byte[] payload) throws IOException {
formatHeaders(httpRequest, resourceFormat, headers);
logRequest(payload);
Response response = getHttpClient().newCall(httpRequest.build()).execute();
T resource = unmarshalReference(response, resourceFormat);
return new ResourceRequest<T>(resource, response.code(), getLocationHeader(response.headers()));
}
public Bundle executeAsBatch() throws IOException {
public void logRequest(byte[] payload) {
if (logger != null) {
List<String> headerList = new ArrayList<>(Collections.emptyList());
Map<String, List<String>> headerMap = headers.toMultimap();
headerMap.keySet().forEach(key -> headerMap.get(key).forEach(value -> headerList.add(key + ":" + value)));
logger.logRequest(httpRequest.getMethod$okhttp().toString(), httpRequest.getUrl$okhttp().toString(), headerList, payload);
}
}
public Bundle executeAsBatch(byte[] payload) throws IOException {
formatHeaders(httpRequest, resourceFormat, null);
logRequest(payload);
Response response = getHttpClient().newCall(httpRequest.build()).execute();
return unmarshalFeed(response, resourceFormat);
}
@ -302,6 +316,7 @@ public class FhirRequestBuilder {
}
}
/**
* Logs the given {@link Response}, using the current {@link ToolingClientLogger}. If the current
* {@link FhirRequestBuilder#logger} is null, no action is taken.