Move fixtures to gist
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
package de.jeyp91.gists;
|
||||
|
||||
import com.google.api.client.util.DateTime;
|
||||
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.client.methods.HttpPatch;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
@@ -15,6 +17,7 @@ import org.json.simple.parser.ParseException;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.Set;
|
||||
@@ -33,8 +36,94 @@ public class GistProvider {
|
||||
return stringToJSONArray(gistsRaw);
|
||||
}
|
||||
|
||||
public String getTimestamp() {
|
||||
return "";
|
||||
public DateTime getGistUpdatedTimestamp(String gistDescription) {
|
||||
DateTime updated = null;
|
||||
for (Object o : this.gistList) {
|
||||
JSONObject gist = (JSONObject) o;
|
||||
String desc = gist.get("description").toString();
|
||||
if (gistDescription.equals(desc)) {
|
||||
updated = new DateTime(gist.get("updated_at").toString());
|
||||
}
|
||||
}
|
||||
return updated;
|
||||
}
|
||||
|
||||
public String getGistID(String gistDescription) {
|
||||
String id = null;
|
||||
for (Object o : this.gistList) {
|
||||
JSONObject gist = (JSONObject) o;
|
||||
String desc = gist.get("description").toString();
|
||||
if (gistDescription.equals(desc)) {
|
||||
id = gist.get("id").toString();
|
||||
}
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
public String updateGist(String id, String file, String content) throws UnsupportedEncodingException {
|
||||
String requestUrl = "https://api.github.com/gists/" + id;
|
||||
String username = "tippligawuerzburg";
|
||||
String password = "1154f448eae29212971cb82ca8850f54bff23629";
|
||||
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();
|
||||
HttpPatch request = new HttpPatch(requestUrl);
|
||||
request.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
|
||||
request.setHeader(HttpHeaders.ACCEPT, "application/vnd.github.v3+json");
|
||||
|
||||
JSONObject contentObject = new JSONObject();
|
||||
contentObject.put("content", content);
|
||||
JSONObject fileObject = new JSONObject();
|
||||
fileObject.put(file, contentObject);
|
||||
JSONObject filesObject = new JSONObject();
|
||||
filesObject.put("files", fileObject);
|
||||
|
||||
String bodyString = filesObject.toString();
|
||||
|
||||
StringEntity body = new StringEntity(filesObject.toString());
|
||||
body.setContentType("application/json");
|
||||
body.setContentEncoding("UTF-8");
|
||||
request.setEntity(body);
|
||||
|
||||
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(), StandardCharsets.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();
|
||||
}
|
||||
|
||||
public JSONArray getTeamIdMatcher() {
|
||||
@@ -68,8 +157,8 @@ public class GistProvider {
|
||||
|
||||
private String getRawData(String requestUrl) {
|
||||
|
||||
String username = "jeyp91";
|
||||
String password = "e73485180c52e66e7e6f5550bb53aae3ff5c745a";
|
||||
String username = "tippligawuerzburg";
|
||||
String password = "1154f448eae29212971cb82ca8850f54bff23629";
|
||||
String auth = username + ":" + password;
|
||||
byte[] encodedAuth = Base64.encodeBase64(
|
||||
auth.getBytes(StandardCharsets.ISO_8859_1));
|
||||
|
||||
Reference in New Issue
Block a user