api_helper.dart 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 JobCommand) {
  55. return JobCommandTypeTransformer().encode(value).toString();
  56. }
  57. if (value is JobName) {
  58. return JobNameTypeTransformer().encode(value).toString();
  59. }
  60. if (value is SharedLinkType) {
  61. return SharedLinkTypeTypeTransformer().encode(value).toString();
  62. }
  63. if (value is TagTypeEnum) {
  64. return TagTypeEnumTypeTransformer().encode(value).toString();
  65. }
  66. if (value is ThumbnailFormat) {
  67. return ThumbnailFormatTypeTransformer().encode(value).toString();
  68. }
  69. if (value is TimeGroupEnum) {
  70. return TimeGroupEnumTypeTransformer().encode(value).toString();
  71. }
  72. return value.toString();
  73. }
  74. /// Returns the decoded body as UTF-8 if the given headers indicate an 'application/json'
  75. /// content type. Otherwise, returns the decoded body as decoded by dart:http package.
  76. Future<String> _decodeBodyBytes(Response response) async {
  77. final contentType = response.headers['content-type'];
  78. return contentType != null && contentType.toLowerCase().startsWith('application/json')
  79. ? response.bodyBytes.isEmpty ? '' : utf8.decode(response.bodyBytes)
  80. : response.body;
  81. }
  82. /// Returns a valid [T] value found at the specified Map [key], null otherwise.
  83. T? mapValueOfType<T>(dynamic map, String key) {
  84. final dynamic value = map is Map ? map[key] : null;
  85. return value is T ? value : null;
  86. }
  87. /// Returns a valid Map<K, V> found at the specified Map [key], null otherwise.
  88. Map<K, V>? mapCastOfType<K, V>(dynamic map, String key) {
  89. final dynamic value = map is Map ? map[key] : null;
  90. return value is Map ? value.cast<K, V>() : null;
  91. }
  92. /// Returns a valid [DateTime] found at the specified Map [key], null otherwise.
  93. DateTime? mapDateTime(dynamic map, String key, [String? pattern]) {
  94. final dynamic value = map is Map ? map[key] : null;
  95. if (value != null) {
  96. int? millis;
  97. if (value is int) {
  98. millis = value;
  99. } else if (value is String) {
  100. if (pattern == _dateEpochMarker) {
  101. millis = int.tryParse(value);
  102. } else {
  103. return DateTime.tryParse(value);
  104. }
  105. }
  106. if (millis != null) {
  107. return DateTime.fromMillisecondsSinceEpoch(millis, isUtc: true);
  108. }
  109. }
  110. return null;
  111. }