package tasks.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.xmlbeans.impl.util.Base64;

import main.Config;

/**
 * Utility to query the GitHib API
 * 
 * @author Lisa Nguyen Quang Do
 */

public class Curl {

	public static String getHTML(String addr) throws Exception {
		StringBuilder result = new StringBuilder();
		URL url = new URL(addr);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();

		String userCredentials = Config.gitCredentials;
		String basicAuth = "Basic " + new String(Base64.encode(userCredentials.getBytes()));
		conn.setRequestProperty("Authorization", basicAuth);

		conn.setRequestMethod("GET");
		BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
		String line;
		while ((line = rd.readLine()) != null) {
			result.append(line);
		}

		rd.close();
		return result.toString();
	}
}
