HTTP4
The http4
package provides enhanced HTTP request functionality in Flutter by leveraging the http
package and http_interceptor
for customizable HTTP requests with additional features and a new call-to-action style.
Features
- HTTP Methods: Simplified HTTP methods (GET, POST, PUT, PATCH, DELETE, MULTIPART) with customizable headers, parameters, and retry policies.
- HTTP Interceptors: Ability to use interceptors for modifying request headers, logging, and retrying failed requests.
- Configurable Base URL: Configure a base URL for all HTTP requests within the package.
- Debug Mode: Enable debug mode to print detailed information about HTTP requests and exceptions.
Installation
Add lazylist
to your pubspec.yaml
file:
dependencies:
http4: ^1.0.0
Usage
1st way
import 'package:http4/http4.dart';
void fetchData() async {
final http4 = Http4();
final response = await http4.get(
'/api/data',
headers: {'Authorization': 'Bearer <token>'},
params: {'page': 1},
retryPolicy: RetryOptions(maxAttempts: 3),
);
if (response.isSuccessed && response.isOkey) {
// Handle successful response
print('Response: ${response.decodedBody}');
} else {
// Handle failed response
print('Request failed');
}
}
2nd way (inherit from Http4)
import 'package:http4/http4.dart' as http4;
class ExampleService extends http4.Http4 {
Future<List<Map<String,dynamic>>> fetchData() async {
final response = await get(
'/posts?_page=1&_per_page=10',
interceptors: [AuthInterceptor()],
);
return response.decodedBody;
}
}
How to use interceptor
import 'package:http4/http4.dart' as http4;
class ExampleService extends http4.Http4 {
Future<List<Map<String,dynamic>>> fetchData() async {
final response = await get(
'/posts?_page=1&_per_page=10',
interceptors: [
AuthInterceptor(token: 'Your-Token-Here'),
],
);
return response.decodedBody;
}
}
class AuthInterceptor extends http4.InterceptorContract {
late String token;
AuthInterceptor({
required this.token,
});
@override
Future<http4.BaseRequest> interceptRequest(
{required http4.BaseRequest request}) {
request.headers.addAll({'Authorization': 'Bearer $token'});
// Before sending the request, Authorization will add to the headers
return Future.value(request);
}
@override
Future<http4.BaseResponse> interceptResponse(
{required http4.BaseResponse response}) {
if (response.statusCode == 401) {
print('Unauthorized request');
// After calling the request, it will be redirected to the login page
}
return Future.value(response);
}
}
Configuration
To configure the base URL and debug mode, use Http4Config
:
final config = Http4Config();
config.baseUrl = 'https://api.example.com';
config.debugMode = true;
Contributing
Contributions are welcome! Please feel free to submit issues, fork the repository, and submit pull requests.
To contribute to this package, follow these steps:
- Fork the repository.
- Create a new branch
(git checkout -b feature/my-feature)
. - Make your changes.
- Commit your changes
(git commit -am 'Add new feature')
. - Push to the branch
(git push origin feature/my-feature)
. - Create a new Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Libraries
- http4
- package_content/data/http_methods_enum
- package_content/data/http_methods_ext
- package_content/domain/http4_config
- package_content/domain/http4_implementation
- package_content/domain/http4_response
- package_content/domain/methods/http4_delete_method
- package_content/domain/methods/http4_get_method
- package_content/domain/methods/http4_multipart_method
- package_content/domain/methods/http4_patch_method
- package_content/domain/methods/http4_post_method
- package_content/domain/methods/http4_put_method
- package_content/repository/http4_repository