Skip to content

Commit

Permalink
Adding constants to avoid duplicated literals
Browse files Browse the repository at this point in the history
  • Loading branch information
sashirestela committed Jan 10, 2025
1 parent 5c2519b commit 72b1b9c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
public abstract class HttpClientAdapter {

private static Logger logger = LoggerFactory.getLogger(HttpClientAdapter.class);
protected static final String REQUEST_BODY_FORMAT = "Request Body : {}";
protected static final String RESPONSE_CODE_FORMAT = "Response Code : {}";
protected static final String RESPONSE_FORMAT = "Response : {}";

public Object sendRequest(RequestData originalRequest, UnaryOperator<HttpRequestData> requestInterceptor) {
var actualRequest = interceptRequest(originalRequest, requestInterceptor);
Expand Down Expand Up @@ -68,7 +71,7 @@ protected void throwExceptionIfErrorIsPresent(ResponseData response) {
} else {
data = (String) response.getBody();
}
logger.error("Response : {}", data);
logger.error(RESPONSE_FORMAT, data);
throw new CleverClientException(fillResponseInfo(response, data));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ protected Object send(RequestData request) {
var httpRequest = convertToHttpRequest(request);
try {
var httpResponse = httpClient.send(httpRequest, functions.bodyHandler.get());
logger.debug("Response Code : {}", httpResponse.statusCode());
logger.debug(RESPONSE_CODE_FORMAT, httpResponse.statusCode());
throwExceptionIfErrorIsPresent(convertToResponseData(httpResponse));
var response = httpResponse.body();
if (!returnType.isStream()) {
logger.debug("Response : {}", response);
logger.debug(RESPONSE_FORMAT, response);
}
return functions.responseConverter.apply(response, returnType);
} catch (IOException | InterruptedException e) {
Expand All @@ -69,11 +69,11 @@ protected Object sendAsync(RequestData request) {
var httpRequest = convertToHttpRequest(request);
var httpResponseFuture = httpClient.sendAsync(httpRequest, functions.bodyHandler.get());
return httpResponseFuture.thenApply(httpResponse -> {
logger.debug("Response Code : {}", httpResponse.statusCode());
logger.debug(RESPONSE_CODE_FORMAT, httpResponse.statusCode());
throwExceptionIfErrorIsPresent(convertToResponseData(httpResponse));
var response = httpResponse.body();
if (!returnType.isStream()) {
logger.debug("Response : {}", response);
logger.debug(RESPONSE_FORMAT, response);
}
return functions.responseConverter.apply(response, returnType);
});
Expand Down Expand Up @@ -103,14 +103,14 @@ private HttpRequest convertToHttpRequest(RequestData request) {
private BodyPublisher createBodyPublisher(Object bodyObject, ContentType contentType) {
BodyPublisher bodyPublisher = null;
if (contentType == null) {
logger.debug("Request Body : (Empty)");
logger.debug(REQUEST_BODY_FORMAT, "(Empty)");
bodyPublisher = BodyPublishers.noBody();
} else if (contentType == ContentType.MULTIPART_FORMDATA) {
logger.debug("Request Body : {}", bodyObject);
logger.debug(REQUEST_BODY_FORMAT, bodyObject);
var bodyBytes = HttpMultipart.toByteArrays((Map<String, Object>) bodyObject);
bodyPublisher = BodyPublishers.ofByteArrays(bodyBytes);
} else if (contentType == ContentType.APPLICATION_JSON) {
logger.debug("Request Body : {}", bodyObject);
logger.debug(REQUEST_BODY_FORMAT, bodyObject);
bodyPublisher = BodyPublishers.ofString((String) bodyObject);
}
return bodyPublisher;
Expand Down Expand Up @@ -194,7 +194,7 @@ private Stream<?> convertToStreamOfObjects(Stream<String> response, ReturnType r
final var lineRecord = new CleverClientSSE.LineRecord();
return response
.map(line -> {
logger.debug("Response : {}", line);
logger.debug(RESPONSE_FORMAT, line);
lineRecord.updateWith(line);
return new CleverClientSSE(lineRecord);
})
Expand All @@ -208,7 +208,7 @@ private Stream<?> convertToStreamOfEvents(Stream<String> response, ReturnType re

return response
.map(line -> {
logger.debug("Response : {}", line);
logger.debug(RESPONSE_FORMAT, line);
lineRecord.updateWith(line);
return new CleverClientSSE(lineRecord, events);
})
Expand Down

0 comments on commit 72b1b9c

Please sign in to comment.