Commit c685cb80 authored by Adam Barth's avatar Adam Barth

Fix analyzer warnings in mojo_client.dart

parent 953f1904
...@@ -2,19 +2,16 @@ ...@@ -2,19 +2,16 @@
// for details. All rights reserved. Use of this source code is governed by a // for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file. // BSD-style license that can be found in the LICENSE file.
library base_client;
import 'dart:async'; import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:flutter/src/services/shell.dart';
import 'package:mojo_services/mojo/network_service.mojom.dart' as mojo;
import 'package:mojo_services/mojo/url_loader.mojom.dart' as mojo;
import 'package:mojo/core.dart' as mojo; import 'package:mojo/core.dart' as mojo;
import 'package:mojo/mojo/url_request.mojom.dart' as mojo; import 'package:mojo/mojo/url_request.mojom.dart' as mojo;
import 'package:mojo/mojo/url_response.mojom.dart' as mojo; import 'package:mojo/mojo/url_response.mojom.dart' as mojo;
import 'package:mojo_services/mojo/network_service.mojom.dart' as mojo;
import 'package:mojo_services/mojo/url_loader.mojom.dart' as mojo;
import 'package:flutter/src/services/shell.dart';
import 'response.dart'; import 'response.dart';
...@@ -74,32 +71,29 @@ class MojoClient { ...@@ -74,32 +71,29 @@ class MojoClient {
if (body != null) { if (body != null) {
mojo.MojoDataPipe pipe = new mojo.MojoDataPipe(); mojo.MojoDataPipe pipe = new mojo.MojoDataPipe();
request.body = <mojo.MojoDataPipeConsumer>[pipe.consumer]; request.body = <mojo.MojoDataPipeConsumer>[pipe.consumer];
ByteData data = new ByteData.view(UTF8.encode(body).buffer); Uint8List encodedBody = UTF8.encode(body);
ByteData data = new ByteData.view(encodedBody.buffer);
mojo.DataPipeFiller.fillHandle(pipe.producer, data); mojo.DataPipeFiller.fillHandle(pipe.producer, data);
} }
try { try {
_networkService.ptr.createUrlLoader(loader); _networkService.ptr.createUrlLoader(loader);
mojo.UrlResponse response = (await loader.ptr.start(request)).response; mojo.UrlResponse response = (await loader.ptr.start(request)).response;
ByteData data = await mojo.DataPipeDrainer.drainHandle(response.body); ByteData data = await mojo.DataPipeDrainer.drainHandle(response.body);
Uint8List bodyAsList = new Uint8List.view(data.buffer); Uint8List bodyBytes = new Uint8List.view(data.buffer);
String bodyAsString = new String.fromCharCodes(bodyAsList); String body = new String.fromCharCodes(bodyBytes);
return new Response(bodyAsString, response.statusCode); return new Response(body: body, bodyBytes: bodyBytes, statusCode: response.statusCode);
} catch (e) { } catch (e) {
print("NetworkService unavailable $e"); print("NetworkService unavailable $e");
return new Response(null, 500); return new Response(statusCode: 500);
} finally { } finally {
loader.close(); loader.close();
} }
} }
void _checkResponseSuccess(url, Response response) { void _checkResponseSuccess(url, Response response) {
if (response.statusCode < 400) return; if (response.statusCode < 400)
var message = "Request to $url failed with status ${response.statusCode}"; return;
if (response.reasonPhrase != null) { throw new Exception("Request to $url failed with status ${response.statusCode}.");
message = "$message: ${response.reasonPhrase}";
}
if (url is String) url = Uri.parse(url);
throw new ClientException("$message.", url);
} }
void close() {} void close() {}
......
...@@ -2,11 +2,12 @@ ...@@ -2,11 +2,12 @@
// for details. All rights reserved. Use of this source code is governed by a // for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file. // BSD-style license that can be found in the LICENSE file.
library response; import 'dart:typed_data';
/// An HTTP response where the entire response body is known in advance. /// An HTTP response where the entire response body is known in advance.
class Response { class Response {
const Response(this.body, this.statusCode); const Response({ this.body, this.bodyBytes, this.statusCode });
final String body; final String body;
final Uint8List bodyBytes;
final int statusCode; final int statusCode;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment