getOracleData method

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

return a value of Oracle Uco_Price in {OracleUcoPrice} from a timestamp if timestamp = 0 or not precised, the last price is returned @param {int} timestamp

Implementation

Future<OracleUcoPrice> getOracleData({int timestamp = 0}) async {
  final completer = Completer<OracleUcoPrice>();
  var oracleUcoPrice = 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 } } } }"}';
    }

    log('getOracleData: requestHttp.body=$body');
    final responseHttp = await http.post(Uri.parse('${endpoint!}/api'),
        body: body, headers: requestHeaders,);
    log('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(
            uco: Uco(
                eur: oracleDataResponse.data!.oracleData!.services!.uco!.eur,
                usd:
                    oracleDataResponse.data!.oracleData!.services!.uco!.usd,),);
      }
    }
  } catch (e) {
    log('getOracleData: error=$e');
  }

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