package com.tisawesomeness.minecord.network;

import lombok.Getter;
import lombok.NonNull;
import okhttp3.ConnectionPool;
import okhttp3.Dispatcher;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.net.URL;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * A simplified HTTP client that can be used to communicate with web JSON APIs.
 */
public class APIClient {

    private static final int MAX_REQUESTS_PER_HOST = 25;
    /**
     * The builder used to construct a {@link OkHttpClient} instance
     */
    @Getter private final OkHttpClient.Builder httpClientBuilder;
    private final Dispatcher dispatcher;
    private final ConnectionPool connectionPool;

    /**
     * Creates a new API client with the default settings.
     */
    public APIClient() {
        // client is set to JDA defaults
        dispatcher = new Dispatcher();
        dispatcher.setMaxRequestsPerHost(MAX_REQUESTS_PER_HOST);
        connectionPool = new ConnectionPool(5, 10, TimeUnit.SECONDS);
        httpClientBuilder = new OkHttpClient.Builder()
                .connectionPool(connectionPool)
                .dispatcher(dispatcher);
    }

    /**
     * Performs a GET request.
     * @param url The URL to send a GET request to
     * @return The response of the request, which may be successful or unsuccessful
     * @throws IOException If an I/O error occurs
     */
    public @NonNull Response get(@NonNull URL url) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .header("Content-Type", "application/json")
                .build();
        OkHttpClient client = httpClientBuilder.build();
        return Objects.requireNonNull(client.newCall(request).execute());
    }

    public int getQueuedCallsCount() {
        return dispatcher.queuedCallsCount();
    }
    public int getRunningCallsCount() {
        return dispatcher.runningCallsCount();
    }
    public int getIdleConnectionCount() {
        return connectionPool.idleConnectionCount();
    }
    public int getConnectionCount() {
        return connectionPool.connectionCount();
    }

    public void close() {
        connectionPool.evictAll();
        dispatcher.executorService().shutdown();
    }

}

@@ -103,127 +103,9 @@
 tp3.
-ConnectionPool;%0Aimport okhttp3.Dispatcher;%0Aimport okhttp3.OkHttpClient;%0Aimport okhttp3.Request;%0Aimport okhttp3.Response
+*
 ;%0A%0Ai
@@ -156,34 +156,8 @@
 RL;%0A
-import java.util.Objects;%0A
 impo
@@ -1100,575 +1100,1470 @@
 s a 
-GET request.%0A     * @param url The URL to send a GET request to%0A     * @return The response of the request, which may be successful or unsuccessful%0A     * @throws IOException If an I/O error occurs%0A     */%0A    public @NonNull Response get(@NonNull URL url) throws IOException %7B%0A        Request request = new Request.Builder()%0A                .url(url)%0A                .header(%22Content-Type%22, %22application/json%22)%0A                .build();%0A        OkHttpClient client = httpClientBuilder.build();%0A        return Objects.requireNonNull(client.newCall(request).execute())
+HEAD request.%0A     * @param url The URL to send a HEAD request to%0A     * @return The response of the request, which may be successful or unsuccessful%0A     * @throws IOException If an I/O error occurs%0A     */%0A    public @NonNull Response head(@NonNull URL url) throws IOException %7B%0A        Request request = new Request.Builder()%0A                .url(url)%0A                .head()%0A                .build();%0A        return dispatch(request);%0A    %7D%0A%0A    /**%0A     * Performs a GET request.%0A     * @param url The URL to send a GET request to%0A     * @return The response of the request, which may be successful or unsuccessful%0A     * @throws IOException If an I/O error occurs%0A     */%0A    public @NonNull Response get(@NonNull URL url) throws IOException %7B%0A        Request request = new Request.Builder()%0A                .url(url)%0A                .header(%22Content-Type%22, %22application/json%22)%0A                .build();%0A        return dispatch(request);%0A    %7D%0A%0A    private @NonNull Response dispatch(@NonNull Request request) throws IOException %7B%0A        OkHttpClient client = httpClientBuilder.build();%0A        return client.newCall(request).execute();%0A    %7D%0A%0A    /**%0A     * Checks if a URL exists and is responsive.%0A     * @param url The URL to request%0A     * @return Whether the URL is responsive%0A     * @throws IOException If an I/O error occurs%0A     */%0A    public boolean exists(@NonNull URL url) throws IOException %7B%0A        return head(url).code() == StatusCodes.OK
 ;%0A  
@@ -2938,32 +2938,103 @@
 Count();%0A    %7D%0A%0A
+    /**%0A     * Closes this client and all current connections.%0A     */%0A
     public void 

Add code to check if URL exists