forked from loafle/openapi-generator-original
Added api template android-volley
This commit is contained in:
parent
16bf228a41
commit
efac324b73
@ -0,0 +1,128 @@
|
||||
package {{package}};
|
||||
|
||||
import {{invokerPackage}}.ApiException;
|
||||
import {{invokerPackage}}.ApiInvoker;
|
||||
import {{invokerPackage}}.Pair;
|
||||
|
||||
import {{modelPackage}}.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
{{#imports}}import {{import}};
|
||||
{{/imports}}
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.io.File;
|
||||
|
||||
{{#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}}}{{/returnType}}{{^returnType}}void{{/returnType}}
|
||||
*/
|
||||
public void {{nickname}} ({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}, final {{#returnBaseType}}{{returnBaseType}}Responses.{{#returnContainer}}{{returnContainer}}{{/returnContainer}}{{returnBaseType}}Response{{/returnBaseType}}{{^returnBaseType}}Response.Listener<String>{{/returnBaseType}} responseListener) {
|
||||
Object postBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};
|
||||
{{#allParams}}{{#required}}
|
||||
// verify the required parameter '{{paramName}}' is set
|
||||
if ({{paramName}} == null) {
|
||||
throw 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<Pair> queryParams = new ArrayList<Pair>();
|
||||
// header params
|
||||
Map<String, String> headerParams = new HashMap<String, String>();
|
||||
// form params
|
||||
Map<String, String> formParams = new HashMap<String, String>();
|
||||
|
||||
{{#queryParams}}
|
||||
queryParams.addAll(ApiInvoker.parameterToPairs("{{#collectionFormat}}{{{collectionFormat}}}{{/collectionFormat}}", "{{baseName}}", {{paramName}}));
|
||||
{{/queryParams}}
|
||||
|
||||
{{#headerParams}}
|
||||
headerParams.put("{{baseName}}", ApiInvoker.parameterToString({{paramName}}));
|
||||
{{/headerParams}}
|
||||
|
||||
String[] contentTypes = {
|
||||
{{#consumes}}"{{mediaType}}"{{#hasMore}},{{/hasMore}}{{/consumes}}
|
||||
};
|
||||
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
|
||||
|
||||
if (contentType.startsWith("multipart/form-data")) {
|
||||
// file uploading
|
||||
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
||||
{{#formParams}}{{#notFile}}
|
||||
if ({{paramName}} != null) {
|
||||
builder.addTextBody("{{baseName}}", ApiInvoker.parameterToString({{paramName}}), ApiInvoker.TEXT_PLAIN_UTF8);
|
||||
}
|
||||
{{/notFile}}{{#isFile}}
|
||||
if ({{paramName}} != null) {
|
||||
builder.addBinaryBody("{{baseName}}", {{paramName}});
|
||||
}
|
||||
{{/isFile}}{{/formParams}}
|
||||
|
||||
HttpEntity httpEntity = builder.build();
|
||||
postBody = httpEntity;
|
||||
} else {
|
||||
// normal form params
|
||||
{{#formParams}}{{#notFile}}formParams.put("{{baseName}}", ApiInvoker.parameterToString({{paramName}}));{{/notFile}}
|
||||
{{/formParams}}
|
||||
}
|
||||
|
||||
try {
|
||||
apiInvoker.invokeAPI(basePath, path, "{{httpMethod}}", queryParams, postBody, headerParams, formParams, contentType,
|
||||
new Response.Listener<String>() {
|
||||
@Override
|
||||
public void onResponse(String response) {
|
||||
try {
|
||||
responseListener.onResponse((List<Phone>) ApiInvoker.deserialize(response, "array", Phone.class));
|
||||
} catch (ApiException exception) {
|
||||
//TODO onError
|
||||
Log.d("ERROR", "error");
|
||||
}
|
||||
}
|
||||
}, new Response.ErrorListener() {
|
||||
@Override
|
||||
public void onErrorResponse(VolleyError error) {
|
||||
//TODO onError
|
||||
Log.d("ERROR", "error");
|
||||
}
|
||||
});
|
||||
);
|
||||
} catch (ApiException ex) {
|
||||
//TODO onError
|
||||
}
|
||||
}
|
||||
{{/operation}}
|
||||
}
|
||||
{{/operations}}
|
Loading…
x
Reference in New Issue
Block a user