stack_wallet/lib/networking/http.dart

125 lines
2.7 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'dart:convert';
2023-08-07 15:18:08 +00:00
import 'dart:io';
import 'package:flutter/foundation.dart';
2023-08-07 16:46:34 +00:00
import 'package:socks5_proxy/socks_client.dart';
2023-08-07 15:18:08 +00:00
import 'package:stackwallet/utilities/logger.dart';
// WIP wrapper layer
2023-09-08 15:18:55 +00:00
// TODO expand this class
class Response {
final int code;
final List<int> bodyBytes;
2023-09-08 15:18:55 +00:00
String get body => utf8.decode(bodyBytes, allowMalformed: true);
Response(this.bodyBytes, this.code);
2023-09-08 15:18:55 +00:00
}
class HTTP {
Future<Response> get({
required Uri url,
Map<String, String>? headers,
required ({
InternetAddress host,
int port,
})? proxyInfo,
}) async {
2023-08-07 15:18:08 +00:00
final httpClient = HttpClient();
try {
if (proxyInfo != null) {
2023-08-07 16:46:34 +00:00
SocksTCPClient.assignToHttpClient(httpClient, [
ProxySettings(
proxyInfo.host,
proxyInfo.port,
2023-08-07 16:46:34 +00:00
),
]);
}
final HttpClientRequest request = await httpClient.getUrl(
url,
);
2023-08-07 15:18:08 +00:00
2023-08-07 16:46:34 +00:00
if (headers != null) {
2023-09-13 21:49:04 +00:00
headers.forEach((key, value) => request.headers.add(key, value));
2023-08-07 15:18:08 +00:00
}
2023-08-07 16:46:34 +00:00
2023-09-08 15:18:55 +00:00
final response = await request.close();
2023-09-08 15:18:55 +00:00
return Response(
await _bodyBytes(response),
2023-09-08 15:18:55 +00:00
response.statusCode,
);
2023-08-07 15:18:08 +00:00
} catch (e, s) {
Logging.instance.log(
"HTTP.get() rethrew: $e\n$s",
level: LogLevel.Info,
);
rethrow;
} finally {
httpClient.close(force: true);
}
}
2023-09-08 15:18:55 +00:00
Future<Response> post({
required Uri url,
Map<String, String>? headers,
Object? body,
Encoding? encoding,
required ({
InternetAddress host,
int port,
})? proxyInfo,
}) async {
2023-08-07 15:18:08 +00:00
final httpClient = HttpClient();
try {
if (proxyInfo != null) {
2023-08-07 17:19:09 +00:00
SocksTCPClient.assignToHttpClient(httpClient, [
ProxySettings(
proxyInfo.host,
proxyInfo.port,
2023-08-07 17:19:09 +00:00
),
]);
2023-08-07 16:46:34 +00:00
}
final HttpClientRequest request = await httpClient.postUrl(
url,
);
2023-08-07 15:18:08 +00:00
2023-08-07 16:46:34 +00:00
if (headers != null) {
2023-09-13 21:49:04 +00:00
headers.forEach((key, value) => request.headers.add(key, value));
2023-08-07 15:18:08 +00:00
}
2023-08-07 16:46:34 +00:00
request.write(body);
2023-09-08 15:18:55 +00:00
final response = await request.close();
return Response(
await _bodyBytes(response),
2023-09-08 15:18:55 +00:00
response.statusCode,
);
2023-08-07 15:18:08 +00:00
} catch (e, s) {
Logging.instance.log(
"HTTP.post() rethrew: $e\n$s",
level: LogLevel.Info,
);
2023-08-07 15:18:08 +00:00
rethrow;
} finally {
httpClient.close(force: true);
}
}
Future<Uint8List> _bodyBytes(HttpClientResponse response) {
final completer = Completer<Uint8List>();
final List<int> bytes = [];
response.listen(
(data) {
bytes.addAll(data);
},
onDone: () => completer.complete(
Uint8List.fromList(bytes),
),
);
return completer.future;
}
}