dart2-v3template.api_helper.mustache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of connect-openapi-dart Show documentation
Show all versions of connect-openapi-dart Show documentation
dart2 generator from openapi 3.x spec files. Changing to openapitools dependencies.
part of {{pubName}}.api;
const _delimiters = const {'csv': ',', 'ssv': ' ', 'tsv': '\t', 'pipes': '|'};
// port from Java version
Iterable _convertParametersForCollectionFormat(
String collectionFormat, String name, dynamic value) {
var params = [];
// preconditions
if (name == null || name.isEmpty || value == null) return params;
if (value is! List) {
params.add(QueryParam(name, parameterToString(value)));
return params;
}
List values = value as List;
// get the collection format
collectionFormat = (collectionFormat == null || collectionFormat.isEmpty)
? "csv"
: collectionFormat; // default: csv
if (collectionFormat == "multi") {
return values.map((v) => QueryParam(name, parameterToString(v)));
}
String delimiter = _delimiters[collectionFormat] ?? ",";
params.add(QueryParam(name, values.map((v) => parameterToString(v)).join(delimiter)));
return params;
}
/// Format the given parameter object into string.
String parameterToString(dynamic value) {
if (value == null) {
return '';
} else if (value is DateTime) {
return value.toUtc().toIso8601String();
{{#models}}
{{#model}}
{{#isEnum}}
} else if (value is {{classname}}) {
return {{classname}}TypeTransformer().encode(value).toString();
{{/isEnum}}
{{/model}}
{{/models}}
} else {
return value.toString();
}
}
/// Returns the decoded body by utf-8 if application/json with the given headers.
/// Else, returns the decoded body by default algorithm of dart:http.
/// Because avoid to text garbling when header only contains "application/json" without "; charset=utf-8".
String _decodeBodyBytes(Response response) {
var contentType = response.headers['content-type'];
if (contentType != null && contentType.contains("application/json")) {
return utf8.decode(response.bodyBytes);
} else {
return response.body;
}
}