From 0dd629886d70ce4588d4240aba9342605ab943e4 Mon Sep 17 00:00:00 2001 From: sb-dor Date: Sun, 22 Jun 2025 21:40:45 +0500 Subject: [PATCH 1/8] feat: add platform-specific HTTP client with Cronet and Cupertino support --- .../lib/src/http/custom_client_browser.dart | 5 +++++ .../lib/src/http/custom_client_io.dart | 17 +++++++++++++++++ .../lib/src/http/rest_client_http.dart | 13 ++++--------- 3 files changed, 26 insertions(+), 9 deletions(-) create mode 100644 core/rest_client/lib/src/http/custom_client_browser.dart create mode 100644 core/rest_client/lib/src/http/custom_client_io.dart diff --git a/core/rest_client/lib/src/http/custom_client_browser.dart b/core/rest_client/lib/src/http/custom_client_browser.dart new file mode 100644 index 00000000..374e2846 --- /dev/null +++ b/core/rest_client/lib/src/http/custom_client_browser.dart @@ -0,0 +1,5 @@ +import 'package:http/http.dart' as http; + +http.Client? createCustomClient() { + return http.Client(); +} diff --git a/core/rest_client/lib/src/http/custom_client_io.dart b/core/rest_client/lib/src/http/custom_client_io.dart new file mode 100644 index 00000000..41171586 --- /dev/null +++ b/core/rest_client/lib/src/http/custom_client_io.dart @@ -0,0 +1,17 @@ +import 'package:cronet_http/cronet_http.dart' show CronetClient; +import 'package:cupertino_http/cupertino_http.dart' show CupertinoClient; +import 'package:flutter/foundation.dart' show TargetPlatform, defaultTargetPlatform; +import 'package:http/http.dart' as http; + +/// Creates an [http.Client] based on the current platform. +/// +/// For Android, it returns a [CronetClient] with the default Cronet engine. +/// For iOS and macOS, it returns a [CupertinoClient] +/// with the default session configuration. +http.Client? createCustomClient() { + return switch (defaultTargetPlatform) { + TargetPlatform.android => CronetClient.defaultCronetEngine(), + TargetPlatform.iOS || TargetPlatform.macOS => CupertinoClient.defaultSessionConfiguration(), + _ => null, + }; +} diff --git a/core/rest_client/lib/src/http/rest_client_http.dart b/core/rest_client/lib/src/http/rest_client_http.dart index 56dc262e..99f4eefa 100644 --- a/core/rest_client/lib/src/http/rest_client_http.dart +++ b/core/rest_client/lib/src/http/rest_client_http.dart @@ -1,10 +1,9 @@ import 'dart:async'; - -import 'package:cronet_http/cronet_http.dart' show CronetClient; -import 'package:cupertino_http/cupertino_http.dart' show CupertinoClient; -import 'package:flutter/foundation.dart' show TargetPlatform, defaultTargetPlatform; +import 'package:flutter/foundation.dart' show defaultTargetPlatform; import 'package:http/http.dart' as http; import 'package:rest_client/rest_client.dart'; +import 'package:rest_client/src/http/custom_client_io.dart' + if (dart.library.js_interop) 'package:rest_client/src/http/custom_client_browser.dart'; import 'package:rest_client/src/http/check_exception_io.dart' if (dart.library.js_interop) 'package:rest_client/src/http/check_exception_browser.dart'; @@ -19,11 +18,7 @@ http.Client createDefaultHttpClient() { final platform = defaultTargetPlatform; try { - client = switch (platform) { - TargetPlatform.android => CronetClient.defaultCronetEngine(), - TargetPlatform.iOS || TargetPlatform.macOS => CupertinoClient.defaultSessionConfiguration(), - _ => null, - }; + client = createCustomClient(); } on Object catch (e, stackTrace) { Zone.current.print( 'Failed to create a default http client for platform $platform $e $stackTrace', From 823d25847eecae746305e1ed36c3d0134a2cbd0f Mon Sep 17 00:00:00 2001 From: sb-dor Date: Mon, 23 Jun 2025 06:07:23 +0500 Subject: [PATCH 2/8] style: sort imports and fix doc references in rest_client_http.dart --- core/rest_client/lib/src/http/rest_client_http.dart | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/rest_client/lib/src/http/rest_client_http.dart b/core/rest_client/lib/src/http/rest_client_http.dart index 99f4eefa..20cfa9e1 100644 --- a/core/rest_client/lib/src/http/rest_client_http.dart +++ b/core/rest_client/lib/src/http/rest_client_http.dart @@ -2,10 +2,11 @@ import 'dart:async'; import 'package:flutter/foundation.dart' show defaultTargetPlatform; import 'package:http/http.dart' as http; import 'package:rest_client/rest_client.dart'; -import 'package:rest_client/src/http/custom_client_io.dart' - if (dart.library.js_interop) 'package:rest_client/src/http/custom_client_browser.dart'; + import 'package:rest_client/src/http/check_exception_io.dart' if (dart.library.js_interop) 'package:rest_client/src/http/check_exception_browser.dart'; +import 'package:rest_client/src/http/custom_client_io.dart' + if (dart.library.js_interop) 'package:rest_client/src/http/custom_client_browser.dart'; // coverage:ignore-start /// Creates an [http.Client] based on the current platform. From 483c1e90d85270f1dde1bda4bc149330ebd8193d Mon Sep 17 00:00:00 2001 From: sb-dor Date: Mon, 23 Jun 2025 06:14:40 +0500 Subject: [PATCH 3/8] moved platform-specific client comment into custom_client_io.dart --- core/rest_client/lib/src/http/rest_client_http.dart | 4 ---- 1 file changed, 4 deletions(-) diff --git a/core/rest_client/lib/src/http/rest_client_http.dart b/core/rest_client/lib/src/http/rest_client_http.dart index 20cfa9e1..60231271 100644 --- a/core/rest_client/lib/src/http/rest_client_http.dart +++ b/core/rest_client/lib/src/http/rest_client_http.dart @@ -10,10 +10,6 @@ import 'package:rest_client/src/http/custom_client_io.dart' // coverage:ignore-start /// Creates an [http.Client] based on the current platform. -/// -/// For Android, it returns a [CronetClient] with the default Cronet engine. -/// For iOS and macOS, it returns a [CupertinoClient] -/// with the default session configuration. http.Client createDefaultHttpClient() { http.Client? client; final platform = defaultTargetPlatform; From 26e7a68ad31ccebaa099c172d9017161b5bd4d48 Mon Sep 17 00:00:00 2001 From: sb-dor Date: Mon, 23 Jun 2025 06:22:07 +0500 Subject: [PATCH 4/8] style: remove unnecessary nullable return type in createCustomClient --- core/rest_client/lib/src/http/custom_client_browser.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/rest_client/lib/src/http/custom_client_browser.dart b/core/rest_client/lib/src/http/custom_client_browser.dart index 374e2846..4d228e4a 100644 --- a/core/rest_client/lib/src/http/custom_client_browser.dart +++ b/core/rest_client/lib/src/http/custom_client_browser.dart @@ -1,5 +1,5 @@ import 'package:http/http.dart' as http; -http.Client? createCustomClient() { +http.Client createCustomClient() { return http.Client(); } From 28b4c2b8b98d5c85cafeb575cacea29d897f8942 Mon Sep 17 00:00:00 2001 From: sb-dor Date: Tue, 24 Jun 2025 04:58:31 +0500 Subject: [PATCH 5/8] refactor: moved platform-specific fallback logic into createCustomClient --- .../lib/src/http/custom_client_io.dart | 27 ++++++++++++++----- .../lib/src/http/rest_client_http.dart | 15 +---------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/core/rest_client/lib/src/http/custom_client_io.dart b/core/rest_client/lib/src/http/custom_client_io.dart index 41171586..75e727cc 100644 --- a/core/rest_client/lib/src/http/custom_client_io.dart +++ b/core/rest_client/lib/src/http/custom_client_io.dart @@ -1,17 +1,32 @@ +import 'dart:async'; + import 'package:cronet_http/cronet_http.dart' show CronetClient; import 'package:cupertino_http/cupertino_http.dart' show CupertinoClient; import 'package:flutter/foundation.dart' show TargetPlatform, defaultTargetPlatform; import 'package:http/http.dart' as http; +// coverage:ignore-start /// Creates an [http.Client] based on the current platform. /// /// For Android, it returns a [CronetClient] with the default Cronet engine. /// For iOS and macOS, it returns a [CupertinoClient] /// with the default session configuration. -http.Client? createCustomClient() { - return switch (defaultTargetPlatform) { - TargetPlatform.android => CronetClient.defaultCronetEngine(), - TargetPlatform.iOS || TargetPlatform.macOS => CupertinoClient.defaultSessionConfiguration(), - _ => null, - }; +http.Client createCustomClient() { + http.Client? client; + + final platform = defaultTargetPlatform; + + try { + client = switch (defaultTargetPlatform) { + TargetPlatform.android => CronetClient.defaultCronetEngine(), + TargetPlatform.iOS || TargetPlatform.macOS => CupertinoClient.defaultSessionConfiguration(), + _ => null, + }; + } on Object catch (e, stackTrace) { + Zone.current.print( + 'Failed to create a default http client for platform $platform $e $stackTrace', + ); + } + + return client ?? http.Client(); } diff --git a/core/rest_client/lib/src/http/rest_client_http.dart b/core/rest_client/lib/src/http/rest_client_http.dart index 60231271..eeac5125 100644 --- a/core/rest_client/lib/src/http/rest_client_http.dart +++ b/core/rest_client/lib/src/http/rest_client_http.dart @@ -10,20 +10,7 @@ import 'package:rest_client/src/http/custom_client_io.dart' // coverage:ignore-start /// Creates an [http.Client] based on the current platform. -http.Client createDefaultHttpClient() { - http.Client? client; - final platform = defaultTargetPlatform; - - try { - client = createCustomClient(); - } on Object catch (e, stackTrace) { - Zone.current.print( - 'Failed to create a default http client for platform $platform $e $stackTrace', - ); - } - - return client ?? http.Client(); -} +http.Client createDefaultHttpClient() => createCustomClient(); // coverage:ignore-end /// {@template rest_client_http} From 78cdeac4d8adc3fd8e596140a83609aaa8c37784 Mon Sep 17 00:00:00 2001 From: sb-dor Date: Tue, 24 Jun 2025 04:59:41 +0500 Subject: [PATCH 6/8] removed unnecessary import --- core/rest_client/lib/src/http/rest_client_http.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/core/rest_client/lib/src/http/rest_client_http.dart b/core/rest_client/lib/src/http/rest_client_http.dart index eeac5125..b2b6e409 100644 --- a/core/rest_client/lib/src/http/rest_client_http.dart +++ b/core/rest_client/lib/src/http/rest_client_http.dart @@ -1,5 +1,4 @@ import 'dart:async'; -import 'package:flutter/foundation.dart' show defaultTargetPlatform; import 'package:http/http.dart' as http; import 'package:rest_client/rest_client.dart'; From da2c16982e64876b7070ae02da4f4963125d471d Mon Sep 17 00:00:00 2001 From: sb-dor Date: Tue, 24 Jun 2025 05:05:20 +0500 Subject: [PATCH 7/8] minor improvements --- core/rest_client/lib/src/http/custom_client_io.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/rest_client/lib/src/http/custom_client_io.dart b/core/rest_client/lib/src/http/custom_client_io.dart index 75e727cc..592df65f 100644 --- a/core/rest_client/lib/src/http/custom_client_io.dart +++ b/core/rest_client/lib/src/http/custom_client_io.dart @@ -17,7 +17,7 @@ http.Client createCustomClient() { final platform = defaultTargetPlatform; try { - client = switch (defaultTargetPlatform) { + client = switch (platform) { TargetPlatform.android => CronetClient.defaultCronetEngine(), TargetPlatform.iOS || TargetPlatform.macOS => CupertinoClient.defaultSessionConfiguration(), _ => null, From c3846bd14b8cf15014d7972d88825313f84f18d5 Mon Sep 17 00:00:00 2001 From: sb-dor Date: Tue, 24 Jun 2025 17:46:46 +0500 Subject: [PATCH 8/8] refactor: renamed createCustomClient to createPlatformHttpClient for clarity --- core/rest_client/lib/src/http/custom_client_browser.dart | 2 +- core/rest_client/lib/src/http/custom_client_io.dart | 2 +- core/rest_client/lib/src/http/rest_client_http.dart | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core/rest_client/lib/src/http/custom_client_browser.dart b/core/rest_client/lib/src/http/custom_client_browser.dart index 4d228e4a..3a4a8c4f 100644 --- a/core/rest_client/lib/src/http/custom_client_browser.dart +++ b/core/rest_client/lib/src/http/custom_client_browser.dart @@ -1,5 +1,5 @@ import 'package:http/http.dart' as http; -http.Client createCustomClient() { +http.Client createPlatformHttpClient() { return http.Client(); } diff --git a/core/rest_client/lib/src/http/custom_client_io.dart b/core/rest_client/lib/src/http/custom_client_io.dart index 592df65f..968653fe 100644 --- a/core/rest_client/lib/src/http/custom_client_io.dart +++ b/core/rest_client/lib/src/http/custom_client_io.dart @@ -11,7 +11,7 @@ import 'package:http/http.dart' as http; /// For Android, it returns a [CronetClient] with the default Cronet engine. /// For iOS and macOS, it returns a [CupertinoClient] /// with the default session configuration. -http.Client createCustomClient() { +http.Client createPlatformHttpClient() { http.Client? client; final platform = defaultTargetPlatform; diff --git a/core/rest_client/lib/src/http/rest_client_http.dart b/core/rest_client/lib/src/http/rest_client_http.dart index b2b6e409..0a290ac7 100644 --- a/core/rest_client/lib/src/http/rest_client_http.dart +++ b/core/rest_client/lib/src/http/rest_client_http.dart @@ -9,7 +9,7 @@ import 'package:rest_client/src/http/custom_client_io.dart' // coverage:ignore-start /// Creates an [http.Client] based on the current platform. -http.Client createDefaultHttpClient() => createCustomClient(); +http.Client createDefaultHttpClient() => createPlatformHttpClient(); // coverage:ignore-end /// {@template rest_client_http}