dart2-v3template.api.mustache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of openapi-dart-generator Show documentation
Show all versions of openapi-dart-generator Show documentation
dart2 generator from openapi 3.x spec files.
part of {{pubName}}.api;
{{#operations}}
class {{classname}} {
final {{classname}}Delegate apiDelegate;
{{classname}}(ApiClient apiClient) : assert(apiClient != null), apiDelegate = {{classname}}Delegate(apiClient);
{{#operation}}
/// {{summary}}
///
/// {{notes}}
{{#vendorExtensions.x-dart-produces-json}}
{{#returnType}}Future<{{{returnType}}}> {{/returnType}}{{^returnType}}Future {{/returnType}}
{{/vendorExtensions.x-dart-produces-json}}
{{^vendorExtensions.x-dart-produces-json}}
{{#returnType}}Future {{/returnType}}{{^returnType}}Future {{/returnType}}
{{/vendorExtensions.x-dart-produces-json}}
{{nickname}}({{#allParams}}{{#required}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/required}}{{/allParams}}{{#hasOptionalParams}}{Options options{{#hasParams}}, {{/hasParams}}{{#allParams}}{{^required}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/required}}{{/allParams}} }{{/hasOptionalParams}}{{^hasOptionalParams}}{{#hasParams}}, {{/hasParams}}{Options options}{{/hasOptionalParams}}) async {
final response = await apiDelegate.{{nickname}}({{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}} options: options, {{#hasOptionalParams}}{{#hasParams}}{{/hasParams}}{{#allParams}}{{^required}}{{paramName}}: {{paramName}}{{^-last}}, {{/-last}}{{/required}}{{/allParams}}{{/hasOptionalParams}});
if(response.statusCode >= 400) {
throw ApiException(response.statusCode, await decodeBodyBytes(response));
} else {
return await apiDelegate.{{nickname}}_decode(response);
}
}
/// {{summary}}
///
/// {{notes}}
{{#vendorExtensions.x-dart-extension-name}}
{{#vendorExtensions.x-dart-produces-json}}
{{#returnType}}Future {{/returnType}}{{^returnType}}Future {{/returnType}}
{{/vendorExtensions.x-dart-produces-json}}
{{^vendorExtensions.x-dart-produces-json}}
Future
{{/vendorExtensions.x-dart-produces-json}}
{{vendorExtensions.x-dart-extension-name}}({{#allParams}}{{#required}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/required}}{{/allParams}}{{#hasOptionalParams}}{Options options{{#hasParams}}, {{/hasParams}}{{#allParams}}{{^required}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/required}}{{/allParams}} }{{/hasOptionalParams}}{{^hasOptionalParams}}{{#hasParams}}, {{/hasParams}}{Options options}{{/hasOptionalParams}}) async {
final response = await apiDelegate.{{nickname}}({{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}} options: options, {{#hasOptionalParams}}{{#hasParams}}{{/hasParams}}{{#allParams}}{{^required}}{{paramName}}: {{paramName}}{{^-last}}, {{/-last}}{{/required}}{{/allParams}}{{/hasOptionalParams}});
{{#vendorExtensions.x-dart-produces-json}}
return ApiResponse()
{{/vendorExtensions.x-dart-produces-json}}
{{^vendorExtensions.x-dart-produces-json}}
return ApiResponse()
{{/vendorExtensions.x-dart-produces-json}}
..statusCode = response.statusCode
..headers = response.headers
..body = response.body;
}
{{/vendorExtensions.x-dart-extension-name}}
{{/operation}}
}
class {{classname}}Delegate {
final ApiClient apiClient;
{{classname}}Delegate(this.apiClient) : assert(apiClient != null);
{{#operation}}
Future
{{nickname}}({{#allParams}}{{#required}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/required}}{{/allParams}}{{#hasOptionalParams}}{Options options{{#hasParams}}, {{/hasParams}}{{#allParams}}{{^required}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/required}}{{/allParams}} }{{/hasOptionalParams}}{{^hasOptionalParams}}{{#hasParams}}, {{/hasParams}}{Options options}{{/hasOptionalParams}}) async {
Object postBody{{#bodyParam}} = {{paramName}}{{/bodyParam}};
// verify required params are set
{{#allParams}}
{{#required}}
if({{paramName}} == null) {
throw ApiException(400, 'Missing required param: {{paramName}}');
}
{{/required}}
{{/allParams}}
// create path and map variables
final __path = '{{{path}}}'{{#pathParams}}.replaceAll('{' + '{{baseName}}' + '}', {{{paramName}}}.toString()){{/pathParams}};
// query params
final queryParams = [];
final headerParams = {}..addAll(options?.headers?.cast() ?? {});
if(headerParams['Accept'] == null) {
{{#vendorExtensions.x-dart-produces-json}}
// we only want to accept this format as we can parse it
headerParams['Accept'] = 'application/json';
{{/vendorExtensions.x-dart-produces-json}}
{{^vendorExtensions.x-dart-produces-json}}
{{#hasProduces}}headerParams['Accept'] = '{{#produces}}{{mediaType}}{{^-last}},{{/-last}}{{/produces}}';{{/hasProduces}}
{{/vendorExtensions.x-dart-produces-json}}
}
{{#queryParams}}
{{^required}}
if({{paramName}} != null) {
{{/required}}
queryParams.addAll(convertParametersForCollectionFormat(LocalApiClient.parameterToString, '{{collectionFormat}}', '{{baseName}}', {{paramName}}));
{{^required}}
}
{{/required}}
{{/queryParams}}
{{#headerParams}}
headerParams['{{baseName}}'] = {{paramName}};
{{/headerParams}}
final authNames = [{{#authMethods}}'{{name}}'{{^-last}}, {{/-last}}{{/authMethods}}];
final opt = options ?? Options();
{{#vendorExtensions.x-dart-consumes-json}}
final contentTypes = [{{#consumes}}'{{{mediaType}}}'{{^-last}},{{/-last}}{{/consumes}}];
if (contentTypes.isNotEmpty && headerParams['Content-Type'] == null) {
headerParams['Content-Type'] = contentTypes[0];
}
if (postBody != null) {
postBody = LocalApiClient.serialize(postBody);
}
{{/vendorExtensions.x-dart-consumes-json}}
{{#vendorExtensions.x-dart-consumes-form}}
if (headerParams['Content-Type'] == null) {
headerParams['Content-Type'] = 'application/x-www-form-urlencoded';
}
final formData = {};
{{#formParams}}
{{^isFile}}
formData['{{baseName}}'] = Uri.encodeFull(LocalApiClient.parameterToString({{paramName}}));
{{/isFile}}
{{/formParams}}
{{#hasFormParams}}
postBody = formData.entries.map((me) => me.key + "=" + me.value).join("&");
{{/hasFormParams}}
{{/vendorExtensions.x-dart-consumes-form}}
{{#vendorExtensions.x-dart-consumes-multipartform}}
if (headerParams['Content-Type'] == null) {
headerParams['Content-Type'] = 'multipart/form-data';
}
final formData = {};
{{#formParams}}
{{^isFile}}
if ({{paramName}} != null) {
formData['{{baseName}}'] = Uri.encodeFull(LocalApiClient.parameterToString({{paramName}}));
}
{{/isFile}}
{{#isFile}}
if ({{paramName}} != null) {
formData['{{baseName}}'] = {{paramName}};
}
{{/isFile}}
{{/formParams}}
if (formData.isNotEmpty) {
postBody = FormData.fromMap(formData);
}
{{/vendorExtensions.x-dart-consumes-multipartform}}
opt.headers = headerParams;
opt.method = '{{httpMethod}}';
return await apiClient.invokeAPI(__path, queryParams, postBody, authNames, opt);
}
{{#vendorExtensions.x-dart-produces-json}}
{{#returnType}}Future<{{{returnType}}}> {{/returnType}}{{^returnType}}Future {{/returnType}}
{{/vendorExtensions.x-dart-produces-json}}
{{^vendorExtensions.x-dart-produces-json}}
{{#returnType}}Future {{/returnType}}{{^returnType}}Future {{/returnType}}
{{/vendorExtensions.x-dart-produces-json}}
{{nickname}}_decode(ApiResponse response) async {
if(response.body != null) {
{{#vendorExtensions.x-dart-produces-json}}
{{#isArray}}
{{#returnType}}
return (LocalApiClient.deserializeFromString(await decodeBodyBytes(response), '{{{returnType}}}') as List).map((item) => item as {{returnBaseType}}).toList();
{{/returnType}}
{{/isArray}}
{{^isArray}}
{{#isMap}}
{{#returnType}}
return {{{returnType}}}.from(LocalApiClient.deserializeFromString(await decodeBodyBytes(response), '{{{returnType}}}'));
{{/returnType}};
{{/isMap}}
{{^isMap}}
{{#returnType}}
return LocalApiClient.deserializeFromString(await decodeBodyBytes(response), '{{{returnType}}}') as {{{returnType}}};
{{/returnType}}
{{/isMap}}
{{/isArray}}
{{/vendorExtensions.x-dart-produces-json}}
{{^vendorExtensions.x-dart-produces-json}}
return await decodeBodyBytes(response);
{{/vendorExtensions.x-dart-produces-json}}
}
return{{#returnType}} null{{/returnType}};
}
{{/operation}}
}
{{/operations}}