Add support for gist
This commit is contained in:
@@ -1,2 +1,132 @@
|
||||
package de.jeyp91.gists;public class GistProvider {
|
||||
package de.jeyp91.gists;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.http.HttpHeaders;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.Set;
|
||||
|
||||
public class GistProvider {
|
||||
|
||||
private JSONArray gistList = null;
|
||||
public HashMap<String, String> files = new HashMap<>();
|
||||
|
||||
public GistProvider() {
|
||||
this.gistList = listAllGists();
|
||||
}
|
||||
|
||||
public JSONArray listAllGists() {
|
||||
String gistsRaw = getRawData("https://api.github.com/gists");
|
||||
return stringToJSONArray(gistsRaw);
|
||||
}
|
||||
|
||||
public String getTimestamp() {
|
||||
return "";
|
||||
}
|
||||
|
||||
public JSONArray getTeamIdMatcher() {
|
||||
return stringToJSONArray(getFile("Team_ID_Matcher.json"));
|
||||
}
|
||||
|
||||
public String getFile(String filename) {
|
||||
if(!this.files.containsKey(filename)) {
|
||||
files.put(filename, getFileFromGist(filename));
|
||||
}
|
||||
return this.files.get(filename);
|
||||
}
|
||||
|
||||
private String getFileFromGist(String filename) {
|
||||
String fileurl = null;
|
||||
for (Object o : this.gistList) {
|
||||
JSONObject gist = (JSONObject) o;
|
||||
JSONObject files = (JSONObject)gist.get("files");
|
||||
Set<String> filenames = files.keySet();
|
||||
if(filenames.contains(filename)) {
|
||||
fileurl = ((JSONObject)files.get(filename)).get("raw_url").toString();
|
||||
}
|
||||
}
|
||||
|
||||
String content = null;
|
||||
if(fileurl != null) {
|
||||
content = getRawData(fileurl);
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
private String getRawData(String requestUrl) {
|
||||
|
||||
String username = "jeyp91";
|
||||
String password = "e73485180c52e66e7e6f5550bb53aae3ff5c745a";
|
||||
String auth = username + ":" + password;
|
||||
byte[] encodedAuth = Base64.encodeBase64(
|
||||
auth.getBytes(StandardCharsets.ISO_8859_1));
|
||||
String authHeader = "Basic " + new String(encodedAuth);
|
||||
|
||||
HttpClient client = HttpClientBuilder
|
||||
.create()
|
||||
.build();
|
||||
HttpGet request = new HttpGet(requestUrl);
|
||||
request.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
|
||||
|
||||
HttpResponse response = null;
|
||||
try {
|
||||
response = client.execute(request);
|
||||
} catch (IOException e) {
|
||||
/* TODO */
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
BufferedReader rd = null;
|
||||
try {
|
||||
rd = new BufferedReader(
|
||||
new InputStreamReader(response.getEntity().getContent(), "UTF-8")
|
||||
);
|
||||
} catch (IOException e) {
|
||||
/* TODO */
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
StringBuilder result = new StringBuilder();
|
||||
String line = "";
|
||||
while (true) {
|
||||
try {
|
||||
line = rd.readLine();
|
||||
} catch (IOException e) {
|
||||
/* TODO */
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Stop reading if last line was found.
|
||||
if (line == null) break;
|
||||
|
||||
result.append(line);
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
private JSONArray stringToJSONArray(String rawData) {
|
||||
|
||||
JSONParser parser = new JSONParser();
|
||||
JSONArray data = null;
|
||||
try {
|
||||
data = (JSONArray) parser.parse(rawData);
|
||||
} catch (ParseException e) {
|
||||
/* TODO */
|
||||
e.printStackTrace();
|
||||
}
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1,28 @@
|
||||
package de.jeyp91.gists;public class GistProviderTest {
|
||||
package de.jeyp91.gists;
|
||||
|
||||
import org.json.simple.JSONArray;
|
||||
import org.junit.Test;
|
||||
|
||||
public class GistProviderTest {
|
||||
|
||||
@Test
|
||||
public void listAllGistsTest() {
|
||||
GistProvider prov = new GistProvider();
|
||||
JSONArray gists = prov.listAllGists();
|
||||
System.out.println(gists);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getFileTest() {
|
||||
GistProvider prov = new GistProvider();
|
||||
String file = prov.getFile("Team_ID_Matcher.json");
|
||||
System.out.println(file);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTeamIdMatcherTest() {
|
||||
GistProvider prov = new GistProvider();
|
||||
JSONArray matcher = prov.getTeamIdMatcher();
|
||||
System.out.println(matcher);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user