Call Onion API from http

OmarHatem committed Mar 2, 2023 at 19:24 UTC 29e9bb2181ef60e419d67938f62afa8e5d39ca9c
1 file changed +35 -44
lib/exchange/trocador/trocador_exchange_provider.dart
+35 -44
@@ -16,7 +16,7 @@ class TrocadorExchangeProvider extends ExchangeProvider {
16 TrocadorExchangeProvider({this.useTorOnly = false})
17 : _lastUsedRateId = '',
18 super(pairList: _supportedPairs());
19 -
19 +
20 bool useTorOnly;
21
22 static const List<CryptoCurrency> _notSupported = [
@@ -85,9 +85,7 @@ class TrocadorExchangeProvider extends ExchangeProvider {
85 params['id'] = _lastUsedRateId;
86 }
87
88 - final String apiAuthority = await _getAuthority();
89 -
90 - final uri = Uri.https(apiAuthority, createTradePath, params);
88 + final uri = await _getUri(createTradePath, params);
89 final response = await get(uri);
90
91 if (response.statusCode == 400) {
@@ -137,31 +135,28 @@ class TrocadorExchangeProvider extends ExchangeProvider {
135 {required CryptoCurrency from,
136 required CryptoCurrency to,
137 required bool isFixedRateMode}) async {
140 -
141 - final params = <String, String> {
142 - 'api_key': apiKey,
143 - 'ticker': from.title.toLowerCase(),
144 - 'name': from.name,
145 - };
146 -
147 - final String apiAuthority = await _getAuthority();
148 - final uri = Uri.https(apiAuthority, coinPath, params);
149 -
150 - final response = await get(uri);
138 + final params = <String, String>{
139 + 'api_key': apiKey,
140 + 'ticker': from.title.toLowerCase(),
141 + 'name': from.name,
142 + };
143
152 - if (response.statusCode != 200) {
153 - throw Exception('Unexpected http status: ${response.statusCode}');
154 - }
144 + final uri = await _getUri(coinPath, params);
145 +
146 + final response = await get(uri);
147 +
148 + if (response.statusCode != 200) {
149 + throw Exception('Unexpected http status: ${response.statusCode}');
150 + }
151 +
152 + final responseJSON = json.decode(response.body) as List<dynamic>;
153 +
154 + if (responseJSON.isEmpty) {
155 + throw Exception('No data');
156 + }
157 +
158 + final coinJson = responseJSON.first as Map<String, dynamic>;
159
156 - final responseJSON = json.decode(response.body) as List<dynamic>;
157 -
158 - if (responseJSON.isEmpty) {
159 - throw Exception('No data');
160 - }
161 -
162 - final coinJson = responseJSON.first as Map<String, dynamic>;
163 -
164 -
160 return Limits(
161 min: coinJson['minimum'] as double,
162 max: coinJson['maximum'] as double,
@@ -180,8 +175,6 @@ class TrocadorExchangeProvider extends ExchangeProvider {
175 return 0.0;
176 }
177
183 - final String apiAuthority = await _getAuthority();
184 -
178 final params = <String, String>{
179 'api_key': apiKey,
180 'ticker_from': from.title.toLowerCase(),
@@ -196,7 +189,7 @@ class TrocadorExchangeProvider extends ExchangeProvider {
189 'best_only': 'True',
190 };
191
199 - final uri = Uri.https(apiAuthority, newRatePath, params);
192 + final uri = await _getUri(newRatePath, params);
193 final response = await get(uri);
194 final responseJSON = json.decode(response.body) as Map<String, dynamic>;
195 final fromAmount = double.parse(responseJSON['amount_from'].toString());
@@ -216,8 +209,7 @@ class TrocadorExchangeProvider extends ExchangeProvider {
209
210 @override
211 Future<Trade> findTradeById({required String id}) async {
219 - final String apiAuthority = await _getAuthority();
220 - final uri = Uri.https(apiAuthority, tradePath, {'api_key': apiKey, 'id': id});
212 + final uri = await _getUri(tradePath, {'api_key': apiKey, 'id': id});
213 return get(uri).then((response) {
214 if (response.statusCode != 200) {
215 throw Exception('Unexpected http status: ${response.statusCode}');
@@ -298,23 +290,22 @@ class TrocadorExchangeProvider extends ExchangeProvider {
290 }
291 }
292
301 - Future<String> _getAuthority() async {
302 - if(!supportsOnionAddress){
303 - return clearNetAuthority;
304 - }
293 + Future<Uri> _getUri(String createTradePath, Map<String, String> queryParams) async {
294 + if (!supportsOnionAddress) {
295 + return Uri.https(clearNetAuthority, tradePath, queryParams);
296 + }
297 +
298 + if (useTorOnly) {
299 + return Uri.http(onionApiAuthority, tradePath, queryParams);
300 + }
301
302 try {
307 - if (useTorOnly) {
308 - return onionApiAuthority;
309 - }
310 -
311 - final uri = Uri.https(onionApiAuthority, tradePath);
303 + final uri = Uri.http(onionApiAuthority, tradePath);
304 await get(uri);
305
314 - return onionApiAuthority;
306 + return Uri.http(onionApiAuthority, tradePath, queryParams);
307 } catch (e) {
316 -
317 - return clearNetAuthority;
308 + return Uri.https(clearNetAuthority, tradePath, queryParams);
309 }
310 }
311 }