Skip to content

Commit

Permalink
Improves request/response logging of WorkspaceApiClient.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrowndotje committed Jan 16, 2025
1 parent 11ff1e8 commit f00c975
Showing 1 changed file with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;

Expand Down Expand Up @@ -201,11 +202,10 @@ private boolean manageLockForWorkspace(long workspaceId, boolean lock) throws St
debugRequest(httpRequest, null);

try (CloseableHttpResponse response = httpClient.execute(httpRequest)) {
debugResponse(response);
String json = EntityUtils.toString(response.getEntity());
debugResponse(response, json);

String responseText = EntityUtils.toString(response.getEntity());
ApiResponse apiResponse = ApiResponse.parse(responseText);
log.info(responseText);
ApiResponse apiResponse = ApiResponse.parse(json);

if (response.getCode() == HttpStatus.SC_OK) {
return apiResponse.isSuccess();
Expand Down Expand Up @@ -245,9 +245,9 @@ public Workspace getWorkspace(long workspaceId) throws StructurizrClientExceptio
debugRequest(httpGet, null);

try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
debugResponse(response);

String json = EntityUtils.toString(response.getEntity());
debugResponse(response, json);

if (response.getCode() == HttpStatus.SC_OK) {
archiveWorkspace(workspaceId, json);

Expand Down Expand Up @@ -339,10 +339,9 @@ public void putWorkspace(long workspaceId, Workspace workspace) throws Structuri
log.info("Putting workspace with ID " + workspaceId);
try (CloseableHttpResponse response = httpClient.execute(httpPut)) {
String json = EntityUtils.toString(response.getEntity());
if (response.getCode() == HttpStatus.SC_OK) {
debugResponse(response);
log.info(json);
} else {
debugResponse(response, json);

if (response.getCode() != HttpStatus.SC_OK) {
ApiResponse apiResponse = ApiResponse.parse(json);
throw new StructurizrClientException(apiResponse.getMessage());
}
Expand All @@ -355,19 +354,29 @@ public void putWorkspace(long workspaceId, Workspace workspace) throws Structuri

private void debugRequest(HttpUriRequestBase httpRequest, String content) {
if (log.isDebugEnabled()) {
log.debug(httpRequest.getMethod() + " " + httpRequest.getPath());
log.debug("Request");
log.debug("HTTP method: " + httpRequest.getMethod());
log.debug("Path: " + httpRequest.getPath());
Header[] headers = httpRequest.getHeaders();
for (Header header : headers) {
log.debug(header.getName() + ": " + header.getValue());
log.debug("Header: " + header.getName() + "=" + header.getValue());
}
if (content != null) {
log.debug("---Start content---");
log.debug(content);
log.debug("---End content---");
}
}
}

private void debugResponse(CloseableHttpResponse response) {
log.debug(response.getCode());
private void debugResponse(CloseableHttpResponse response, String content) {
log.debug("Response");
log.debug("HTTP status code: " + response.getCode());
if (content != null) {
log.debug("---Start content---");
log.debug(content);
log.debug("---End content---");
}
}

private void addHeaders(HttpUriRequestBase httpRequest, String content, String contentType) throws Exception {
Expand Down

0 comments on commit f00c975

Please sign in to comment.