package org.shipkit.internal.util;

import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.shipkit.internal.notes.util.IOUtil;

import javax.net.ssl.HttpsURLConnection;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Optional;

/**
 * Wrapper for making REST requests to GitHub API
 */
public class GitHubApi {

    private static final Logger LOG = Logging.getLogger(GitHubApi.class);

    private final String gitHubApiUrl;
    private final String authToken;

    public GitHubApi(String gitHubApiUrl, String authToken) {
        this.gitHubApiUrl = gitHubApiUrl;
        this.authToken = authToken;
    }

    public String post(String relativeUrl, String body) throws IOException {
        return doRequest(relativeUrl, "POST", Optional.of(body));
    }

    public String put(String relativeUrl, String body) throws IOException {
        return doRequest(relativeUrl, "PUT", Optional.of(body));
    }

    public String get(String relativeUrl) throws IOException {
        return doRequest(relativeUrl, "GET", Optional.empty());
    }

    public String patch(String relativeUrl, String body) throws IOException {
        return doRequest(relativeUrl, "PATCH", Optional.of(body));
    }

    public String delete(String relativeUrl) throws IOException {
        return doRequest(relativeUrl, "DELETE", Optional.empty());
    }

    private String doRequest(String relativeUrl, String method, Optional<String> body) throws IOException {
        URL url = new URL(gitHubApiUrl + relativeUrl);

        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setRequestMethod(method);
        conn.setDoOutput(true);
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Authorization", "token " + authToken);

        if (body.isPresent()) {
            try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
                wr.writeBytes(body.get());
                wr.flush();
            }
        }

        return call(method, conn);
    }

    private String call(String method, HttpsURLConnection conn) throws IOException {
        LOG.info("  Calling {} {}. Turn on debug logging to see response headers.", method, conn.getURL());

        if (conn.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) {
            return IOUtil.readFully(conn.getInputStream());
        } else {
            String errorMessage =
                String.format("%s %s failed, response code = %s, response body:\n%s",
                    method, conn.getURL(), conn.getResponseCode(), IOUtil.readFully(conn.getErrorStream()));
            throw new IOException(errorMessage);
        }
    }
}

@@ -210,32 +210,27 @@
 java.io.
-DataOutputStream
+IOException
 ;%0Aimport
@@ -234,35 +234,36 @@
 ort java.io.
-IOException
+OutputStream
 ;%0Aimport jav
@@ -308,16 +308,49 @@
 et.URL;%0A
+import java.nio.charset.Charset;%0A
 import j
@@ -1977,17 +1977,8 @@
     
-try (Data
 Outp
@@ -1990,34 +1990,13 @@
 eam 
-wr
+os
  = 
-new DataOutputStream(
 conn
@@ -2013,20 +2013,17 @@
 Stream()
-)) %7B
+;
 %0A       
@@ -2031,25 +2031,16 @@
     
-    wr
+os
 .write
-Bytes
 (bod
@@ -2046,16 +2046,51 @@
 dy.get()
+.getBytes(Charset.forName(%22UTF-8%22))
 );%0A     
@@ -2100,14 +2100,10 @@
     
-  
-  wr
+os
 .flu
@@ -2112,22 +2112,8 @@
 ();%0A
-            %7D%0A
     
@@ -2803,8 +2803,126 @@
     %7D%0A%7D%0A
+//https://api.github.com/repos/mstachniuk/mockito/releases%0A//https://api.github.com/repos/mstachniuk/mockito/releases%0A

Fix GitHub release notes encoding