示例#1
0
 /**
  * Execute the request and return the POJO that represent the response.
  *
  * @param <T> The POJO that represents the response object
  * @param resourcePath the resource path
  * @param params the request parameters
  * @param returnType the POJO class to be parsed from the response
  * @return the POJO object that represent the response
  */
 private <T> T executeRequest(
     String resourcePath, Map<String, Object> params, Class<T> returnType) {
   Request request = Request.Get(resourcePath);
   if (params != null && !params.isEmpty()) {
     for (Map.Entry<String, Object> entry : params.entrySet()) {
       request.withQuery(entry.getKey(), entry.getValue());
     }
   }
   HttpRequestBase requestBase = request.build();
   try {
     HttpResponse response = execute(requestBase);
     return ResponseUtil.getObject(response, returnType);
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
 }
示例#2
0
  /**
   * Identifies concepts in a piece of text.
   *
   * @param parameters The parameters to be used in the service call, account_id, graph and text are
   *     required.
   *     <ul>
   *       <li>String account_id - The account identifier.<br>
   *       <li>String graph - The graph name.<br>
   *       <li>String text - The text to annotate.<br>
   *     </ul>
   *
   * @return {@link Annotations}
   */
  public Annotations annotateText(Map<String, Object> parameters) {
    Validate.notNull(parameters.get(ACCOUNT_ID), "account_id can't be null");
    Validate.notNull(parameters.get(GRAPH), "graph can't be null");
    Validate.notNull(parameters.get(TEXT), "text can't be null");
    String graphId =
        createGraphIdPath((String) parameters.get(ACCOUNT_ID), (String) parameters.get(GRAPH));

    HttpRequestBase request =
        Request.Post(graphId + ANNOTATE_TEXT_PATH)
            .withContent((String) parameters.get(TEXT), MediaType.TEXT_PLAIN)
            .withHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON)
            .build();

    try {
      HttpResponse response = execute(request);
      Annotations annotations =
          GsonSingleton.getGson().fromJson(ResponseUtil.getString(response), Annotations.class);
      return annotations;
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }