package com.sourcefellows.resillience;

import org.apache.hc.client5.http.config.ConnectionConfig;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.BasicHttpClientConnectionManager;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.support.ClassicRequestBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

public class Main {

    private static final Logger logger = LoggerFactory.getLogger(Main.class);

    public static void main(String[] args) throws Exception {

        System.out.println("starting sys");
        logger.info("starting");

        var connConfig = ConnectionConfig.custom()
                .setConnectTimeout(200, TimeUnit.MILLISECONDS)
                .setSocketTimeout(200, TimeUnit.MILLISECONDS)
                .build();

        var conMan = new BasicHttpClientConnectionManager();
        conMan.setConnectionConfig(connConfig);

        try (var httpclient = HttpClients.custom().setConnectionManager(conMan).build()) {
            ClassicHttpRequest httpGet = ClassicRequestBuilder.get("http://localhost:13131/get")
                    .build();
            httpclient.execute(httpGet, response -> {
                System.out.println(response.getCode() + " " + response.getReasonPhrase());
                final HttpEntity entity1 = response.getEntity();
                EntityUtils.consume(entity1);
                return null;
            });

        }
    }

}
