Skip to content

Commit e4305cc

Browse files
authored
Merge pull request #4 from flutter-news-app-full-source-code/build/-update-deps
Build/ update deps
2 parents be0149d + 9520bba commit e4305cc

File tree

5 files changed

+638
-104
lines changed

5 files changed

+638
-104
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
.dart_tool/
55
.packages
66
build/
7-
pubspec.lock
7+
coverage/

README.md

Lines changed: 31 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,46 @@
1-
# http_client
1+
<div align="center">
2+
<img src="https://avatars.githubusercontent.com/u/202675624?s=400&u=dc72a2b53e8158956a3b672f8e52e39394b6b610&v=4" alt="Flutter News App Toolkit Logo" width="220">
3+
<h1>HTTP Client</h1>
4+
<p><strong>A robust and reusable Dart HTTP client built on top of the `dio` package, serving as a crucial abstraction layer within the Flutter News App Full Source Code Toolkit.</strong></p>
5+
</div>
26

3-
![coverage: percentage](https://img.shields.io/badge/coverage-XX-green)
4-
[![style: very good analysis](https://img.shields.io/badge/style-very_good_analysis-B22C89.svg)](https://pub.dev/packages/very_good_analysis)
5-
[![License: PolyForm Free Trial](https://img.shields.io/badge/License-PolyForm%20Free%20Trial-blue)](https://polyformproject.org/licenses/free-trial/1.0.0)
7+
<p align="center">
8+
<img src="https://img.shields.io/badge/coverage-68%25-green?style=for-the-badge" alt="coverage: XX%">
9+
<a href="https://flutter-news-app-full-source-code.github.io/docs/"><img src="https://img.shields.io/badge/LIVE_DOCS-VIEW-slategray?style=for-the-badge" alt="Live Docs: View"></a>
10+
<a href="https://github.com/flutter-news-app-full-source-code"><img src="https://img.shields.io/badge/MAIN_PROJECT-BROWSE-purple?style=for-the-badge" alt="Main Project: Browse"></a>
11+
</p>
612

7-
A robust and reusable Dart HTTP client built on top of the `dio` package. It simplifies API interactions by providing common HTTP methods, automatic authentication header injection, and mapping network/status code errors to specific custom exceptions.
13+
This `http_client` package serves as the foundational HTTP client for the [**Flutter News App Full Source Code Toolkit**](https://github.com/flutter-news-app-full-source-code). Built on top of the `dio` package, it simplifies API interactions by providing common HTTP methods, automatic authentication header injection, and mapping network/status code errors to specific custom exceptions. It ensures consistent and robust communication with backend services across the Flutter mobile app, web dashboard, and Dart Frog backend API.
814

9-
## Description
15+
## ⭐ Feature Showcase: Streamlined & Secure API Communication
1016

11-
This package provides an `HttpClient` class designed to be used as a foundational data access component in Dart or Flutter applications. It abstracts away the complexities of setting up `dio`, handling authentication tokens, and interpreting various HTTP error conditions, offering a cleaner interface for making API calls.
17+
This package provides a comprehensive set of features for managing HTTP requests.
1218

13-
Key features include:
14-
* Base URL configuration.
15-
* Simplified `get`, `post`, `put`, `delete` methods.
16-
* Automatic injection of `Authorization: Bearer <token>` headers via an interceptor.
17-
* Token retrieval via a flexible `TokenProvider` function.
18-
* Mapping of `DioException` types and non-2xx HTTP status codes to specific `HttpException` subtypes (`NetworkException`, `BadRequestException`, `UnauthorizedException`, etc.) defined in the `core` package.
19-
* Support for request cancellation using `dio`'s `CancelToken`.
19+
<details>
20+
<summary><strong>🧱 Core Functionality</strong></summary>
2021

21-
## Getting Started
22+
### 🚀 Robust `HttpClient`
23+
- **`HttpClient` Class:** The main entry point for making HTTP requests, abstracting away `dio` complexities.
24+
- **Simplified HTTP Methods:** Provides `get<T>()`, `post<T>()`, `put<T>()`, and `delete<T>()` methods for common API interactions.
2225

23-
Add the package to your `pubspec.yaml`:
26+
### 🔐 Automatic Authentication
27+
- **`AuthInterceptor`:** Automatically injects `Authorization: Bearer <token>` headers into requests.
28+
- **Flexible `TokenProvider`:** Retrieves authentication tokens via a configurable asynchronous function, allowing integration with various authentication mechanisms.
2429

25-
```yaml
26-
dependencies:
27-
http_client:
28-
git:
29-
url: https://github.com/flutter-news-app-full-source-code/http-client.git
30-
dio: ^5.8.0+1 # http_client relies on dio
31-
```
30+
### 🛡️ Standardized Error Handling
31+
- **`ErrorInterceptor`:** Maps `DioException` types and non-2xx HTTP status codes to specific `HttpException` subtypes (e.g., `NetworkException`, `BadRequestException`, `UnauthorizedException`, `NotFoundException`, `ServerException`, `UnknownException`) defined in the `core` package. This ensures predictable and consistent error management across the application layers.
3232

33-
Then run `dart pub get` or `flutter pub get`.
33+
### ⚡ Performance & Control
34+
- **Request Cancellation:** Supports request cancellation using `dio`'s `CancelToken` for improved resource management and user experience.
35+
- **Configurable Timeouts:** Allows configuration of connection, receive, and send timeouts for robust network operations.
3436

35-
## Features
37+
### 🌐 Platform Adaptability
38+
- **`BrowserHttpClientAdapter` & `IOHttpClientAdapter`:** Automatically selects the appropriate HTTP client adapter for web and non-web platforms, ensuring seamless operation across different environments.
3639

37-
* **`HttpClient` Class:** The main entry point for making requests.
38-
* **HTTP Methods:** `get<T>()`, `post<T>()`, `put<T>()`, `delete<T>()`.
39-
* **Authentication:** Automatic Bearer token injection using a provided `TokenProvider`.
40-
* **Error Handling:** Throws specific `HttpException` subtypes (defined in `core`) for easier error management.
41-
* **Cancellation:** Supports request cancellation via `CancelToken`.
40+
> **💡 Your Advantage:** You get a meticulously designed, production-quality HTTP client that simplifies API interactions, ensures secure authentication, provides robust error handling, and adapts to different platforms. This package accelerates development by providing a solid foundation for network communication.
4241
43-
## Usage
44-
45-
```dart
46-
import 'package:http_client/http_client.dart';
47-
import 'package:core/core.dart'; // Import for HttpException types
48-
import 'package:dio/dio.dart'; // For CancelToken
49-
50-
// 1. Define your token provider function
51-
// (This would typically fetch the token from secure storage, auth state, etc.)
52-
Future<String?> myTokenProvider() async {
53-
// Replace with your actual token retrieval logic
54-
await Future.delayed(const Duration(milliseconds: 50)); // Simulate async fetch
55-
return 'your_super_secret_auth_token';
56-
}
57-
58-
void main() async {
59-
// 2. Create an instance of the client
60-
final client = HttpClient(
61-
baseUrl: 'https://api.example.com/v1',
62-
tokenProvider: myTokenProvider,
63-
);
64-
65-
// 3. Make requests
66-
67-
// Example GET request
68-
try {
69-
final headlines = await client.get<List<dynamic>>('/headlines');
70-
print('Fetched headlines: $headlines');
71-
72-
// Example GET with query parameters and cancellation
73-
final cancelToken = CancelToken();
74-
Future.delayed(const Duration(milliseconds: 100), () {
75-
print('Cancelling search request...');
76-
cancelToken.cancel('User navigated away');
77-
});
78-
79-
final searchResults = await client.get<Map<String, dynamic>>(
80-
'/search',
81-
queryParameters: {'q': 'flutter', 'limit': 10},
82-
cancelToken: cancelToken, // Pass the token here
83-
);
84-
print('Search results: $searchResults');
85-
86-
} on UnauthorizedException catch (e) {
87-
print('Authentication error: ${e.message}');
88-
// Handle token refresh or prompt user to login
89-
} on NetworkException catch (e) {
90-
print('Network error: ${e.message}');
91-
// Handle offline state or retry logic
92-
} on NotFoundException catch (e) {
93-
print('Resource not found: ${e.message}');
94-
} on HttpException catch (e) { // Catch other specific or general client errors
95-
print('HTTP Client Error: ${e.message}');
96-
} catch (e) { // Catch any other unexpected errors
97-
print('An unexpected error occurred: $e');
98-
}
99-
100-
// Example POST request
101-
try {
102-
final newItem = await client.post<Map<String, dynamic>>(
103-
'/items',
104-
data: {'name': 'New Item', 'value': 123},
105-
);
106-
print('Created item: $newItem');
107-
} on HttpException catch (e) {
108-
print('Error creating item: ${e.message}');
109-
}
110-
}
111-
112-
```
42+
</details>
11343

11444
## 🔑 Licensing
11545

116-
This package is source-available and licensed under the [PolyForm Free Trial 1.0.0](LICENSE). Please review the terms before use.
117-
118-
For commercial licensing options that grant the right to build and distribute unlimited applications, please visit the main [**Flutter News App - Full Source Code Toolkit**](https://github.com/flutter-news-app-full-source-code) organization.
46+
This `http_client` package is an integral part of the [**Flutter News App Full Source Code Toolkit**](https://github.com/flutter-news-app-full-source-code). For comprehensive details regarding licensing, including trial and commercial options for the entire toolkit, please refer to the main toolkit organization page.

coverage/lcov.info

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
SF:lib\src\http_client.dart
2+
DA:33,1
3+
DA:39,0
4+
DA:40,0
5+
DA:42,3
6+
DA:50,3
7+
DA:53,4
8+
DA:54,1
9+
DA:55,1
10+
DA:57,0
11+
DA:81,1
12+
DA:87,3
13+
DA:89,2
14+
DA:95,0
15+
DA:96,0
16+
DA:99,0
17+
DA:100,1
18+
DA:101,0
19+
DA:102,0
20+
DA:103,0
21+
DA:105,0
22+
DA:107,0
23+
DA:122,1
24+
DA:129,2
25+
DA:130,1
26+
DA:133,2
27+
DA:140,0
28+
DA:141,0
29+
DA:143,0
30+
DA:144,1
31+
DA:145,0
32+
DA:146,0
33+
DA:147,0
34+
DA:149,0
35+
DA:151,0
36+
DA:166,1
37+
DA:173,2
38+
DA:174,1
39+
DA:177,2
40+
DA:184,0
41+
DA:185,0
42+
DA:187,0
43+
DA:188,1
44+
DA:189,0
45+
DA:190,0
46+
DA:191,0
47+
DA:193,0
48+
DA:195,0
49+
DA:210,1
50+
DA:217,2
51+
DA:218,1
52+
DA:221,2
53+
DA:228,0
54+
DA:229,0
55+
DA:231,0
56+
DA:232,1
57+
DA:233,0
58+
DA:234,0
59+
DA:235,0
60+
DA:237,0
61+
DA:239,0
62+
LF:60
63+
LH:25
64+
end_of_record
65+
SF:lib\src\interceptors\auth_interceptor.dart
66+
DA:16,1
67+
DA:27,1
68+
DA:32,2
69+
DA:34,1
70+
DA:35,3
71+
DA:39,1
72+
LF:6
73+
LH:6
74+
end_of_record
75+
SF:lib\src\interceptors\error_interceptor.dart
76+
DA:9,1
77+
DA:13,1
78+
DA:14,1
79+
DA:15,1
80+
DA:16,1
81+
DA:17,1
82+
DA:20,1
83+
DA:22,2
84+
DA:23,2
85+
DA:26,1
86+
DA:27,1
87+
DA:32,1
88+
DA:33,1
89+
DA:38,1
90+
DA:39,1
91+
DA:40,1
92+
DA:41,1
93+
DA:42,1
94+
DA:43,1
95+
DA:44,1
96+
DA:45,1
97+
DA:46,1
98+
DA:47,1
99+
DA:49,1
100+
DA:50,1
101+
DA:54,1
102+
DA:58,1
103+
DA:62,1
104+
DA:64,2
105+
DA:68,1
106+
DA:69,1
107+
DA:76,1
108+
DA:77,1
109+
DA:78,1
110+
DA:79,1
111+
DA:80,1
112+
DA:88,1
113+
DA:91,1
114+
DA:93,3
115+
DA:94,0
116+
DA:95,0
117+
DA:96,0
118+
DA:101,1
119+
DA:102,2
120+
DA:103,1
121+
DA:105,1
122+
DA:106,2
123+
DA:107,1
124+
DA:109,1
125+
DA:110,2
126+
DA:111,1
127+
DA:113,1
128+
LF:52
129+
LH:49
130+
end_of_record
131+
SF:lib\src\adapters\http_adapter_io.dart
132+
DA:5,2
133+
LF:1
134+
LH:1
135+
end_of_record

0 commit comments

Comments
 (0)