Call GitHub's Rest API from Java's Socket API

At the beginning

I tried calling Rest API on GitHub from Java's Socket API

Reference site

Source

Test.java


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;

public class Test {
	//Get GitHub repository information using GitHub REST API v3
	//The format of REST API v3 is
	// /repos/:owner/:repo/contents/:path
	//Becomes
	private static final String GITHUB_RESTAPI_PATH = "https://api.github.com/repos/triple4649/MLLearning_Java/contents/src";
	public static void main(String[] args) throws Exception {

		HttpsURLConnection con = createHttpsURLConnection(GITHUB_RESTAPI_PATH);

		//Output header information
		System.out.println("----- Headers -----");
		printHeaderFields(con);
		
		//Output Body information
		System.out.println("----- Body -----");
		printBody(con);
		

		con.disconnect();
	}
	
	//Generate a URLConnection for a TLS connection(Unverified version of certification)
	private static HttpsURLConnection  createHttpsURLConnection (String path) throws Exception{
		SSLSocketFactory factory = null;
		SSLContext ctx = SSLContext.getInstance("TLS");
		ctx.init(null, new NonAuthentication[] { new NonAuthentication() },
				null);
		factory = ctx.getSocketFactory();

		URL url = new URL(path);
		HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
		con.setSSLSocketFactory(factory);
		
		return con;
	}
	
	//Output header information
	private static void printHeaderFields(HttpsURLConnection con){
		con.getHeaderFields()
		.entrySet()
		.stream()
		.map(e->String.format("key:%s value:%s", e.getKey(),e.getValue()))
		.forEach(System.out::println);
	}
	
	//Output Body information
	private static void printBody(HttpsURLConnection con) throws Exception{
		new BufferedReader(new InputStreamReader(
				con.getInputStream()))
		.lines()
		.forEach(System.out::println);

	}
}

class NonAuthentication implements X509TrustManager {
	@Override
	public void checkClientTrusted(X509Certificate[] chain, String authType)
			throws CertificateException {
	}

	@Override
	public void checkServerTrusted(X509Certificate[] chain, String authType)
			throws CertificateException {
	}

	@Override
	public X509Certificate[] getAcceptedIssuers() {
		return null;
	}
}

Information output to Body information

You can get GitHub repository information in JSON format as shown below.

[{"name":"base64",
"path":"src/base64",
"sha":"7fe220c6549e5c90c49a992c8524688122609944",
"size":0,
"url":"https://api.github.com/repos/triple4649/MLLearning_Java/contents/src/base64?ref=master",
"html_url":"https://github.com/triple4649/MLLearning_Java/tree/master/src/base64",
"git_url":"https://api.github.com/repos/triple4649/MLLearning_Java/git/trees/7fe220c6549e5c90c49a992c8524688122609944",
"download_url":null,
"type":"dir",
"_links":{"self":"https://api.github.com/repos/triple4649/MLLearning_Java/contents/src/base64?ref=master",
"git":"https://api.github.com/repos/triple4649/MLLearning_Java/git/trees/7fe220c6549e5c90c49a992c8524688122609944",
"html":"https://github.com/triple4649/MLLearning_Java/tree/master/src/base64"}},

Recommended Posts

Call GitHub's Rest API from Java's Socket API
Call GitHub API from Java Socket API part2
Call TensorFlow Java API from Scala
Call API [Call]
Hit the Salesforce REST API from Java
Call API [Preparation]
Call API [Handling]
Call Java from JRuby
Call API [Core Edition]