コード例 #1
0
  /**
   * Accesses an endpoint that lists objects.
   *
   * @param endpoint The endpoint to be accessed. Examples: "products", "scripts"
   * @param additionalParams Additional parameters to send with the request.
   * @return A {@link ListResponse} with the results.
   */
  public ListResponse list(String endpoint, Map<String, String> additionalParams)
      throws ApiException {

    try {
      if (additionalParams == null) {
        additionalParams = Maps.newHashMap();
      }
      if (!additionalParams.containsKey("page")) {
        additionalParams.put("page", "1");
      }
      if (!additionalParams.containsKey("per_page")) {
        additionalParams.put("per_page", "30");
      }

      InternalApiResponse response = client.get(endpoint, additionalParams);

      validateResponse(response);

      int page = Integer.valueOf(additionalParams.get("page"));
      int perPage = Integer.valueOf(additionalParams.get("per_page"));

      List<String> xTotalCount = response.getHeader("X-Total-Count");
      // Fix for Google App Engine, otherwise, X Total Count would be null
      // and then we would have null point exception ---> TODO: make getHeader case insensitive
      if (xTotalCount == null) {
        xTotalCount = response.getHeader("x-total-count");
      }

      int totalCount = Integer.valueOf(xTotalCount.get(0));

      return new ListResponse(
          new JSONArray(response.getResponse()),
          page,
          totalCount,
          page * perPage >= totalCount,
          endpoint,
          additionalParams,
          response.getHeaders());

    } catch (JSONException e) {
      throw new ApiException("Invalid JSON responded by API", e);
    }
  }