add getting addition data from the github api
continuous-integration/drone/push Build is passing Details

This commit is contained in:
qvalentin 2022-05-17 17:37:51 +02:00
parent a03206a8e1
commit 46382334cb
Signed by: qvalentin
GPG Key ID: C979FA1EAFCABF1C
4 changed files with 225 additions and 30 deletions

View File

@ -0,0 +1,77 @@
package tag.matcherImplementations;
import datastructures.json.JSONParser;
import link.LinkUrl;
import tag.OptionalTag;
import tag.Tag;
import tag.TagMatcher;
import tag.TagName;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Map;
import java.util.Scanner;
import static java.nio.charset.StandardCharsets.UTF_8;
public class GitHubTagMatcher extends TagMatcher {
public GitHubTagMatcher() {
tagName = new TagName("github");
}
public TagName getName() {
return tagName;
}
public String getAdditionInfo(String urlPath) {
try {
URL url = new URL("https://api.github.com/repos" + urlPath);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("accept", "application/vnd.github.v3+json");
// This line makes the request
InputStream responseStream = con.getInputStream();
String text = new String(responseStream.readAllBytes(), UTF_8);
var parsedJSON = (Map<?, ?>) JSONParser.parseJSON(text);
return (String) parsedJSON.get("description");
}
catch (JSONParser.JSONParseException e) {
e.printStackTrace();
}
catch (ProtocolException e) {
return "";
}
catch (MalformedURLException e) {
return "";
}
catch (IOException e) {
return "";
}
catch (ClassCastException e) {
return "";
}
return "";
}
@Override
public OptionalTag ifMatches(LinkUrl linkUrl) {
if (linkUrl.getUrl().getHost().equals("github.com")) {
String additionInfo = getAdditionInfo(linkUrl.getUrl().getPath());
return OptionalTag.of(new Tag(getName()).addAdditionalData(additionInfo));
}
return OptionalTag.empty();
}
}

View File

@ -0,0 +1,20 @@
package tag.matcherImplementations;
import link.LinkUrl;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class GitHubTagMatcherTest {
@Test
void gettingDescriptionWorks() {
var sut = new GitHubTagMatcher();
var tag = sut.ifMatches(new LinkUrl("https://github.com/filefighter/Webapp")).getTag().orElseThrow();
assertEquals("Frontend Service for FileFighter", tag.getAdditionalData().orElseThrow());
}
}

View File

@ -1,30 +0,0 @@
package tag.matcherImplementations;
import link.LinkUrl;
import tag.OptionalTag;
import tag.Tag;
import tag.TagMatcher;
import tag.TagName;
public class GitHubTagMatcher extends TagMatcher {
public GitHubTagMatcher() {
tagName = new TagName("github");
}
public TagName getName() {
return tagName;
}
public String getAdditionInfo() {
return "";//TODO
}
@Override
public OptionalTag ifMatches(LinkUrl linkUrl) {
if (linkUrl.getUrl().getHost().equals("github.com")) {
return OptionalTag.of(new Tag(getName()).addAdditionalData(getAdditionInfo()));
}
return OptionalTag.empty();
}
}

View File

@ -0,0 +1,128 @@
package datastructures.json;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
/**
* Totally not stolen from https://github.com/KasparNagu/plain-java-json
*
*/
public class JSONParser {
public static Object parseJSONFile(String file) throws JSONParseException, IOException {
return parseJSON(Paths.get(file));
}
public static Object parseJSON(String text) throws JSONParseException {
return parseJSON(new Scanner(text));
}
public static Object parseJSON(Path file) throws JSONParseException, IOException {
return parseJSON(new Scanner(file));
}
public static Object parseJSON(Scanner s) throws JSONParseException {
Object ret = null;
skipWhitespace(s);
if (s.findWithinHorizon("\\{", 1) != null) {
HashMap<Object, Object> retMap = new HashMap<>();
ret = retMap;
skipWhitespace(s);
if (s.findWithinHorizon("\\}", 1) == null) {
while (s.hasNext()) {
Object key = parseJSON(s);
skipWhitespace(s);
if (s.findWithinHorizon(":", 1) == null) {
fail(s, ":");
}
Object value = parseJSON(s);
retMap.put(key, value);
skipWhitespace(s);
if (s.findWithinHorizon(",", 1) == null) {
break;
}
}
if (s.findWithinHorizon("\\}", 1) == null) {
fail(s, "}");
}
}
}
else if (s.findWithinHorizon("\"", 1) != null) {
ret = s.findWithinHorizon("(\\\\\\\\|\\\\\"|[^\"])*", 0)
.replace("\\\\", "\\")
.replace("\\\"", "\"");
if (s.findWithinHorizon("\"", 1) == null) {
fail(s, "quote");
}
}
else if (s.findWithinHorizon("'", 1) != null) {
ret = s.findWithinHorizon("(\\\\\\\\|\\\\'|[^'])*", 0);
if (s.findWithinHorizon("'", 1) == null) {
fail(s, "quote");
}
}
else if (s.findWithinHorizon("\\[", 1) != null) {
ArrayList<Object> retList = new ArrayList<>();
ret = retList;
skipWhitespace(s);
if (s.findWithinHorizon("\\]", 1) == null) {
while (s.hasNext()) {
retList.add(parseJSON(s));
skipWhitespace(s);
if (s.findWithinHorizon(",", 1) == null) {
break;
}
}
if (s.findWithinHorizon("\\]", 1) == null) {
fail(s, ", or ]");
}
}
}
else if (s.findWithinHorizon("true", 4) != null) {
ret = true;
}
else if (s.findWithinHorizon("false", 5) != null) {
ret = false;
}
else if (s.findWithinHorizon("null", 4) != null) {
ret = null;
}
else {
String numberStart = s.findWithinHorizon("[-0-9+eE]", 1);
if (numberStart != null) {
String numStr = numberStart + s.findWithinHorizon("[-0-9+eE.]*", 0);
if (numStr.contains(".") | numStr.contains("e")) {
ret = Double.valueOf(numStr);
}
else {
ret = Long.valueOf(numStr);
}
}
else {
throw new JSONParseException("No JSON value found. Found: " + s.findWithinHorizon(".{0,5}", 5));
}
}
return ret;
}
private static void fail(Scanner scanner, String expected) throws JSONParseException {
throw new JSONParseException("Expected " + expected + " but found:" + scanner.findWithinHorizon(".{0,5}", 5));
}
private static void skipWhitespace(Scanner s) {
s.findWithinHorizon("\\s*", 0);
}
public static class JSONParseException extends Exception {
private static final long serialVersionUID = 7078320816065460L;
public JSONParseException(String cause) {
super(cause);
}
}
}