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<OracleUcoPrice> completer = Completer<OracleUcoPrice>();
  OracleUcoPrice oracleUcoPrice = OracleUcoPrice(uco: Uco(eur: 0, usd: 0));
  final Map<String, String> requestHeaders = <String, String>{
    'Content-type': 'application/json',
    'Accept': 'application/json',
  };
  String 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 http.Response responseHttp = await http.post(
        Uri.parse('${endpoint!}/api'),
        body: body,
        headers: requestHeaders);
    log('getOracleData: responseHttp.body=${responseHttp.body}');
    if (responseHttp.statusCode == 200) {
      final OracleDataResponse 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;
}