Skip to content

Commit

Permalink
update sample
Browse files Browse the repository at this point in the history
  • Loading branch information
hoc081098 committed Oct 27, 2024
1 parent 4734717 commit 5c4525e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Simple and powerful HTTP client for Flutter and Dart application.
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:http/retry.dart' as httpRetry;
import 'package:http_client_hoc081098/http_client_hoc081098.dart';
import 'user.dart';
Expand All @@ -23,11 +24,30 @@ void main() async {
),
);
final innerClient = httpRetry.RetryClient(
http.Client(),
retries: 3,
when: (response) {
print(
'[RetryClient] Checking response: request=${response.request} statusCode=${response.statusCode}');
print('-' * 128);
return response.statusCode == HttpStatus.unauthorized;
},
onRetry: (request, response, retryCount) async {
print(
'[RetryClient] Retrying request: request=$request, response=$response, retryCount=$retryCount');
// Simulate refreshing token
await Future<void>.delayed(const Duration(seconds: 1));
print('-' * 128);
},
);
final client = SimpleHttpClient(
client: http.Client(),
client: innerClient,
timeout: const Duration(seconds: 10),
requestInterceptors: [
(request) async {
print('[requestInterceptors] request=$request');
await Future<void>.delayed(const Duration(milliseconds: 100));
final token = 'hoc081098';
Expand Down
22 changes: 21 additions & 1 deletion example/lib/http_client_hoc081098_example.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:io';

import 'package:http/http.dart' as http;
import 'package:http/retry.dart' as httpRetry;
import 'package:http_client_hoc081098/http_client_hoc081098.dart';

import 'user.dart';
Expand All @@ -15,11 +16,30 @@ void main() async {
),
);

final innerClient = httpRetry.RetryClient(
http.Client(),
retries: 3,
when: (response) {
print(
'[RetryClient] Checking response: request=${response.request} statusCode=${response.statusCode}');
print('-' * 128);
return response.statusCode == HttpStatus.unauthorized;
},
onRetry: (request, response, retryCount) async {
print(
'[RetryClient] Retrying request: request=$request, response=$response, retryCount=$retryCount');
// Simulate refreshing token
await Future<void>.delayed(const Duration(seconds: 1));
print('-' * 128);
},
);

final client = SimpleHttpClient(
client: http.Client(),
client: innerClient,
timeout: const Duration(seconds: 10),
requestInterceptors: [
(request) async {
print('[requestInterceptors] request=$request');
await Future<void>.delayed(const Duration(milliseconds: 100));

final token = 'hoc081098';
Expand Down

0 comments on commit 5c4525e

Please sign in to comment.