https://github.com/hoc081098/http_client_hoc081098
Simple and powerful HTTP client for Flutter and Dart application, built on top of http, rxdart_ext and cancellation_token_hoc081098 packages.
https://github.com/hoc081098/http_client_hoc081098
dio-flutter dio-interceptor flutter-dio flutter-http flutter-http-client http http-client
Last synced: 14 days ago
JSON representation
Simple and powerful HTTP client for Flutter and Dart application, built on top of http, rxdart_ext and cancellation_token_hoc081098 packages.
- Host: GitHub
- URL: https://github.com/hoc081098/http_client_hoc081098
- Owner: hoc081098
- License: mit
- Created: 2022-08-31T15:55:52.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2025-02-27T00:37:24.000Z (2 months ago)
- Last Synced: 2025-04-14T02:29:46.423Z (14 days ago)
- Topics: dio-flutter, dio-interceptor, flutter-dio, flutter-http, flutter-http-client, http, http-client
- Language: Dart
- Homepage: https://pub.dev/packages/http_client_hoc081098
- Size: 93.8 KB
- Stars: 3
- Watchers: 1
- Forks: 2
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# http_client_hoc081098
Simple and powerful HTTP client for Flutter and Dart application.
[](https://codecov.io/gh/hoc081098/http_client_hoc081098)
[](https://hits.seeyoufarm.com)```dart
import 'dart:io';import 'package:http/http.dart' as http;
import 'package:http/retry.dart' as http_retry;
import 'package:http_client_hoc081098/http_client_hoc081098.dart';import 'user.dart';
void main() async {
final loggingInterceptor = SimpleLoggingInterceptor(
SimpleLogger(
level: SimpleLogLevel.body,
headersToRedact: {
HttpHeaders.authorizationHeader,
},
),
);final innerClient = http_retry.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.delayed(const Duration(seconds: 1));
print('-' * 128);
},
);final client = SimpleHttpClient(
client: innerClient,
timeout: const Duration(seconds: 10),
requestInterceptors: [
(request) async {
print('[requestInterceptors] request=$request');
await Future.delayed(const Duration(milliseconds: 100));final token = 'hoc081098';
request.headers[HttpHeaders.authorizationHeader] = 'Bearer $token';return request;
},
loggingInterceptor.requestInterceptor,
],
responseInterceptors: [
loggingInterceptor.responseInterceptor,
],
);await getExample(client);
print('-' * 128);await getSingleExample(client);
print('-' * 128);await postExample(client);
print('-' * 128);client.close();
print('Client closed gratefully.');
}Future postExample(SimpleHttpClient client) async {
try {
final json = await client.postJson(
Uri.parse('https://jsonplaceholder.typicode.com/users'),
body: {
'name': 'hoc081098',
'username': 'hoc081098',
'email': '[email protected]',
},
) as Map;
print(json);
} catch (e) {
print(e);
}
}Future getSingleExample(SimpleHttpClient client) async {
final single = useCancellationToken(
(cancelToken) => client.getJson(
Uri.parse('https://jsonplaceholder.typicode.com/users/2'),
headers: {},
cancelToken: cancelToken,
),
).cast>().map(User.fromJson);
final subscription = single.listen(print, onError: print);() async {
await Future.delayed(const Duration(milliseconds: 120));
await subscription.cancel();
print('Cancelling single...');
}()
.ignore();await Future.delayed(const Duration(seconds: 1));
}Future getExample(SimpleHttpClient client) async {
final cancelToken = CancellationToken();
final uri = Uri.parse('https://jsonplaceholder.typicode.com/users/1');() async {
await Future.delayed(const Duration(milliseconds: 300));
cancelToken.cancel();
print('Cancelling...');
}()
.ignore();try {
final json = await client.getJson(
uri,
headers: {},
cancelToken: cancelToken,
) as Map;
print(json);
} catch (e) {
print(e);
}
}
```