diff --git a/lib/networking/http.dart b/lib/networking/http.dart index 62c8d225e..dc2ee2e8b 100644 --- a/lib/networking/http.dart +++ b/lib/networking/http.dart @@ -7,8 +7,16 @@ import 'package:stackwallet/utilities/logger.dart'; // WIP wrapper layer -abstract class HTTP { - static Future get({ +// TODO expand this class +class Response { + final int code; + final String body; + + Response(this.body, this.code); +} + +class HTTP { + Future get({ required Uri url, Map? headers, required bool routeOverTor, @@ -32,7 +40,11 @@ abstract class HTTP { headers.forEach((key, value) => request.headers.add); } - return request.close(); + final response = await request.close(); + return Response( + await response.transform(utf8.decoder).join(), + response.statusCode, + ); } catch (e, s) { Logging.instance.log( "HTTP.get() rethrew: $e\n$s", @@ -44,7 +56,7 @@ abstract class HTTP { } } - static Future post({ + Future post({ required Uri url, Map? headers, Object? body, @@ -73,7 +85,11 @@ abstract class HTTP { request.write(body); - return request.close(); + final response = await request.close(); + return Response( + await response.transform(utf8.decoder).join(), + response.statusCode, + ); } catch (e, s) { Logging.instance.log( "HTTP.post() rethrew: $e\n$s", diff --git a/lib/services/exchange/change_now/change_now_api.dart b/lib/services/exchange/change_now/change_now_api.dart index 1a9067376..d7cd5bcb9 100644 --- a/lib/services/exchange/change_now/change_now_api.dart +++ b/lib/services/exchange/change_now/change_now_api.dart @@ -38,6 +38,8 @@ class ChangeNowAPI { static const String apiVersion = "/v1"; static const String apiVersionV2 = "/v2"; + HTTP client = HTTP(); + ChangeNowAPI._(); static final ChangeNowAPI _instance = ChangeNowAPI._(); static ChangeNowAPI get instance => _instance; @@ -52,14 +54,14 @@ class ChangeNowAPI { Future _makeGetRequest(Uri uri) async { try { - final response = await HTTP.get( + final response = await client.get( url: uri, headers: {'Content-Type': 'application/json'}, routeOverTor: Prefs.instance.useTor, ); String? data; try { - data = await response.transform(utf8.decoder).join(); + data = response.body; final parsed = jsonDecode(data); return parsed; @@ -78,7 +80,7 @@ class ChangeNowAPI { Future _makeGetRequestV2(Uri uri, String apiKey) async { try { - final response = await HTTP.get( + final response = await client.get( url: uri, headers: { // 'Content-Type': 'application/json', @@ -87,7 +89,7 @@ class ChangeNowAPI { routeOverTor: Prefs.instance.useTor, ); - final data = await response.transform(utf8.decoder).join(); + final data = response.body; final parsed = jsonDecode(data); return parsed; @@ -103,7 +105,7 @@ class ChangeNowAPI { Map body, ) async { try { - final response = await HTTP.post( + final response = await client.post( url: uri, headers: {'Content-Type': 'application/json'}, body: jsonEncode(body), @@ -112,7 +114,7 @@ class ChangeNowAPI { String? data; try { - data = await response.transform(utf8.decoder).join(); + data = response.body; final parsed = jsonDecode(data); return parsed;