| 1 | import 'dart:convert'; |
| 2 | |
| 3 | import 'package:cw_core/utils/proxy_wrapper.dart'; |
| 4 | import 'package:on_chain/tron/tron.dart'; |
| 5 | import '.secrets.g.dart' as secrets; |
| 6 | |
| 7 | class TronHTTPProvider implements TronServiceProvider { |
| 8 | TronHTTPProvider({required this.url, this.defaultRequestTimeout = const Duration(seconds: 30)}); |
| 9 | |
| 10 | @override |
| 11 | final String url; |
| 12 | late final client = ProxyWrapper().getHttpIOClient(); |
| 13 | final Duration defaultRequestTimeout; |
| 14 | |
| 15 | @override |
| 16 | Future<Map<String, dynamic>> get(TronRequestDetails params, [Duration? timeout]) async { |
| 17 | final response = await client.get(Uri.parse(params.url(url)), headers: { |
| 18 | 'Content-Type': 'application/json', |
| 19 | if (url.contains("trongrid")) 'TRON-PRO-API-KEY': secrets.tronGridApiKey, |
| 20 | if (url.contains("nownodes")) 'api-key': secrets.tronNowNodesApiKey, |
| 21 | }).timeout(timeout ?? defaultRequestTimeout); |
| 22 | final data = json.decode(response.body) as Map<String, dynamic>; |
| 23 | return data; |
| 24 | } |
| 25 | |
| 26 | @override |
| 27 | Future<Map<String, dynamic>> post(TronRequestDetails params, [Duration? timeout]) async { |
| 28 | final response = await client |
| 29 | .post(Uri.parse(params.url(url)), |
| 30 | headers: { |
| 31 | 'Content-Type': 'application/json', |
| 32 | if (url.contains("trongrid")) 'TRON-PRO-API-KEY': secrets.tronGridApiKey, |
| 33 | if (url.contains("nownodes")) 'api-key': secrets.tronNowNodesApiKey, |
| 34 | }, |
| 35 | body: params.toRequestBody()) |
| 36 | .timeout(timeout ?? defaultRequestTimeout); |
| 37 | final data = json.decode(response.body) as Map<String, dynamic>; |
| 38 | return data; |
| 39 | } |
| 40 | } |