Browse Source

feat(dynamite): Support multiple status codes with same schema

Signed-off-by: jld3103 <jld3103yt@gmail.com>
pull/715/head
jld3103 1 year ago
parent
commit
033e1088e9
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 42
      packages/dynamite/dynamite/lib/src/builder/client.dart

42
packages/dynamite/dynamite/lib/src/builder/client.dart

@ -8,6 +8,7 @@ import 'package:dynamite/src/helpers/dynamite.dart';
import 'package:dynamite/src/helpers/type_result.dart';
import 'package:dynamite/src/models/open_api.dart';
import 'package:dynamite/src/models/path_item.dart';
import 'package:dynamite/src/models/response.dart';
import 'package:dynamite/src/models/schema.dart';
import 'package:dynamite/src/type_result/type_result.dart';
@ -297,13 +298,28 @@ Iterable<Method> buildTags(
b.annotations.add(refer('Deprecated').call([refer("''")]));
}
final acceptHeader = operation.responses?.values
var responses = <Response, List<int>>{};
if (operation.responses != null) {
for (final responseEntry in operation.responses!.entries) {
final statusCode = int.parse(responseEntry.key);
final response = responseEntry.value;
responses[response] ??= [];
responses[response]!.add(statusCode);
}
if (responses.length > 1) {
print('$operationId uses more than one response schema but we only generate the first one');
responses = Map.fromEntries([responses.entries.first]);
}
}
final acceptHeader = responses.keys
.map((final response) => response.content?.keys)
.whereNotNull()
.expand((final element) => element)
.toSet()
.join(',') ??
'';
.join(',');
final code = StringBuffer('''
var _path = '${pathEntry.key}';
final _queryParameters = <String, dynamic>{};
@ -497,14 +513,12 @@ final _response = await ${isRootClient ? 'this' : '_rootClient'}.doRequest(
''',
);
if (operation.responses != null) {
if (operation.responses!.length > 1) {
throw Exception('Can not work with multiple status codes right now');
}
for (final responseEntry in operation.responses!.entries) {
final statusCode = responseEntry.key;
final response = responseEntry.value;
code.write('if (_response.statusCode == $statusCode) {');
for (final responseEntry in responses.entries) {
final response = responseEntry.key;
final statusCodes = responseEntry.value;
code.write(
'if (${statusCodes.map((final statusCode) => '_response.statusCode == $statusCode').join(' || ')}) {',
);
String? headersType;
String? headersValue;
@ -544,7 +558,7 @@ final _response = await ${isRootClient ? 'this' : '_rootClient'}.doRequest(
spec,
state,
toDartName(
'$operationId-response-$statusCode-$mimeType',
'$operationId-response${responses.entries.length > 1 ? '-${responses.entries.toList().indexOf(responseEntry)}' : ''}-$mimeType',
uppercaseFirstCharacter: true,
),
mediaType.schema!,
@ -599,9 +613,7 @@ final _response = await ${isRootClient ? 'this' : '_rootClient'}.doRequest(
code.write(
'throw await ${state.classPrefix}ApiException.fromResponse(_response); // coverage:ignore-line\n',
);
} else {
b.returns = refer('Future');
}
b.body = Code(code.toString());
},
);

Loading…
Cancel
Save