Пример #1
0
  /**
   * Execute the request and return the POJO that represent the response.
   *
   * @param <T> The POJO that represents the response object
   * @param params the request parameters
   * @param operation the alchemy operation
   * @param returnType the POJO class to be parsed from the response
   * @param acceptedFormats the accepted input formats e.g. "html", "text"...
   * @return the POJO object that represent the response
   */
  private <T> T executeRequest(
      Map<String, Object> params,
      AlchemyAPI operation,
      Class<T> returnType,
      String... acceptedFormats) {
    // Get the input format and check for missing parameters
    String format = getInputFormat(params, acceptedFormats);
    // Get the path that represent this operation based on the operation and format
    String path = AlchemyEndPoints.getPath(operation, format);

    // Return json
    params.put(OUTPUT_MODE, "json");

    // Prevent jsonp to be returned
    params.remove(JSONP);

    Request request = Request.Post(path);
    for (String param : params.keySet()) {
      request.withForm(param, params.get(param));
    }
    HttpRequestBase requestBase = request.build();
    try {
      HttpResponse response = execute(requestBase);
      return ResponseUtil.getObject(response, returnType);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
Пример #2
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);
   }
 }