forked from deriv-com/flutter-deriv-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto_estimations_response_extended.dart
57 lines (53 loc) · 2.54 KB
/
crypto_estimations_response_extended.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import 'package:deriv_dependency_injector/dependency_injector.dart';
import 'package:flutter_deriv_api/api/exceptions/base_api_exception.dart';
import 'package:flutter_deriv_api/api/models/base_exception_model.dart';
import 'package:flutter_deriv_api/api/response/crypto_estimations_response_result.dart';
import 'package:flutter_deriv_api/basic_api/generated/crypto_estimations_receive.dart';
import 'package:flutter_deriv_api/basic_api/generated/crypto_estimations_send.dart';
import 'package:flutter_deriv_api/basic_api/generated/forget_receive.dart';
import 'package:flutter_deriv_api/basic_api/response.dart';
import 'package:flutter_deriv_api/helpers/miscellaneous_helper.dart';
import 'package:flutter_deriv_api/services/connection/api_manager/base_api.dart';
import 'package:flutter_deriv_api/services/connection/call_manager/base_call_manager.dart';
/// Extended functionality for [CryptoEstimationsResponse] class.
class CryptoEstimationsResponseExtended extends CryptoEstimationsResponse {
static final BaseAPI _api = Injector()<BaseAPI>();
/// This will subscribe to crypto estimations.<br>
/// Inside [CryptoEstimationsRequest] class:
/// Incase of error, It will throw [BaseAPIException].
static Stream<CryptoEstimationsResponse?> subscribeToCryptoEstimates(
CryptoEstimationsRequest request, {
RequestCompareFunction? comparePredicate,
}) =>
_api
.subscribe(request: request, comparePredicate: comparePredicate)!
.map<CryptoEstimationsResponse?>(
(Response response) {
checkException(
response: response,
exceptionCreator: ({BaseExceptionModel? baseExceptionModel}) =>
BaseAPIException(baseExceptionModel: baseExceptionModel),
);
if (response is CryptoEstimationsReceive) {
return CryptoEstimationsResponse.fromJson(
response.cryptoEstimations, response.subscription);
} else {
throw Exception('Bad response received');
}
},
);
/// unsubscribe from the subscribed Crypto Estimates <br>
/// In case of error, It will throw [BaseAPIException].
static Future<bool> unsubscribeFromCryptoEstimates(
String subscriptionId,
) async {
final ForgetReceive response =
await _api.unsubscribe(subscriptionId: subscriptionId);
checkException(
response: response,
exceptionCreator: ({BaseExceptionModel? baseExceptionModel}) =>
BaseAPIException(baseExceptionModel: baseExceptionModel),
);
return response.forget!;
}
}