|
| 1 | +// ignore_for_file: type=lint |
| 2 | + |
| 3 | +import 'package:json_annotation/json_annotation.dart'; |
| 4 | +import 'package:collection/collection.dart'; |
| 5 | +import 'dart:convert'; |
| 6 | + |
| 7 | +import 'package:chopper/chopper.dart'; |
| 8 | + |
| 9 | +import 'client_mapping.dart'; |
| 10 | +import 'dart:async'; |
| 11 | +import 'package:http/http.dart' as http; |
| 12 | +import 'package:http/http.dart' show MultipartFile; |
| 13 | +import 'package:chopper/chopper.dart' as chopper; |
| 14 | + |
| 15 | +part 'openapi.swagger.chopper.dart'; |
| 16 | + |
| 17 | +// ************************************************************************** |
| 18 | +// SwaggerChopperGenerator |
| 19 | +// ************************************************************************** |
| 20 | + |
| 21 | +@ChopperApi() |
| 22 | +abstract class Openapi extends ChopperService { |
| 23 | + static Openapi create({ |
| 24 | + ChopperClient? client, |
| 25 | + http.Client? httpClient, |
| 26 | + Authenticator? authenticator, |
| 27 | + ErrorConverter? errorConverter, |
| 28 | + Converter? converter, |
| 29 | + Uri? baseUrl, |
| 30 | + Iterable<dynamic>? interceptors, |
| 31 | + }) { |
| 32 | + if (client != null) { |
| 33 | + return _$Openapi(client); |
| 34 | + } |
| 35 | + |
| 36 | + final newClient = ChopperClient( |
| 37 | + services: [_$Openapi()], |
| 38 | + converter: converter ?? $JsonSerializableConverter(), |
| 39 | + interceptors: interceptors ?? [], |
| 40 | + client: httpClient, |
| 41 | + authenticator: authenticator, |
| 42 | + errorConverter: errorConverter, |
| 43 | + baseUrl: baseUrl ?? Uri.parse('http://')); |
| 44 | + return _$Openapi(newClient); |
| 45 | + } |
| 46 | + |
| 47 | + /// |
| 48 | + ///@param name Get parameter `name` from request url query. |
| 49 | + Future<chopper.Response<String>> helloGet({String? name}) { |
| 50 | + return _helloGet(name: name); |
| 51 | + } |
| 52 | + |
| 53 | + /// |
| 54 | + ///@param name Get parameter `name` from request url query. |
| 55 | + @Get(path: '/hello') |
| 56 | + Future<chopper.Response<String>> _helloGet({@Query('name') String? name}); |
| 57 | +} |
| 58 | + |
| 59 | +typedef $JsonFactory<T> = T Function(Map<String, dynamic> json); |
| 60 | + |
| 61 | +class $CustomJsonDecoder { |
| 62 | + $CustomJsonDecoder(this.factories); |
| 63 | + |
| 64 | + final Map<Type, $JsonFactory> factories; |
| 65 | + |
| 66 | + dynamic decode<T>(dynamic entity) { |
| 67 | + if (entity is Iterable) { |
| 68 | + return _decodeList<T>(entity); |
| 69 | + } |
| 70 | + |
| 71 | + if (entity is T) { |
| 72 | + return entity; |
| 73 | + } |
| 74 | + |
| 75 | + if (isTypeOf<T, Map>()) { |
| 76 | + return entity; |
| 77 | + } |
| 78 | + |
| 79 | + if (isTypeOf<T, Iterable>()) { |
| 80 | + return entity; |
| 81 | + } |
| 82 | + |
| 83 | + if (entity is Map<String, dynamic>) { |
| 84 | + return _decodeMap<T>(entity); |
| 85 | + } |
| 86 | + |
| 87 | + return entity; |
| 88 | + } |
| 89 | + |
| 90 | + T _decodeMap<T>(Map<String, dynamic> values) { |
| 91 | + final jsonFactory = factories[T]; |
| 92 | + if (jsonFactory == null || jsonFactory is! $JsonFactory<T>) { |
| 93 | + return throw "Could not find factory for type $T. Is '$T: $T.fromJsonFactory' included in the CustomJsonDecoder instance creation in bootstrapper.dart?"; |
| 94 | + } |
| 95 | + |
| 96 | + return jsonFactory(values); |
| 97 | + } |
| 98 | + |
| 99 | + List<T> _decodeList<T>(Iterable values) => |
| 100 | + values.where((v) => v != null).map<T>((v) => decode<T>(v) as T).toList(); |
| 101 | +} |
| 102 | + |
| 103 | +class $JsonSerializableConverter extends chopper.JsonConverter { |
| 104 | + @override |
| 105 | + FutureOr<chopper.Response<ResultType>> convertResponse<ResultType, Item>( |
| 106 | + chopper.Response response) async { |
| 107 | + if (response.bodyString.isEmpty) { |
| 108 | + // In rare cases, when let's say 204 (no content) is returned - |
| 109 | + // we cannot decode the missing json with the result type specified |
| 110 | + return chopper.Response(response.base, null, error: response.error); |
| 111 | + } |
| 112 | + |
| 113 | + if (ResultType == String) { |
| 114 | + return response.copyWith(); |
| 115 | + } |
| 116 | + |
| 117 | + if (ResultType == DateTime) { |
| 118 | + return response.copyWith( |
| 119 | + body: DateTime.parse((response.body as String).replaceAll('"', '')) |
| 120 | + as ResultType); |
| 121 | + } |
| 122 | + |
| 123 | + final jsonRes = await super.convertResponse(response); |
| 124 | + return jsonRes.copyWith<ResultType>( |
| 125 | + body: $jsonDecoder.decode<Item>(jsonRes.body) as ResultType); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +final $jsonDecoder = $CustomJsonDecoder(generatedMapping); |
| 130 | + |
| 131 | +// ignore: unused_element |
| 132 | +String? _dateToJson(DateTime? date) { |
| 133 | + if (date == null) { |
| 134 | + return null; |
| 135 | + } |
| 136 | + |
| 137 | + final year = date.year.toString(); |
| 138 | + final month = date.month < 10 ? '0${date.month}' : date.month.toString(); |
| 139 | + final day = date.day < 10 ? '0${date.day}' : date.day.toString(); |
| 140 | + |
| 141 | + return '$year-$month-$day'; |
| 142 | +} |
| 143 | + |
| 144 | +class Wrapped<T> { |
| 145 | + final T value; |
| 146 | + const Wrapped.value(this.value); |
| 147 | +} |
0 commit comments