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 @@
import 'dart:convert';
import 'dart:math' as math;
import 'package:flutter/services.dart';
import 'package:flutter/http.dart' as http;
final math.Random _rng = new math.Random();
......@@ -83,8 +83,8 @@ class StockDataFetcher {
if (!actuallyFetchData)
return;
fetchBody(_urlToFetch(_nextChunk++)).then((Response response) {
String json = response.bodyAsString();
http.get(_urlToFetch(_nextChunk++)).then((http.Response response) {
String json = response.body;
if (json == null) {
print("Failed to load stock data chunk ${_nextChunk - 1}");
return;
......
......@@ -16,14 +16,6 @@ import 'package:mojo/mojo/http_header.mojom.dart' as mojo;
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
class MojoClient {
......@@ -66,7 +58,7 @@ class MojoClient {
Map<String, String> headers, [body, Encoding encoding]) async {
mojo.UrlLoaderProxy loader = new mojo.UrlLoaderProxy.unbound();
List<mojo.HttpHeader> mojoHeaders = <mojo.HttpHeader>[];
headers.forEach((String name, String value) {
headers?.forEach((String name, String value) {
mojo.HttpHeader header = new mojo.HttpHeader()
..name = name
..value = value;
......@@ -84,12 +76,12 @@ class MojoClient {
mojo.DataPipeFiller.fillHandle(pipe.producer, data);
}
try {
_networkService.ptr.createUrlLoader(loader);
networkService.ptr.createUrlLoader(loader);
mojo.UrlResponse response = (await loader.ptr.start(request)).response;
ByteData data = await mojo.DataPipeDrainer.drainHandle(response.body);
Uint8List bodyBytes = new Uint8List.view(data.buffer);
String body = new String.fromCharCodes(bodyBytes);
return new Response(body: body, bodyBytes: bodyBytes, statusCode: response.statusCode);
String bodyString = new String.fromCharCodes(bodyBytes);
return new Response(body: bodyString, bodyBytes: bodyBytes, statusCode: response.statusCode);
} catch (e) {
print("NetworkService unavailable $e");
return new Response(statusCode: 500);
......@@ -105,4 +97,12 @@ class MojoClient {
}
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;
import 'dart:ui_internals' as internals;
import 'dart:typed_data';
import 'package:flutter/http.dart' as http;
import 'package:mojo/core.dart' as core;
import 'package:mojo_services/mojo/asset_bundle/asset_bundle.mojom.dart';
......@@ -38,7 +40,9 @@ class NetworkAssetBundle extends AssetBundle {
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 {
......
......@@ -3,42 +3,19 @@
// found in the LICENSE file.
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_response.mojom.dart';
import 'package:mojo_services/mojo/network_service.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;
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 {
UrlLoaderProxy loader = new UrlLoaderProxy.unbound();
try {
_networkService.ptr.createUrlLoader(loader);
MojoClient.networkService.ptr.createUrlLoader(loader);
UrlResponse response = (await loader.ptr.start(request)).response;
return response;
} catch (e) {
......@@ -56,16 +33,3 @@ Future<UrlResponse> fetchUrl(String relativeUrl) {
..autoFollowRedirects = true;
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';
import 'dart:io';
import 'package:mojo/core.dart';
import 'package:flutter/http.dart' as http;
import 'package:flutter/services.dart';
import 'package:flx/bundle.dart';
import 'package:sky_services/updater/update_service.mojom.dart';
......@@ -82,7 +83,7 @@ class UpdateTask {
Future<yaml.YamlMap> _fetchManifest() async {
String manifestUrl = _currentManifest['update-url'] + '/' + kManifestFile;
String manifestData = await fetchString(manifestUrl);
String manifestData = await http.get(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