MagentoApiClient class
Main API client for Magento integration.
This singleton class handles all HTTP communication with Magento REST API. It manages authentication tokens, request/response interceptors, and provides both authenticated and guest request methods.
Features
- Token Management: Automatic storage and retrieval of authentication tokens
- Request Interceptors: Automatic token injection and error handling
- Secure Storage: Uses Flutter Secure Storage for token persistence
- Timeout Configuration: Configurable connection and receive timeouts
- Error Handling: Comprehensive error handling with proper exception types
Usage
final client = MagentoApiClient.instance;
// Initialize the client
await client.initialize(
baseUrl: 'https://yourstore.com',
connectionTimeout: 30000,
);
// Make authenticated request
final response = await client.authenticatedRequest('/rest/V1/customers/me');
Properties
- baseUrl → String?
-
Get base URL
no setter
- currentCustomerId → int?
-
Get current customer ID
no setter
- dio → Dio
-
Get Dio instance for custom requests
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- isAuthenticated → bool
-
Check if user is authenticated
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
authenticatedRequest<
T> (String path, {Object? data, Map< String, dynamic> ? queryParameters, Options? options, CancelToken? cancelToken, ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress}) → Future<Response< T> > - Make authenticated request
-
dispose(
) → void - Dispose the client
-
guestRequest<
T> (String path, {Object? data, Map< String, dynamic> ? queryParameters, Options? options, CancelToken? cancelToken, ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress}) → Future<Response< T> > - Make guest request (no authentication required)
-
initialize(
{required String baseUrl, Map< String, String> ? headers, int? connectionTimeout, int? receiveTimeout}) → Future<bool> - Initialize the API client.
-
logout(
) → Future< void> - Logout and clear tokens
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
storeTokens(
{required String accessToken, required String refreshToken, int? customerId}) → Future< void> - Store tokens in secure storage
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- instance → MagentoApiClient
-
Get singleton instance of MagentoApiClient.
no setter