ApiMate

Tired of writing repetitive try-catch, status code checks, and logging for every API call?

ApiMate is the simplest way to wrap Dio + Retrofit with clean success/failure handling, global config, and beautiful logging โ€” all in one line.


๐Ÿฏ From this ๐Ÿ‘‡

try {
  final response = await dio.get('/user');
  if (response.statusCode == 200) {
    // success
  } else {
    // handle error
  }
} catch (e) {
  // error
}

โœจ To this ๐Ÿ‘‡

final result = await ApiMate(() => client.getUser()).call();

switch (result) {
  case ApiMateSuccess(): print(result.data);
  case ApiMateFailure(): print(result.errorMessage);
}

๐Ÿš€ Features

  • โœ… Supports both Future<T> and Future<HttpResponse<T>>
  • ๐Ÿ“ฆ Sealed result type: ApiMateSuccess, ApiMateFailure
  • ๐Ÿชต Pretty console logger for request/response/error
  • ๐ŸŒ Global config with ApiMateConfig.enableLogging
  • ๐Ÿงผ Clean structure without callbacks or magic

๐Ÿ”ง Installation

dependencies:
  api_mate: ^0.2.3

๐Ÿ› ๏ธ Usage

1. Optional: Disable logging globally

void main() {
  ApiMateConfig.enableLogging = false; // disable logs globally
  runApp(MyApp());
}

2. Make an API call

final request = ApiMate(() => client.getPost(1));
final result = await request.call();

switch (result) {
  case ApiMateSuccess<Post>():
    print('โœ… Success: ${result.data.title}');
    break;
  case ApiMateFailure():
    print('โŒ Error: ${result.errorMessage}');
    break;
}

3. Override logging per request (optional)

final request = ApiMate(
  () => client.getPost(1),
  enableLogging: true, // force enable for this call
);
await request.call();

๐Ÿ“ Folder Structure

lib/
  api_mate/
    api_mate.dart
    api_mate_config.dart
    api_mate_logger.dart
    api_mate_result.dart
    api_mate_exception.dart