Skip to content

Commit 9c45e83

Browse files
committed
Create crypto_estimations_response_extended.dart
1 parent db226e6 commit 9c45e83

File tree

1 file changed

+56
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)