Optimize HTTP calls

This commit is contained in:
yuluo
2024-04-19 16:57:44 +08:00
parent bf57a504b7
commit c1def9e738
10 changed files with 48 additions and 35 deletions

15
Cargo.lock generated
View File

@@ -3019,7 +3019,7 @@ dependencies = [
"tokio",
"tokio-native-tls",
"tokio-rustls 0.26.0",
"tokio-socks",
"tokio-socks 0.5.1-2",
"tokio-util",
"toml 0.7.8",
"url",
@@ -5347,6 +5347,7 @@ dependencies = [
"tokio",
"tokio-native-tls",
"tokio-rustls 0.24.1",
"tokio-socks 0.5.1",
"tokio-util",
"tower-service",
"url",
@@ -6606,6 +6607,18 @@ dependencies = [
"tokio-util",
]
[[package]]
name = "tokio-socks"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51165dfa029d2a65969413a6cc96f354b86b464498702f174a4efa13608fd8c0"
dependencies = [
"either",
"futures-util",
"thiserror",
"tokio",
]
[[package]]
name = "tokio-util"
version = "0.7.10"

View File

@@ -10,8 +10,8 @@ import 'package:flutter_hbb/models/peer_model.dart';
import 'package:flutter_hbb/models/platform_model.dart';
import 'package:get/get.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:http/http.dart' as http;
import '../utils/http_service.dart' as http;
import '../common.dart';
final syncAbOption = 'sync-ab-with-recent-sessions';

View File

@@ -7,7 +7,7 @@ import 'package:flutter_hbb/models/peer_model.dart';
import 'package:flutter_hbb/models/platform_model.dart';
import 'package:get/get.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
import '../utils/http_service.dart' as http;
class GroupModel {
final RxBool groupLoading = false.obs;

View File

@@ -6,10 +6,9 @@ import 'package:flutter/material.dart';
import 'package:flutter_hbb/common/hbbs/hbbs.dart';
import 'package:flutter_hbb/models/ab_model.dart';
import 'package:get/get.dart';
import 'package:http/http.dart' as http;
import '../common.dart';
import '../utils/http_service.dart' as httpR;
import '../utils/http_service.dart' as http;
import 'model.dart';
import 'platform_model.dart';
@@ -18,6 +17,7 @@ bool refreshingUser = false;
class UserModel {
final RxString userName = ''.obs;
final RxBool isAdmin = false.obs;
bool get isLogin => userName.isNotEmpty;
WeakReference<FFI> parent;
@@ -136,7 +136,8 @@ class UserModel {
/// throw [RequestException]
Future<LoginResponse> login(LoginRequest loginRequest) async {
final url = await bind.mainGetApiServer();
final resp = await httpR.post('$url/api/login', body: jsonEncode(loginRequest.toJson()));
final resp = await http.post(Uri.parse('$url/api/login'),
body: jsonEncode(loginRequest.toJson()));
final Map<String, dynamic> body;
try {

View File

@@ -1,13 +1,13 @@
import 'dart:convert';
import 'package:http/http.dart' as http;
import '../models/platform_model.dart';
export 'package:http/http.dart' show Response;
enum HttpMethod { get, post, put, delete }
class HttpService {
Future<http.Response> sendRequest(
String url,
Uri url,
HttpMethod method, {
Map<String, String>? headers,
dynamic body,
@@ -23,12 +23,8 @@ class HttpService {
return await _pollFultterHttp(url, method, headers: headers, body: body);
}
if (body is! String) {
throw Exception('Unsupported HTTP body type');
}
await bind.mainHttpRequest(
url: url,
url: url.toString(),
method: methodName.toLowerCase(),
body: body,
header: headersJson);
@@ -38,26 +34,25 @@ class HttpService {
}
Future<http.Response> _pollFultterHttp(
String url,
Uri url,
HttpMethod method, {
Map<String, String>? headers,
dynamic body,
}) async {
var response = http.Response('', 400); // 默认响应
Uri uri = Uri.parse(url);
switch (method) {
case HttpMethod.get:
response = await http.get(uri, headers: headers);
response = await http.get(url, headers: headers);
break;
case HttpMethod.post:
response = await http.post(uri, headers: headers, body: body);
response = await http.post(url, headers: headers, body: body);
break;
case HttpMethod.put:
response = await http.put(uri, headers: headers, body: body);
response = await http.put(url, headers: headers, body: body);
break;
case HttpMethod.delete:
response = await http.delete(uri, headers: headers, body: body);
response = await http.delete(url, headers: headers, body: body);
break;
default:
throw Exception('Unsupported HTTP method');
@@ -91,23 +86,23 @@ class HttpService {
}
}
Future<http.Response> get(String url, {Map<String, String>? headers}) async {
Future<http.Response> get(Uri url, {Map<String, String>? headers}) async {
return await HttpService().sendRequest(url, HttpMethod.get, headers: headers);
}
Future<http.Response> post(String url,
Future<http.Response> post(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
return await HttpService()
.sendRequest(url, HttpMethod.post, body: body, headers: headers);
}
Future<http.Response> put(String url,
Future<http.Response> put(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
return await HttpService()
.sendRequest(url, HttpMethod.put, body: body, headers: headers);
}
Future<http.Response> delete(String url,
Future<http.Response> delete(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
return await HttpService()
.sendRequest(url, HttpMethod.delete, body: body, headers: headers);

View File

@@ -145,6 +145,7 @@ use hbb_common::{
};
// #[cfg(any(target_os = "android", target_os = "ios", feature = "cli"))]
use hbb_common::{config::RENDEZVOUS_PORT, futures::future::join_all};
use hbb_common::log::debug;
use crate::hbbs_http::create_http_client_async;
use crate::ui_interface::{get_option, set_option};
@@ -1090,10 +1091,11 @@ pub async fn post_request_sync(url: String, body: String, header: &str) -> Resul
pub async fn http_request_sync(
url: String,
method: String,
body: String,
body: Option<String>,
header: String,
) -> ResultType<String> {
let http_client = create_http_client_async();
debug!("url: {}, method: {}, body: {:?}, header: {}", url, method, body,header);
let mut http_client = match method.as_str() {
"get" => http_client.get(url),
"post" => http_client.post(url),
@@ -1106,14 +1108,16 @@ pub async fn http_request_sync(
if let Value::Object(obj) = v {
for (key, value) in obj.iter() {
http_client = http_client.header(key, value.as_str().unwrap_or_default());
println!("Key: {}, Value: {}", key, value);
}
} else {
return Err(anyhow!("HTTP header information parsing failed!"));
}
if let Some(b) = body {
http_client = http_client.body(b);
}
let response = http_client
.body(body)
.timeout(std::time::Duration::from_secs(12))
.send()
.await?;
@@ -1142,6 +1146,7 @@ pub async fn http_request_sync(
// Convert map to JSON string
serde_json::to_string(&result).map_err(|e| anyhow!("Failed to serialize response: {}", e))
}
#[inline]
pub fn make_privacy_mode_msg_with_details(
state: back_notification::PrivacyModeState,

View File

@@ -878,7 +878,7 @@ pub fn main_get_api_server() -> String {
get_api_server()
}
pub fn main_http_request(url: String, method: String, body: String, header: String) {
pub fn main_http_request(url: String, method: String, body: Option<String>, header: String) {
http_request(url,method, body, header)
}

View File

@@ -14,11 +14,9 @@ macro_rules! configure_http_client {
match proxy_result {
Ok(proxy) => {
let proxy_setup = match &proxy.intercept {
ProxyScheme::Http { host, .. } => reqwest::Proxy::http(host),
ProxyScheme::Https { host, .. } => reqwest::Proxy::https(host),
ProxyScheme::Socks5 { addr, .. } => {
reqwest::Proxy::all(&format!("socks5://{}", addr))
}
ProxyScheme::Http { host, .. } =>{ reqwest::Proxy::http(format!("http://{}", host))},
ProxyScheme::Https { host, .. } => {reqwest::Proxy::https(format!("https://{}", host))},
ProxyScheme::Socks5 { addr, .. } => { reqwest::Proxy::all(&format!("socks5://{}", addr)) }
};
match proxy_setup {

View File

@@ -548,7 +548,7 @@ impl UI {
change_id_shared(id, old_id);
}
fn http_request(&self, url: String, method: String, body: String, header: String) {
fn http_request(&self, url: String, method: String, body: Option<String>, header: String) {
http_request(url, method, body, header)
}

View File

@@ -24,6 +24,7 @@ use std::{
collections::HashMap,
sync::{Arc, Mutex},
};
use hbb_common::log::error;
use crate::common::SOFTWARE_UPDATE_URL;
#[cfg(feature = "flutter")]
@@ -717,12 +718,12 @@ pub fn change_id(id: String) {
}
#[inline]
pub fn http_request(url: String, method: String, body: String, header: String) {
pub fn http_request(url: String, method: String, body: Option<String>, header: String) {
*ASYNC_JOB_STATUS.lock().unwrap() = " ".to_owned();
std::thread::spawn(move || {
*ASYNC_JOB_STATUS.lock().unwrap() =
match crate::http_request_sync(url, method, body, header) {
Err(err) => err.to_string(),
Err(err) => { error!("{}", err); err.to_string() },
Ok(text) => text,
};
});