Search in sources :

Example 1 with NetPostException

use of org.monarchinitiative.loinc2hpo.exception.NetPostException in project loinc2hpo by monarch-initiative.

the class GitHubPoster method postIssue.

public void postIssue() throws Exception {
    URL url = new URL("https://api.github.com/repos/obophenotype/human-phenotype-ontology/issues");
    URLConnection con = url.openConnection();
    String userpass = String.format("%s:%s", username, password);
    String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());
    HttpURLConnection http = (HttpURLConnection) con;
    // PUT is another valid option
    http.setRequestMethod("POST");
    http.setDoOutput(true);
    byte[] out = payload.toString().getBytes(StandardCharsets.UTF_8);
    int length = out.length;
    http.setFixedLengthStreamingMode(length);
    http.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
    http.setRequestProperty("Authorization", basicAuth);
    http.connect();
    try (OutputStream os = http.getOutputStream()) {
        os.write(out);
        os.close();
    }
    if (http.getResponseCode() == 400) {
        String erro = String.format("URL:%s\nPayload=%s\nServer response: %s [%d]", http.toString(), payload, http.getResponseMessage(), http.getResponseCode());
        throw new NetPostException(erro);
    } else {
        this.response = http.getResponseMessage();
        this.responsecode = http.getResponseCode();
    }
}
Also used : NetPostException(org.monarchinitiative.loinc2hpo.exception.NetPostException) HttpURLConnection(java.net.HttpURLConnection) OutputStream(java.io.OutputStream) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection)

Example 2 with NetPostException

use of org.monarchinitiative.loinc2hpo.exception.NetPostException in project loinc2hpo by monarch-initiative.

the class AnnotateTabController method postGitHubIssue.

private void postGitHubIssue(String message, String title, String uname, String pword, List<String> chosenLabels) {
    GitHubPoster poster = new GitHubPoster(uname, pword, title, message);
    this.githubUsername = uname;
    this.githubPassword = pword;
    if (chosenLabels != null && !chosenLabels.isEmpty()) {
        logger.trace("Labels being chosen: ");
        chosenLabels.forEach(logger::trace);
        poster.setLabel(chosenLabels);
        logger.trace("Labels sent to poster: \t");
        logger.trace(poster.debugLabelsArray4Json());
    }
    try {
        logger.trace("Message sent to Github: \t" + poster.debugReformatpayloadWithLabel());
        poster.postIssue();
    } catch (NetPostException he) {
        PopUps.showException("GitHub error", "Bad Request (400): Could not post issue", he);
    } catch (Exception ex) {
        PopUps.showException("GitHub error", "GitHub error: Could not post issue", ex);
        return;
    }
    String response = poster.getHttpResponse();
    PopUps.showInfoMessage(String.format("Created issue for %s\nServer response: %s", loincIdSelected.toString(), response), "Created new issue");
}
Also used : NetPostException(org.monarchinitiative.loinc2hpo.exception.NetPostException) GitHubPoster(org.monarchinitiative.loinc2hpo.github.GitHubPoster) MalformedLoincCodeException(org.monarchinitiative.loinc2hpo.exception.MalformedLoincCodeException) LoincCodeNotFoundException(org.monarchinitiative.loinc2hpo.exception.LoincCodeNotFoundException) NetPostException(org.monarchinitiative.loinc2hpo.exception.NetPostException)

Aggregations

NetPostException (org.monarchinitiative.loinc2hpo.exception.NetPostException)2 OutputStream (java.io.OutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 LoincCodeNotFoundException (org.monarchinitiative.loinc2hpo.exception.LoincCodeNotFoundException)1 MalformedLoincCodeException (org.monarchinitiative.loinc2hpo.exception.MalformedLoincCodeException)1 GitHubPoster (org.monarchinitiative.loinc2hpo.github.GitHubPoster)1