@@ -130,28 +130,26 @@ class MojoClient {
130130 ///
131131 /// The Future will emit a [ClientException] if the response doesn't have a
132132 /// success status code.
133- Future <mojo.MojoDataPipeConsumer > readDataPipe (dynamic url, { Map <String , String > headers }) async {
133+ Future <mojo.MojoDataPipeConsumer > readDataPipe (dynamic url, { Map <String , String > headers }) {
134+ Completer <mojo.MojoDataPipeConsumer > completer = new Completer <mojo.MojoDataPipeConsumer >();
134135 mojom.UrlLoaderProxy loader = new mojom.UrlLoaderProxy .unbound ();
135- mojom.UrlRequest request = _prepareRequest ('GET' , url, headers);
136- mojom.UrlResponse response;
137- try {
138- networkService.createUrlLoader (loader);
139- response = (await loader.start (request)).response;
140- } catch (exception, stack) {
141- FlutterError .reportError (new FlutterErrorDetails (
142- exception: exception,
143- stack: stack,
144- library: 'networking HTTP library' ,
145- context: 'while sending bytes to the Mojo network library' ,
146- silent: true
147- ));
148- return null ;
149- } finally {
136+ networkService.createUrlLoader (loader);
137+ loader.start (_prepareRequest ('GET' , url, headers), (mojom.UrlResponse response) {
150138 loader.close ();
151- }
152- if (response.statusCode < 400 )
153- return response.body;
154- throw new Exception ("Request to $url failed with status ${response .statusCode }." );
139+ if (response.statusCode < 400 ) {
140+ completer.complete (response.body);
141+ } else {
142+ Exception exception = new Exception ("Request to $url failed with status ${response .statusCode }." );
143+ FlutterError .reportError (new FlutterErrorDetails (
144+ exception: exception,
145+ library: 'networking HTTP library' ,
146+ context: 'while sending bytes to the Mojo network library' ,
147+ silent: true
148+ ));
149+ completer.completeError (exception);
150+ }
151+ });
152+ return completer.future;
155153 }
156154
157155 mojom.UrlRequest _prepareRequest (String method, dynamic url, Map <String , String > headers, [dynamic body, Encoding encoding = UTF8 ]) {
@@ -177,35 +175,37 @@ class MojoClient {
177175 return request;
178176 }
179177
180- Future <Response > _send (String method, dynamic url, Map <String , String > headers, [dynamic body, Encoding encoding = UTF8 ]) async {
178+ Future <Response > _send (String method, dynamic url, Map <String , String > headers, [dynamic body, Encoding encoding = UTF8 ]) {
179+ Completer <Response > completer = new Completer <Response >();
181180 mojom.UrlLoaderProxy loader = new mojom.UrlLoaderProxy .unbound ();
181+ networkService.createUrlLoader (loader);
182182 mojom.UrlRequest request = _prepareRequest (method, url, headers, body, encoding);
183- try {
184- networkService.createUrlLoader (loader);
185- mojom.UrlResponse response = (await loader.start (request)).response;
186- ByteData data = await mojo.DataPipeDrainer .drainHandle (response.body);
187- Uint8List bodyBytes = new Uint8List .view (data.buffer);
188- Map <String , String > headers = < String , String > {};
189- if (response.headers != null ) {
190- for (mojom.HttpHeader header in response.headers) {
191- String headerName = header.name.toLowerCase ();
192- String existingValue = headers[headerName];
193- headers[headerName] = existingValue != null ? '$existingValue , ${header .value }' : header.value;
183+ loader.start (request, (mojom.UrlResponse response) async {
184+ loader.close ();
185+ try {
186+ ByteData data = await mojo.DataPipeDrainer .drainHandle (response.body);
187+ Uint8List bodyBytes = new Uint8List .view (data.buffer);
188+ Map <String , String > headers = < String , String > {};
189+ if (response.headers != null ) {
190+ for (mojom.HttpHeader header in response.headers) {
191+ String headerName = header.name.toLowerCase ();
192+ String existingValue = headers[headerName];
193+ headers[headerName] = existingValue != null ? '$existingValue , ${header .value }' : header.value;
194+ }
194195 }
196+ completer.complete (new Response .bytes (bodyBytes, response.statusCode, headers: headers));
197+ } catch (exception, stack) {
198+ FlutterError .reportError (new FlutterErrorDetails (
199+ exception: exception,
200+ stack: stack,
201+ library: 'networking HTTP library' ,
202+ context: 'while sending bytes to the Mojo network library' ,
203+ silent: true
204+ ));
205+ completer.complete (new Response .bytes (null , 500 ));
195206 }
196- return new Response .bytes (bodyBytes, response.statusCode, headers: headers);
197- } catch (exception, stack) {
198- FlutterError .reportError (new FlutterErrorDetails (
199- exception: exception,
200- stack: stack,
201- library: 'networking HTTP library' ,
202- context: 'while sending bytes to the Mojo network library' ,
203- silent: true
204- ));
205- return new Response .bytes (null , 500 );
206- } finally {
207- loader.close ();
208- }
207+ });
208+ return completer.future;
209209 }
210210
211211 void _checkResponseSuccess (dynamic url, Response response) {
0 commit comments