@@ -126,11 +126,14 @@ class ClientGenerator extends BaseGenerator {
126
126
file.writeAsStringSync ("""
127
127
${getHeader (ignoreForFile : 'invalid_annotation_target, unused_import' )}
128
128
129
- import 'dart:io' as io;
130
129
import 'dart:convert';
131
- import 'package:http/http. dart' as http ;
130
+ import 'dart:io ' as io ;
132
131
import 'dart:typed_data';
132
+
133
+ import 'package:http/http.dart' as http;
133
134
import 'package:http/retry.dart';
135
+ import 'package:meta/meta.dart';
136
+
134
137
import 'schema/schema.dart';
135
138
136
139
/// Enum of HTTP methods
@@ -234,6 +237,17 @@ class $clientName {
234
237
Future<http.BaseRequest> onRequest(http.BaseRequest request) {
235
238
return Future.value(request);
236
239
}
240
+
241
+ // ------------------------------------------
242
+ // METHOD: onStreamedResponse
243
+ // ------------------------------------------
244
+
245
+ /// Middleware for HTTP streamed responses (user can override)
246
+ Future<http.StreamedResponse> onStreamedResponse(
247
+ final http.StreamedResponse response,
248
+ ) {
249
+ return Future.value(response);
250
+ }
237
251
238
252
// ------------------------------------------
239
253
// METHOD: onResponse
@@ -243,13 +257,14 @@ class $clientName {
243
257
Future<http.Response> onResponse(http.Response response) {
244
258
return Future.value(response);
245
259
}
246
-
260
+
247
261
// ------------------------------------------
248
- // METHOD: _request
262
+ // METHOD: makeRequestStream
249
263
// ------------------------------------------
250
264
251
- /// Reusable request method
252
- Future<http.Response> _request({
265
+ /// Reusable request stream method
266
+ @protected
267
+ Future<http.StreamedResponse> makeRequestStream({
253
268
required String baseUrl,
254
269
required String path,
255
270
required HttpMethod method,
@@ -303,7 +318,7 @@ class $clientName {
303
318
headers.addAll(this.headers);
304
319
305
320
// Build the request object
306
- late http.Response response;
321
+ late http.StreamedResponse response;
307
322
try {
308
323
http.BaseRequest request;
309
324
if (isMultipart) {
@@ -341,10 +356,10 @@ class $clientName {
341
356
request = await onRequest(request);
342
357
343
358
// Submit request
344
- response = await http.Response.fromStream(await client.send(request) );
359
+ response = await client.send(request);
345
360
346
361
// Handle user response middleware
347
- response = await onResponse (response);
362
+ response = await onStreamedResponse (response);
348
363
} catch (e) {
349
364
// Handle request and response errors
350
365
throw $clientException (
@@ -366,8 +381,53 @@ class $clientName {
366
381
method: method,
367
382
message: 'Unsuccessful response',
368
383
code: response.statusCode,
369
- body: response.body,
384
+ body: (await http.Response.fromStream( response)) .body,
370
385
);
386
+ }
387
+
388
+ // ------------------------------------------
389
+ // METHOD: makeRequest
390
+ // ------------------------------------------
391
+
392
+ /// Reusable request method
393
+ @protected
394
+ Future<http.Response> makeRequest({
395
+ required String baseUrl,
396
+ required String path,
397
+ required HttpMethod method,
398
+ Map<String, dynamic> queryParams = const {},
399
+ Map<String, String> headerParams = const {},
400
+ bool isMultipart = false,
401
+ String requestType = '',
402
+ String responseType = '',
403
+ Object? body,
404
+ }) async {
405
+ try {
406
+ final streamedResponse = await makeRequestStream(
407
+ baseUrl: baseUrl,
408
+ path: path,
409
+ method: method,
410
+ queryParams: queryParams,
411
+ headerParams: headerParams,
412
+ requestType: requestType,
413
+ responseType: responseType,
414
+ body: body,
415
+ );
416
+ final response = await http.Response.fromStream(streamedResponse);
417
+
418
+ // Handle user response middleware
419
+ return await onResponse(response);
420
+ } on $clientException {
421
+ rethrow;
422
+ } catch (e) {
423
+ // Handle request and response errors
424
+ throw $clientException (
425
+ uri: Uri.parse((this.baseUrl ?? baseUrl) + path),
426
+ method: method,
427
+ message: 'Response error',
428
+ body: e,
429
+ );
430
+ }
371
431
}\n
372
432
""" );
373
433
@@ -857,7 +917,7 @@ class $clientName {
857
917
/// `${method .name .toUpperCase ()}` `$uriDecoded `
858
918
Future<$returnType > $methodName ($inputCode ) async {
859
919
860
- final ${returnType == 'void' ? '_' : 'r' } = await _request (
920
+ final ${returnType == 'void' ? '_' : 'r' } = await makeRequest (
861
921
baseUrl: '$baseUrlDecoded ',
862
922
path: '$path ',
863
923
method: $method ,
0 commit comments