forked from loafle/openapi-generator-original
Make ApiClient more pluggable for Java
- Rename ApiInvoker to ApiClient - Make ApiClient pluggable by allowing setting the ApiClient field of API classes - Introduce a Configuration class, containing the default ApiClient (which is also customizable) - Move basePath from API class to ApiClient - Change static methods in ApiClient to instance level
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
package {{package}};
|
||||
|
||||
import {{invokerPackage}}.ApiException;
|
||||
import {{invokerPackage}}.ApiInvoker;
|
||||
import {{invokerPackage}}.ApiClient;
|
||||
import {{invokerPackage}}.Configuration;
|
||||
|
||||
import {{modelPackage}}.*;
|
||||
|
||||
@@ -21,19 +22,22 @@ import java.util.HashMap;
|
||||
|
||||
{{#operations}}
|
||||
public class {{classname}} {
|
||||
String basePath = "{{basePath}}";
|
||||
ApiInvoker apiInvoker = ApiInvoker.getInstance();
|
||||
private ApiClient apiClient;
|
||||
|
||||
public ApiInvoker getInvoker() {
|
||||
return apiInvoker;
|
||||
public {{classname}}() {
|
||||
this(Configuration.getDefaultApiClient());
|
||||
}
|
||||
|
||||
public void setBasePath(String basePath) {
|
||||
this.basePath = basePath;
|
||||
public {{classname}}(ApiClient apiClient) {
|
||||
this.apiClient = apiClient;
|
||||
}
|
||||
|
||||
public String getBasePath() {
|
||||
return basePath;
|
||||
public ApiClient getApiClient() {
|
||||
return apiClient;
|
||||
}
|
||||
|
||||
public void setApiClient(ApiClient apiClient) {
|
||||
this.apiClient = apiClient;
|
||||
}
|
||||
|
||||
{{#operation}}
|
||||
@@ -54,7 +58,7 @@ public class {{classname}} {
|
||||
|
||||
// create path and map variables
|
||||
String path = "{{path}}".replaceAll("\\{format\\}","json"){{#pathParams}}
|
||||
.replaceAll("\\{" + "{{paramName}}" + "\\}", apiInvoker.escapeString({{{paramName}}}.toString())){{/pathParams}};
|
||||
.replaceAll("\\{" + "{{paramName}}" + "\\}", apiClient.escapeString({{{paramName}}}.toString())){{/pathParams}};
|
||||
|
||||
// query params
|
||||
Map<String, String> queryParams = new HashMap<String, String>();
|
||||
@@ -62,9 +66,9 @@ public class {{classname}} {
|
||||
Map<String, String> formParams = new HashMap<String, String>();
|
||||
|
||||
{{#queryParams}}if ({{paramName}} != null)
|
||||
queryParams.put("{{baseName}}", ApiInvoker.parameterToString({{paramName}}));
|
||||
queryParams.put("{{baseName}}", apiClient.parameterToString({{paramName}}));
|
||||
{{/queryParams}}
|
||||
{{#headerParams}}headerParams.put("{{baseName}}", ApiInvoker.parameterToString({{paramName}}));
|
||||
{{#headerParams}}headerParams.put("{{baseName}}", apiClient.parameterToString({{paramName}}));
|
||||
{{/headerParams}}
|
||||
String[] contentTypes = {
|
||||
{{#consumes}}"{{mediaType}}"{{#hasMore}},{{/hasMore}}{{/consumes}}
|
||||
@@ -77,7 +81,7 @@ public class {{classname}} {
|
||||
FormDataMultiPart mp = new FormDataMultiPart();
|
||||
{{#formParams}}{{#notFile}}
|
||||
hasFields = true;
|
||||
mp.field("{{baseName}}", ApiInvoker.parameterToString({{paramName}}), MediaType.MULTIPART_FORM_DATA_TYPE);
|
||||
mp.field("{{baseName}}", apiClient.parameterToString({{paramName}}), MediaType.MULTIPART_FORM_DATA_TYPE);
|
||||
{{/notFile}}{{#isFile}}
|
||||
hasFields = true;
|
||||
mp.field("{{baseName}}", file.getName());
|
||||
@@ -87,14 +91,14 @@ public class {{classname}} {
|
||||
postBody = mp;
|
||||
}
|
||||
else {
|
||||
{{#formParams}}{{#notFile}}formParams.put("{{baseName}}", ApiInvoker.parameterToString({{paramName}}));{{/notFile}}
|
||||
{{#formParams}}{{#notFile}}formParams.put("{{baseName}}", apiClient.parameterToString({{paramName}}));{{/notFile}}
|
||||
{{/formParams}}
|
||||
}
|
||||
|
||||
try {
|
||||
String response = apiInvoker.invokeAPI(basePath, path, "{{httpMethod}}", queryParams, postBody, headerParams, formParams, contentType);
|
||||
String response = apiClient.invokeAPI(path, "{{httpMethod}}", queryParams, postBody, headerParams, formParams, contentType);
|
||||
if(response != null){
|
||||
return {{#returnType}}({{{returnType}}}) ApiInvoker.deserialize(response, "{{returnContainer}}", {{returnBaseType}}.class){{/returnType}};
|
||||
return {{#returnType}}({{{returnType}}}) apiClient.deserialize(response, "{{returnContainer}}", {{returnBaseType}}.class){{/returnType}};
|
||||
}
|
||||
else {
|
||||
return {{#returnType}}null{{/returnType}};
|
||||
|
||||
Reference in New Issue
Block a user