Commit 6f0464c0 authored by Collin Jackson's avatar Collin Jackson

Merge pull request #418 from collinjackson/redundant_fetch

Remove redundant fetch functions and avoid crashing when no headers are set
parents 725474c6 31f6b8f6
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
import 'dart:convert'; import 'dart:convert';
import 'dart:math' as math; import 'dart:math' as math;
import 'package:flutter/services.dart'; import 'package:flutter/http.dart' as http;
final math.Random _rng = new math.Random(); final math.Random _rng = new math.Random();
...@@ -83,8 +83,8 @@ class StockDataFetcher { ...@@ -83,8 +83,8 @@ class StockDataFetcher {
if (!actuallyFetchData) if (!actuallyFetchData)
return; return;
fetchBody(_urlToFetch(_nextChunk++)).then((Response response) { http.get(_urlToFetch(_nextChunk++)).then((http.Response response) {
String json = response.bodyAsString(); String json = response.body;
if (json == null) { if (json == null) {
print("Failed to load stock data chunk ${_nextChunk - 1}"); print("Failed to load stock data chunk ${_nextChunk - 1}");
return; return;
......
...@@ -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 {
...@@ -66,7 +58,7 @@ class MojoClient { ...@@ -66,7 +58,7 @@ class MojoClient {
Map<String, String> headers, [body, Encoding encoding]) async { Map<String, String> headers, [body, Encoding encoding]) async {
mojo.UrlLoaderProxy loader = new mojo.UrlLoaderProxy.unbound(); mojo.UrlLoaderProxy loader = new mojo.UrlLoaderProxy.unbound();
List<mojo.HttpHeader> mojoHeaders = <mojo.HttpHeader>[]; List<mojo.HttpHeader> mojoHeaders = <mojo.HttpHeader>[];
headers.forEach((String name, String value) { headers?.forEach((String name, String value) {
mojo.HttpHeader header = new mojo.HttpHeader() mojo.HttpHeader header = new mojo.HttpHeader()
..name = name ..name = name
..value = value; ..value = value;
...@@ -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();
} }
...@@ -7,6 +7,8 @@ import 'dart:ui' as ui; ...@@ -7,6 +7,8 @@ import 'dart:ui' as ui;
import 'dart:ui_internals' as internals; import 'dart:ui_internals' as internals;
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:flutter/http.dart' as http;
import 'package:mojo/core.dart' as core; import 'package:mojo/core.dart' as core;
import 'package:mojo_services/mojo/asset_bundle/asset_bundle.mojom.dart'; import 'package:mojo_services/mojo/asset_bundle/asset_bundle.mojom.dart';
...@@ -38,7 +40,9 @@ class NetworkAssetBundle extends AssetBundle { ...@@ -38,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) => fetchString(_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,42 +3,19 @@ ...@@ -3,42 +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();
class Response {
ByteData body;
Response(this.body);
String bodyAsString() {
if (body == null)
return null;
return new String.fromCharCodes(new Uint8List.view(body.buffer));
}
}
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) {
...@@ -56,16 +33,3 @@ Future<UrlResponse> fetchUrl(String relativeUrl) { ...@@ -56,16 +33,3 @@ Future<UrlResponse> fetchUrl(String relativeUrl) {
..autoFollowRedirects = true; ..autoFollowRedirects = true;
return fetch(request); return fetch(request);
} }
Future<Response> fetchBody(String relativeUrl) async {
UrlResponse response = await fetchUrl(relativeUrl);
if (response.body == null) return new Response(null);
ByteData data = await core.DataPipeDrainer.drainHandle(response.body);
return new Response(data);
}
Future<String> fetchString(String relativeUrl) async {
Response response = await fetchBody(relativeUrl);
return response.bodyAsString();
}
...@@ -6,6 +6,7 @@ import 'dart:async'; ...@@ -6,6 +6,7 @@ import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'package:mojo/core.dart'; import 'package:mojo/core.dart';
import 'package:flutter/http.dart' as http;
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flx/bundle.dart'; import 'package:flx/bundle.dart';
import 'package:sky_services/updater/update_service.mojom.dart'; import 'package:sky_services/updater/update_service.mojom.dart';
...@@ -82,7 +83,7 @@ class UpdateTask { ...@@ -82,7 +83,7 @@ class UpdateTask {
Future<yaml.YamlMap> _fetchManifest() async { Future<yaml.YamlMap> _fetchManifest() async {
String manifestUrl = _currentManifest['update-url'] + '/' + kManifestFile; String manifestUrl = _currentManifest['update-url'] + '/' + kManifestFile;
String manifestData = await fetchString(manifestUrl); String manifestData = await http.get(manifestUrl);
return yaml.loadYaml(manifestData, sourceUrl: manifestUrl); return yaml.loadYaml(manifestData, sourceUrl: manifestUrl);
} }
......
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