easy_api_call 1.0.1 copy "easy_api_call: ^1.0.1" to clipboard
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.


โœ๏ธ Author Nahiduzzaman Badhon

Nahiduzzaman Badhon

Built and maintained with โค๏ธ

๐Ÿค Contributor ZAHIDUL ISLAM

ZAHIDUL ISLAM

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.

10
likes
160
points
14
downloads

Publisher

unverified uploader

Weekly Downloads

A reusable HTTP API service with token management.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

dio, flutter, pretty_dio_logger

More

Packages that depend on easy_api_call