Change app http client
This commit is contained in:
95
lib/core/http_client/dio_http_client.dart
Normal file
95
lib/core/http_client/dio_http_client.dart
Normal file
@@ -0,0 +1,95 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:dio_smart_retry/dio_smart_retry.dart';
|
||||
import 'package:flutter_loggy_dio/flutter_loggy_dio.dart';
|
||||
import 'package:hiddify/utils/custom_loggers.dart';
|
||||
|
||||
class DioHttpClient with InfraLogger {
|
||||
DioHttpClient({
|
||||
required Duration timeout,
|
||||
required String userAgent,
|
||||
required bool debug,
|
||||
}) {
|
||||
_dio = Dio(
|
||||
BaseOptions(
|
||||
connectTimeout: timeout,
|
||||
sendTimeout: timeout,
|
||||
receiveTimeout: timeout,
|
||||
headers: {"User-Agent": userAgent},
|
||||
),
|
||||
);
|
||||
|
||||
_dio.interceptors.add(
|
||||
RetryInterceptor(
|
||||
dio: _dio,
|
||||
retryDelays: const [
|
||||
Duration(seconds: 1),
|
||||
Duration(seconds: 2),
|
||||
Duration(seconds: 3),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
if (debug) {
|
||||
_dio.interceptors.add(LoggyDioInterceptor(requestHeader: true));
|
||||
}
|
||||
}
|
||||
|
||||
late final Dio _dio;
|
||||
|
||||
Future<Response<T>> get<T>(
|
||||
String url, {
|
||||
CancelToken? cancelToken,
|
||||
String? userAgent,
|
||||
({String username, String password})? credentials,
|
||||
}) async {
|
||||
return _dio.get<T>(
|
||||
url,
|
||||
cancelToken: cancelToken,
|
||||
options: _options(url, userAgent: userAgent, credentials: credentials),
|
||||
);
|
||||
}
|
||||
|
||||
Future<Response> download(
|
||||
String url,
|
||||
String path, {
|
||||
CancelToken? cancelToken,
|
||||
String? userAgent,
|
||||
({String username, String password})? credentials,
|
||||
}) async {
|
||||
return _dio.download(
|
||||
url,
|
||||
path,
|
||||
cancelToken: cancelToken,
|
||||
options: _options(url, userAgent: userAgent, credentials: credentials),
|
||||
);
|
||||
}
|
||||
|
||||
Options _options(
|
||||
String url, {
|
||||
String? userAgent,
|
||||
({String username, String password})? credentials,
|
||||
}) {
|
||||
final uri = Uri.parse(url);
|
||||
|
||||
String? userInfo;
|
||||
if (credentials != null) {
|
||||
userInfo = "${credentials.username}:${credentials.password}";
|
||||
} else if (uri.userInfo.isNotEmpty) {
|
||||
userInfo = uri.userInfo;
|
||||
}
|
||||
|
||||
String? basicAuth;
|
||||
if (userInfo != null) {
|
||||
basicAuth = "Basic ${base64.encode(utf8.encode(userInfo))}";
|
||||
}
|
||||
|
||||
return Options(
|
||||
headers: {
|
||||
if (userAgent != null) "User-Agent": userAgent,
|
||||
if (basicAuth != null) "authorization": basicAuth,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,26 +1,15 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:hiddify/core/app_info/app_info_provider.dart';
|
||||
import 'package:hiddify/core/http_client/dio_http_client.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
part 'http_client_provider.g.dart';
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
Dio httpClient(HttpClientRef ref) {
|
||||
final dio = Dio(
|
||||
BaseOptions(
|
||||
connectTimeout: const Duration(seconds: 15),
|
||||
sendTimeout: const Duration(seconds: 15),
|
||||
receiveTimeout: const Duration(seconds: 15),
|
||||
headers: {
|
||||
"User-Agent": ref.watch(appInfoProvider).requireValue.userAgent,
|
||||
},
|
||||
),
|
||||
DioHttpClient httpClient(HttpClientRef ref) {
|
||||
return DioHttpClient(
|
||||
timeout: const Duration(seconds: 15),
|
||||
userAgent: ref.watch(appInfoProvider).requireValue.userAgent,
|
||||
debug: kDebugMode,
|
||||
);
|
||||
// https://github.com/dart-lang/http/issues/1047
|
||||
// https://github.com/cfug/dio/issues/2042
|
||||
// final debug = ref.read(debugModeNotifierProvider);
|
||||
// if (debug && (Platform.isAndroid || Platform.isIOS || Platform.isMacOS)) {
|
||||
// dio.httpClientAdapter = NativeAdapter();
|
||||
// }
|
||||
return dio;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user