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

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