@@ -22,14 +22,16 @@ import 'package:flutter_deriv_api/services/connection/call_manager/subscription_
22
22
23
23
/// This class is for handling Binary API connection and calling Binary APIs.
24
24
class BinaryAPI extends BaseAPI {
25
- /// Initializes binary api .
26
- BinaryAPI ({UniqueKey ? uniqueKey })
27
- : super (uniqueKey : uniqueKey ?? UniqueKey ());
25
+ /// Initializes [BinaryAPI] instance .
26
+ BinaryAPI ({String ? key, bool enableDebug = false })
27
+ : super (key : key ?? '${ UniqueKey ()}' , enableDebug : enableDebug );
28
28
29
29
static const Duration _disconnectTimeOut = Duration (seconds: 5 );
30
30
static const Duration _websocketConnectTimeOut = Duration (seconds: 10 );
31
31
32
- /// Represents the active web socket connection.
32
+ /// Represents the active websocket connection.
33
+ ///
34
+ /// This is used to send and receive data from the websocket server.
33
35
IOWebSocketChannel ? _webSocketChannel;
34
36
35
37
/// Stream subscription to API data.
@@ -69,11 +71,11 @@ class BinaryAPI extends BaseAPI {
69
71
},
70
72
);
71
73
72
- dev. log ( '$ runtimeType $ uniqueKey connecting to $uri .' );
74
+ _logDebugInfo ( ' connecting to $uri .' );
73
75
74
76
await _setUserAgent ();
75
77
76
- // Initialize connection to web socket server.
78
+ // Initialize connection to websocket server.
77
79
_webSocketChannel = IOWebSocketChannel .connect (
78
80
'$uri ' ,
79
81
pingInterval: _websocketConnectTimeOut,
@@ -83,27 +85,28 @@ class BinaryAPI extends BaseAPI {
83
85
.map <Map <String , dynamic >?>((Object ? result) => jsonDecode ('$result ' ))
84
86
.listen (
85
87
(Map <String , dynamic >? message) {
86
- onOpen? .call (uniqueKey );
88
+ onOpen? .call (key );
87
89
88
90
if (message != null ) {
89
91
_handleResponse (message, printResponse: printResponse);
90
92
}
91
93
},
92
94
onDone: () async {
93
- dev. log ( '$ runtimeType $ uniqueKey web socket is closed.' );
95
+ _logDebugInfo ( 'the websocket is closed.' );
94
96
95
- onDone? .call (uniqueKey );
97
+ onDone? .call (key );
96
98
},
97
99
onError: (Object error) {
98
- dev.log (
99
- '$runtimeType $uniqueKey the web socket connection is closed: $error .' ,
100
+ _logDebugInfo (
101
+ 'the websocket connection is closed with error.' ,
102
+ error: error,
100
103
);
101
104
102
- onError? .call (uniqueKey );
105
+ onError? .call (key );
103
106
},
104
107
);
105
108
106
- dev. log ( '$ runtimeType $ uniqueKey send initial message.' );
109
+ _logDebugInfo ( ' send initial message.' );
107
110
}
108
111
109
112
void _resetCallManagers () {
@@ -116,8 +119,8 @@ class BinaryAPI extends BaseAPI {
116
119
try {
117
120
_webSocketChannel? .sink.add (utf8.encode (jsonEncode (request)));
118
121
// ignore: avoid_catches_without_on_clauses
119
- } catch (e ) {
120
- dev. log ( '$ runtimeType $ uniqueKey error while adding to channel: $ e ' );
122
+ } catch (error ) {
123
+ _logDebugInfo ( ' error while adding to channel.' , error : error );
121
124
}
122
125
}
123
126
@@ -170,7 +173,7 @@ class BinaryAPI extends BaseAPI {
170
173
);
171
174
// ignore: avoid_catches_without_on_clauses
172
175
} catch (e) {
173
- dev. log ( '$ runtimeType $ uniqueKey disconnect error' , error: e);
176
+ _logDebugInfo ( ' disconnect error. ' , error: e);
174
177
} finally {
175
178
_webSocketListener = null ;
176
179
_webSocketChannel = null ;
@@ -184,21 +187,19 @@ class BinaryAPI extends BaseAPI {
184
187
required bool printResponse,
185
188
}) {
186
189
try {
187
- dev. log ( '$ runtimeType $ uniqueKey web socket is connected.' );
190
+ _logDebugInfo ( 'the websocket is connected.' );
188
191
189
192
// Make sure that the received message is a map and it's parsable otherwise it throws an exception.
190
193
final Map <String , dynamic > message = Map <String , dynamic >.from (response);
191
194
192
195
if (printResponse) {
193
- dev. log ( '$ runtimeType $ uniqueKey api response: $message .' );
196
+ _logDebugInfo ( ' api response: $message .' );
194
197
}
195
198
196
199
if (message.containsKey ('req_id' )) {
197
200
final int requestId = message['req_id' ];
198
201
199
- if (printResponse) {
200
- dev.log ('$runtimeType $uniqueKey have request id: $requestId .' );
201
- }
202
+ _logDebugInfo ('have request id: $requestId .' );
202
203
203
204
if (_callManager? .contains (requestId) ?? false ) {
204
205
_callManager! .handleResponse (
@@ -211,18 +212,15 @@ class BinaryAPI extends BaseAPI {
211
212
response: message,
212
213
);
213
214
} else {
214
- dev. log (
215
+ _logDebugInfo (
215
216
'$runtimeType $requestId , does not match anything in our pending queue.' ,
216
217
);
217
218
}
218
219
} else {
219
- dev. log ( '$ runtimeType $ uniqueKey no req_id, ignoring.' );
220
+ _logDebugInfo ( ' no req_id, ignoring.' );
220
221
}
221
222
} on Exception catch (e) {
222
- dev.log (
223
- '$runtimeType $uniqueKey failed to process $response - $e ' ,
224
- error: e,
225
- );
223
+ _logDebugInfo ('failed to process $response .' , error: e);
226
224
}
227
225
}
228
226
@@ -233,4 +231,10 @@ class BinaryAPI extends BaseAPI {
233
231
WebSocket .userAgent = userAgent;
234
232
}
235
233
}
234
+
235
+ void _logDebugInfo (String message, {Object ? error}) {
236
+ if (enableDebug) {
237
+ dev.log ('$runtimeType $key $message ' , error: error);
238
+ }
239
+ }
236
240
}
0 commit comments