-
Notifications
You must be signed in to change notification settings - Fork 0
Build/ update deps #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b871ca8
chore(.gitignore): stop ignoring build/ directory and start ignoring …
fulleni b18a26c
chore(deps): update core package to latest version
fulleni 9b98cb5
docs(README): rewrite and enhance for better clarity and integration
fulleni cd88c6a
ci(README): update code coverage badge to 68%
fulleni 9520bba
chore: update .gitignore to exclude coverage
fulleni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,4 @@ | |
# Files and directories created by pub | ||
.dart_tool/ | ||
.packages | ||
build/ | ||
pubspec.lock | ||
build/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,118 +1,46 @@ | ||
# http_client | ||
<div align="center"> | ||
<img src="https://avatars.githubusercontent.com/u/202675624?s=400&u=dc72a2b53e8158956a3b672f8e52e39394b6b610&v=4" alt="Flutter News App Toolkit Logo" width="220"> | ||
<h1>HTTP Client</h1> | ||
<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> | ||
</div> | ||
|
||
 | ||
[](https://pub.dev/packages/very_good_analysis) | ||
[](https://polyformproject.org/licenses/free-trial/1.0.0) | ||
<p align="center"> | ||
<img src="https://img.shields.io/badge/coverage-68%25-green?style=for-the-badge" alt="coverage: XX%"> | ||
<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> | ||
<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> | ||
</p> | ||
|
||
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. | ||
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. | ||
|
||
## Description | ||
## ⭐ Feature Showcase: Streamlined & Secure API Communication | ||
|
||
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. | ||
This package provides a comprehensive set of features for managing HTTP requests. | ||
|
||
Key features include: | ||
* Base URL configuration. | ||
* Simplified `get`, `post`, `put`, `delete` methods. | ||
* Automatic injection of `Authorization: Bearer <token>` headers via an interceptor. | ||
* Token retrieval via a flexible `TokenProvider` function. | ||
* Mapping of `DioException` types and non-2xx HTTP status codes to specific `HttpException` subtypes (`NetworkException`, `BadRequestException`, `UnauthorizedException`, etc.) defined in the `core` package. | ||
* Support for request cancellation using `dio`'s `CancelToken`. | ||
<details> | ||
<summary><strong>🧱 Core Functionality</strong></summary> | ||
|
||
## Getting Started | ||
### 🚀 Robust `HttpClient` | ||
- **`HttpClient` Class:** The main entry point for making HTTP requests, abstracting away `dio` complexities. | ||
- **Simplified HTTP Methods:** Provides `get<T>()`, `post<T>()`, `put<T>()`, and `delete<T>()` methods for common API interactions. | ||
|
||
Add the package to your `pubspec.yaml`: | ||
### 🔐 Automatic Authentication | ||
- **`AuthInterceptor`:** Automatically injects `Authorization: Bearer <token>` headers into requests. | ||
- **Flexible `TokenProvider`:** Retrieves authentication tokens via a configurable asynchronous function, allowing integration with various authentication mechanisms. | ||
|
||
```yaml | ||
dependencies: | ||
http_client: | ||
git: | ||
url: https://github.com/flutter-news-app-full-source-code/http-client.git | ||
dio: ^5.8.0+1 # http_client relies on dio | ||
``` | ||
### 🛡️ Standardized Error Handling | ||
- **`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. | ||
|
||
Then run `dart pub get` or `flutter pub get`. | ||
### ⚡ Performance & Control | ||
- **Request Cancellation:** Supports request cancellation using `dio`'s `CancelToken` for improved resource management and user experience. | ||
- **Configurable Timeouts:** Allows configuration of connection, receive, and send timeouts for robust network operations. | ||
|
||
## Features | ||
### 🌐 Platform Adaptability | ||
- **`BrowserHttpClientAdapter` & `IOHttpClientAdapter`:** Automatically selects the appropriate HTTP client adapter for web and non-web platforms, ensuring seamless operation across different environments. | ||
|
||
* **`HttpClient` Class:** The main entry point for making requests. | ||
* **HTTP Methods:** `get<T>()`, `post<T>()`, `put<T>()`, `delete<T>()`. | ||
* **Authentication:** Automatic Bearer token injection using a provided `TokenProvider`. | ||
* **Error Handling:** Throws specific `HttpException` subtypes (defined in `core`) for easier error management. | ||
* **Cancellation:** Supports request cancellation via `CancelToken`. | ||
> **💡 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. | ||
## Usage | ||
|
||
```dart | ||
import 'package:http_client/http_client.dart'; | ||
import 'package:core/core.dart'; // Import for HttpException types | ||
import 'package:dio/dio.dart'; // For CancelToken | ||
// 1. Define your token provider function | ||
// (This would typically fetch the token from secure storage, auth state, etc.) | ||
Future<String?> myTokenProvider() async { | ||
// Replace with your actual token retrieval logic | ||
await Future.delayed(const Duration(milliseconds: 50)); // Simulate async fetch | ||
return 'your_super_secret_auth_token'; | ||
} | ||
void main() async { | ||
// 2. Create an instance of the client | ||
final client = HttpClient( | ||
baseUrl: 'https://api.example.com/v1', | ||
tokenProvider: myTokenProvider, | ||
); | ||
// 3. Make requests | ||
// Example GET request | ||
try { | ||
final headlines = await client.get<List<dynamic>>('/headlines'); | ||
print('Fetched headlines: $headlines'); | ||
// Example GET with query parameters and cancellation | ||
final cancelToken = CancelToken(); | ||
Future.delayed(const Duration(milliseconds: 100), () { | ||
print('Cancelling search request...'); | ||
cancelToken.cancel('User navigated away'); | ||
}); | ||
final searchResults = await client.get<Map<String, dynamic>>( | ||
'/search', | ||
queryParameters: {'q': 'flutter', 'limit': 10}, | ||
cancelToken: cancelToken, // Pass the token here | ||
); | ||
print('Search results: $searchResults'); | ||
} on UnauthorizedException catch (e) { | ||
print('Authentication error: ${e.message}'); | ||
// Handle token refresh or prompt user to login | ||
} on NetworkException catch (e) { | ||
print('Network error: ${e.message}'); | ||
// Handle offline state or retry logic | ||
} on NotFoundException catch (e) { | ||
print('Resource not found: ${e.message}'); | ||
} on HttpException catch (e) { // Catch other specific or general client errors | ||
print('HTTP Client Error: ${e.message}'); | ||
} catch (e) { // Catch any other unexpected errors | ||
print('An unexpected error occurred: $e'); | ||
} | ||
// Example POST request | ||
try { | ||
final newItem = await client.post<Map<String, dynamic>>( | ||
'/items', | ||
data: {'name': 'New Item', 'value': 123}, | ||
); | ||
print('Created item: $newItem'); | ||
} on HttpException catch (e) { | ||
print('Error creating item: ${e.message}'); | ||
} | ||
} | ||
``` | ||
</details> | ||
|
||
## 🔑 Licensing | ||
|
||
This package is source-available and licensed under the [PolyForm Free Trial 1.0.0](LICENSE). Please review the terms before use. | ||
|
||
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. | ||
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
SF:lib\src\http_client.dart | ||
DA:33,1 | ||
DA:39,0 | ||
DA:40,0 | ||
DA:42,3 | ||
DA:50,3 | ||
DA:53,4 | ||
DA:54,1 | ||
DA:55,1 | ||
DA:57,0 | ||
DA:81,1 | ||
DA:87,3 | ||
DA:89,2 | ||
DA:95,0 | ||
DA:96,0 | ||
DA:99,0 | ||
DA:100,1 | ||
DA:101,0 | ||
DA:102,0 | ||
DA:103,0 | ||
DA:105,0 | ||
DA:107,0 | ||
DA:122,1 | ||
DA:129,2 | ||
DA:130,1 | ||
DA:133,2 | ||
DA:140,0 | ||
DA:141,0 | ||
DA:143,0 | ||
DA:144,1 | ||
DA:145,0 | ||
DA:146,0 | ||
DA:147,0 | ||
DA:149,0 | ||
DA:151,0 | ||
DA:166,1 | ||
DA:173,2 | ||
DA:174,1 | ||
DA:177,2 | ||
DA:184,0 | ||
DA:185,0 | ||
DA:187,0 | ||
DA:188,1 | ||
DA:189,0 | ||
DA:190,0 | ||
DA:191,0 | ||
DA:193,0 | ||
DA:195,0 | ||
DA:210,1 | ||
DA:217,2 | ||
DA:218,1 | ||
DA:221,2 | ||
DA:228,0 | ||
DA:229,0 | ||
DA:231,0 | ||
DA:232,1 | ||
DA:233,0 | ||
DA:234,0 | ||
DA:235,0 | ||
DA:237,0 | ||
DA:239,0 | ||
LF:60 | ||
LH:25 | ||
end_of_record | ||
SF:lib\src\interceptors\auth_interceptor.dart | ||
DA:16,1 | ||
DA:27,1 | ||
DA:32,2 | ||
DA:34,1 | ||
DA:35,3 | ||
DA:39,1 | ||
LF:6 | ||
LH:6 | ||
end_of_record | ||
SF:lib\src\interceptors\error_interceptor.dart | ||
DA:9,1 | ||
DA:13,1 | ||
DA:14,1 | ||
DA:15,1 | ||
DA:16,1 | ||
DA:17,1 | ||
DA:20,1 | ||
DA:22,2 | ||
DA:23,2 | ||
DA:26,1 | ||
DA:27,1 | ||
DA:32,1 | ||
DA:33,1 | ||
DA:38,1 | ||
DA:39,1 | ||
DA:40,1 | ||
DA:41,1 | ||
DA:42,1 | ||
DA:43,1 | ||
DA:44,1 | ||
DA:45,1 | ||
DA:46,1 | ||
DA:47,1 | ||
DA:49,1 | ||
DA:50,1 | ||
DA:54,1 | ||
DA:58,1 | ||
DA:62,1 | ||
DA:64,2 | ||
DA:68,1 | ||
DA:69,1 | ||
DA:76,1 | ||
DA:77,1 | ||
DA:78,1 | ||
DA:79,1 | ||
DA:80,1 | ||
DA:88,1 | ||
DA:91,1 | ||
DA:93,3 | ||
DA:94,0 | ||
DA:95,0 | ||
DA:96,0 | ||
DA:101,1 | ||
DA:102,2 | ||
DA:103,1 | ||
DA:105,1 | ||
DA:106,2 | ||
DA:107,1 | ||
DA:109,1 | ||
DA:110,2 | ||
DA:111,1 | ||
DA:113,1 | ||
LF:52 | ||
LH:49 | ||
end_of_record | ||
SF:lib\src\adapters\http_adapter_io.dart | ||
DA:5,2 | ||
LF:1 | ||
LH:1 | ||
end_of_record | ||
fulleni marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.