Dio API Handler
This library provides a simplified way to handle API requests using the Dio package in Flutter. It includes built-in caching, logging, and error handling.
Features
- Dio Integration: Provides a seamless integration with Dio for HTTP requests.
- Caching: Built-in caching mechanism using dio_cache_interceptor.
- Logging: Detailed request and response logging.
- Error Handling: Comprehensive error handling with descriptive messages.
- cURL Representation: Generate cURL commands for debugging.
Installation
Add the following dependencies to your pubspec.yaml
file:
dependencies:
dio_api_handler:
path: ../ # Adjust the path according to your project structure
Usage
Setting Up
To use the DioAPIHandler
, you need to create a class that extends it:
import 'package:dio_api_handler/dio_api_handler.dart';
class MyApiHandler extends DioAPIHandler {
@override
Dio generateRequestConfiguration(Dio dio) {
dio.options.baseUrl = 'https://api.example.com';
return dio;
}
}
Initializing Caching
Set up caching by calling the setupAPICache
method:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await DioAPIHandler.setupAPICache();
runApp(MyApp());
}
Making Requests
You can now make API requests using your custom API handler:
void fetchUserData() async {
MyApiHandler apiHandler = MyApiHandler();
try {
Response response = await apiHandler.get('/user');
print(response.data);
} catch (e) {
print('Error: $e');
}
}
Example Project
For a complete example, refer to the implementation in the ./example/
directory.
License
This project is licensed under the MIT License - see the LICENSE file for details.