api_helper.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // AUTO-GENERATED FILE, DO NOT MODIFY!
  3. //
  4. // @dart=2.12
  5. // ignore_for_file: unused_element, unused_import
  6. // ignore_for_file: always_put_required_named_parameters_first
  7. // ignore_for_file: constant_identifier_names
  8. // ignore_for_file: lines_longer_than_80_chars
  9. part of openapi.api;
  10. class QueryParam {
  11. const QueryParam(this.name, this.value);
  12. final String name;
  13. final String value;
  14. @override
  15. String toString() => '${Uri.encodeQueryComponent(name)}=${Uri.encodeQueryComponent(value)}';
  16. }
  17. // Ported from the Java version.
  18. Iterable<QueryParam> _queryParams(String collectionFormat, String name, dynamic value,) {
  19. // Assertions to run in debug mode only.
  20. assert(name.isNotEmpty, 'Parameter cannot be an empty string.');
  21. final params = <QueryParam>[];
  22. if (value is List) {
  23. if (collectionFormat == 'multi') {
  24. return value.map((dynamic v) => QueryParam(name, parameterToString(v)),);
  25. }
  26. // Default collection format is 'csv'.
  27. if (collectionFormat.isEmpty) {
  28. collectionFormat = 'csv'; // ignore: parameter_assignments
  29. }
  30. final delimiter = _delimiters[collectionFormat] ?? ',';
  31. params.add(QueryParam(name, value.map<dynamic>(parameterToString).join(delimiter),));
  32. } else if (value != null) {
  33. params.add(QueryParam(name, parameterToString(value)));
  34. }
  35. return params;
  36. }
  37. /// Format the given parameter object into a [String].
  38. String parameterToString(dynamic value) {
  39. if (value == null) {
  40. return '';
  41. }
  42. if (value is DateTime) {
  43. return value.toUtc().toIso8601String();
  44. }
  45. if (value is AssetTypeEnum) {
  46. return AssetTypeEnumTypeTransformer().encode(value).toString();
  47. }
  48. if (value is DeleteAssetStatus) {
  49. return DeleteAssetStatusTypeTransformer().encode(value).toString();
  50. }
  51. if (value is DeviceTypeEnum) {
  52. return DeviceTypeEnumTypeTransformer().encode(value).toString();
  53. }
  54. if (value is ThumbnailFormat) {
  55. return ThumbnailFormatTypeTransformer().encode(value).toString();
  56. }
  57. return value.toString();
  58. }
  59. /// Returns the decoded body as UTF-8 if the given headers indicate an 'application/json'
  60. /// content type. Otherwise, returns the decoded body as decoded by dart:http package.
  61. Future<String> _decodeBodyBytes(Response response) async {
  62. final contentType = response.headers['content-type'];
  63. return contentType != null && contentType.toLowerCase().startsWith('application/json')
  64. ? response.bodyBytes.isEmpty ? '' : utf8.decode(response.bodyBytes)
  65. : response.body;
  66. }
  67. /// Returns a valid [T] value found at the specified Map [key], null otherwise.
  68. T? mapValueOfType<T>(dynamic map, String key) {
  69. final dynamic value = map is Map ? map[key] : null;
  70. return value is T ? value : null;
  71. }
  72. /// Returns a valid Map<K, V> found at the specified Map [key], null otherwise.
  73. Map<K, V>? mapCastOfType<K, V>(dynamic map, String key) {
  74. final dynamic value = map is Map ? map[key] : null;
  75. return value is Map ? value.cast<K, V>() : null;
  76. }
  77. /// Returns a valid [DateTime] found at the specified Map [key], null otherwise.
  78. DateTime? mapDateTime(dynamic map, String key, [String? pattern]) {
  79. final dynamic value = map is Map ? map[key] : null;
  80. if (value != null) {
  81. int? millis;
  82. if (value is int) {
  83. millis = value;
  84. } else if (value is String) {
  85. if (pattern == _dateEpochMarker) {
  86. millis = int.tryParse(value);
  87. } else {
  88. return DateTime.tryParse(value);
  89. }
  90. }
  91. if (millis != null) {
  92. return DateTime.fromMillisecondsSinceEpoch(millis, isUtc: true);
  93. }
  94. }
  95. return null;
  96. }