Skip to content

Commit

Permalink
Ramin/test proxy aware ws connection (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramin-deriv authored Feb 29, 2024
1 parent 3729f1f commit f277e80
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
17 changes: 11 additions & 6 deletions lib/services/connection/api_manager/binary_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:developer' as dev;
import 'dart:io';

import 'package:flutter/widgets.dart';
import 'package:flutter_system_proxy/flutter_system_proxy.dart';
import 'package:web_socket_channel/io.dart';

import 'package:flutter_deriv_api/api/models/enums.dart';
Expand All @@ -23,8 +24,10 @@ import 'package:flutter_deriv_api/services/connection/call_manager/subscription_
/// This class is for handling Binary API connection and calling Binary APIs.
class BinaryAPI extends BaseAPI {
/// Initializes [BinaryAPI] instance.
BinaryAPI({String? key, bool enableDebug = false})
: super(key: key ?? '${UniqueKey()}', enableDebug: enableDebug);
BinaryAPI({
String? key,
bool enableDebug = false,
}) : super(key: key ?? '${UniqueKey()}', enableDebug: enableDebug);

static const Duration _disconnectTimeOut = Duration(seconds: 5);
static const Duration _websocketConnectTimeOut = Duration(seconds: 10);
Expand Down Expand Up @@ -74,12 +77,14 @@ class BinaryAPI extends BaseAPI {
_logDebugInfo('connecting to $uri.');

await _setUserAgent();
final String proxy = await FlutterSystemProxy.findProxyFromEnvironment(
uri.toString().replaceAll('wss', 'https'));

final HttpClient client = HttpClient()..findProxy = (Uri uri) => proxy;

// Initialize connection to websocket server.
_webSocketChannel = IOWebSocketChannel.connect(
'$uri',
pingInterval: _websocketConnectTimeOut,
);
_webSocketChannel = IOWebSocketChannel.connect('$uri',
pingInterval: _websocketConnectTimeOut, customClient: client);

_webSocketListener = _webSocketChannel?.stream
.map<Map<String, dynamic>?>((Object? result) => jsonDecode('$result'))
Expand Down
6 changes: 5 additions & 1 deletion lib/state/connection/connection_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ class ConnectionCubit extends Cubit<ConnectionState> {
this.printResponse = false,
}) : super(const ConnectionInitialState()) {
APIInitializer().initialize(
api: api ?? BinaryAPI(key: _key, enableDebug: enableDebug),
api: api ??
BinaryAPI(
key: _key,
enableDebug: enableDebug,
),
);

_api = Injector()<BaseAPI>();
Expand Down
5 changes: 4 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ dependencies:
meta: ^1.8.0
recase: ^4.0.0
rxdart: ^0.27.7
web_socket_channel: ^2.3.0
device_info_plus: ^8.1.0
flutter_system_proxy:
git:
url: [email protected]:BrowserStackCE/flutter_system_proxy.git
ref: main
package_info_plus: ^4.2.0
connectivity_plus: ^5.0.2

Expand Down
3 changes: 2 additions & 1 deletion test/state/connection/connection_bloc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ void main() {
setUpAll(
() {
connectionCubit = ConnectionCubit(
ConnectionInformation(appId: '', brand: '', endpoint: '',authEndpoint: ''),
ConnectionInformation(
appId: '', brand: '', endpoint: '', authEndpoint: ''),
api: MockAPI(),
);
},
Expand Down

0 comments on commit f277e80

Please sign in to comment.