getOracleData method

Future<OracleUcoPrice> getOracleData({
  1. int timestamp = 0,
})

Returns a value of Oracle Uco_Price in {OracleUcoPrice} from a timestamp.

if timestamp = 0 or not precised, the last price is returned.

Implementation

Future<OracleUcoPrice> getOracleData({int timestamp = 0}) async {
  final completer = Completer<OracleUcoPrice>();
  var oracleUcoPrice = const OracleUcoPrice(uco: Uco(eur: 0, usd: 0));
  final requestHeaders = <String, String>{
    'Content-type': 'application/json',
    'Accept': 'application/json',
  };
  var body = '';

  try {
    if (timestamp == 0) {
      body =
          '{"query": "query { oracleData { timestamp services { uco { eur, usd } } } }"}';
    } else {
      body =
          '{"query": "query { oracleData(timestamp: $timestamp) { timestamp services { uco { eur, usd } } } }"}';
    }

    _logger.fine('getOracleData: requestHttp.body=$body');
    final responseHttp = await http.post(
      Uri.parse('${endpoint!}/api'),
      body: body,
      headers: requestHeaders,
    );
    _logger.fine('getOracleData: responseHttp.body=${responseHttp.body}');
    if (responseHttp.statusCode == 200) {
      final oracleDataResponse =
          oracleDataResponseFromJson(responseHttp.body);
      if (oracleDataResponse.data != null &&
          oracleDataResponse.data!.oracleData != null &&
          oracleDataResponse.data!.oracleData!.services != null &&
          oracleDataResponse.data!.oracleData!.services!.uco != null) {
        oracleUcoPrice = OracleUcoPrice(
          timestamp: oracleDataResponse.data!.oracleData!.timestamp,
          uco: Uco(
            eur: oracleDataResponse.data!.oracleData!.services!.uco!.eur,
            usd: oracleDataResponse.data!.oracleData!.services!.uco!.usd,
          ),
        );
      }
    }
  } catch (e, stack) {
    _logger.severe(
      'getOracleData failed',
      e,
      stack,
    );
  }

  completer.complete(oracleUcoPrice);
  return completer.future;
}