改用 rust 实现的 http client

This commit is contained in:
2024-02-07 00:20:06 +08:00
parent a3f6ecf8b6
commit 7e1352c0be
14 changed files with 589 additions and 40 deletions

View File

@ -0,0 +1,19 @@
// This file is automatically generated, so please do not edit it.
// Generated by `flutter_rust_bridge`@ 2.0.0-dev.23.
// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import
import '../frb_generated.dart';
import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';
Future<String> getString(
{required String url, Map<String, String>? headers, dynamic hint}) =>
RustLib.instance.api.getString(url: url, headers: headers, hint: hint);
Future<String> postJsonString(
{required String url,
Map<String, String>? headers,
String? jsonData,
dynamic hint}) =>
RustLib.instance.api.postJsonString(
url: url, headers: headers, jsonData: jsonData, hint: hint);

View File

@ -4,6 +4,7 @@
// ignore_for_file: unused_import, unused_element, unnecessary_import, duplicate_ignore, invalid_use_of_internal_member, annotate_overrides, non_constant_identifier_names, curly_braces_in_flow_control_structures, prefer_const_literals_to_create_immutables, unused_field
import 'api/downloader_api.dart';
import 'api/http_api.dart';
import 'dart:async';
import 'dart:convert';
import 'downloader.dart';
@ -71,6 +72,15 @@ abstract class RustLibApi extends BaseApi {
required String fileName,
required int connectionCount,
dynamic hint});
Future<String> getString(
{required String url, Map<String, String>? headers, dynamic hint});
Future<String> postJsonString(
{required String url,
Map<String, String>? headers,
String? jsonData,
dynamic hint});
}
class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
@ -139,6 +149,71 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
argNames: ["url", "savePath", "fileName", "connectionCount"],
);
@override
Future<String> getString(
{required String url, Map<String, String>? headers, dynamic hint}) {
return handler.executeNormal(NormalTask(
callFfi: (port_) {
final serializer = SseSerializer(generalizedFrbRustBinding);
sse_encode_String(url, serializer);
sse_encode_opt_Map_String_String(headers, serializer);
pdeCallFfi(generalizedFrbRustBinding, serializer,
funcId: 3, port: port_);
},
codec: SseCodec(
decodeSuccessData: sse_decode_String,
decodeErrorData: null,
),
constMeta: kGetStringConstMeta,
argValues: [url, headers],
apiImpl: this,
hint: hint,
));
}
TaskConstMeta get kGetStringConstMeta => const TaskConstMeta(
debugName: "get_string",
argNames: ["url", "headers"],
);
@override
Future<String> postJsonString(
{required String url,
Map<String, String>? headers,
String? jsonData,
dynamic hint}) {
return handler.executeNormal(NormalTask(
callFfi: (port_) {
final serializer = SseSerializer(generalizedFrbRustBinding);
sse_encode_String(url, serializer);
sse_encode_opt_Map_String_String(headers, serializer);
sse_encode_opt_String(jsonData, serializer);
pdeCallFfi(generalizedFrbRustBinding, serializer,
funcId: 4, port: port_);
},
codec: SseCodec(
decodeSuccessData: sse_decode_String,
decodeErrorData: null,
),
constMeta: kPostJsonStringConstMeta,
argValues: [url, headers, jsonData],
apiImpl: this,
hint: hint,
));
}
TaskConstMeta get kPostJsonStringConstMeta => const TaskConstMeta(
debugName: "post_json_string",
argNames: ["url", "headers", "jsonData"],
);
@protected
Map<String, String> dco_decode_Map_String_String(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs
return Map.fromEntries(dco_decode_list_record_string_string(raw)
.map((e) => MapEntry(e.$1, e.$2)));
}
@protected
String dco_decode_String(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs
@ -172,6 +247,12 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
return raw as Uint8List;
}
@protected
List<(String, String)> dco_decode_list_record_string_string(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs
return (raw as List<dynamic>).map(dco_decode_record_string_string).toList();
}
@protected
MyDownloaderStatus dco_decode_my_downloader_status(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs
@ -202,6 +283,31 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
return MyNetworkItemPendingType.values[raw as int];
}
@protected
Map<String, String>? dco_decode_opt_Map_String_String(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs
return raw == null ? null : dco_decode_Map_String_String(raw);
}
@protected
String? dco_decode_opt_String(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs
return raw == null ? null : dco_decode_String(raw);
}
@protected
(String, String) dco_decode_record_string_string(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs
final arr = raw as List<dynamic>;
if (arr.length != 2) {
throw Exception('Expected 2 elements, got ${arr.length}');
}
return (
dco_decode_String(arr[0]),
dco_decode_String(arr[1]),
);
}
@protected
int dco_decode_u_64(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs
@ -220,6 +326,14 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
return;
}
@protected
Map<String, String> sse_decode_Map_String_String(
SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
var inner = sse_decode_list_record_string_string(deserializer);
return Map.fromEntries(inner.map((e) => MapEntry(e.$1, e.$2)));
}
@protected
String sse_decode_String(SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
@ -257,6 +371,19 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
return deserializer.buffer.getUint8List(len_);
}
@protected
List<(String, String)> sse_decode_list_record_string_string(
SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
var len_ = sse_decode_i_32(deserializer);
var ans_ = <(String, String)>[];
for (var idx_ = 0; idx_ < len_; ++idx_) {
ans_.add(sse_decode_record_string_string(deserializer));
}
return ans_;
}
@protected
MyDownloaderStatus sse_decode_my_downloader_status(
SseDeserializer deserializer) {
@ -289,6 +416,38 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
return MyNetworkItemPendingType.values[inner];
}
@protected
Map<String, String>? sse_decode_opt_Map_String_String(
SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
if (sse_decode_bool(deserializer)) {
return (sse_decode_Map_String_String(deserializer));
} else {
return null;
}
}
@protected
String? sse_decode_opt_String(SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
if (sse_decode_bool(deserializer)) {
return (sse_decode_String(deserializer));
} else {
return null;
}
}
@protected
(String, String) sse_decode_record_string_string(
SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
var var_field0 = sse_decode_String(deserializer);
var var_field1 = sse_decode_String(deserializer);
return (var_field0, var_field1);
}
@protected
int sse_decode_u_64(SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
@ -312,6 +471,14 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
return deserializer.buffer.getUint8() != 0;
}
@protected
void sse_encode_Map_String_String(
Map<String, String> self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
sse_encode_list_record_string_string(
self.entries.map((e) => (e.key, e.value)).toList(), serializer);
}
@protected
void sse_encode_String(String self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
@ -343,6 +510,16 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
serializer.buffer.putUint8List(self);
}
@protected
void sse_encode_list_record_string_string(
List<(String, String)> self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
sse_encode_i_32(self.length, serializer);
for (final item in self) {
sse_encode_record_string_string(item, serializer);
}
}
@protected
void sse_encode_my_downloader_status(
MyDownloaderStatus self, SseSerializer serializer) {
@ -370,6 +547,35 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
sse_encode_i_32(self.index, serializer);
}
@protected
void sse_encode_opt_Map_String_String(
Map<String, String>? self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
sse_encode_bool(self != null, serializer);
if (self != null) {
sse_encode_Map_String_String(self, serializer);
}
}
@protected
void sse_encode_opt_String(String? self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
sse_encode_bool(self != null, serializer);
if (self != null) {
sse_encode_String(self, serializer);
}
}
@protected
void sse_encode_record_string_string(
(String, String) self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
sse_encode_String(self.$1, serializer);
sse_encode_String(self.$2, serializer);
}
@protected
void sse_encode_u_64(int self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs

View File

@ -4,6 +4,7 @@
// ignore_for_file: unused_import, unused_element, unnecessary_import, duplicate_ignore, invalid_use_of_internal_member, annotate_overrides, non_constant_identifier_names, curly_braces_in_flow_control_structures, prefer_const_literals_to_create_immutables, unused_field
import 'api/downloader_api.dart';
import 'api/http_api.dart';
import 'dart:async';
import 'dart:convert';
import 'dart:ffi' as ffi;
@ -19,6 +20,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
required super.portManager,
});
@protected
Map<String, String> dco_decode_Map_String_String(dynamic raw);
@protected
String dco_decode_String(dynamic raw);
@ -31,12 +35,24 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
Uint8List dco_decode_list_prim_u_8_strict(dynamic raw);
@protected
List<(String, String)> dco_decode_list_record_string_string(dynamic raw);
@protected
MyDownloaderStatus dco_decode_my_downloader_status(dynamic raw);
@protected
MyNetworkItemPendingType dco_decode_my_network_item_pending_type(dynamic raw);
@protected
Map<String, String>? dco_decode_opt_Map_String_String(dynamic raw);
@protected
String? dco_decode_opt_String(dynamic raw);
@protected
(String, String) dco_decode_record_string_string(dynamic raw);
@protected
int dco_decode_u_64(dynamic raw);
@ -46,6 +62,10 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
void dco_decode_unit(dynamic raw);
@protected
Map<String, String> sse_decode_Map_String_String(
SseDeserializer deserializer);
@protected
String sse_decode_String(SseDeserializer deserializer);
@ -59,6 +79,10 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
Uint8List sse_decode_list_prim_u_8_strict(SseDeserializer deserializer);
@protected
List<(String, String)> sse_decode_list_record_string_string(
SseDeserializer deserializer);
@protected
MyDownloaderStatus sse_decode_my_downloader_status(
SseDeserializer deserializer);
@ -67,6 +91,17 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
MyNetworkItemPendingType sse_decode_my_network_item_pending_type(
SseDeserializer deserializer);
@protected
Map<String, String>? sse_decode_opt_Map_String_String(
SseDeserializer deserializer);
@protected
String? sse_decode_opt_String(SseDeserializer deserializer);
@protected
(String, String) sse_decode_record_string_string(
SseDeserializer deserializer);
@protected
int sse_decode_u_64(SseDeserializer deserializer);
@ -79,6 +114,10 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
bool sse_decode_bool(SseDeserializer deserializer);
@protected
void sse_encode_Map_String_String(
Map<String, String> self, SseSerializer serializer);
@protected
void sse_encode_String(String self, SseSerializer serializer);
@ -93,6 +132,10 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
void sse_encode_list_prim_u_8_strict(
Uint8List self, SseSerializer serializer);
@protected
void sse_encode_list_record_string_string(
List<(String, String)> self, SseSerializer serializer);
@protected
void sse_encode_my_downloader_status(
MyDownloaderStatus self, SseSerializer serializer);
@ -101,6 +144,17 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
void sse_encode_my_network_item_pending_type(
MyNetworkItemPendingType self, SseSerializer serializer);
@protected
void sse_encode_opt_Map_String_String(
Map<String, String>? self, SseSerializer serializer);
@protected
void sse_encode_opt_String(String? self, SseSerializer serializer);
@protected
void sse_encode_record_string_string(
(String, String) self, SseSerializer serializer);
@protected
void sse_encode_u_64(int self, SseSerializer serializer);

View File

@ -4,6 +4,7 @@
// ignore_for_file: unused_import, unused_element, unnecessary_import, duplicate_ignore, invalid_use_of_internal_member, annotate_overrides, non_constant_identifier_names, curly_braces_in_flow_control_structures, prefer_const_literals_to_create_immutables, unused_field
import 'api/downloader_api.dart';
import 'api/http_api.dart';
import 'dart:async';
import 'dart:convert';
import 'downloader.dart';
@ -18,6 +19,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
required super.portManager,
});
@protected
Map<String, String> dco_decode_Map_String_String(dynamic raw);
@protected
String dco_decode_String(dynamic raw);
@ -30,12 +34,24 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
Uint8List dco_decode_list_prim_u_8_strict(dynamic raw);
@protected
List<(String, String)> dco_decode_list_record_string_string(dynamic raw);
@protected
MyDownloaderStatus dco_decode_my_downloader_status(dynamic raw);
@protected
MyNetworkItemPendingType dco_decode_my_network_item_pending_type(dynamic raw);
@protected
Map<String, String>? dco_decode_opt_Map_String_String(dynamic raw);
@protected
String? dco_decode_opt_String(dynamic raw);
@protected
(String, String) dco_decode_record_string_string(dynamic raw);
@protected
int dco_decode_u_64(dynamic raw);
@ -45,6 +61,10 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
void dco_decode_unit(dynamic raw);
@protected
Map<String, String> sse_decode_Map_String_String(
SseDeserializer deserializer);
@protected
String sse_decode_String(SseDeserializer deserializer);
@ -58,6 +78,10 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
Uint8List sse_decode_list_prim_u_8_strict(SseDeserializer deserializer);
@protected
List<(String, String)> sse_decode_list_record_string_string(
SseDeserializer deserializer);
@protected
MyDownloaderStatus sse_decode_my_downloader_status(
SseDeserializer deserializer);
@ -66,6 +90,17 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
MyNetworkItemPendingType sse_decode_my_network_item_pending_type(
SseDeserializer deserializer);
@protected
Map<String, String>? sse_decode_opt_Map_String_String(
SseDeserializer deserializer);
@protected
String? sse_decode_opt_String(SseDeserializer deserializer);
@protected
(String, String) sse_decode_record_string_string(
SseDeserializer deserializer);
@protected
int sse_decode_u_64(SseDeserializer deserializer);
@ -78,6 +113,10 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
bool sse_decode_bool(SseDeserializer deserializer);
@protected
void sse_encode_Map_String_String(
Map<String, String> self, SseSerializer serializer);
@protected
void sse_encode_String(String self, SseSerializer serializer);
@ -92,6 +131,10 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
void sse_encode_list_prim_u_8_strict(
Uint8List self, SseSerializer serializer);
@protected
void sse_encode_list_record_string_string(
List<(String, String)> self, SseSerializer serializer);
@protected
void sse_encode_my_downloader_status(
MyDownloaderStatus self, SseSerializer serializer);
@ -100,6 +143,17 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
void sse_encode_my_network_item_pending_type(
MyNetworkItemPendingType self, SseSerializer serializer);
@protected
void sse_encode_opt_Map_String_String(
Map<String, String>? self, SseSerializer serializer);
@protected
void sse_encode_opt_String(String? self, SseSerializer serializer);
@protected
void sse_encode_record_string_string(
(String, String) self, SseSerializer serializer);
@protected
void sse_encode_u_64(int self, SseSerializer serializer);