|
|
@ -34,13 +34,45 @@ class OpenAPIBuilder implements Builder { |
|
|
|
final registeredJsonObjects = <String>[]; |
|
|
|
final registeredJsonObjects = <String>[]; |
|
|
|
final output = <String>[ |
|
|
|
final output = <String>[ |
|
|
|
"import 'dart:convert';", |
|
|
|
"import 'dart:convert';", |
|
|
|
|
|
|
|
"import 'dart:io';", |
|
|
|
"import 'dart:typed_data';", |
|
|
|
"import 'dart:typed_data';", |
|
|
|
'', |
|
|
|
'', |
|
|
|
"import 'package:http/http.dart' as http;", |
|
|
|
"import 'package:cookie_jar/cookie_jar.dart';", |
|
|
|
"import 'package:json_annotation/json_annotation.dart';", |
|
|
|
"import 'package:json_annotation/json_annotation.dart';", |
|
|
|
'', |
|
|
|
'', |
|
|
|
|
|
|
|
"export 'package:cookie_jar/cookie_jar.dart';", |
|
|
|
|
|
|
|
'', |
|
|
|
"part '${p.basename(outputId.changeExtension('.g.dart').path)}';", |
|
|
|
"part '${p.basename(outputId.changeExtension('.g.dart').path)}';", |
|
|
|
'', |
|
|
|
'', |
|
|
|
|
|
|
|
Extension( |
|
|
|
|
|
|
|
(final b) => b |
|
|
|
|
|
|
|
..name = 'HttpClientResponseBody' |
|
|
|
|
|
|
|
..on = refer('HttpClientResponse') |
|
|
|
|
|
|
|
..methods.addAll([ |
|
|
|
|
|
|
|
Method( |
|
|
|
|
|
|
|
(final b) => b |
|
|
|
|
|
|
|
..name = 'bodyBytes' |
|
|
|
|
|
|
|
..returns = refer('Future<Uint8List>') |
|
|
|
|
|
|
|
..type = MethodType.getter |
|
|
|
|
|
|
|
..modifier = MethodModifier.async |
|
|
|
|
|
|
|
..lambda = true |
|
|
|
|
|
|
|
..body = const Code( |
|
|
|
|
|
|
|
'Uint8List.fromList((await toList()).reduce((final value, final element) => [...value, ...element]))', |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
Method( |
|
|
|
|
|
|
|
(final b) => b |
|
|
|
|
|
|
|
..name = 'body' |
|
|
|
|
|
|
|
..returns = refer('Future<String>') |
|
|
|
|
|
|
|
..type = MethodType.getter |
|
|
|
|
|
|
|
..modifier = MethodModifier.async |
|
|
|
|
|
|
|
..lambda = true |
|
|
|
|
|
|
|
..body = const Code( |
|
|
|
|
|
|
|
'utf8.decode(await bodyBytes)', |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
]), |
|
|
|
|
|
|
|
).accept(emitter).toString(), |
|
|
|
Class( |
|
|
|
Class( |
|
|
|
(final b) => b |
|
|
|
(final b) => b |
|
|
|
..name = 'Response' |
|
|
|
..name = 'Response' |
|
|
@ -707,6 +739,19 @@ class OpenAPIBuilder implements Builder { |
|
|
|
..modifier = FieldModifier.final$ |
|
|
|
..modifier = FieldModifier.final$ |
|
|
|
..late = true, |
|
|
|
..late = true, |
|
|
|
), |
|
|
|
), |
|
|
|
|
|
|
|
Field( |
|
|
|
|
|
|
|
(final b) => b |
|
|
|
|
|
|
|
..name = 'httpClient' |
|
|
|
|
|
|
|
..type = refer('HttpClient') |
|
|
|
|
|
|
|
..modifier = FieldModifier.final$ |
|
|
|
|
|
|
|
..late = true, |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
Field( |
|
|
|
|
|
|
|
(final b) => b |
|
|
|
|
|
|
|
..name = 'cookieJar' |
|
|
|
|
|
|
|
..type = refer('CookieJar?') |
|
|
|
|
|
|
|
..modifier = FieldModifier.final$, |
|
|
|
|
|
|
|
), |
|
|
|
if (hasAnySecurity) ...[ |
|
|
|
if (hasAnySecurity) ...[ |
|
|
|
Field( |
|
|
|
Field( |
|
|
|
(final b) => b |
|
|
|
(final b) => b |
|
|
@ -733,6 +778,18 @@ class OpenAPIBuilder implements Builder { |
|
|
|
..type = refer('Map<String, String>?') |
|
|
|
..type = refer('Map<String, String>?') |
|
|
|
..named = true, |
|
|
|
..named = true, |
|
|
|
), |
|
|
|
), |
|
|
|
|
|
|
|
Parameter( |
|
|
|
|
|
|
|
(final b) => b |
|
|
|
|
|
|
|
..name = 'httpClient' |
|
|
|
|
|
|
|
..type = refer('HttpClient?') |
|
|
|
|
|
|
|
..named = true, |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
Parameter( |
|
|
|
|
|
|
|
(final b) => b |
|
|
|
|
|
|
|
..name = 'cookieJar' |
|
|
|
|
|
|
|
..toThis = true |
|
|
|
|
|
|
|
..named = true, |
|
|
|
|
|
|
|
), |
|
|
|
if (hasAnySecurity) ...[ |
|
|
|
if (hasAnySecurity) ...[ |
|
|
|
Parameter( |
|
|
|
Parameter( |
|
|
|
(final b) => b |
|
|
|
(final b) => b |
|
|
@ -753,6 +810,7 @@ class OpenAPIBuilder implements Builder { |
|
|
|
}, |
|
|
|
}, |
|
|
|
''' : ''} |
|
|
|
''' : ''} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
this.httpClient = httpClient ?? HttpClient(); |
|
|
|
'''), |
|
|
|
'''), |
|
|
|
), |
|
|
|
), |
|
|
|
) |
|
|
|
) |
|
|
@ -797,18 +855,30 @@ class OpenAPIBuilder implements Builder { |
|
|
|
), |
|
|
|
), |
|
|
|
]) |
|
|
|
]) |
|
|
|
..body = const Code(r''' |
|
|
|
..body = const Code(r''' |
|
|
|
final request = http.Request(method, Uri.parse('$baseURL$path')); |
|
|
|
final uri = Uri.parse('$baseURL$path'); |
|
|
|
request.headers.addAll(baseHeaders); |
|
|
|
final request = await httpClient.openUrl(method, uri); |
|
|
|
request.headers.addAll(headers); |
|
|
|
for (final header in {...baseHeaders, ...headers}.entries) { |
|
|
|
|
|
|
|
request.headers.add(header.key, header.value); |
|
|
|
|
|
|
|
} |
|
|
|
if (body != null) { |
|
|
|
if (body != null) { |
|
|
|
request.bodyBytes = body.toList(); |
|
|
|
request.add(body.toList()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (cookieJar != null) { |
|
|
|
|
|
|
|
request.cookies.addAll(await cookieJar!.loadForRequest(uri)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
final response = await http.Response.fromStream(await request.send()); |
|
|
|
final response = await request.close(); |
|
|
|
|
|
|
|
if (cookieJar != null) { |
|
|
|
|
|
|
|
await cookieJar!.saveFromResponse(uri, response.cookies); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
final responseHeaders = <String, String>{}; |
|
|
|
|
|
|
|
response.headers.forEach((final name, final values) { |
|
|
|
|
|
|
|
responseHeaders[name] = values.last; |
|
|
|
|
|
|
|
}); |
|
|
|
return Response( |
|
|
|
return Response( |
|
|
|
response.statusCode, |
|
|
|
response.statusCode, |
|
|
|
response.headers, |
|
|
|
responseHeaders, |
|
|
|
response.bodyBytes, |
|
|
|
await response.bodyBytes, |
|
|
|
); |
|
|
|
); |
|
|
|
'''), |
|
|
|
'''), |
|
|
|
), |
|
|
|
), |
|
|
|