JavaClient.api.mustache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of codegen Show documentation
Show all versions of codegen Show documentation
This artefact generates other artefacts such as API client, API server out of API Model
{{>licenseInfo}}
package {{package}};
import {{invokerPackage}}.*;
import {{modelPackage}}.*;{{#imports}}
import {{import}};{{/imports}}
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import ch.qos.logback.classic.Logger;
import org.slf4j.LoggerFactory;
{{#operations}}
public class {{classname}} {
{{#_enums}}
{{>modelInnerEnum}}
{{/_enums}}
private static final Logger LOGGER = (Logger) LoggerFactory.getLogger({{classname}}.class);
private ApiClient apiClient;
public {{classname}}() {
this(new ApiClient());
}
public {{classname}}(ApiClient apiClient) {
this.apiClient = apiClient;
}
public ApiClient getApiClient() {
return apiClient;
}
public void setApiClient(ApiClient apiClient) {
this.apiClient = apiClient;
}
{{#operation}}
/**
* Build call for {{operationId}}{{#nonHeaderParams}}
* @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{^isContainer}}{{#defaultValue}}, default to {{.}}{{/defaultValue}}{{/isContainer}}){{/required}}{{/nonHeaderParams}}
* @param _callback Callback for upload/download progress
* @return Call to execute
* @throws ApiException If fail to serialize the request body object
{{#responses.0}}
* http.response.details
Status Code Description Response Headers
{{#responses}}
{{code}} {{message}} {{#headers}} * {{baseName}} - {{description}}
{{/headers}}{{^headers.0}} - {{/headers.0}}
{{/responses}}
{{/responses.0}}
{{#isDeprecated}}
* @deprecated
{{/isDeprecated}}
{{#externalDocs}}
* {{description}}
* @see {{summary}} Documentation
{{/externalDocs}}
*/
{{#isDeprecated}}
@Deprecated
{{/isDeprecated}}
public okhttp3.Call {{operationId}}Call({{#nonHeaderParams}}{{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}} {{paramName}}, {{/nonHeaderParams}}final ApiCallback _callback) throws ApiException {
Object postBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};
// create path and map variables
String path = "{{{path}}}"{{#pathParams}}
.replaceAll("\\{" + "{{baseName}}" + "\\}", apiClient.escapeString({{#collectionFormat}}apiClient.collectionPathParameterToString("{{{collectionFormat}}}", {{{paramName}}}){{/collectionFormat}}{{^collectionFormat}}{{{paramName}}}{{^isString}}.toString(){{/isString}}{{/collectionFormat}})){{/pathParams}};
LOGGER.trace("Building Call for {{operationId}} with path: {}{{#nonHeaderParams}}, {{baseName}}: {{openBracket}}{{closeBracket}}{{/nonHeaderParams}}",
path{{#nonHeaderParams}},
{{paramName}}{{/nonHeaderParams}});
{{javaUtilPrefix}}List queryParams = new {{javaUtilPrefix}}ArrayList<>();
{{javaUtilPrefix}}List collectionQueryParams = new {{javaUtilPrefix}}ArrayList<>();
{{#queryParams}}
addQueryParam({{#collectionFormat}}collectionQueryParams, "{{{collectionFormat}}}"{{/collectionFormat}}{{^collectionFormat}}queryParams, "{{baseName}}"{{/collectionFormat}}, {{paramName}});
{{/queryParams}}
{{javaUtilPrefix}}Map headerParams = new {{javaUtilPrefix}}HashMap<>();
String[] authNames = new String[] { {{#authMethods}}"{{name}}"{{^-last}}, {{/-last}}{{/authMethods}} };
return apiClient.buildCall(path, "{{httpMethod}}", queryParams, collectionQueryParams, postBody, headerParams, authNames, _callback);
}
{{#isDeprecated}}
@Deprecated
{{/isDeprecated}}
@SuppressWarnings("rawtypes")
private okhttp3.Call {{operationId}}ValidateBeforeCall({{#nonHeaderParams}}{{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}} {{paramName}}, {{/nonHeaderParams}}final ApiCallback _callback) throws ApiException {
{{#nonHeaderParams}}{{#required}}
// verify the required parameter '{{paramName}}' is set
if ({{paramName}} == null) {
throw new ApiException("Missing the required parameter '{{paramName}}' when calling {{operationId}}(Async)");
}
{{/required}}{{/nonHeaderParams}}
return {{operationId}}Call({{#nonHeaderParams}}{{paramName}}, {{/nonHeaderParams}}_callback);
}
/**
* {{summary}}
* {{notes}}{{#nonHeaderParams}}
* @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{^isContainer}}{{#defaultValue}}, default to {{.}}{{/defaultValue}}{{/isContainer}}){{/required}}{{/nonHeaderParams}}{{#returnType}}
* @return {{returnType}}{{/returnType}}
* @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
{{#responses.0}}
* http.response.details
Status Code Description Response Headers
{{#responses}}
{{code}} {{message}} {{#headers}} * {{baseName}} - {{description}}
{{/headers}}{{^headers.0}} - {{/headers.0}}
{{/responses}}
{{/responses.0}}
{{#isDeprecated}}
* @deprecated
{{/isDeprecated}}
{{#externalDocs}}
* {{description}}
* @see {{summary}} Documentation
{{/externalDocs}}
*/
{{#isDeprecated}}
@Deprecated
{{/isDeprecated}}
public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}}{{operationId}}({{#nonHeaderParams}}{{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}} {{paramName}}{{^-last}}, {{/-last}}{{/nonHeaderParams}}) throws ApiException {
LOGGER.trace("{{operationId}}{{#hasNonHeaderParams}} with {{#nonHeaderParams}}{{baseName}}: {{openBracket}}{{closeBracket}}{{^-last}}, {{last}}{{/-last}}{{/nonHeaderParams}}",{{#nonHeaderParams}}
{{paramName}}{{^-last}},{{/-last}}{{/nonHeaderParams}}{{/hasNonHeaderParams}}{{^hasNonHeaderParams}}"{{/hasNonHeaderParams}});
{{#returnType}}ApiResponse<{{{returnType}}}> resp = {{/returnType}}{{operationId}}WithHttpInfo({{#nonHeaderParams}}{{paramName}}{{^-last}}, {{/-last}}{{/nonHeaderParams}});{{#returnType}}
return resp.getData();{{/returnType}}
}
/**
* {{summary}}
* {{notes}}{{#nonHeaderParams}}
* @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{^isContainer}}{{#defaultValue}}, default to {{.}}{{/defaultValue}}{{/isContainer}}){{/required}}{{/nonHeaderParams}}
* @return ApiResponse<{{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Void{{/returnType}}>
* @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
{{#responses.0}}
* http.response.details
Status Code Description Response Headers
{{#responses}}
{{code}} {{message}} {{#headers}} * {{baseName}} - {{description}}
{{/headers}}{{^headers.0}} - {{/headers.0}}
{{/responses}}
{{/responses.0}}
{{#isDeprecated}}
* @deprecated
{{/isDeprecated}}
{{#externalDocs}}
* {{description}}
* @see {{summary}} Documentation
{{/externalDocs}}
*/
{{#isDeprecated}}
@Deprecated
{{/isDeprecated}}
public ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {{operationId}}WithHttpInfo({{#nonHeaderParams}}{{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}} {{paramName}}{{^-last}}, {{/-last}}{{/nonHeaderParams}}) throws ApiException {
okhttp3.Call call = {{operationId}}ValidateBeforeCall({{#nonHeaderParams}}{{paramName}}, {{/nonHeaderParams}}null);
{{#returnType}}Type returnType = new TypeToken<{{{returnType}}}>(){}.getType();
return apiClient.execute(call, returnType);{{/returnType}}{{^returnType}}return apiClient.execute(call);{{/returnType}}
}
/**
* {{summary}} (asynchronously)
* {{notes}}{{#nonHeaderParams}}
* @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{^isContainer}}{{#defaultValue}}, default to {{.}}{{/defaultValue}}{{/isContainer}}){{/required}}{{/nonHeaderParams}}
* @param _callback The callback to be executed when the API call finishes
* @return The request call
* @throws ApiException If fail to process the API call, e.g. serializing the request body object
{{#responses.0}}
* http.response.details
Status Code Description Response Headers
{{#responses}}
{{code}} {{message}} {{#headers}} * {{baseName}} - {{description}}
{{/headers}}{{^headers.0}} - {{/headers.0}}
{{/responses}}
{{/responses.0}}
{{#isDeprecated}}
* @deprecated
{{/isDeprecated}}
{{#externalDocs}}
* {{description}}
* @see {{summary}} Documentation
{{/externalDocs}}
*/
{{#isDeprecated}}
@Deprecated
{{/isDeprecated}}
public okhttp3.Call {{operationId}}Async({{#nonHeaderParams}}{{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}} {{paramName}}, {{/nonHeaderParams}}final ApiCallback<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> _callback) throws ApiException {
LOGGER.trace("Asynchronously {{operationId}}{{#hasNonHeaderParams}} with {{#nonHeaderParams}}{{baseName}}: {{openBracket}}{{closeBracket}}{{^-last}}, {{last}}{{/-last}}{{/nonHeaderParams}}",{{#nonHeaderParams}}
{{paramName}}{{^-last}},{{/-last}}{{/nonHeaderParams}}{{/hasNonHeaderParams}}{{^hasNonHeaderParams}}"{{/hasNonHeaderParams}});
okhttp3.Call call = {{operationId}}ValidateBeforeCall({{#nonHeaderParams}}{{paramName}}, {{/nonHeaderParams}}_callback);
{{#returnType}}Type returnType = new TypeToken<{{{returnType}}}>(){}.getType();
apiClient.executeAsync(call, returnType, _callback);{{/returnType}}{{^returnType}}apiClient.executeAsync(call, _callback);{{/returnType}}
return call;
}
{{/operation}}
private void addQueryParam(List queryParams, String paramName, Object paramValue) {
if (paramValue != null) {
LOGGER.trace("Adding query parameter of {} with value of {}", paramName, paramValue);
queryParams.addAll(apiClient.parameterToPair(paramName, paramValue));
}
}
}
{{/operations}}