Skip to content

Commit

Permalink
Change app http client
Browse files Browse the repository at this point in the history
  • Loading branch information
0xConsumer committed Jan 3, 2024
1 parent 7c8e632 commit 58f3e93
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 38 deletions.
95 changes: 95 additions & 0 deletions lib/core/http_client/dio_http_client.dart
Original file line number Diff line number Diff line change
@@ -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,
},
);
}
}
25 changes: 7 additions & 18 deletions lib/core/http_client/http_client_provider.dart
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ part 'app_update_data_providers.g.dart';
AppUpdateRepository appUpdateRepository(
AppUpdateRepositoryRef ref,
) {
return AppUpdateRepositoryImpl(dio: ref.watch(httpClientProvider));
return AppUpdateRepositoryImpl(httpClient: ref.watch(httpClientProvider));
}
9 changes: 5 additions & 4 deletions lib/features/app_update/data/app_update_repository.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:dio/dio.dart';
import 'package:fpdart/fpdart.dart';
import 'package:hiddify/core/http_client/dio_http_client.dart';
import 'package:hiddify/core/model/constants.dart';
import 'package:hiddify/core/model/environment.dart';
import 'package:hiddify/core/utils/exception_handler.dart';
Expand All @@ -18,9 +18,9 @@ abstract interface class AppUpdateRepository {
class AppUpdateRepositoryImpl
with ExceptionHandler, InfraLogger
implements AppUpdateRepository {
AppUpdateRepositoryImpl({required this.dio});
AppUpdateRepositoryImpl({required this.httpClient});

final Dio dio;
final DioHttpClient httpClient;

@override
TaskEither<AppUpdateFailure, RemoteVersionEntity> getLatestVersion({
Expand All @@ -32,7 +32,8 @@ class AppUpdateRepositoryImpl
if (!release.allowCustomUpdateChecker) {
throw Exception("custom update checkers are not supported");
}
final response = await dio.get<List>(Constants.githubReleasesApiUrl);
final response =
await httpClient.get<List>(Constants.githubReleasesApiUrl);
if (response.statusCode != 200 || response.data == null) {
loggy.warning("failed to fetch latest version info");
return left(const AppUpdateFailure.unexpected());
Expand Down
2 changes: 1 addition & 1 deletion lib/features/geo_asset/data/geo_asset_data_providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Future<GeoAssetRepository> geoAssetRepository(GeoAssetRepositoryRef ref) async {
final repo = GeoAssetRepositoryImpl(
geoAssetDataSource: ref.watch(geoAssetDataSourceProvider),
geoAssetPathResolver: ref.watch(geoAssetPathResolverProvider),
dio: ref.watch(httpClientProvider),
httpClient: ref.watch(httpClientProvider),
);
await repo.init().getOrElse((l) => throw l).run();
return repo;
Expand Down
10 changes: 5 additions & 5 deletions lib/features/geo_asset/data/geo_asset_repository.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'dart:io';

import 'package:dartx/dartx_io.dart';
import 'package:dio/dio.dart';
import 'package:drift/drift.dart';
import 'package:flutter/services.dart';
import 'package:fpdart/fpdart.dart';
import 'package:hiddify/core/database/app_database.dart';
import 'package:hiddify/core/http_client/dio_http_client.dart';
import 'package:hiddify/core/utils/exception_handler.dart';
import 'package:hiddify/features/geo_asset/data/geo_asset_data_mapper.dart';
import 'package:hiddify/features/geo_asset/data/geo_asset_data_source.dart';
Expand Down Expand Up @@ -35,12 +35,12 @@ class GeoAssetRepositoryImpl
GeoAssetRepositoryImpl({
required this.geoAssetDataSource,
required this.geoAssetPathResolver,
required this.dio,
required this.httpClient,
});

final GeoAssetDataSource geoAssetDataSource;
final GeoAssetPathResolver geoAssetPathResolver;
final Dio dio;
final DioHttpClient httpClient;

@override
TaskEither<GeoAssetFailure, Unit> init() {
Expand Down Expand Up @@ -141,7 +141,7 @@ class GeoAssetRepositoryImpl
loggy.debug(
"checking latest release of [${geoAsset.name}] on [${geoAsset.repositoryUrl}]",
);
final response = await dio.get<Map>(geoAsset.repositoryUrl);
final response = await httpClient.get<Map>(geoAsset.repositoryUrl);
if (response.statusCode != 200 || response.data == null) {
return left(
GeoAssetUnexpectedFailure.new(
Expand Down Expand Up @@ -180,7 +180,7 @@ class GeoAssetRepositoryImpl
loggy.debug("[${geoAsset.name}] download url: [$downloadUrl]");
final tempPath = "${file.path}.tmp";
await file.parent.create(recursive: true);
await dio.download(downloadUrl, tempPath);
await httpClient.download(downloadUrl, tempPath);
await File(tempPath).rename(file.path);

await geoAssetDataSource.patch(
Expand Down
2 changes: 1 addition & 1 deletion lib/features/profile/data/profile_data_providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Future<ProfileRepository> profileRepository(ProfileRepositoryRef ref) async {
profileDataSource: ref.watch(profileDataSourceProvider),
profilePathResolver: ref.watch(profilePathResolverProvider),
singbox: ref.watch(singboxServiceProvider),
dio: ref.watch(httpClientProvider),
httpClient: ref.watch(httpClientProvider),
);
await repo.init().getOrElse((l) => throw l).run();
return repo;
Expand Down
13 changes: 5 additions & 8 deletions lib/features/profile/data/profile_repository.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:drift/drift.dart';
import 'package:fpdart/fpdart.dart';
import 'package:hiddify/core/database/app_database.dart';
import 'package:hiddify/core/http_client/dio_http_client.dart';
import 'package:hiddify/core/utils/exception_handler.dart';
import 'package:hiddify/features/profile/data/profile_data_mapper.dart';
import 'package:hiddify/features/profile/data/profile_data_source.dart';
Expand All @@ -16,7 +16,6 @@ import 'package:hiddify/singbox/service/singbox_service.dart';
import 'package:hiddify/utils/custom_loggers.dart';
import 'package:hiddify/utils/link_parsers.dart';
import 'package:meta/meta.dart';
import 'package:retry/retry.dart';
import 'package:uuid/uuid.dart';

abstract interface class ProfileRepository {
Expand Down Expand Up @@ -63,13 +62,13 @@ class ProfileRepositoryImpl
required this.profileDataSource,
required this.profilePathResolver,
required this.singbox,
required this.dio,
required this.httpClient,
});

final ProfileDataSource profileDataSource;
final ProfilePathResolver profilePathResolver;
final SingboxService singbox;
final Dio dio;
final DioHttpClient httpClient;

@override
TaskEither<ProfileFailure, Unit> init() {
Expand Down Expand Up @@ -366,11 +365,9 @@ class ProfileRepositoryImpl
() async {
final file = profilePathResolver.file(fileName);
final tempFile = profilePathResolver.tempFile(fileName);

try {
final response = await retry(
() async => dio.download(url.trim(), tempFile.path),
maxAttempts: 3,
);
final response = await httpClient.download(url.trim(), tempFile.path);
final headers =
await _populateHeaders(response.headers.map, tempFile.path);
return await validateConfig(file.path, tempFile.path, false)
Expand Down
16 changes: 16 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "5.4.0"
dio_smart_retry:
dependency: "direct main"
description:
name: dio_smart_retry
sha256: "3d71450c19b4d91ef4c7d726a55a284bfc11eb3634f1f25006cdfab3f8595653"
url: "https://pub.dev"
source: hosted
version: "6.0.0"
drift:
dependency: "direct main"
description:
Expand Down Expand Up @@ -499,6 +507,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.0.2"
flutter_loggy_dio:
dependency: "direct main"
description:
name: flutter_loggy_dio
sha256: "682fe1fe136ae11cc9094364a4256d831b8cbe627b1370bb8fb9f135415265e2"
url: "https://pub.dev"
source: hosted
version: "3.0.1"
flutter_native_splash:
dependency: "direct main"
description:
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ dependencies:
native_dio_adapter: ^1.2.0
flutter_displaymode: ^0.6.0
windows_single_instance: ^1.0.1
flutter_loggy_dio: ^3.0.1
dio_smart_retry: ^6.0.0

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 58f3e93

Please sign in to comment.