diff --git a/src/main/java/de/jeyp91/gists/GistProvider.java b/src/main/java/de/jeyp91/gists/GistProvider.java index e9186f7..a0b9436 100644 --- a/src/main/java/de/jeyp91/gists/GistProvider.java +++ b/src/main/java/de/jeyp91/gists/GistProvider.java @@ -1,6 +1,7 @@ package de.jeyp91.gists; import com.google.api.client.util.DateTime; +import com.google.common.io.Resources; import org.apache.commons.codec.binary.Base64; import org.apache.http.HttpHeaders; import org.apache.http.HttpResponse; @@ -18,16 +19,37 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; +import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.Set; public class GistProvider { private static GistProvider prov = null; - private JSONArray gistList = null; + private JSONArray gistList; + private String username; + private String password; private GistProvider() { this.gistList = listAllGists(); + readConfig(); + } + + private void readConfig() { + JSONParser jsonParser = new JSONParser(); + URL url = Resources.getResource("Gist_Config.json"); + JSONObject gistConfig; + + try { + String jsonConfig = Resources.toString(url, StandardCharsets.UTF_8); + //Read JSON file + gistConfig = (JSONObject) jsonParser.parse(jsonConfig); + this.username = gistConfig.get("username").toString(); + this.password = gistConfig.get("password").toString(); + + } catch (IOException | ParseException e) { + e.printStackTrace(); + } } public static GistProvider getInstance() { @@ -68,9 +90,7 @@ public class GistProvider { 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; + String auth = this.username + ":" + this.password; byte[] encodedAuth = Base64.encodeBase64( auth.getBytes(StandardCharsets.ISO_8859_1)); String authHeader = "Basic " + new String(encodedAuth); @@ -157,9 +177,7 @@ public class GistProvider { private String getRawData(String requestUrl) { - String username = "tippligawuerzburg"; - String password = "1154f448eae29212971cb82ca8850f54bff23629"; - String auth = username + ":" + password; + String auth = this.username + ":" + this.password; byte[] encodedAuth = Base64.encodeBase64( auth.getBytes(StandardCharsets.ISO_8859_1)); String authHeader = "Basic " + new String(encodedAuth); diff --git a/src/main/resources/Gist_Config.json b/src/main/resources/Gist_Config.json new file mode 100644 index 0000000..9f4d963 --- /dev/null +++ b/src/main/resources/Gist_Config.json @@ -0,0 +1,4 @@ +{ + "username": "tippligawuerzburg", + "password": "1154f448eae29212971cb82ca8850f54bff23629" +} \ No newline at end of file