Call Onion API from http

This commit is contained in:
OmarHatem 2023-03-02 19:24:52 +02:00
parent d5c4bd0236
commit 29e9bb2181

View file

@ -85,9 +85,7 @@ class TrocadorExchangeProvider extends ExchangeProvider {
params['id'] = _lastUsedRateId; params['id'] = _lastUsedRateId;
} }
final String apiAuthority = await _getAuthority(); final uri = await _getUri(createTradePath, params);
final uri = Uri.https(apiAuthority, createTradePath, params);
final response = await get(uri); final response = await get(uri);
if (response.statusCode == 400) { if (response.statusCode == 400) {
@ -137,15 +135,13 @@ class TrocadorExchangeProvider extends ExchangeProvider {
{required CryptoCurrency from, {required CryptoCurrency from,
required CryptoCurrency to, required CryptoCurrency to,
required bool isFixedRateMode}) async { required bool isFixedRateMode}) async {
final params = <String, String>{
final params = <String, String> {
'api_key': apiKey, 'api_key': apiKey,
'ticker': from.title.toLowerCase(), 'ticker': from.title.toLowerCase(),
'name': from.name, 'name': from.name,
}; };
final String apiAuthority = await _getAuthority(); final uri = await _getUri(coinPath, params);
final uri = Uri.https(apiAuthority, coinPath, params);
final response = await get(uri); final response = await get(uri);
@ -161,7 +157,6 @@ class TrocadorExchangeProvider extends ExchangeProvider {
final coinJson = responseJSON.first as Map<String, dynamic>; final coinJson = responseJSON.first as Map<String, dynamic>;
return Limits( return Limits(
min: coinJson['minimum'] as double, min: coinJson['minimum'] as double,
max: coinJson['maximum'] as double, max: coinJson['maximum'] as double,
@ -180,8 +175,6 @@ class TrocadorExchangeProvider extends ExchangeProvider {
return 0.0; return 0.0;
} }
final String apiAuthority = await _getAuthority();
final params = <String, String>{ final params = <String, String>{
'api_key': apiKey, 'api_key': apiKey,
'ticker_from': from.title.toLowerCase(), 'ticker_from': from.title.toLowerCase(),
@ -196,7 +189,7 @@ class TrocadorExchangeProvider extends ExchangeProvider {
'best_only': 'True', 'best_only': 'True',
}; };
final uri = Uri.https(apiAuthority, newRatePath, params); final uri = await _getUri(newRatePath, params);
final response = await get(uri); final response = await get(uri);
final responseJSON = json.decode(response.body) as Map<String, dynamic>; final responseJSON = json.decode(response.body) as Map<String, dynamic>;
final fromAmount = double.parse(responseJSON['amount_from'].toString()); final fromAmount = double.parse(responseJSON['amount_from'].toString());
@ -216,8 +209,7 @@ class TrocadorExchangeProvider extends ExchangeProvider {
@override @override
Future<Trade> findTradeById({required String id}) async { Future<Trade> findTradeById({required String id}) async {
final String apiAuthority = await _getAuthority(); final uri = await _getUri(tradePath, {'api_key': apiKey, 'id': id});
final uri = Uri.https(apiAuthority, tradePath, {'api_key': apiKey, 'id': id});
return get(uri).then((response) { return get(uri).then((response) {
if (response.statusCode != 200) { if (response.statusCode != 200) {
throw Exception('Unexpected http status: ${response.statusCode}'); throw Exception('Unexpected http status: ${response.statusCode}');
@ -298,23 +290,22 @@ class TrocadorExchangeProvider extends ExchangeProvider {
} }
} }
Future<String> _getAuthority() async { Future<Uri> _getUri(String createTradePath, Map<String, String> queryParams) async {
if(!supportsOnionAddress){ if (!supportsOnionAddress) {
return clearNetAuthority; return Uri.https(clearNetAuthority, tradePath, queryParams);
}
if (useTorOnly) {
return Uri.http(onionApiAuthority, tradePath, queryParams);
} }
try { try {
if (useTorOnly) { final uri = Uri.http(onionApiAuthority, tradePath);
return onionApiAuthority;
}
final uri = Uri.https(onionApiAuthority, tradePath);
await get(uri); await get(uri);
return onionApiAuthority; return Uri.http(onionApiAuthority, tradePath, queryParams);
} catch (e) { } catch (e) {
return Uri.https(clearNetAuthority, tradePath, queryParams);
return clearNetAuthority;
} }
} }
} }