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..3a4a8c4f --- /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 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 new file mode 100644 index 00000000..968653fe --- /dev/null +++ b/core/rest_client/lib/src/http/custom_client_io.dart @@ -0,0 +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 createPlatformHttpClient() { + http.Client? client; + + final platform = defaultTargetPlatform; + + try { + client = switch (platform) { + 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 56dc262e..0a290ac7 100644 --- a/core/rest_client/lib/src/http/rest_client_http.dart +++ b/core/rest_client/lib/src/http/rest_client_http.dart @@ -1,37 +1,15 @@ 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; import 'package:rest_client/rest_client.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. -/// -/// 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; - - try { - client = switch (platform) { - 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(); -} +http.Client createDefaultHttpClient() => createPlatformHttpClient(); // coverage:ignore-end /// {@template rest_client_http}