/**
   * Build HTTP call with the given options.
   *
   * @param path The sub-path of the HTTP URL
   * @param method The request method, one of "GET", "HEAD", "OPTIONS", "POST", "PUT", "PATCH" and
   *     "DELETE"
   * @param queryParams The query parameters
   * @param body The request body object
   * @param headerParams The header parameters
   * @param formParams The form parameters
   * @param authNames The authentications to apply
   * @param progressRequestListener Progress request listener
   * @return The HTTP call
   * @throws ApiException If fail to serialize the request body object
   */
  public Call buildCall(
      String path,
      String method,
      List<Pair> queryParams,
      Object body,
      Map<String, String> headerParams,
      Map<String, Object> formParams,
      String[] authNames,
      ProgressRequestBody.ProgressRequestListener progressRequestListener)
      throws ApiException {
    updateParamsForAuth(authNames, queryParams, headerParams);

    final String url = buildUrl(path, queryParams);
    final Request.Builder reqBuilder = new Request.Builder().url(url);
    processHeaderParams(headerParams, reqBuilder);

    String contentType = (String) headerParams.get("Content-Type");
    // ensuring a default content type
    if (contentType == null) {
      contentType = "application/json";
    }

    RequestBody reqBody;
    if (!HttpMethod.permitsRequestBody(method)) {
      reqBody = null;
    } else if ("application/x-www-form-urlencoded".equals(contentType)) {
      reqBody = buildRequestBodyFormEncoding(formParams);
    } else if ("multipart/form-data".equals(contentType)) {
      reqBody = buildRequestBodyMultipart(formParams);
    } else if (body == null) {
      if ("DELETE".equals(method)) {
        // allow calling DELETE without sending a request body
        reqBody = null;
      } else {
        // use an empty request body (for POST, PUT and PATCH)
        reqBody = RequestBody.create(MediaType.parse(contentType), "");
      }
    } else {
      reqBody = serialize(body, contentType);
    }

    Request request = null;

    if (progressRequestListener != null && reqBody != null) {
      ProgressRequestBody progressRequestBody =
          new ProgressRequestBody(reqBody, progressRequestListener);
      request = reqBuilder.method(method, progressRequestBody).build();
    } else {
      request = reqBuilder.method(method, reqBody).build();
    }

    return httpClient.newCall(request);
  }
Exemple #2
0
 public er method(String s, RequestBody requestbody)
 {
     if (s == null || s.length() == 0)
     {
         throw new IllegalArgumentException("method == null || method.length() == 0");
     }
     if (requestbody != null && !HttpMethod.permitsRequestBody(s))
     {
         throw new IllegalArgumentException((new StringBuilder()).append("method ").append(s).append(" must not have a request body.").toString());
     }
     RequestBody requestbody1 = requestbody;
     if (requestbody == null)
     {
         requestbody1 = requestbody;
         if (HttpMethod.permitsRequestBody(s))
         {
             requestbody1 = RequestBody.create(null, Util.EMPTY_BYTE_ARRAY);
         }
     }
     method = s;
     body = requestbody1;
     return this;
 }