All Downloads are FREE. Search and download functionalities are using the official Maven repository.

android.libraries.volley.api.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
{{>licenseInfo}}
package {{package}};

import {{invokerPackage}}.ApiInvoker;
import {{invokerPackage}}.ApiException;
import {{invokerPackage}}.Pair;

{{#hasModel}}
import {{modelPackage}}.*;
{{/hasModel}}

import java.util.*;

import com.android.volley.Response;
import com.android.volley.VolleyError;

{{#imports}}import {{import}};
{{/imports}}

import org.apache.http.HttpEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

{{#operations}}
public class {{classname}} {
  String basePath = "{{{basePath}}}";
  ApiInvoker apiInvoker = ApiInvoker.getInstance();

  public void addHeader(String key, String value) {
    getInvoker().addDefaultHeader(key, value);
  }

  public ApiInvoker getInvoker() {
    return apiInvoker;
  }

  public void setBasePath(String basePath) {
    this.basePath = basePath;
  }

  public String getBasePath() {
    return basePath;
  }

  {{#operation}}
  /**
  * {{summary}}
  * {{notes}}
{{#allParams}}   * @param {{paramName}} {{description}}
{{/allParams}}   * @return {{{returnType}}}{{^returnType}}void{{/returnType}}
  */
  public {{{returnType}}}{{^returnType}}void{{/returnType}} {{nickname}} ({{#allParams}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}) throws TimeoutException, ExecutionException, InterruptedException, ApiException {
    Object postBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};
    {{#allParams}}
    {{#required}}
    // verify the required parameter '{{paramName}}' is set
    if ({{paramName}} == null) {
      VolleyError error = new VolleyError("Missing the required parameter '{{paramName}}' when calling {{nickname}}",
        new ApiException(400, "Missing the required parameter '{{paramName}}' when calling {{nickname}}"));
    }
    {{/required}}
    {{/allParams}}

    // create path and map variables
    String path = "{{{path}}}"{{#pathParams}}.replaceAll("\\{" + "{{baseName}}" + "\\}", apiInvoker.escapeString({{{paramName}}}.toString())){{/pathParams}};

    // query params
    List queryParams = new ArrayList();
    // header params
    Map headerParams = new HashMap();
    // form params
    Map formParams = new HashMap();
    {{#queryParams}}
    queryParams.addAll(ApiInvoker.parameterToPairs("{{{collectionFormat}}}", "{{baseName}}", {{paramName}}));
    {{/queryParams}}
    {{#headerParams}}
    headerParams.put("{{baseName}}", ApiInvoker.parameterToString({{paramName}}));
    {{/headerParams}}
    String[] contentTypes = {
      {{#consumes}}
      "{{{mediaType}}}"{{^-last}},{{/-last}}
      {{/consumes}}
    };
    String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";

    if (contentType.startsWith("multipart/form-data")) {
      // file uploading
      MultipartEntityBuilder localVarBuilder = MultipartEntityBuilder.create();
      {{#formParams}}
      {{^isFile}}
      if ({{paramName}} != null) {
        localVarBuilder.addTextBody("{{baseName}}", ApiInvoker.parameterToString({{paramName}}), ApiInvoker.TEXT_PLAIN_UTF8);
      }
      {{/isFile}}
      {{#isFile}}
      if ({{paramName}} != null) {
        localVarBuilder.addBinaryBody("{{baseName}}", {{paramName}});
      }
      {{/isFile}}
      {{/formParams}}
      HttpEntity httpEntity = localVarBuilder.build();
      postBody = httpEntity;
    } else {
      // normal form params
      {{#formParams}}
      {{^isFile}}
      formParams.put("{{baseName}}", ApiInvoker.parameterToString({{paramName}}));
      {{/isFile}}
      {{/formParams}}
    }

    String[] authNames = new String[] { {{#authMethods}}"{{name}}"{{^-last}}, {{/-last}}{{/authMethods}} };

    try {
      String localVarResponse = apiInvoker.invokeAPI (basePath, path, "{{httpMethod}}", queryParams, postBody, headerParams, formParams, contentType, authNames);
      if (localVarResponse != null) {
         return {{#returnType}}({{{returnType}}}) ApiInvoker.deserialize(localVarResponse, "{{returnContainer}}", {{returnBaseType}}.class){{/returnType}};
      } else {
         return {{#returnType}}null{{/returnType}};
      }
    } catch (ApiException ex) {
       throw ex;
    } catch (InterruptedException ex) {
       throw ex;
    } catch (ExecutionException ex) {
      if (ex.getCause() instanceof VolleyError) {
        VolleyError volleyError = (VolleyError)ex.getCause();
        if (volleyError.networkResponse != null) {
          throw new ApiException(volleyError.networkResponse.statusCode, volleyError.getMessage());
        }
      }
      throw ex;
    } catch (TimeoutException ex) {
      throw ex;
    }
  }

      /**
   * {{summary}}
   * {{notes}}
{{#allParams}}   * @param {{paramName}} {{description}}{{/allParams}}
  */
  public void {{nickname}} ({{#allParams}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{#-last}}, {{/-last}}{{/allParams}}final Response.Listener<{{{returnType}}}{{^returnType}}String{{/returnType}}> responseListener, final Response.ErrorListener errorListener) {
    Object postBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};

    {{#allParams}}
    {{#required}}
    // verify the required parameter '{{paramName}}' is set
    if ({{paramName}} == null) {
      VolleyError error = new VolleyError("Missing the required parameter '{{paramName}}' when calling {{nickname}}",
        new ApiException(400, "Missing the required parameter '{{paramName}}' when calling {{nickname}}"));
    }
    {{/required}}
    {{/allParams}}

    // create path and map variables
    String path = "{{{path}}}".replaceAll("\\{format\\}","json"){{#pathParams}}.replaceAll("\\{" + "{{baseName}}" + "\\}", apiInvoker.escapeString({{{paramName}}}.toString())){{/pathParams}};

    // query params
    List queryParams = new ArrayList();
    // header params
    Map headerParams = new HashMap();
    // form params
    Map formParams = new HashMap();

    {{#queryParams}}
    queryParams.addAll(ApiInvoker.parameterToPairs("{{{collectionFormat}}}", "{{baseName}}", {{paramName}}));
    {{/queryParams}}

    {{#headerParams}}
    headerParams.put("{{baseName}}", ApiInvoker.parameterToString({{paramName}}));
    {{/headerParams}}

    String[] contentTypes = {
      {{#consumes}}"{{{mediaType}}}"{{^-last}},{{/-last}}{{/consumes}}
    };
    String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";

    if (contentType.startsWith("multipart/form-data")) {
      // file uploading
      MultipartEntityBuilder localVarBuilder = MultipartEntityBuilder.create();
      {{#formParams}}{{^isFile}}
      if ({{paramName}} != null) {
        localVarBuilder.addTextBody("{{baseName}}", ApiInvoker.parameterToString({{paramName}}), ApiInvoker.TEXT_PLAIN_UTF8);
      }
      {{/isFile}}{{#isFile}}
      if ({{paramName}} != null) {
        localVarBuilder.addBinaryBody("{{baseName}}", {{paramName}});
      }
      {{/isFile}}{{/formParams}}

      HttpEntity httpEntity = localVarBuilder.build();
      postBody = httpEntity;
    } else {
      // normal form params
      {{#formParams}}{{^isFile}}formParams.put("{{baseName}}", ApiInvoker.parameterToString({{paramName}}));{{/isFile}}
      {{/formParams}}
    }

    String[] authNames = new String[] { {{#authMethods}}"{{name}}"{{^-last}}, {{/-last}}{{/authMethods}} };

    try {
      apiInvoker.invokeAPI(basePath, path, "{{httpMethod}}", queryParams, postBody, headerParams, formParams, contentType, authNames,
        new Response.Listener() {
          @Override
          public void onResponse(String localVarResponse) {
            {{#returnType}}
            try {
              responseListener.onResponse(({{{returnType}}}) ApiInvoker.deserialize(localVarResponse,  "{{returnContainer}}", {{returnBaseType}}.class));
              {{/returnType}}
              {{^returnType}}
              responseListener.onResponse(localVarResponse);
              {{/returnType}}
            {{#returnType}}
            } catch (ApiException exception) {
               errorListener.onErrorResponse(new VolleyError(exception));
            }
            {{/returnType}}
          }
      }, new Response.ErrorListener() {
          @Override
          public void onErrorResponse(VolleyError error) {
            errorListener.onErrorResponse(error);
          }
      });
    } catch (ApiException ex) {
      errorListener.onErrorResponse(new VolleyError(ex));
    }
  }
  {{/operation}}
}
{{/operations}}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy