Created
November 10, 2017 15:09
-
-
Save jkutner/e516e21f41618f965a6546cdfe3e6386 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.net.ssl.HttpsURLConnection; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.URL; | |
import sun.misc.BASE64Encoder; | |
public class Sources { | |
private static final String URL_STRING = "https://api.heroku.com/sources"; | |
private URL url; | |
public static void main(String[] args) throws Exception { | |
new Sources("").post(); | |
} | |
public Sources(String rawApiKey) throws IOException { | |
this.url = new URL(URL_STRING); | |
} | |
public void post() throws Exception { | |
HttpsURLConnection con = (HttpsURLConnection) this.url.openConnection(); | |
con.setRequestMethod("POST"); | |
con.setRequestProperty("Authorization", encodeApiKey()); | |
con.setRequestProperty("Content-Type", "application/json"); | |
con.setRequestProperty("Accept", "application/vnd.heroku+json; version=3"); | |
con.setDoOutput(true); | |
System.out.println("responseCode=" + con.getResponseCode() ); | |
System.out.println("requestMethod=" + con.getRequestMethod()); | |
System.out.println("cipherSuite=" + con.getCipherSuite()); | |
String[] headers = new String[]{ | |
"Request-Id", "Content-Length", "Content-Type", "Ratelimit-Multiplier", "Ratelimit-Remaining" | |
}; | |
for (String headerName : headers) { | |
System.out.println(headerName + ": " + con.getHeaderField(headerName)); | |
} | |
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); | |
String inputLine; | |
StringBuilder response = new StringBuilder(); | |
while ((inputLine = in.readLine()) != null) { | |
response.append(inputLine); | |
} | |
in.close(); | |
System.out.println("response=" + response ); | |
} | |
private String encodeApiKey() throws IOException { | |
String apiKey = System.getenv("HEROKU_API_KEY"); | |
if (null == apiKey || apiKey.equals("")) { | |
System.out.println("apiKeySource=CLI"); | |
ProcessBuilder pb = new ProcessBuilder().command("heroku", "auth:token"); | |
Process p = pb.start(); | |
BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream())); | |
String line; | |
StringBuilder rawApiKey = new StringBuilder(); | |
while ((line = bri.readLine()) != null) { | |
rawApiKey.append(line); | |
} | |
apiKey = rawApiKey.toString(); | |
} else { | |
System.out.println("apiKeySource=HEROKU_API_KEY"); | |
} | |
return new BASE64Encoder().encode((":" + apiKey).getBytes()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment