io.vrap.rmf.base.client.utils.Utils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rmf-java-base Show documentation
Show all versions of rmf-java-base Show documentation
The e-commerce SDK from commercetools Composable Commerce for Java
package io.vrap.rmf.base.client.utils;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletionException;
import java.util.function.Function;
import io.vrap.rmf.base.client.ApiHttpResponse;
import io.vrap.rmf.base.client.utils.json.JsonUtils;
public final class Utils {
public static Function wrapToCompletionException(final ExceptionalFunction exceptionalFunction) {
return u -> {
try {
return exceptionalFunction.apply(u);
}
catch (Exception e) {
throw new CompletionException(e);
}
};
}
public static String bytesToString(final byte[] input) {
if (input == null) {
return null;
}
return new String(input);
}
public static String bytesToUTF8String(final byte[] input) {
if (input == null) {
return null;
}
return new String(input, StandardCharsets.UTF_8);
}
public static ApiHttpResponse convertResponse(final ApiHttpResponse response,
final Class outputType) {
if (response.getBody() == null) {
return (ApiHttpResponse) response;
}
O newBody = JsonUtils.fromJsonByteArray(response.getBody(), outputType);
return new ApiHttpResponse<>(response.getStatusCode(), response.getHeaders(), newBody, response.getMessage(),
response.getContextMap());
}
}