JsonElement call()

in src/main/java/com/googlesource/gerrit/plugins/its/phabricator/conduit/ConduitConnection.java [93:136]


  JsonElement call(String method, Map<String, Object> params, String token)
      throws ConduitException {
    String methodUrl = apiUrlBase + method;

    HttpPost httppost = new HttpPost(methodUrl);

    if (token != null) {
      Map<String, Object> conduitParams = new HashMap<>();
      conduitParams.put("token", token);
      params.put("__conduit__", conduitParams);
    }

    String json = gson.toJson(params);

    logger.atFinest().log("Calling phabricator method %s with the parameters %s", method, json);

    List<NameValuePair> values = new ArrayList<NameValuePair>();
    values.add(new BasicNameValuePair("params", json));
    httppost.setEntity(new UrlEncodedFormEntity(values, StandardCharsets.UTF_8));

    try (CloseableHttpResponse response = getClient().execute(httppost)) {
      logger.atFinest().log("Phabricator HTTP response status: %s", response.getStatusLine());
      HttpEntity entity = response.getEntity();
      String entityString;
      try {
        entityString = EntityUtils.toString(entity);
      } catch (IOException e) {
        throw new ConduitException("Could not read the API response", e);
      }

      logger.atFinest().log("Phabricator response: %s", entityString);
      CallCapsule callCapsule = gson.fromJson(entityString, CallCapsule.class);
      logger.atFinest().log("callCapsule.result: %s", callCapsule.getResult());
      logger.atFinest().log("callCapsule.error_code: %s", callCapsule.getErrorCode());
      logger.atFinest().log("callCapsule.error_info: %s", callCapsule.getErrorInfo());
      if (callCapsule.getErrorCode() != null || callCapsule.getErrorInfo() != null) {
        throw new ConduitErrorException(
            method, callCapsule.getErrorCode(), callCapsule.getErrorInfo());
      }
      return callCapsule.getResult();
    } catch (IOException e) {
      throw new ConduitException("Could not execute Phabricator API call", e);
    }
  }