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

com.hashicorp.nomad.javasdk.ResponseAdapter Maven / Gradle / Ivy

There is a newer version: 0.11.3.0
Show newest version
package com.hashicorp.nomad.javasdk;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import javax.annotation.Nullable;
import java.io.IOException;

/**
 * Creates a specific type of {@link NomadResponse}
 * from an async-http-client response.
 *
 * This abstract class is used by package-private methods that allow endpoint implementations to be
 * consistent and concise.
 *
 * @param  type extracted from response bodies
 * @param  type of response created
 */
abstract class ResponseAdapter> {
    private final ValueExtractor valueExtractor;

    protected ResponseAdapter(@Nullable ValueExtractor valueExtractor) {
        this.valueExtractor = valueExtractor;
    }

    public R apply(HttpResponse httpResponse)
            throws IOException, ResponseParsingException, ErrorFoundInResponseEntityException {

        String rawEntity = EntityUtils.toString(httpResponse.getEntity());
        T value = valueExtractor == null
                ? null
                : valueExtractor.extractValue(rawEntity);
        return buildResponse(httpResponse, rawEntity, value);
    }

    protected abstract R buildResponse(HttpResponse httpResponse, String rawEntity, @Nullable T value);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy