easy_api_call 1.0.1
easy_api_call: ^1.0.1 copied to clipboard
A reusable HTTP API service with token management.
๐ Easy API Call #
A powerful and easy-to-use Flutter/Dart package for making HTTP requests with built-in authentication, token refresh, and comprehensive logging. Built on top of Dio with a clean, type-safe API.
Built and maintained with โค๏ธ
Contributed with โค๏ธ
โจ Features #
- ๐ All HTTP Methods - Full support for GET, POST, PUT, PATCH, and DELETE
- ๐ Authentication - Built-in token-based authentication with automatic header injection
- ๐ Token Refresh - Automatic token refresh with configurable refresh logic
- ๐ Logging - Integrated PrettyDioLogger for beautiful request/response logging
- ๐ง Flexible Configuration - Customize timeouts, headers, and base URLs
- ๐ก๏ธ SSL Support - Option to bypass SSL certificate validation (for development)
- ๐งช Tested - Reliable and production-ready
๐ฆ Installation #
Add this to your package's pubspec.yaml file:
dependencies:
easy_api_call: ^1.0.0 # Use the latest version
dio: ^5.0.0 # Required dependency
๐ Quick Start #
1. Initialize the API Service #
import 'package:easy_api_call/easy_api_call.dart';
void main() {
// Configure the API service once at app startup
ApiService().configure(
ApiConfig(
baseUrl: 'https://api.example.com',
enableLogging: true, // Enable request/response logging
// Optional configurations:
// token: 'your_access_token',
// refreshToken: 'your_refresh_token',
// refreshTokenUrl: '/auth/refresh',
// connectTimeout: const Duration(seconds: 30),
// receiveTimeout: const Duration(seconds: 30),
// bypassSSLCertificate: false, // For development only
),
);
runApp(MyApp());
}
2. Make API Calls #
// GET request
final response = await ApiService().get<List<dynamic>>('/posts');
// POST request with data
final newPost = await ApiService().post<Map<String, dynamic>>(
'/posts',
data: {
'title': 'Hello',
'body': 'This is a new post',
'userId': 1,
},
);
// PUT request
final updated = await ApiService().put<Map<String, dynamic>>(
'/posts/1',
data: {
'id': 1,
'title': 'Updated title',
'body': 'Updated content',
'userId': 1,
},
);
// PATCH request
final patched = await ApiService().patch<Map<String, dynamic>>(
'/posts/1',
data: {
'title': 'New title',
},
);
// DELETE request
await ApiService().delete('/posts/1');
3. Using Authentication #
// Set tokens after login
ApiService().updateTokens(
token: 'new_access_token',
refreshToken: 'new_refresh_token',
);
// Make authenticated request
final profile = await ApiService().get<Map<String, dynamic>>(
'/user/profile',
requiresAuth: true, // This will include the Authorization header
);
// Clear tokens on logout
ApiService().clearTokens();
๐ง Advanced Configuration #
Custom Headers #
ApiService().configure(
ApiConfig(
baseUrl: 'https://api.example.com',
defaultHeaders: {
'X-Custom-Header': 'value',
'Accept': 'application/json',
},
),
);
Token Refresh #
ApiService().configure(
ApiConfig(
baseUrl: 'https://api.example.com',
token: 'access_token',
refreshToken: 'refresh_token',
refreshTokenUrl: '/auth/refresh',
),
onTokenUpdate: (newToken, newRefreshToken) {
// Save new tokens to secure storage
// This will be called automatically when tokens are refreshed
},
);
Custom Logger #
ApiService().configure(
ApiConfig(
baseUrl: 'https://api.example.com',
customLogger: PrettyDioLogger(
requestHeader: true,
requestBody: true,
responseBody: true,
responseHeader: false,
error: true,
compact: true,
),
),
);
๐ Example #
Check out the complete example in the example directory for a full CRUD implementation.
๐ค Contributing #
Contributions are welcome! Please feel free to submit a Pull Request.
๐ License #
This project is licensed under the MIT License - see the LICENSE file for details.