Skip to content

Commit

Permalink
feat: use retry client instead of http.get
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommytrg committed Dec 13, 2023
1 parent 404bcc1 commit e8e6851
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions lib/src/network/explorer/explorer_client.dart
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<String, dynamic> params = const {}]) {
Expand All @@ -47,7 +50,18 @@ class ExplorerClient {
}

Future<Map<String, dynamic>> _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<String, dynamic>;
Expand All @@ -58,10 +72,18 @@ class ExplorerClient {

Future<Map<String, dynamic>> _processPost(
Uri uri, Map<String, dynamic> 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<String, dynamic>;
Expand Down

0 comments on commit e8e6851

Please sign in to comment.