wheeOs
Committed by GitHub

Update http.dart

1 import 'dart:async'; 1 import 'dart:async';
2 import 'dart:convert'; 2 import 'dart:convert';
  3 +import 'dart:io';
3 4
4 import '../src/certificates/certificates.dart'; 5 import '../src/certificates/certificates.dart';
5 import '../src/exceptions/exceptions.dart'; 6 import '../src/exceptions/exceptions.dart';
@@ -17,6 +18,8 @@ typedef Decoder<T> = T Function(dynamic data); @@ -17,6 +18,8 @@ typedef Decoder<T> = T Function(dynamic data);
17 18
18 typedef Progress = Function(double percent); 19 typedef Progress = Function(double percent);
19 20
  21 +typedef ResponseInterceptor<T> = Future<Response<T>?> Function(Request<T> request, Type targetType, HttpClientResponse response);
  22 +
20 class GetHttpClient { 23 class GetHttpClient {
21 String userAgent; 24 String userAgent;
22 String? baseUrl; 25 String? baseUrl;
@@ -31,6 +34,7 @@ class GetHttpClient { @@ -31,6 +34,7 @@ class GetHttpClient {
31 bool sendContentLength; 34 bool sendContentLength;
32 35
33 Decoder? defaultDecoder; 36 Decoder? defaultDecoder;
  37 + ResponseInterceptor? defaultResponseInterceptor;
34 38
35 Duration timeout; 39 Duration timeout;
36 40
@@ -101,6 +105,7 @@ class GetHttpClient { @@ -101,6 +105,7 @@ class GetHttpClient {
101 String method, 105 String method,
102 Map<String, dynamic>? query, 106 Map<String, dynamic>? query,
103 Decoder<T>? decoder, 107 Decoder<T>? decoder,
  108 + ResponseInterceptor<T>? responseInterceptor,
104 Progress? uploadProgress, 109 Progress? uploadProgress,
105 ) async { 110 ) async {
106 List<int>? bodyBytes; 111 List<int>? bodyBytes;
@@ -113,7 +118,7 @@ class GetHttpClient { @@ -113,7 +118,7 @@ class GetHttpClient {
113 118
114 if (body is FormData) { 119 if (body is FormData) {
115 bodyBytes = await body.toBytes(); 120 bodyBytes = await body.toBytes();
116 - _setContentLenght(headers, bodyBytes.length); 121 + headers['content-length'] = bodyBytes.length.toString();
117 headers['content-type'] = 122 headers['content-type'] =
118 'multipart/form-data; boundary=${body.boundary}'; 123 'multipart/form-data; boundary=${body.boundary}';
119 } else if (contentType != null && 124 } else if (contentType != null &&
@@ -161,6 +166,7 @@ class GetHttpClient { @@ -161,6 +166,7 @@ class GetHttpClient {
161 followRedirects: followRedirects, 166 followRedirects: followRedirects,
162 maxRedirects: maxRedirects, 167 maxRedirects: maxRedirects,
163 decoder: decoder, 168 decoder: decoder,
  169 + responseInterceptor: responseInterceptor
164 ); 170 );
165 } 171 }
166 172
@@ -269,6 +275,7 @@ class GetHttpClient { @@ -269,6 +275,7 @@ class GetHttpClient {
269 String? contentType, 275 String? contentType,
270 Map<String, dynamic>? query, 276 Map<String, dynamic>? query,
271 Decoder<T>? decoder, 277 Decoder<T>? decoder,
  278 + ResponseInterceptor<T>? responseInterceptor,
272 ) { 279 ) {
273 final headers = <String, String>{}; 280 final headers = <String, String>{};
274 _setSimpleHeaders(headers, contentType); 281 _setSimpleHeaders(headers, contentType);
@@ -279,12 +286,21 @@ class GetHttpClient { @@ -279,12 +286,21 @@ class GetHttpClient {
279 url: uri, 286 url: uri,
280 headers: headers, 287 headers: headers,
281 decoder: decoder ?? (defaultDecoder as Decoder<T>?), 288 decoder: decoder ?? (defaultDecoder as Decoder<T>?),
  289 + responseInterceptor: _responseInterceptor(responseInterceptor),
282 contentLength: 0, 290 contentLength: 0,
283 followRedirects: followRedirects, 291 followRedirects: followRedirects,
284 maxRedirects: maxRedirects, 292 maxRedirects: maxRedirects,
285 )); 293 ));
286 } 294 }
287 295
  296 + ResponseInterceptor<T>? _responseInterceptor<T>(ResponseInterceptor<T>? actual) {
  297 + if(actual != null) return actual;
  298 + final defaultInterceptor = defaultResponseInterceptor;
  299 + return defaultInterceptor != null
  300 + ? (request, targetType, response) async => await defaultInterceptor(request, targetType, response) as Response<T>?
  301 + : null;
  302 + }
  303 +
288 Future<Request<T>> _request<T>( 304 Future<Request<T>> _request<T>(
289 String? url, 305 String? url,
290 String method, { 306 String method, {
@@ -292,6 +308,7 @@ class GetHttpClient { @@ -292,6 +308,7 @@ class GetHttpClient {
292 required dynamic body, 308 required dynamic body,
293 required Map<String, dynamic>? query, 309 required Map<String, dynamic>? query,
294 Decoder<T>? decoder, 310 Decoder<T>? decoder,
  311 + ResponseInterceptor<T>? responseInterceptor,
295 required Progress? uploadProgress, 312 required Progress? uploadProgress,
296 }) { 313 }) {
297 return _requestWithBody<T>( 314 return _requestWithBody<T>(
@@ -301,6 +318,7 @@ class GetHttpClient { @@ -301,6 +318,7 @@ class GetHttpClient {
301 method, 318 method,
302 query, 319 query,
303 decoder ?? (defaultDecoder as Decoder<T>?), 320 decoder ?? (defaultDecoder as Decoder<T>?),
  321 + _responseInterceptor(responseInterceptor),
304 uploadProgress, 322 uploadProgress,
305 ); 323 );
306 } 324 }
@@ -310,6 +328,7 @@ class GetHttpClient { @@ -310,6 +328,7 @@ class GetHttpClient {
310 String? contentType, 328 String? contentType,
311 Map<String, dynamic>? query, 329 Map<String, dynamic>? query,
312 Decoder<T>? decoder, 330 Decoder<T>? decoder,
  331 + ResponseInterceptor<T>? responseInterceptor,
313 ) { 332 ) {
314 final headers = <String, String>{}; 333 final headers = <String, String>{};
315 _setSimpleHeaders(headers, contentType); 334 _setSimpleHeaders(headers, contentType);
@@ -320,6 +339,7 @@ class GetHttpClient { @@ -320,6 +339,7 @@ class GetHttpClient {
320 url: uri, 339 url: uri,
321 headers: headers, 340 headers: headers,
322 decoder: decoder ?? (defaultDecoder as Decoder<T>?), 341 decoder: decoder ?? (defaultDecoder as Decoder<T>?),
  342 + responseInterceptor: _responseInterceptor(responseInterceptor),
323 ); 343 );
324 } 344 }
325 345
@@ -330,6 +350,7 @@ class GetHttpClient { @@ -330,6 +350,7 @@ class GetHttpClient {
330 Map<String, String>? headers, 350 Map<String, String>? headers,
331 Map<String, dynamic>? query, 351 Map<String, dynamic>? query,
332 Decoder<T>? decoder, 352 Decoder<T>? decoder,
  353 + ResponseInterceptor<T>? responseInterceptor,
333 Progress? uploadProgress, 354 Progress? uploadProgress,
334 // List<MultipartFile> files, 355 // List<MultipartFile> files,
335 }) async { 356 }) async {
@@ -342,6 +363,7 @@ class GetHttpClient { @@ -342,6 +363,7 @@ class GetHttpClient {
342 body: body, 363 body: body,
343 query: query, 364 query: query,
344 decoder: decoder, 365 decoder: decoder,
  366 + responseInterceptor: responseInterceptor,
345 uploadProgress: uploadProgress, 367 uploadProgress: uploadProgress,
346 ), 368 ),
347 headers: headers, 369 headers: headers,
@@ -364,6 +386,7 @@ class GetHttpClient { @@ -364,6 +386,7 @@ class GetHttpClient {
364 Map<String, String>? headers, 386 Map<String, String>? headers,
365 Map<String, dynamic>? query, 387 Map<String, dynamic>? query,
366 Decoder<T>? decoder, 388 Decoder<T>? decoder,
  389 + ResponseInterceptor<T>? responseInterceptor,
367 Progress? uploadProgress, 390 Progress? uploadProgress,
368 // List<MultipartFile> files, 391 // List<MultipartFile> files,
369 }) async { 392 }) async {
@@ -376,6 +399,7 @@ class GetHttpClient { @@ -376,6 +399,7 @@ class GetHttpClient {
376 body: body, 399 body: body,
377 query: query, 400 query: query,
378 decoder: decoder, 401 decoder: decoder,
  402 + responseInterceptor: responseInterceptor,
379 uploadProgress: uploadProgress, 403 uploadProgress: uploadProgress,
380 ), 404 ),
381 headers: headers, 405 headers: headers,
@@ -399,6 +423,7 @@ class GetHttpClient { @@ -399,6 +423,7 @@ class GetHttpClient {
399 Map<String, String>? headers, 423 Map<String, String>? headers,
400 Map<String, dynamic>? query, 424 Map<String, dynamic>? query,
401 Decoder<T>? decoder, 425 Decoder<T>? decoder,
  426 + ResponseInterceptor<T>? responseInterceptor,
402 Progress? uploadProgress, 427 Progress? uploadProgress,
403 }) async { 428 }) async {
404 try { 429 try {
@@ -410,6 +435,7 @@ class GetHttpClient { @@ -410,6 +435,7 @@ class GetHttpClient {
410 query: query, 435 query: query,
411 body: body, 436 body: body,
412 decoder: decoder, 437 decoder: decoder,
  438 + responseInterceptor: responseInterceptor,
413 uploadProgress: uploadProgress, 439 uploadProgress: uploadProgress,
414 ), 440 ),
415 headers: headers, 441 headers: headers,
@@ -432,6 +458,7 @@ class GetHttpClient { @@ -432,6 +458,7 @@ class GetHttpClient {
432 Map<String, String>? headers, 458 Map<String, String>? headers,
433 Map<String, dynamic>? query, 459 Map<String, dynamic>? query,
434 Decoder<T>? decoder, 460 Decoder<T>? decoder,
  461 + ResponseInterceptor<T>? responseInterceptor,
435 Progress? uploadProgress, 462 Progress? uploadProgress,
436 }) async { 463 }) async {
437 try { 464 try {
@@ -443,6 +470,7 @@ class GetHttpClient { @@ -443,6 +470,7 @@ class GetHttpClient {
443 query: query, 470 query: query,
444 body: body, 471 body: body,
445 decoder: decoder, 472 decoder: decoder,
  473 + responseInterceptor: responseInterceptor,
446 uploadProgress: uploadProgress, 474 uploadProgress: uploadProgress,
447 ), 475 ),
448 headers: headers, 476 headers: headers,
@@ -464,10 +492,11 @@ class GetHttpClient { @@ -464,10 +492,11 @@ class GetHttpClient {
464 String? contentType, 492 String? contentType,
465 Map<String, dynamic>? query, 493 Map<String, dynamic>? query,
466 Decoder<T>? decoder, 494 Decoder<T>? decoder,
  495 + ResponseInterceptor<T>? responseInterceptor,
467 }) async { 496 }) async {
468 try { 497 try {
469 var response = await _performRequest<T>( 498 var response = await _performRequest<T>(
470 - () => _get<T>(url, contentType, query, decoder), 499 + () => _get<T>(url, contentType, query, decoder, responseInterceptor),
471 headers: headers, 500 headers: headers,
472 ); 501 );
473 return response; 502 return response;
@@ -548,10 +577,11 @@ class GetHttpClient { @@ -548,10 +577,11 @@ class GetHttpClient {
548 String? contentType, 577 String? contentType,
549 Map<String, dynamic>? query, 578 Map<String, dynamic>? query,
550 Decoder<T>? decoder, 579 Decoder<T>? decoder,
  580 + ResponseInterceptor<T>? responseInterceptor
551 }) async { 581 }) async {
552 try { 582 try {
553 var response = await _performRequest<T>( 583 var response = await _performRequest<T>(
554 - () async => _delete<T>(url, contentType, query, decoder), 584 + () async => _delete<T>(url, contentType, query, decoder, responseInterceptor),
555 headers: headers, 585 headers: headers,
556 ); 586 );
557 return response; 587 return response;