Search in sources :

Example 1 with JsonSlurper

use of net.sf.json.groovy.JsonSlurper in project phabricator-jenkins-plugin by uber.

the class ConduitAPIClient method perform.

/**
 * Call the conduit API of Phabricator
 * @param action Name of the API call
 * @param params The data to send to Harbormaster
 * @return The result as a JSONObject
 * @throws IOException If there was a problem reading the response
 * @throws ConduitAPIException If there was an error calling conduit
 */
public JSONObject perform(String action, JSONObject params) throws IOException, ConduitAPIException {
    CloseableHttpClient client = HttpClientBuilder.create().build();
    HttpUriRequest request = createRequest(action, params);
    HttpResponse response;
    try {
        response = client.execute(request);
    } catch (ClientProtocolException e) {
        throw new ConduitAPIException(e.getMessage());
    }
    InputStream responseBody = response.getEntity().getContent();
    if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
        throw new ConduitAPIException(responseBody.toString(), response.getStatusLine().getStatusCode());
    }
    JsonSlurper jsonParser = new JsonSlurper();
    return (JSONObject) jsonParser.parse(responseBody);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) JsonSlurper(net.sf.json.groovy.JsonSlurper) JSONObject(net.sf.json.JSONObject) InputStream(java.io.InputStream) HttpResponse(org.apache.http.HttpResponse) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 2 with JsonSlurper

use of net.sf.json.groovy.JsonSlurper in project phabricator-jenkins-plugin by uber.

the class UberallsClient method getParentCoverage.

public CodeCoverageMetrics getParentCoverage(String sha) {
    if (sha == null) {
        return null;
    }
    try {
        String coverageJSON = getCoverage(sha);
        JsonSlurper jsonParser = new JsonSlurper();
        JSON responseJSON = jsonParser.parseText(coverageJSON);
        if (responseJSON instanceof JSONNull) {
            return null;
        }
        JSONObject coverage = (JSONObject) responseJSON;
        return new CodeCoverageMetrics(((Double) coverage.getDouble(PACKAGE_COVERAGE_KEY)).floatValue(), ((Double) coverage.getDouble(FILES_COVERAGE_KEY)).floatValue(), ((Double) coverage.getDouble(CLASSES_COVERAGE_KEY)).floatValue(), ((Double) coverage.getDouble(METHOD_COVERAGE_KEY)).floatValue(), ((Double) coverage.getDouble(LINE_COVERAGE_KEY)).floatValue(), ((Double) coverage.getDouble(CONDITIONAL_COVERAGE_KEY)).floatValue());
    } catch (Exception e) {
        e.printStackTrace(logger.getStream());
    }
    return null;
}
Also used : CodeCoverageMetrics(com.uber.jenkins.phabricator.coverage.CodeCoverageMetrics) JsonSlurper(net.sf.json.groovy.JsonSlurper) JSONNull(net.sf.json.JSONNull) JSONObject(net.sf.json.JSONObject) JSON(net.sf.json.JSON) ClientProtocolException(org.apache.http.client.ClientProtocolException) URISyntaxException(java.net.URISyntaxException) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException)

Aggregations

JSONObject (net.sf.json.JSONObject)2 JsonSlurper (net.sf.json.groovy.JsonSlurper)2 ClientProtocolException (org.apache.http.client.ClientProtocolException)2 CodeCoverageMetrics (com.uber.jenkins.phabricator.coverage.CodeCoverageMetrics)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URISyntaxException (java.net.URISyntaxException)1 JSON (net.sf.json.JSON)1 JSONNull (net.sf.json.JSONNull)1 HttpResponse (org.apache.http.HttpResponse)1 HttpResponseException (org.apache.http.client.HttpResponseException)1 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1