Search in sources :

Example 1 with PutRequest

use of io.swagger.client.request.PutRequest in project ef-app_android by eurofurence.

the class ApiInvoker method createRequest.

public Request<String> createRequest(String host, String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, String> formParams, String contentType, String[] authNames, Response.Listener<String> stringRequest, Response.ErrorListener errorListener) throws ApiException {
    StringBuilder b = new StringBuilder();
    b.append("?");
    updateParamsForAuth(authNames, queryParams, headerParams);
    if (queryParams != null) {
        for (Pair queryParam : queryParams) {
            if (!queryParam.getName().isEmpty()) {
                b.append(escapeString(queryParam.getName()));
                b.append("=");
                b.append(escapeString(queryParam.getValue()));
                b.append("&");
            }
        }
    }
    String querystring = b.substring(0, b.length() - 1);
    String url = host + path + querystring;
    HashMap<String, String> headers = new HashMap<String, String>();
    for (String key : headerParams.keySet()) {
        headers.put(key, headerParams.get(key));
    }
    for (String key : defaultHeaderMap.keySet()) {
        if (!headerParams.containsKey(key)) {
            headers.put(key, defaultHeaderMap.get(key));
        }
    }
    headers.put("Accept", "application/json");
    // URL encoded string from form parameters
    String formParamStr = null;
    // for form data
    if ("application/x-www-form-urlencoded".equals(contentType)) {
        StringBuilder formParamBuilder = new StringBuilder();
        // encode the form params
        for (String key : formParams.keySet()) {
            String value = formParams.get(key);
            if (value != null && !"".equals(value.trim())) {
                if (formParamBuilder.length() > 0) {
                    formParamBuilder.append("&");
                }
                try {
                    formParamBuilder.append(URLEncoder.encode(key, "utf8")).append("=").append(URLEncoder.encode(value, "utf8"));
                } catch (Exception e) {
                // move on to next
                }
            }
        }
        formParamStr = formParamBuilder.toString();
    }
    Request request = null;
    if ("GET".equals(method)) {
        request = new GetRequest(url, headers, null, stringRequest, errorListener);
    } else if ("POST".equals(method)) {
        request = null;
        if (formParamStr != null) {
            request = new PostRequest(url, headers, contentType, new StringEntity(formParamStr, "UTF-8"), stringRequest, errorListener);
        } else if (body != null) {
            if (body instanceof HttpEntity) {
                request = new PostRequest(url, headers, null, (HttpEntity) body, stringRequest, errorListener);
            } else {
                request = new PostRequest(url, headers, contentType, new StringEntity(serialize(body), "UTF-8"), stringRequest, errorListener);
            }
        } else {
            request = new PostRequest(url, headers, null, null, stringRequest, errorListener);
        }
    } else if ("PUT".equals(method)) {
        request = null;
        if (formParamStr != null) {
            request = new PutRequest(url, headers, contentType, new StringEntity(formParamStr, "UTF-8"), stringRequest, errorListener);
        } else if (body != null) {
            if (body instanceof HttpEntity) {
                request = new PutRequest(url, headers, null, (HttpEntity) body, stringRequest, errorListener);
            } else {
                request = new PutRequest(url, headers, contentType, new StringEntity(serialize(body), "UTF-8"), stringRequest, errorListener);
            }
        } else {
            request = new PutRequest(url, headers, null, null, stringRequest, errorListener);
        }
    } else if ("DELETE".equals(method)) {
        request = null;
        if (formParamStr != null) {
            request = new DeleteRequest(url, headers, contentType, new StringEntity(formParamStr, "UTF-8"), stringRequest, errorListener);
        } else if (body != null) {
            if (body instanceof HttpEntity) {
                request = new DeleteRequest(url, headers, null, (HttpEntity) body, stringRequest, errorListener);
            } else {
                request = new DeleteRequest(url, headers, contentType, new StringEntity(serialize(body), "UTF-8"), stringRequest, errorListener);
            }
        } else {
            request = new DeleteRequest(url, headers, null, null, stringRequest, errorListener);
        }
    } else if ("PATCH".equals(method)) {
        request = null;
        if (formParamStr != null) {
            request = new PatchRequest(url, headers, contentType, new StringEntity(formParamStr, "UTF-8"), stringRequest, errorListener);
        } else if (body != null) {
            if (body instanceof HttpEntity) {
                request = new PatchRequest(url, headers, null, (HttpEntity) body, stringRequest, errorListener);
            } else {
                request = new PatchRequest(url, headers, contentType, new StringEntity(serialize(body), "UTF-8"), stringRequest, errorListener);
            }
        } else {
            request = new PatchRequest(url, headers, null, null, stringRequest, errorListener);
        }
    }
    return request;
}
Also used : HttpEntity(org.apache.http.HttpEntity) HashMap(java.util.HashMap) GetRequest(io.swagger.client.request.GetRequest) DeleteRequest(io.swagger.client.request.DeleteRequest) PutRequest(io.swagger.client.request.PutRequest) PostRequest(io.swagger.client.request.PostRequest) PatchRequest(io.swagger.client.request.PatchRequest) Request(com.android.volley.Request) PutRequest(io.swagger.client.request.PutRequest) PatchRequest(io.swagger.client.request.PatchRequest) JsonParseException(com.google.gson.JsonParseException) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) StringEntity(org.apache.http.entity.StringEntity) PostRequest(io.swagger.client.request.PostRequest) GetRequest(io.swagger.client.request.GetRequest) DeleteRequest(io.swagger.client.request.DeleteRequest)

Aggregations

Request (com.android.volley.Request)1 JsonParseException (com.google.gson.JsonParseException)1 DeleteRequest (io.swagger.client.request.DeleteRequest)1 GetRequest (io.swagger.client.request.GetRequest)1 PatchRequest (io.swagger.client.request.PatchRequest)1 PostRequest (io.swagger.client.request.PostRequest)1 PutRequest (io.swagger.client.request.PutRequest)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 HttpEntity (org.apache.http.HttpEntity)1 StringEntity (org.apache.http.entity.StringEntity)1