改用 rust 实现的 http client

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

View File

@ -1,17 +1,19 @@
import 'package:dio/dio.dart'; import 'dart:convert';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:starcitizen_doctor/common/conf/url_conf.dart'; import 'package:starcitizen_doctor/common/conf/url_conf.dart';
import 'package:starcitizen_doctor/common/utils/base_utils.dart'; import 'package:starcitizen_doctor/common/utils/base_utils.dart';
import 'package:starcitizen_doctor/common/rust/api/http_api.dart' as rust_http;
class AnalyticsApi { class AnalyticsApi {
static final Dio _dio = Dio();
static touch(String key) async { static touch(String key) async {
// debug // debug
if (kDebugMode) return; if (kDebugMode) return;
dPrint("AnalyticsApi.touch === $key start"); dPrint("AnalyticsApi.touch === $key start");
try { try {
await _dio.post("${URLConf.xkeycApiHome}/analytics/$key"); await rust_http.postJsonString(
url: "${URLConf.xkeycApiHome}/analytics/$key",
jsonData: json.encode({"test": "a"}));
dPrint("AnalyticsApi.touch === $key over"); dPrint("AnalyticsApi.touch === $key over");
} catch (e) { } catch (e) {
dPrint("AnalyticsApi.touch === $key Error:$e"); dPrint("AnalyticsApi.touch === $key Error:$e");

View File

@ -1,16 +1,13 @@
import 'dart:convert'; import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:starcitizen_doctor/common/conf/url_conf.dart'; import 'package:starcitizen_doctor/common/conf/url_conf.dart';
import 'package:starcitizen_doctor/common/rust/api/http_api.dart' as rust_http;
import 'package:starcitizen_doctor/data/app_placard_data.dart'; import 'package:starcitizen_doctor/data/app_placard_data.dart';
import 'package:starcitizen_doctor/data/app_version_data.dart'; import 'package:starcitizen_doctor/data/app_version_data.dart';
import 'package:starcitizen_doctor/data/countdown_festival_item_data.dart'; import 'package:starcitizen_doctor/data/countdown_festival_item_data.dart';
import 'package:starcitizen_doctor/data/sc_localization_data.dart'; import 'package:starcitizen_doctor/data/sc_localization_data.dart';
class Api { class Api {
static final dio =
Dio(BaseOptions(connectTimeout: const Duration(seconds: 10)));
static Future<AppVersionData> getAppVersion() async { static Future<AppVersionData> getAppVersion() async {
return AppVersionData.fromJson( return AppVersionData.fromJson(
await getRepoJson("sc_doctor", "version.json")); await getRepoJson("sc_doctor", "version.json"));
@ -36,9 +33,10 @@ class Api {
static Future<Map<String, dynamic>> getAppReleaseDataByVersionName( static Future<Map<String, dynamic>> getAppReleaseDataByVersionName(
String version) async { String version) async {
final r = await dio.get( final r = await rust_http.getString(
url:
"${URLConf.gitlabApiPath}/repos/SCToolBox/Release/releases/tags/$version"); "${URLConf.gitlabApiPath}/repos/SCToolBox/Release/releases/tags/$version");
return r.data; return json.decode(r);
} }
static Future<List<ScLocalizationData>> getScLocalizationData( static Future<List<ScLocalizationData>> getScLocalizationData(
@ -54,9 +52,10 @@ class Api {
} }
static Future<List> getScServerStatus() async { static Future<List> getScServerStatus() async {
final r = final r = await rust_http.getString(
await dio.get("https://status.robertsspaceindustries.com/index.json"); url: "https://status.robertsspaceindustries.com/index.json");
return r.data["systems"]; final map = json.decode(r);
return map["systems"];
} }
static Future<Map<String, dynamic>> getRepoJson( static Future<Map<String, dynamic>> getRepoJson(
@ -65,8 +64,9 @@ class Api {
return json.decode(data); return json.decode(data);
} }
static Future getRepoData(String dir, String name) async { static Future<String> getRepoData(String dir, String name) async {
final r = await dio.get("${URLConf.apiRepoPath}/$dir/$name"); final r =
return r.data; await rust_http.getString(url: "${URLConf.apiRepoPath}/$dir/$name");
return r;
} }
} }

View File

@ -1,25 +1,21 @@
import 'dart:io'; import 'dart:io';
import 'package:dart_rss/dart_rss.dart'; import 'package:dart_rss/dart_rss.dart';
import 'package:dio/dio.dart'; import 'package:starcitizen_doctor/common/rust/api/http_api.dart' as rust_http;
import 'package:starcitizen_doctor/common/conf/url_conf.dart'; import 'package:starcitizen_doctor/common/conf/url_conf.dart';
class RSSApi { class RSSApi {
static final _dio = Dio(BaseOptions(
connectTimeout: const Duration(seconds: 10),
responseType: ResponseType.plain));
static Future<List<RssItem>> getRssVideo() async { static Future<List<RssItem>> getRssVideo() async {
final r = await _dio.get(URLConf.rssVideoUrl); final r = await rust_http.getString(url: URLConf.rssVideoUrl);
final f = RssFeed.parse(r.data); final f = RssFeed.parse(r);
return f.items.sublist(0, 8); return f.items.sublist(0, 8);
} }
static Future<List<RssItem>> getRssText() async { static Future<List<RssItem>> getRssText() async {
final r1 = await _dio.get(URLConf.rssTextUrl1); final r1 = await rust_http.getString(url: URLConf.rssTextUrl1);
final r1f = RssFeed.parse(r1.data); final r1f = RssFeed.parse(r1);
final r2 = await _dio.get(URLConf.rssTextUrl2); final r2 = await rust_http.getString(url: URLConf.rssTextUrl2);
final r2f = RssFeed.parse(r2.data); final r2f = RssFeed.parse(r2);
final items = r1f.items..addAll(r2f.items); final items = r1f.items..addAll(r2f.items);
items.sort((a, b) { items.sort((a, b) {
final aDate = HttpDate.parse(a.pubDate ?? "").millisecondsSinceEpoch; final aDate = HttpDate.parse(a.pubDate ?? "").millisecondsSinceEpoch;

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 // 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/downloader_api.dart';
import 'api/http_api.dart';
import 'dart:async'; import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'downloader.dart'; import 'downloader.dart';
@ -71,6 +72,15 @@ abstract class RustLibApi extends BaseApi {
required String fileName, required String fileName,
required int connectionCount, required int connectionCount,
dynamic hint}); 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 { class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
@ -139,6 +149,71 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
argNames: ["url", "savePath", "fileName", "connectionCount"], 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 @protected
String dco_decode_String(dynamic raw) { String dco_decode_String(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs // Codec=Dco (DartCObject based), see doc to use other codecs
@ -172,6 +247,12 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
return raw as Uint8List; 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 @protected
MyDownloaderStatus dco_decode_my_downloader_status(dynamic raw) { MyDownloaderStatus dco_decode_my_downloader_status(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs // 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]; 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 @protected
int dco_decode_u_64(dynamic raw) { int dco_decode_u_64(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs // Codec=Dco (DartCObject based), see doc to use other codecs
@ -220,6 +326,14 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
return; 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 @protected
String sse_decode_String(SseDeserializer deserializer) { String sse_decode_String(SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs // 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_); 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 @protected
MyDownloaderStatus sse_decode_my_downloader_status( MyDownloaderStatus sse_decode_my_downloader_status(
SseDeserializer deserializer) { SseDeserializer deserializer) {
@ -289,6 +416,38 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
return MyNetworkItemPendingType.values[inner]; 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 @protected
int sse_decode_u_64(SseDeserializer deserializer) { int sse_decode_u_64(SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs // 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; 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 @protected
void sse_encode_String(String self, SseSerializer serializer) { void sse_encode_String(String self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs // Codec=Sse (Serialization based), see doc to use other codecs
@ -343,6 +510,16 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
serializer.buffer.putUint8List(self); 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 @protected
void sse_encode_my_downloader_status( void sse_encode_my_downloader_status(
MyDownloaderStatus self, SseSerializer serializer) { MyDownloaderStatus self, SseSerializer serializer) {
@ -370,6 +547,35 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
sse_encode_i_32(self.index, serializer); 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 @protected
void sse_encode_u_64(int self, SseSerializer serializer) { void sse_encode_u_64(int self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs // 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 // 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/downloader_api.dart';
import 'api/http_api.dart';
import 'dart:async'; import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'dart:ffi' as ffi; import 'dart:ffi' as ffi;
@ -19,6 +20,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
required super.portManager, required super.portManager,
}); });
@protected
Map<String, String> dco_decode_Map_String_String(dynamic raw);
@protected @protected
String dco_decode_String(dynamic raw); String dco_decode_String(dynamic raw);
@ -31,12 +35,24 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected @protected
Uint8List dco_decode_list_prim_u_8_strict(dynamic raw); Uint8List dco_decode_list_prim_u_8_strict(dynamic raw);
@protected
List<(String, String)> dco_decode_list_record_string_string(dynamic raw);
@protected @protected
MyDownloaderStatus dco_decode_my_downloader_status(dynamic raw); MyDownloaderStatus dco_decode_my_downloader_status(dynamic raw);
@protected @protected
MyNetworkItemPendingType dco_decode_my_network_item_pending_type(dynamic raw); 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 @protected
int dco_decode_u_64(dynamic raw); int dco_decode_u_64(dynamic raw);
@ -46,6 +62,10 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected @protected
void dco_decode_unit(dynamic raw); void dco_decode_unit(dynamic raw);
@protected
Map<String, String> sse_decode_Map_String_String(
SseDeserializer deserializer);
@protected @protected
String sse_decode_String(SseDeserializer deserializer); String sse_decode_String(SseDeserializer deserializer);
@ -59,6 +79,10 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected @protected
Uint8List sse_decode_list_prim_u_8_strict(SseDeserializer deserializer); Uint8List sse_decode_list_prim_u_8_strict(SseDeserializer deserializer);
@protected
List<(String, String)> sse_decode_list_record_string_string(
SseDeserializer deserializer);
@protected @protected
MyDownloaderStatus sse_decode_my_downloader_status( MyDownloaderStatus sse_decode_my_downloader_status(
SseDeserializer deserializer); SseDeserializer deserializer);
@ -67,6 +91,17 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
MyNetworkItemPendingType sse_decode_my_network_item_pending_type( MyNetworkItemPendingType sse_decode_my_network_item_pending_type(
SseDeserializer deserializer); 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 @protected
int sse_decode_u_64(SseDeserializer deserializer); int sse_decode_u_64(SseDeserializer deserializer);
@ -79,6 +114,10 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected @protected
bool sse_decode_bool(SseDeserializer deserializer); bool sse_decode_bool(SseDeserializer deserializer);
@protected
void sse_encode_Map_String_String(
Map<String, String> self, SseSerializer serializer);
@protected @protected
void sse_encode_String(String self, SseSerializer serializer); 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( void sse_encode_list_prim_u_8_strict(
Uint8List self, SseSerializer serializer); Uint8List self, SseSerializer serializer);
@protected
void sse_encode_list_record_string_string(
List<(String, String)> self, SseSerializer serializer);
@protected @protected
void sse_encode_my_downloader_status( void sse_encode_my_downloader_status(
MyDownloaderStatus self, SseSerializer serializer); MyDownloaderStatus self, SseSerializer serializer);
@ -101,6 +144,17 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
void sse_encode_my_network_item_pending_type( void sse_encode_my_network_item_pending_type(
MyNetworkItemPendingType self, SseSerializer serializer); 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 @protected
void sse_encode_u_64(int self, SseSerializer serializer); 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 // 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/downloader_api.dart';
import 'api/http_api.dart';
import 'dart:async'; import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'downloader.dart'; import 'downloader.dart';
@ -18,6 +19,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
required super.portManager, required super.portManager,
}); });
@protected
Map<String, String> dco_decode_Map_String_String(dynamic raw);
@protected @protected
String dco_decode_String(dynamic raw); String dco_decode_String(dynamic raw);
@ -30,12 +34,24 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected @protected
Uint8List dco_decode_list_prim_u_8_strict(dynamic raw); Uint8List dco_decode_list_prim_u_8_strict(dynamic raw);
@protected
List<(String, String)> dco_decode_list_record_string_string(dynamic raw);
@protected @protected
MyDownloaderStatus dco_decode_my_downloader_status(dynamic raw); MyDownloaderStatus dco_decode_my_downloader_status(dynamic raw);
@protected @protected
MyNetworkItemPendingType dco_decode_my_network_item_pending_type(dynamic raw); 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 @protected
int dco_decode_u_64(dynamic raw); int dco_decode_u_64(dynamic raw);
@ -45,6 +61,10 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected @protected
void dco_decode_unit(dynamic raw); void dco_decode_unit(dynamic raw);
@protected
Map<String, String> sse_decode_Map_String_String(
SseDeserializer deserializer);
@protected @protected
String sse_decode_String(SseDeserializer deserializer); String sse_decode_String(SseDeserializer deserializer);
@ -58,6 +78,10 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected @protected
Uint8List sse_decode_list_prim_u_8_strict(SseDeserializer deserializer); Uint8List sse_decode_list_prim_u_8_strict(SseDeserializer deserializer);
@protected
List<(String, String)> sse_decode_list_record_string_string(
SseDeserializer deserializer);
@protected @protected
MyDownloaderStatus sse_decode_my_downloader_status( MyDownloaderStatus sse_decode_my_downloader_status(
SseDeserializer deserializer); SseDeserializer deserializer);
@ -66,6 +90,17 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
MyNetworkItemPendingType sse_decode_my_network_item_pending_type( MyNetworkItemPendingType sse_decode_my_network_item_pending_type(
SseDeserializer deserializer); 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 @protected
int sse_decode_u_64(SseDeserializer deserializer); int sse_decode_u_64(SseDeserializer deserializer);
@ -78,6 +113,10 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected @protected
bool sse_decode_bool(SseDeserializer deserializer); bool sse_decode_bool(SseDeserializer deserializer);
@protected
void sse_encode_Map_String_String(
Map<String, String> self, SseSerializer serializer);
@protected @protected
void sse_encode_String(String self, SseSerializer serializer); 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( void sse_encode_list_prim_u_8_strict(
Uint8List self, SseSerializer serializer); Uint8List self, SseSerializer serializer);
@protected
void sse_encode_list_record_string_string(
List<(String, String)> self, SseSerializer serializer);
@protected @protected
void sse_encode_my_downloader_status( void sse_encode_my_downloader_status(
MyDownloaderStatus self, SseSerializer serializer); MyDownloaderStatus self, SseSerializer serializer);
@ -100,6 +143,17 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
void sse_encode_my_network_item_pending_type( void sse_encode_my_network_item_pending_type(
MyNetworkItemPendingType self, SseSerializer serializer); 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 @protected
void sse_encode_u_64(int self, SseSerializer serializer); void sse_encode_u_64(int self, SseSerializer serializer);

View File

@ -4,7 +4,7 @@ import 'dart:io';
import 'package:dart_rss/dart_rss.dart'; import 'package:dart_rss/dart_rss.dart';
import 'package:desktop_webview_window/desktop_webview_window.dart'; import 'package:desktop_webview_window/desktop_webview_window.dart';
import 'package:dio/dio.dart'; import 'package:starcitizen_doctor/common/rust/api/http_api.dart' as rust_http;
import 'package:hive/hive.dart'; import 'package:hive/hive.dart';
import 'package:starcitizen_doctor/api/analytics.dart'; import 'package:starcitizen_doctor/api/analytics.dart';
import 'package:starcitizen_doctor/api/api.dart'; import 'package:starcitizen_doctor/api/api.dart';
@ -107,10 +107,8 @@ class HomeUIModel extends BaseUIModel {
updateSCServerStatus(); updateSCServerStatus();
notifyListeners(); notifyListeners();
appWebLocalizationVersionsData = AppWebLocalizationVersionsData.fromJson( appWebLocalizationVersionsData = AppWebLocalizationVersionsData.fromJson(
json.decode((await Api.dio.get( json.decode((await rust_http.getString(
"${URLConf.webTranslateHomeUrl}/versions.json", url: "${URLConf.webTranslateHomeUrl}/versions.json"))));
options: Options(responseType: ResponseType.plain)))
.data));
countdownFestivalListData = await Api.getFestivalCountdownList(); countdownFestivalListData = await Api.getFestivalCountdownList();
notifyListeners(); notifyListeners();
_loadRRS(); _loadRRS();

View File

@ -5,7 +5,6 @@ import 'dart:convert';
import 'package:cryptography/cryptography.dart'; import 'package:cryptography/cryptography.dart';
import 'package:desktop_webview_window/desktop_webview_window.dart'; import 'package:desktop_webview_window/desktop_webview_window.dart';
import 'package:dio/dio.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:hive/hive.dart'; import 'package:hive/hive.dart';
import 'package:local_auth/local_auth.dart'; import 'package:local_auth/local_auth.dart';
@ -13,8 +12,8 @@ import 'package:starcitizen_doctor/common/conf/app_conf.dart';
import 'package:starcitizen_doctor/common/conf/url_conf.dart'; import 'package:starcitizen_doctor/common/conf/url_conf.dart';
import 'package:starcitizen_doctor/common/win32/credentials.dart'; import 'package:starcitizen_doctor/common/win32/credentials.dart';
import 'package:starcitizen_doctor/data/app_web_localization_versions_data.dart'; import 'package:starcitizen_doctor/data/app_web_localization_versions_data.dart';
import 'package:starcitizen_doctor/common/rust/api/http_api.dart' as rust_http;
import '../../../api/api.dart';
import '../../../base/ui.dart'; import '../../../base/ui.dart';
typedef RsiLoginCallback = void Function(Map? data, bool success); typedef RsiLoginCallback = void Function(Map? data, bool success);
@ -257,10 +256,9 @@ class WebViewModel {
} }
} }
final startTime = DateTime.now(); final startTime = DateTime.now();
final r = await Api.dio final r = await rust_http.getString(url: url);
.get(url, options: Options(responseType: ResponseType.plain));
final endTime = DateTime.now(); final endTime = DateTime.now();
final data = json.decode(r.data); final data = json.decode(r);
if (cacheKey.isNotEmpty) { if (cacheKey.isNotEmpty) {
dPrint( dPrint(
"update $cacheKey v == $version time == ${(endTime.microsecondsSinceEpoch - startTime.microsecondsSinceEpoch) / 1000 / 1000}s"); "update $cacheKey v == $version time == ${(endTime.microsecondsSinceEpoch - startTime.microsecondsSinceEpoch) / 1000 / 1000}s");

View File

@ -9,7 +9,10 @@ crate-type = ["cdylib", "staticlib"]
[dependencies] [dependencies]
flutter_rust_bridge = "=2.0.0-dev.23" flutter_rust_bridge = "=2.0.0-dev.23"
http-downloader = { version = "0.3.2", features = ["status-tracker", "speed-tracker", "breakpoint-resume", "bson-file-archiver"] } http-downloader = { version = "0.3.2", features = ["status-tracker", "speed-tracker", "breakpoint-resume", "bson-file-archiver"] }
tokio = { version = "1", features = ["rt", "rt-multi-thread", "macros"] } tokio = { version = "1", features = ["full"] }
url = "2.5.0" url = "2.5.0"
uuid = { version = "1.7.0", features = ["v4", "fast-rng", "macro-diagnostics"] } uuid = { version = "1.7.0", features = ["v4", "fast-rng", "macro-diagnostics"] }
async-std = "1.12.0" async-std = "1.12.0"
once_cell = "1.19.0"
reqwest = { version = "0.11", features = ["rustls-tls-native-roots", "cookies", "gzip", "brotli", "deflate", "multipart", "trust-dns", "json"] }
serde_json = { version = "1.0.1", features = [] }

View File

40
rust/src/api/http_api.rs Normal file
View File

@ -0,0 +1,40 @@
use std::collections::HashMap;
use std::time::Duration;
use once_cell::sync::Lazy;
use reqwest;
use reqwest::header::{HeaderMap, HeaderValue};
use reqwest::RequestBuilder;
static HTTP_CLIENT: Lazy<reqwest::Client> = Lazy::new(|| {
let mut header_map = HeaderMap::new();
header_map.insert("User-Agent", HeaderValue::from_static("SCToolBox/2.10.x lib_rust_http"));
reqwest::Client::builder()
.use_rustls_tls()
.connect_timeout(Duration::from_secs(10))
.timeout(Duration::from_secs(10))
.default_headers(header_map).build().unwrap()
});
#[tokio::main]
pub async fn get_string(url: String, headers: Option<HashMap<String, String>>) -> String {
let mut req = _append_header(HTTP_CLIENT.get(url), headers);
req.send().await.unwrap().text().await.unwrap()
}
pub async fn post_json_string(url: String, headers: Option<HashMap<String, String>>, json_data: Option<String>) -> String {
let mut req = _append_header(HTTP_CLIENT.post(url), headers);
if json_data.is_some() {
req = req.body(json_data.unwrap()).header(reqwest::header::CONTENT_TYPE, "application/json");
}
req.send().await.unwrap().text().await.unwrap()
}
fn _append_header(mut req: RequestBuilder, headers: Option<HashMap<String, String>>) -> RequestBuilder {
if headers.is_some() {
for x in headers.unwrap() {
req = req.header(x.0, x.1);
}
}
req
}

View File

@ -3,4 +3,4 @@
// //
pub mod downloader_api; pub mod downloader_api;
mod http; pub mod http_api;

View File

@ -120,9 +120,96 @@ fn wire_start_download_impl(
}, },
) )
} }
fn wire_get_string_impl(
port_: flutter_rust_bridge::for_generated::MessagePort,
ptr_: flutter_rust_bridge::for_generated::PlatformGeneralizedUint8ListPtr,
rust_vec_len_: i32,
data_len_: i32,
) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_normal::<flutter_rust_bridge::for_generated::SseCodec, _, _>(
flutter_rust_bridge::for_generated::TaskInfo {
debug_name: "get_string",
port: Some(port_),
mode: flutter_rust_bridge::for_generated::FfiCallMode::Normal,
},
move || {
let message = unsafe {
flutter_rust_bridge::for_generated::Dart2RustMessageSse::from_wire(
ptr_,
rust_vec_len_,
data_len_,
)
};
let mut deserializer =
flutter_rust_bridge::for_generated::SseDeserializer::new(message);
let api_url = <String>::sse_decode(&mut deserializer);
let api_headers =
<Option<std::collections::HashMap<String, String>>>::sse_decode(&mut deserializer);
deserializer.end();
move |context| {
transform_result_sse((move || {
Result::<_, ()>::Ok(crate::api::http_api::get_string(api_url, api_headers))
})())
}
},
)
}
fn wire_post_json_string_impl(
port_: flutter_rust_bridge::for_generated::MessagePort,
ptr_: flutter_rust_bridge::for_generated::PlatformGeneralizedUint8ListPtr,
rust_vec_len_: i32,
data_len_: i32,
) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_async::<flutter_rust_bridge::for_generated::SseCodec, _, _, _>(
flutter_rust_bridge::for_generated::TaskInfo {
debug_name: "post_json_string",
port: Some(port_),
mode: flutter_rust_bridge::for_generated::FfiCallMode::Normal,
},
move || {
let message = unsafe {
flutter_rust_bridge::for_generated::Dart2RustMessageSse::from_wire(
ptr_,
rust_vec_len_,
data_len_,
)
};
let mut deserializer =
flutter_rust_bridge::for_generated::SseDeserializer::new(message);
let api_url = <String>::sse_decode(&mut deserializer);
let api_headers =
<Option<std::collections::HashMap<String, String>>>::sse_decode(&mut deserializer);
let api_json_data = <Option<String>>::sse_decode(&mut deserializer);
deserializer.end();
move |context| async move {
transform_result_sse(
(move || async move {
Result::<_, ()>::Ok(
crate::api::http_api::post_json_string(
api_url,
api_headers,
api_json_data,
)
.await,
)
})()
.await,
)
}
},
)
}
// Section: dart2rust // Section: dart2rust
impl SseDecode for std::collections::HashMap<String, String> {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
let mut inner = <Vec<(String, String)>>::sse_decode(deserializer);
return inner.into_iter().collect();
}
}
impl SseDecode for String { impl SseDecode for String {
// Codec=Sse (Serialization based), see doc to use other codecs // Codec=Sse (Serialization based), see doc to use other codecs
fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
@ -168,6 +255,18 @@ impl SseDecode for Vec<u8> {
} }
} }
impl SseDecode for Vec<(String, String)> {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
let mut len_ = <i32>::sse_decode(deserializer);
let mut ans_ = vec![];
for idx_ in 0..len_ {
ans_.push(<(String, String)>::sse_decode(deserializer));
}
return ans_;
}
}
impl SseDecode for crate::downloader::MyDownloaderStatus { impl SseDecode for crate::downloader::MyDownloaderStatus {
// Codec=Sse (Serialization based), see doc to use other codecs // Codec=Sse (Serialization based), see doc to use other codecs
fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
@ -212,6 +311,39 @@ impl SseDecode for crate::downloader::MyNetworkItemPendingType {
} }
} }
impl SseDecode for Option<std::collections::HashMap<String, String>> {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
if (<bool>::sse_decode(deserializer)) {
return Some(<std::collections::HashMap<String, String>>::sse_decode(
deserializer,
));
} else {
return None;
}
}
}
impl SseDecode for Option<String> {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
if (<bool>::sse_decode(deserializer)) {
return Some(<String>::sse_decode(deserializer));
} else {
return None;
}
}
}
impl SseDecode for (String, String) {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
let mut var_field0 = <String>::sse_decode(deserializer);
let mut var_field1 = <String>::sse_decode(deserializer);
return (var_field0, var_field1);
}
}
impl SseDecode for u64 { impl SseDecode for u64 {
// Codec=Sse (Serialization based), see doc to use other codecs // Codec=Sse (Serialization based), see doc to use other codecs
fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
@ -249,6 +381,8 @@ fn pde_ffi_dispatcher_primary_impl(
match func_id { match func_id {
2 => wire_cancel_download_impl(port, ptr, rust_vec_len, data_len), 2 => wire_cancel_download_impl(port, ptr, rust_vec_len, data_len),
1 => wire_start_download_impl(port, ptr, rust_vec_len, data_len), 1 => wire_start_download_impl(port, ptr, rust_vec_len, data_len),
3 => wire_get_string_impl(port, ptr, rust_vec_len, data_len),
4 => wire_post_json_string_impl(port, ptr, rust_vec_len, data_len),
_ => unreachable!(), _ => unreachable!(),
} }
} }
@ -341,6 +475,13 @@ impl flutter_rust_bridge::IntoIntoDart<crate::downloader::MyNetworkItemPendingTy
} }
} }
impl SseEncode for std::collections::HashMap<String, String> {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {
<Vec<(String, String)>>::sse_encode(self.into_iter().collect(), serializer);
}
}
impl SseEncode for String { impl SseEncode for String {
// Codec=Sse (Serialization based), see doc to use other codecs // Codec=Sse (Serialization based), see doc to use other codecs
fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {
@ -376,6 +517,16 @@ impl SseEncode for Vec<u8> {
} }
} }
impl SseEncode for Vec<(String, String)> {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {
<i32>::sse_encode(self.len() as _, serializer);
for item in self {
<(String, String)>::sse_encode(item, serializer);
}
}
}
impl SseEncode for crate::downloader::MyDownloaderStatus { impl SseEncode for crate::downloader::MyDownloaderStatus {
// Codec=Sse (Serialization based), see doc to use other codecs // Codec=Sse (Serialization based), see doc to use other codecs
fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {
@ -419,6 +570,34 @@ impl SseEncode for crate::downloader::MyNetworkItemPendingType {
} }
} }
impl SseEncode for Option<std::collections::HashMap<String, String>> {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {
<bool>::sse_encode(self.is_some(), serializer);
if let Some(value) = self {
<std::collections::HashMap<String, String>>::sse_encode(value, serializer);
}
}
}
impl SseEncode for Option<String> {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {
<bool>::sse_encode(self.is_some(), serializer);
if let Some(value) = self {
<String>::sse_encode(value, serializer);
}
}
}
impl SseEncode for (String, String) {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {
<String>::sse_encode(self.0, serializer);
<String>::sse_encode(self.1, serializer);
}
}
impl SseEncode for u64 { impl SseEncode for u64 {
// Codec=Sse (Serialization based), see doc to use other codecs // Codec=Sse (Serialization based), see doc to use other codecs
fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {