/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dinkessample;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author adamr
 */
public class DinkesSample {

    private static final String api_bed_user = "317509";
    private static final String api_bed_key = "$$2y$10$wnudBW3apv9ECayoiML0ouTL.ZHrhDjJ576f";
private static final String url = "http://128.199.82.69/bed/api/v1/bed";
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        postBed();
        getBed();
        deleteBed();
    }

    public static void postBed() {

        try {
            String urlParameters = "kapasitas_vip=15&kapasitas_kelas_1_l=15&kapasitas_kelas_2_l=15&kapasitas_kelas_3_l=15&kapasitas_kelas_1_p=15&kapasitas_kelas_2_p=15&kapasitas_kelas_3_p=15&kapasitas_hcu=15&kapasitas_iccu=15&kapasitas_icu_negatif_ventilator=15&kapasitas_icu_negatif_tanpa_ventilator=15&kapasitas_icu_tanpa_negatif_ventilator=15&kapasitas_icu_tanpa_negatif_tanpa_ventilator=15&kapasitas_isolasi_negatif=15&kapasitas_isolasi_tanpa_negatif=15&kapasitas_nicu_covid=15&kapasitas_perina_covid=15&kapasitas_picu_covid=15&kapasitas_ok_covid=15&kapasitas_hd_covid=15&kosong_vip=15&kosong_kelas_1_l=15&kosong_kelas_2_l=15&kosong_kelas_3_l=15&kosong_kelas_1_p=15&kosong_kelas_2_p=15&kosong_kelas_3_p=15&kosong_hcu=15&kosong_iccu=15&kosong_icu_negatif_ventilator=15&kosong_icu_negatif_tanpa_ventilator=15&kosong_icu_tanpa_negatif_ventilator=15&kosong_icu_tanpa_negatif_tanpa_ventilator=15&kosong_isolasi_negatif=15&kosong_isolasi_tanpa_negatif=15&kosong_nicu_covid=15&kosong_perina_covid=15&kosong_picu_covid=15&kosong_ok_covid=15&kosong_hd_covid=15";
            byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
            int postDataLength = postData.length;

            URL website = new URL(url);

            HttpURLConnection conn = (HttpURLConnection) website.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Api-Bed-User", api_bed_user);
            conn.setRequestProperty("Api-Bed-Key", api_bed_key);
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length", Integer.toString(postDataLength));
            conn.setRequestProperty("Accept", "application/json");
            conn.setUseCaches(false);

            OutputStream os = conn.getOutputStream();
            os.write(postData);
            os.flush();

            if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED
                    && conn.getResponseCode() != HttpURLConnection.HTTP_OK) {

                try {
                    BufferedReader in = new BufferedReader(new InputStreamReader(
                            (conn.getErrorStream())));

                    StringBuilder resp = new StringBuilder();
                    String inputLine;

                    while ((inputLine = in.readLine()) != null) {
                        resp.append(inputLine);
                    }
                    in.close();
                    
                    System.out.println(resp);
                } catch (Exception e) {
                    throw new RuntimeException("Failed : HTTP error code : "
                            + conn.getResponseMessage());
                }
            } else {
                BufferedReader in = new BufferedReader(new InputStreamReader(
                        (conn.getInputStream())));

                StringBuilder resp = new StringBuilder();
                String inputLine;

                while ((inputLine = in.readLine()) != null) {
                    resp.append(inputLine);
                }

                in.close();

                System.out.println(resp);
            }
        } catch (Exception ex) {
            Logger.getLogger(DinkesSample.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void getBed() {

        try {

            URL website = new URL(url);

            HttpURLConnection conn = (HttpURLConnection) website.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Api-Bed-User", api_bed_user);
            conn.setRequestProperty("Api-Bed-Key", api_bed_key);
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Accept", "application/json");
            conn.setUseCaches(false);

            if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED
                    && conn.getResponseCode() != HttpURLConnection.HTTP_OK) {

                try {
                    BufferedReader in = new BufferedReader(new InputStreamReader(
                            (conn.getErrorStream())));

                    StringBuilder resp = new StringBuilder();
                    String inputLine;

                    while ((inputLine = in.readLine()) != null) {
                        resp.append(inputLine);
                    }
                    in.close();
                    
                    System.out.println(resp);
                } catch (Exception e) {
                    throw new RuntimeException("Failed : HTTP error code : "
                            + conn.getResponseMessage());
                }
            } else {
                BufferedReader in = new BufferedReader(new InputStreamReader(
                        (conn.getInputStream())));

                StringBuilder resp = new StringBuilder();
                String inputLine;

                while ((inputLine = in.readLine()) != null) {
                    resp.append(inputLine);
                }

                in.close();

                System.out.println(resp);
            }
        } catch (Exception ex) {
            Logger.getLogger(DinkesSample.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static void deleteBed() {

        try {

            URL website = new URL(url);

            HttpURLConnection conn = (HttpURLConnection) website.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("DELETE");
            conn.setRequestProperty("Api-Bed-User", api_bed_user);
            conn.setRequestProperty("Api-Bed-Key", api_bed_key);
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Accept", "application/json");
            conn.setUseCaches(false);

            if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED
                    && conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                try {
                    BufferedReader in = new BufferedReader(new InputStreamReader(
                            (conn.getErrorStream())));

                    StringBuilder resp = new StringBuilder();
                    String inputLine;

                    while ((inputLine = in.readLine()) != null) {
                        resp.append(inputLine);
                    }
                    in.close();
                    
                    System.out.println(resp);
                } catch (Exception e) {
                    throw new RuntimeException("Failed : HTTP error code : "
                            + conn.getResponseMessage());
                }
            } else {
                BufferedReader in = new BufferedReader(new InputStreamReader(
                        (conn.getInputStream())));

                StringBuilder resp = new StringBuilder();
                String inputLine;

                while ((inputLine = in.readLine()) != null) {
                    resp.append(inputLine);
                }

                in.close();

                System.out.println(resp);
            }
        } catch (Exception ex) {
            Logger.getLogger(DinkesSample.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
