Line data Source code
1 : import 'dart:convert'; 2 : import 'dart:typed_data'; 3 : import 'options.dart'; 4 : import 'redirect_record.dart'; 5 : 6 : typedef CancelWrapper = Future Function(Future); 7 : typedef VoidCallback = dynamic Function(); 8 : 9 : /// HttpAdapter is a bridge between Dio and HttpClient. 10 : /// 11 : /// Dio: Implements standard and friendly API for developer. 12 : /// 13 : /// HttpClient: It is the real object that makes Http 14 : /// requests. 15 : /// 16 : /// We can use any HttpClient not just "dart:io:HttpClient" to 17 : /// make the Http request. All we need is providing a [HttpClientAdapter]. 18 : /// 19 : /// The default HttpClientAdapter for Dio is [DefaultHttpClientAdapter]. 20 : /// 21 : /// ```dart 22 : /// dio.httpClientAdapter = DefaultHttpClientAdapter(); 23 : /// ``` 24 : abstract class HttpClientAdapter { 25 : /// We should implement this method to make real http requests. 26 : /// 27 : /// [options]: The request options 28 : /// 29 : /// [requestStream] The request stream, It will not be null 30 : /// only when http method is one of "POST","PUT","PATCH" 31 : /// and the request body is not empty. 32 : /// 33 : /// We should give priority to using requestStream(not options.data) as request data. 34 : /// because supporting stream ensures the `onSendProgress` works. 35 : /// 36 : /// [cancelFuture]: When cancelled the request, [cancelFuture] will be resolved! 37 : /// you can listen cancel event by it, for example: 38 : /// 39 : /// ```dart 40 : /// cancelFuture?.then((_)=>print("request cancelled!")) 41 : /// ``` 42 : /// [cancelFuture]: will be null when the request is not set [CancelToken]. 43 : 44 : Future<ResponseBody> fetch( 45 : RequestOptions options, 46 : Stream<List<int>> requestStream, 47 : Future cancelFuture, 48 : ); 49 : 50 : void close({bool force = false}); 51 : } 52 : 53 : class ResponseBody { 54 2 : ResponseBody( 55 : this.stream, 56 : this.statusCode, { 57 : this.headers, 58 : this.statusMessage, 59 : this.isRedirect, 60 : this.redirects, 61 : }); 62 : 63 : /// The response stream 64 : Stream<Uint8List> stream; 65 : 66 : /// the response headers 67 : Map<String, List<String>> headers; 68 : 69 : /// Http status code 70 : int statusCode; 71 : 72 : /// Returns the reason phrase associated with the status code. 73 : /// The reason phrase must be set before the body is written 74 : /// to. Setting the reason phrase after writing to the body. 75 : String statusMessage; 76 : 77 : /// Whether this response is a redirect. 78 : final bool isRedirect; 79 : 80 : List<RedirectRecord> redirects; 81 : 82 : Map<String, dynamic> extra = {}; 83 : 84 1 : ResponseBody.fromString( 85 : String text, 86 : this.statusCode, { 87 : this.headers, 88 : this.statusMessage, 89 : this.isRedirect, 90 1 : }) : stream = Stream.fromIterable( 91 6 : utf8.encode(text).map((e) => Uint8List.fromList([e])).toList()); 92 : 93 1 : ResponseBody.fromBytes( 94 : List<int> bytes, 95 : this.statusCode, { 96 : this.headers, 97 : this.statusMessage, 98 : this.isRedirect, 99 1 : }) : stream = Stream.fromIterable( 100 5 : bytes.map((e) => Uint8List.fromList([e])).toList()); 101 : }