Last updated:
0 purchases
http4
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 http4 to your pubspec.yaml file:
dependencies:
http4: ^0.0.4
copied to clipboard
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');
}
}
copied to clipboard
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;
}
}
copied to clipboard
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);
}
}
copied to clipboard
Configuration #
To configure the base URL and debug mode, use Http4Config:
final config = Http4Config();
config.baseUrl = 'https://api.example.com';
config.debugMode = true;
copied to clipboard
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.
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.