All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vrap.rmf.base.client.utils.Utils Maven / Gradle / Ivy

There is a newer version: 17.17.0
Show newest version

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 || response.getBody().length == 0) {
            return (ApiHttpResponse) response;
        }
        O newBody = JsonUtils.fromJsonByteArray(response.getBody(), outputType);
        return new ApiHttpResponse<>(response.getStatusCode(), response.getHeaders(), newBody, response.getMessage(),
            response.getContextMap());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy