mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2025-03-20 22:28:48 +00:00
HTTP basic updates
This commit is contained in:
parent
02ae941a98
commit
3e9a225470
2 changed files with 29 additions and 11 deletions
|
@ -7,8 +7,16 @@ import 'package:stackwallet/utilities/logger.dart';
|
||||||
|
|
||||||
// WIP wrapper layer
|
// WIP wrapper layer
|
||||||
|
|
||||||
abstract class HTTP {
|
// TODO expand this class
|
||||||
static Future<HttpClientResponse> get({
|
class Response {
|
||||||
|
final int code;
|
||||||
|
final String body;
|
||||||
|
|
||||||
|
Response(this.body, this.code);
|
||||||
|
}
|
||||||
|
|
||||||
|
class HTTP {
|
||||||
|
Future<Response> get({
|
||||||
required Uri url,
|
required Uri url,
|
||||||
Map<String, String>? headers,
|
Map<String, String>? headers,
|
||||||
required bool routeOverTor,
|
required bool routeOverTor,
|
||||||
|
@ -32,7 +40,11 @@ abstract class HTTP {
|
||||||
headers.forEach((key, value) => request.headers.add);
|
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) {
|
} catch (e, s) {
|
||||||
Logging.instance.log(
|
Logging.instance.log(
|
||||||
"HTTP.get() rethrew: $e\n$s",
|
"HTTP.get() rethrew: $e\n$s",
|
||||||
|
@ -44,7 +56,7 @@ abstract class HTTP {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static Future<HttpClientResponse> post({
|
Future<Response> post({
|
||||||
required Uri url,
|
required Uri url,
|
||||||
Map<String, String>? headers,
|
Map<String, String>? headers,
|
||||||
Object? body,
|
Object? body,
|
||||||
|
@ -73,7 +85,11 @@ abstract class HTTP {
|
||||||
|
|
||||||
request.write(body);
|
request.write(body);
|
||||||
|
|
||||||
return request.close();
|
final response = await request.close();
|
||||||
|
return Response(
|
||||||
|
await response.transform(utf8.decoder).join(),
|
||||||
|
response.statusCode,
|
||||||
|
);
|
||||||
} catch (e, s) {
|
} catch (e, s) {
|
||||||
Logging.instance.log(
|
Logging.instance.log(
|
||||||
"HTTP.post() rethrew: $e\n$s",
|
"HTTP.post() rethrew: $e\n$s",
|
||||||
|
|
|
@ -38,6 +38,8 @@ class ChangeNowAPI {
|
||||||
static const String apiVersion = "/v1";
|
static const String apiVersion = "/v1";
|
||||||
static const String apiVersionV2 = "/v2";
|
static const String apiVersionV2 = "/v2";
|
||||||
|
|
||||||
|
HTTP client = HTTP();
|
||||||
|
|
||||||
ChangeNowAPI._();
|
ChangeNowAPI._();
|
||||||
static final ChangeNowAPI _instance = ChangeNowAPI._();
|
static final ChangeNowAPI _instance = ChangeNowAPI._();
|
||||||
static ChangeNowAPI get instance => _instance;
|
static ChangeNowAPI get instance => _instance;
|
||||||
|
@ -52,14 +54,14 @@ class ChangeNowAPI {
|
||||||
|
|
||||||
Future<dynamic> _makeGetRequest(Uri uri) async {
|
Future<dynamic> _makeGetRequest(Uri uri) async {
|
||||||
try {
|
try {
|
||||||
final response = await HTTP.get(
|
final response = await client.get(
|
||||||
url: uri,
|
url: uri,
|
||||||
headers: {'Content-Type': 'application/json'},
|
headers: {'Content-Type': 'application/json'},
|
||||||
routeOverTor: Prefs.instance.useTor,
|
routeOverTor: Prefs.instance.useTor,
|
||||||
);
|
);
|
||||||
String? data;
|
String? data;
|
||||||
try {
|
try {
|
||||||
data = await response.transform(utf8.decoder).join();
|
data = response.body;
|
||||||
final parsed = jsonDecode(data);
|
final parsed = jsonDecode(data);
|
||||||
|
|
||||||
return parsed;
|
return parsed;
|
||||||
|
@ -78,7 +80,7 @@ class ChangeNowAPI {
|
||||||
|
|
||||||
Future<dynamic> _makeGetRequestV2(Uri uri, String apiKey) async {
|
Future<dynamic> _makeGetRequestV2(Uri uri, String apiKey) async {
|
||||||
try {
|
try {
|
||||||
final response = await HTTP.get(
|
final response = await client.get(
|
||||||
url: uri,
|
url: uri,
|
||||||
headers: {
|
headers: {
|
||||||
// 'Content-Type': 'application/json',
|
// 'Content-Type': 'application/json',
|
||||||
|
@ -87,7 +89,7 @@ class ChangeNowAPI {
|
||||||
routeOverTor: Prefs.instance.useTor,
|
routeOverTor: Prefs.instance.useTor,
|
||||||
);
|
);
|
||||||
|
|
||||||
final data = await response.transform(utf8.decoder).join();
|
final data = response.body;
|
||||||
final parsed = jsonDecode(data);
|
final parsed = jsonDecode(data);
|
||||||
|
|
||||||
return parsed;
|
return parsed;
|
||||||
|
@ -103,7 +105,7 @@ class ChangeNowAPI {
|
||||||
Map<String, String> body,
|
Map<String, String> body,
|
||||||
) async {
|
) async {
|
||||||
try {
|
try {
|
||||||
final response = await HTTP.post(
|
final response = await client.post(
|
||||||
url: uri,
|
url: uri,
|
||||||
headers: {'Content-Type': 'application/json'},
|
headers: {'Content-Type': 'application/json'},
|
||||||
body: jsonEncode(body),
|
body: jsonEncode(body),
|
||||||
|
@ -112,7 +114,7 @@ class ChangeNowAPI {
|
||||||
|
|
||||||
String? data;
|
String? data;
|
||||||
try {
|
try {
|
||||||
data = await response.transform(utf8.decoder).join();
|
data = response.body;
|
||||||
final parsed = jsonDecode(data);
|
final parsed = jsonDecode(data);
|
||||||
|
|
||||||
return parsed;
|
return parsed;
|
||||||
|
|
Loading…
Reference in a new issue