typed response data

This commit is contained in:
julian 2023-01-14 08:21:32 -06:00
parent 79edf1292d
commit f80b47d467
3 changed files with 12 additions and 12 deletions

View file

@ -28,10 +28,10 @@ class BuyDataLoadingService {
final response = await SimplexAPI.instance.getSupported();
if (response['supportedCryptos'] != null) {
if (response.value != null) {
ref
.read(supportedSimplexCurrenciesProvider)
.updateSupportedCryptos(response['supportedCryptos']);
.updateSupportedCryptos(response.value!.item1);
} else {
error = true;
Logging.instance.log(
@ -40,10 +40,10 @@ class BuyDataLoadingService {
);
}
if (response['supportedFiats'] != null) {
if (response.value != null) {
ref
.read(supportedSimplexCurrenciesProvider)
.updateSupportedFiats(response['supportedFiats']);
.updateSupportedFiats(response.value!.item2);
} else {
error = true;
Logging.instance.log(

View file

@ -12,8 +12,8 @@ class BuyException implements Exception {
}
class BuyResponse<T> {
late final T? value;
late final BuyException? exception;
final T? value;
final BuyException? exception;
BuyResponse({this.value, this.exception});

View file

@ -11,6 +11,7 @@ import 'package:stackwallet/models/buy/response_objects/fiat.dart';
// import 'package:stackwallet/models/buy/response_objects/crypto.dart';
import 'package:stackwallet/services/buy/buy_response.dart';
import 'package:stackwallet/utilities/logger.dart';
import 'package:tuple/tuple.dart';
class SimplexAPI {
static const String scheme = "https";
@ -141,7 +142,7 @@ class SimplexAPI {
// }
// }
dynamic /*Future<BuyResponse<List<Fiat>>>*/ getSupported() async {
Future<BuyResponse<Tuple2<List<Crypto>, List<Fiat>>>> getSupported() async {
// example for quote courtesy of @danrmiller
// curl -H "Content-Type: application/json" -d '{"digital_currency": "BTC", "fiat_currency": "USD", "requested_currency": "USD", "requested_amount": 100}' http://sandbox-api.stackwallet.com/quote
// official docs reference eg
@ -164,7 +165,7 @@ class SimplexAPI {
'getAvailableCurrencies exception: statusCode= ${res.statusCode}');
}
dynamic jsonArray = jsonDecode(res.body);
final jsonArray = jsonDecode(res.body);
return await compute(_parseSupported, jsonArray);
} catch (e, s) {
@ -179,7 +180,8 @@ class SimplexAPI {
}
}
dynamic /*BuyResponse<List<Fiat>>*/ _parseSupported(dynamic jsonArray) {
BuyResponse<Tuple2<List<Crypto>, List<Fiat>>> _parseSupported(
dynamic jsonArray) {
try {
List<Crypto> cryptos = [];
List<Fiat> fiats = [];
@ -204,9 +206,7 @@ class SimplexAPI {
}));
}
var supported = {'supportedCryptos': cryptos, 'supportedFiats': fiats};
return supported;
return BuyResponse(value: Tuple2(cryptos, fiats));
} catch (e, s) {
Logging.instance
.log("_parseSupported exception: $e\n$s", level: LogLevel.Error);