Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Added possible Byte-Array serialization #25

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update Copyright

Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,21 @@ public static <L, R> CsResponseHandler<L, R> of(
public CsResponseEntity<L, R> handleResponse(HttpResponse response) throws IOException {
int status = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
String contentString = entity != null ? EntityUtils.toString(entity) : null;

if (status >= 200 && status < 300) {
if (this.successType.equals(byte[].class)) {
// for TupleLists serialized as Bytes
byte[] contentByteArray = entity != null ? EntityUtils.toByteArray(entity) : null;
return (CsResponseEntity<L, R>) CsResponseEntity.success(status, contentByteArray);
}
String contentString = entity != null ? EntityUtils.toString(entity) : null;
return CsResponseEntity.success(
status,
contentString != null && !contentString.isEmpty()
? OBJECT_MAPPER.readValue(contentString, successType)
: null);
} else {
String contentString = entity != null ? EntityUtils.toString(entity) : null;
log.debug(String.format("Request failed with status code <%s>: %s", status, contentString));
return CsResponseEntity.failed(
status,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
private static final Random RANDOM = new Random();
private static final CsResponseHandler<String, String> RESPONSE_HANDLER =
CsResponseHandler.of(String.class, String.class);
private static final CsResponseHandler<String, byte[]> OCTET_RESPONSE_HANDLER =
CsResponseHandler.of(String.class, byte[].class);

private static HttpResponse getHttpResponseForObject(int httpStatus, Object obj) {
StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1, httpStatus, null);
Expand All @@ -48,6 +50,16 @@
return response;
}

private static HttpResponse getHttpResponseForByteArr(int httpStatus, byte[] bytes) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why abbreviate "array"?

StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1, httpStatus, null);
HttpResponse response = new BasicHttpResponse(statusLine);
BasicHttpEntity httpEntity = new BasicHttpEntity();
httpEntity.setContent(new ByteArrayInputStream(bytes));
httpEntity.setContentLength(bytes.length);
response.setEntity(httpEntity);
return response;
}

@SuppressWarnings("unchecked")
private static <E extends Throwable> String writeObjectAsJsonString(Object obj) throws E {
try {
Expand All @@ -57,6 +69,17 @@
}
}

@Test
public void givenSuccessfulRequestForTupleList_whenHandlingResponse_thenReturnSuccesful()

Check notice on line 73 in src/test/java/io/carbynestack/httpclient/CsResponseHandlerTest.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/test/java/io/carbynestack/httpclient/CsResponseHandlerTest.java#L73

The JUnit 5 test method name 'givenSuccessfulRequestForTupleList_whenHandlingResponse_thenReturnSuccesful' doesn't match '[a-z][a-zA-Z0-9]*'
throws IOException {
byte[] content = new byte[] {1, 2, 3, 5, 3, 5, 6};
int httpStatus = HttpStatus.SC_OK;
HttpResponse httpResponse = getHttpResponseForByteArr(httpStatus, content);
CsResponseEntity<String, byte[]> actualResponse =
OCTET_RESPONSE_HANDLER.handleResponse(httpResponse);
assertThat(actualResponse.get()).isEqualTo(content);
}

@Test
public void givenSuccessfulRequestWithValidBody_whenHandlingResponse_thenReturnSuccessful()
throws IOException {
Expand Down
Loading