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