appliteui_core 0.0.14
appliteui_core: ^0.0.14 copied to clipboard
A Flutter package for making API calls with support for interceptors, error handling, and typed responses. Built on top of Dio for robust HTTP operations.
appliteui_core #
A Flutter package for making API calls with support for interceptors, error handling, and typed responses.
Features #
- 🚀 Simple API client based on Dio
- 🔒 Built-in authentication interceptor
- 📝 Request/response logging interceptor
- ⚠️ Comprehensive error handling
- 🎯 Typed responses with generics
- 🔄 Support for all HTTP methods (GET, POST, PUT, PATCH, DELETE)
Installation #
Add this to your package's pubspec.yaml file:
dependencies:
appliteui_core:
path: ../appliteui_core
Usage #
Basic Setup #
import 'package:appliteui_core/appliteui_core.dart';
// Create API client
final apiClient = ApiClient(
baseUrl: 'https://api.example.com',
headers: {
'Content-Type': 'application/json',
},
);
With Interceptors #
// Create client with authentication and logging
final apiClient = ApiClient(
baseUrl: 'https://api.example.com',
interceptors: [
AuthInterceptor(getToken: () => 'your-token-here'),
LoggingInterceptor(),
],
);
Making Requests #
GET Request
try {
final response = await apiClient.get<Map<String, dynamic>>(
'/users/1',
fromJson: (json) => json as Map<String, dynamic>,
);
if (response.isSuccess) {
print('User data: ${response.data}');
}
} on ApiException catch (e) {
print('Error: ${e.message}');
}
POST Request
try {
final response = await apiClient.post<Map<String, dynamic>>(
'/users',
data: {
'name': 'John Doe',
'email': 'john@example.com',
},
fromJson: (json) => json as Map<String, dynamic>,
);
print('Created user: ${response.data}');
} on ApiException catch (e) {
print('Error: ${e.message}');
}
PUT Request
try {
final response = await apiClient.put<Map<String, dynamic>>(
'/users/1',
data: {
'name': 'Jane Doe',
},
fromJson: (json) => json as Map<String, dynamic>,
);
print('Updated user: ${response.data}');
} on ApiException catch (e) {
print('Error: ${e.message}');
}
DELETE Request
try {
await apiClient.delete('/users/1');
print('User deleted');
} on ApiException catch (e) {
print('Error: ${e.message}');
}
Error Handling #
The package provides structured error handling through ApiException:
try {
final response = await apiClient.get('/endpoint');
} on ApiException catch (e) {
switch (e.type) {
case ApiExceptionType.network:
print('Network error: ${e.message}');
break;
case ApiExceptionType.timeout:
print('Timeout: ${e.message}');
break;
case ApiExceptionType.server:
print('Server error (${e.statusCode}): ${e.message}');
break;
case ApiExceptionType.cancel:
print('Request cancelled');
break;
case ApiExceptionType.unknown:
print('Unknown error: ${e.message}');
break;
}
}
Custom Model Parsing #
class User {
final int id;
final String name;
User({required this.id, required this.name});
factory User.fromJson(Map<String, dynamic> json) {
return User(
id: json['id'],
name: json['name'],
);
}
}
// Use with API client
final response = await apiClient.get<User>(
'/users/1',
fromJson: (json) => User.fromJson(json),
);
print('User: ${response.data.name}');
API Reference #
ApiClient #
Main class for making API requests.
Methods:
get<T>()- Make GET requestpost<T>()- Make POST requestput<T>()- Make PUT requestpatch<T>()- Make PATCH requestdelete<T>()- Make DELETE requestaddInterceptor()- Add interceptorremoveInterceptor()- Remove interceptorclearInterceptors()- Clear all interceptors
ApiResponse #
Generic response wrapper.
Properties:
data- Response datastatusCode- HTTP status codemessage- Response messageisSuccess- Whether request was successful
ApiException #
Custom exception for API errors.
Properties:
message- Error messagestatusCode- HTTP status code (if applicable)type- Type of error (ApiExceptionType)
Interceptors #
AuthInterceptor
Adds authentication headers to requests.
AuthInterceptor(getToken: () => 'your-token')
LoggingInterceptor
Logs requests and responses for debugging.
LoggingInterceptor(
logRequest: true,
logResponse: true,
logError: true,
)
License #
This project is licensed under the MIT License.