package leigh.esp.sdk.link23;

import leigh.eo.EO;
import leigh.eo.io.EOInputStream;
import leigh.eo.json.JsonInput;
import org.apache.http.client.methods.CloseableHttpResponse;

import java.io.IOException;
import java.io.InputStream;

public class Link23Response implements AutoCloseable {
    private final CloseableHttpResponse response;

    @Override
    public String toString() {
        return "Link23Response{" +
                "response=" + response +
                '}';
    }

    public Link23Response(CloseableHttpResponse response) {
        this.response = response;
    }

    public InputStream getInputStream() throws IOException {
        return response.getEntity().getContent();
    }

    public String getContentType() {
        return response.getEntity().getContentType().getValue();
    }

    public EOInputStream getResponseEoStream() throws IOException {
        return new JsonInput(response.getEntity().getContent());
    }

    public EO getResponse() throws Exception {
        try (EOInputStream is = getResponseEoStream()) {
            return is.readEo();
        }
    }

    @Override
    public void close() throws Exception {
        response.close();
    }
}
