import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

public class FetchDataTutorial {
    // Replace with your HostMyApi Data Table URL
    public static final String HOST_MY_API_URL = "https://api.hostmy.io/v2/tables/T-36996afd-1f4f-48bf-8743-4ba85ac2ed43";
    // Replace with your HostMyApi Access Key
    public static final String HOST_MY_API_API_KEY = "K-f96ff8b4-3f1c-46fa-bf2a-a9b7464c414f";

    public static void main(String[] args) {
        try {
            var url = URI.create(HOST_MY_API_URL).toURL();
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // Set Header with the HostMyApi Access Key
            connection.setRequestProperty("X-API-KEY", HOST_MY_API_API_KEY);
            // Set GET request
            connection.setRequestMethod("GET");

            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) { // success
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    System.out.println(inputLine);
                }
                in.close();
                connection.disconnect();
            } else {
                System.out.println("HostMyApi Data request has not worked");
                System.out.println("status: " + responseCode);
                System.out.println("error: \"" + "\"");
                System.out.println("message: \"" + "\"");
                System.out.println("path: \"" + url.getPath() + "\"");
                /**
                 * Try to emulate Curl error response:
                 * timestamp: 1723546524584
                 * status: 400
                 * error: "Bad Request"
                 * message: "You must provide an api key for this request" from HTTP Status Message
                 * path: "/v2/tables/T-c4b024cf-ad56-4b93-a4c7-ccfa501c3929"
                 */
                System.out.println("GET request not worked");
                BufferedReader errorReader = new BufferedReader(new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8));
                String errorLine;
                StringBuilder errorContent = new StringBuilder();
                while ((errorLine = errorReader.readLine()) != null) {
                    errorContent.append(errorLine);
                    System.out.println(errorLine);
                }
                errorReader.close();
                System.out.println("Error Response: " + errorContent.toString());


                Map<String, List<String>> map = connection.getHeaderFields();
                System.out.println("Printing All Response Header for URL: " + HOST_MY_API_URL + "\n");
                for (Map.Entry<String, List<String>> entry : map.entrySet()) {
                    System.out.println(entry.getKey() + " : " + entry.getValue());
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
