Commit 31f6b8f6 authored by Collin Jackson's avatar Collin Jackson

Make fetch.dart and mojo_client.dart share a network service singleton

parent b07d55d1
...@@ -16,14 +16,6 @@ import 'package:mojo/mojo/http_header.mojom.dart' as mojo; ...@@ -16,14 +16,6 @@ import 'package:mojo/mojo/http_header.mojom.dart' as mojo;
import 'response.dart'; import 'response.dart';
mojo.NetworkServiceProxy _initNetworkService() {
mojo.NetworkServiceProxy networkService = new mojo.NetworkServiceProxy.unbound();
shell.connectToService("mojo:authenticated_network_service", networkService);
return networkService;
}
final mojo.NetworkServiceProxy _networkService = _initNetworkService();
/// A `mojo`-based HTTP client /// A `mojo`-based HTTP client
class MojoClient { class MojoClient {
...@@ -84,12 +76,12 @@ class MojoClient { ...@@ -84,12 +76,12 @@ class MojoClient {
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 bodyBytes = new Uint8List.view(data.buffer); Uint8List bodyBytes = new Uint8List.view(data.buffer);
String body = new String.fromCharCodes(bodyBytes); String bodyString = new String.fromCharCodes(bodyBytes);
return new Response(body: body, bodyBytes: bodyBytes, statusCode: response.statusCode); return new Response(body: bodyString, bodyBytes: bodyBytes, statusCode: response.statusCode);
} catch (e) { } catch (e) {
print("NetworkService unavailable $e"); print("NetworkService unavailable $e");
return new Response(statusCode: 500); return new Response(statusCode: 500);
...@@ -105,4 +97,12 @@ class MojoClient { ...@@ -105,4 +97,12 @@ class MojoClient {
} }
void close() {} void close() {}
static mojo.NetworkServiceProxy _initNetworkService() {
mojo.NetworkServiceProxy proxy = new mojo.NetworkServiceProxy.unbound();
shell.connectToService("mojo:authenticated_network_service", proxy);
return proxy;
}
static final mojo.NetworkServiceProxy networkService = _initNetworkService();
} }
...@@ -40,7 +40,9 @@ class NetworkAssetBundle extends AssetBundle { ...@@ -40,7 +40,9 @@ class NetworkAssetBundle extends AssetBundle {
ImageResource loadImage(String key) => imageCache.load(_urlFromKey(key)); ImageResource loadImage(String key) => imageCache.load(_urlFromKey(key));
Future<String> loadString(String key) => http.get(_urlFromKey(key)); Future<String> loadString(String key) async {
return (await http.get(_urlFromKey(key))).body;
}
} }
Future _fetchAndUnpackBundle(String relativeUrl, AssetBundleProxy bundle) async { Future _fetchAndUnpackBundle(String relativeUrl, AssetBundleProxy bundle) async {
......
...@@ -3,30 +3,19 @@ ...@@ -3,30 +3,19 @@
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:async'; import 'dart:async';
import 'dart:typed_data';
import 'package:mojo/core.dart' as core;
import 'package:mojo/mojo/url_request.mojom.dart'; import 'package:mojo/mojo/url_request.mojom.dart';
import 'package:mojo/mojo/url_response.mojom.dart'; import 'package:mojo/mojo/url_response.mojom.dart';
import 'package:mojo_services/mojo/network_service.mojom.dart';
import 'package:mojo_services/mojo/url_loader.mojom.dart'; import 'package:mojo_services/mojo/url_loader.mojom.dart';
import 'shell.dart'; import '../http/mojo_client.dart';
export 'package:mojo/mojo/url_response.mojom.dart' show UrlResponse; export 'package:mojo/mojo/url_response.mojom.dart' show UrlResponse;
NetworkServiceProxy _initNetworkService() {
NetworkServiceProxy networkService = new NetworkServiceProxy.unbound();
shell.connectToService("mojo:authenticated_network_service", networkService);
return networkService;
}
final NetworkServiceProxy _networkService = _initNetworkService();
Future<UrlResponse> fetch(UrlRequest request) async { Future<UrlResponse> fetch(UrlRequest request) async {
UrlLoaderProxy loader = new UrlLoaderProxy.unbound(); UrlLoaderProxy loader = new UrlLoaderProxy.unbound();
try { try {
_networkService.ptr.createUrlLoader(loader); MojoClient.networkService.ptr.createUrlLoader(loader);
UrlResponse response = (await loader.ptr.start(request)).response; UrlResponse response = (await loader.ptr.start(request)).response;
return response; return response;
} catch (e) { } catch (e) {
......
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