|
| 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 | +
|
| 15 | +class CryptoEstimationsResponseExtended extends CryptoEstimationsResponse { |
| 16 | + static final BaseAPI _api = Injector()<BaseAPI>(); |
| 17 | + |
| 18 | + /// This will subscribe to crypto estimations.<br> |
| 19 | + /// Inside [CryptoEstimationsRequest] class: |
| 20 | + /// Incase of error, It will throw [BaseAPIException]. |
| 21 | + static Stream<CryptoEstimationsResponse?> subscribeToCryptoEstimates( |
| 22 | + CryptoEstimationsRequest request, { |
| 23 | + RequestCompareFunction? comparePredicate, |
| 24 | + }) => |
| 25 | + _api |
| 26 | + .subscribe(request: request, comparePredicate: comparePredicate)! |
| 27 | + .map<CryptoEstimationsResponse?>( |
| 28 | + (Response response) { |
| 29 | + checkException( |
| 30 | + response: response, |
| 31 | + exceptionCreator: ({BaseExceptionModel? baseExceptionModel}) => |
| 32 | + BaseAPIException(baseExceptionModel: baseExceptionModel), |
| 33 | + ); |
| 34 | + |
| 35 | + if (response is CryptoEstimationsReceive) { |
| 36 | + return CryptoEstimationsResponse.fromJson( |
| 37 | + response.cryptoEstimations, response.subscription); |
| 38 | + } else { |
| 39 | + throw Exception('Bad response received'); |
| 40 | + } |
| 41 | + }, |
| 42 | + ); |
| 43 | + |
| 44 | + /// unsubscribe from the subscribed Crypto Estimates <br> |
| 45 | + /// In case of error, It will throw [BaseAPIException]. |
| 46 | + static Future<bool> unsubscribeFromCryptoEstimates( |
| 47 | + String subscriptionId) async { |
| 48 | + final ForgetReceive response = |
| 49 | + await _api.unsubscribe(subscriptionId: subscriptionId); |
| 50 | + checkException( |
| 51 | + response: response, |
| 52 | + exceptionCreator: ({BaseExceptionModel? baseExceptionModel}) => |
| 53 | + BaseAPIException(baseExceptionModel: baseExceptionModel), |
| 54 | + ); |
| 55 | + return response.forget!; |
| 56 | + } |
| 57 | +} |
0 commit comments