From e8e68510a49ff02b1e40266e4514d6ec60508033 Mon Sep 17 00:00:00 2001 From: tommytrg Date: Wed, 13 Dec 2023 19:48:31 +0100 Subject: [PATCH] feat: use retry client instead of http.get --- lib/src/network/explorer/explorer_client.dart | 34 +++++++++++++++---- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/lib/src/network/explorer/explorer_client.dart b/lib/src/network/explorer/explorer_client.dart index 81a8e9b..8b63105 100644 --- a/lib/src/network/explorer/explorer_client.dart +++ b/lib/src/network/explorer/explorer_client.dart @@ -1,6 +1,7 @@ import 'dart:convert'; import 'package:http/http.dart' as http; +import 'package:http/retry.dart'; import 'dart:convert' as convert; import 'dart:io' show HttpException; @@ -32,10 +33,12 @@ class ExplorerClient { ExplorerClient({ required this.url, required this.mode, - }) : SSL = (mode == ExplorerMode.production) ? true : false; + }) : SSL = (mode == ExplorerMode.production) ? true : false, + client = http.Client(); final String url; final ExplorerMode mode; + http.Client client; late bool SSL; Uri api(String method, [Map params = const {}]) { @@ -47,7 +50,18 @@ class ExplorerClient { } Future> _processGet(Uri uri) async { - var response = await http.get(uri); + var response; + try { + RetryClient retryClient = RetryClient(client); + response = await retryClient.get(uri); + } on http.ClientException catch (e) { + if (e.message.contains('Client is already closed')) { + client = http.Client(); + RetryClient retryClient = RetryClient(client); + response = await retryClient.get(uri); + } + } + if (response.statusCode == 200) { // response is okay return convert.jsonDecode(response.body) as Map; @@ -58,10 +72,18 @@ class ExplorerClient { Future> _processPost( Uri uri, Map postData) async { - var response = await http.post( - uri, - body: json.encode(postData), - ); + var response; + try { + RetryClient retryClient = RetryClient(client); + response = await retryClient.post(uri, body: json.encode(postData)); + } on http.ClientException catch (e) { + if (e.message.contains('Client is already closed')) { + client = http.Client(); + RetryClient retryClient = RetryClient(client); + response = await retryClient.post(uri, body: json.encode(postData)); + } + } + if (response.statusCode == 200) { // response is okay return convert.jsonDecode(response.body) as Map;