package cz.moneta.test.harness.connectors.rest;

import org.apache.commons.lang3.tuple.Pair;

import jakarta.ws.rs.InternalServerErrorException;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.core.Response;
import java.util.Map;

/**
 * Rest connector returning extendable object. Now the response contains body and response headers.
 */
public class ExtendedRestConnector extends SimpleRestConnector implements RestConnector {

    public ExtendedRestConnector(String endpointUrl, String loggerName) {
        super(endpointUrl, loggerName);
    }

    public ExtendedRestConnector(String endpointUrl, String loggerName, long readTimeout) {
        super(endpointUrl, loggerName, readTimeout);
    }

    @Override
    public <T> Pair<Integer, ExtendedRestResponse<T>> postExtended(String path, Entity<?> request,
                                                                   Class<T> responseType,
                                                                   Map<String, Object> headers) {
        try {
            Response response = doPost(path, request, headers);

            return Pair.of(response.getStatus(), new ExtendedRestResponse<>(response.readEntity(responseType),
                    response.getHeaders()));
        } catch (InternalServerErrorException serverError) {
            return throwWithResponseText(serverError);
        }
    }
}


