RestApiClientImpl class

Implementation of the RestApiClient interface using the Dio HTTP client.

This class provides a complete REST API client with support for:

  • All standard HTTP methods (GET, POST, PUT, PATCH, DELETE, HEAD)
  • JWT authentication with automatic token refresh
  • Response caching with configurable lifetime
  • Request retry with exponential backoff
  • Request deduplication for concurrent identical requests
  • File downloads with progress tracking

Example usage:

final client = RestApiClientImpl(
  options: RestApiClientOptions(baseUrl: 'https://api.example.com'),
  authOptions: AuthOptions(
    refreshTokenEndpoint: '/auth/refresh',
    resolveJwt: (response) => response.data['access_token'],
    resolveRefreshToken: (response) => response.data['refresh_token'],
  ),
);

await client.init();

final result = await client.get<User>(
  '/users/me',
  onSuccess: (data) => User.fromJson(data),
);

if (result.hasData) {
  print(result.data!.name);
}
Implemented types

Constructors

RestApiClientImpl({required RestApiClientOptions options, ExceptionOptions? exceptionOptions, LoggingOptions? loggingOptions, AuthOptions? authOptions, CacheOptions? cacheOptions, RetryOptions? retryOptions, bool enableDeduplication = false, List<Interceptor> interceptors = const []})
Creates a new RestApiClientImpl instance.

Properties

authHandler AuthHandler
Handler for authentication operations (login, logout, token management).
getter/setter pairoverride
cacheHandler CacheHandler
Handler for caching responses and retrieving cached data.
getter/setter pairoverride
exceptionHandler ExceptionHandler
Handler for processing and broadcasting exceptions.
getter/setter pairoverride
hashCode int
The hash code for this object.
no setterinherited
headers Map<String, String>
Returns the current request headers as a string map.
no setteroverride
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

addOrUpdateHeader({required String key, required String value}) → void
Adds or updates a custom header for all requests.
override
authorize({required String jwt, required String refreshToken}) Future<bool>
Stores authentication tokens and sets up the Authorization header.
override
clearStorage() Future
Clears all stored authentication tokens and cached responses.
override
delete<T>(String path, {dynamic data, Map<String, dynamic>? queryParameters, FutureOr<T> onSuccess(dynamic data)?, FutureOr<T> onError(dynamic data)?, RestApiClientRequestOptions? options}) Future<Result<T>>
Performs a DELETE request to the specified path.
override
download<T>(String urlPath, dynamic savePath, {dynamic data, Map<String, dynamic>? queryParameters, RestApiClientRequestOptions? options, ProgressCallback? onReceiveProgress, CancelToken? cancelToken, bool deleteOnError = true, String lengthHeader = Headers.contentLengthHeader, FutureOr<T> onSuccess(dynamic data)?, FutureOr<T> onError(dynamic data)?}) Future<Result<T>>
Downloads a file from the specified urlPath to savePath.
override
get<T>(String path, {Map<String, dynamic>? queryParameters, FutureOr<T> onSuccess(dynamic data)?, FutureOr<T> onError(dynamic data)?, RestApiClientRequestOptions? options, Duration? cacheLifetimeDuration}) Future<Result<T>>
Performs a GET request to the specified path.
override
getCached<T>(String path, {Map<String, dynamic>? queryParameters, FutureOr<T> onSuccess(dynamic data)?, FutureOr<T> onError(dynamic data)?}) Future<Result<T>>
Retrieves a cached response for the specified path.
override
getCachedOrNetwork<T>(String path, {Map<String, dynamic>? queryParameters, FutureOr<T> onSuccess(dynamic data)?, FutureOr<T> onError(dynamic data)?, RestApiClientRequestOptions? options, Duration? cacheLifetimeDuration}) Future<Result<T>>
Retrieves data from cache if available, otherwise makes a network request.
override
getStreamed<T>(String path, {Map<String, dynamic>? queryParameters, FutureOr<T> onSuccess(dynamic data)?, FutureOr<T> onError(dynamic data)?, RestApiClientRequestOptions? options, Duration? cacheLifetimeDuration}) Stream<Result<T>>
Streams GET results, yielding cached data first (if available), then network data.
override
Performs a HEAD request to the specified path.
override
init([bool migrateFromHive = true]) Future<RestApiClient>
Initializes the client and its handlers.
override
isAuthorized() Future<bool>
Checks if the user is currently authorized.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
patch<T>(String path, {dynamic data, Map<String, dynamic>? queryParameters, FutureOr<T> onSuccess(dynamic data)?, FutureOr<T> onError(dynamic data)?, RestApiClientRequestOptions? options}) Future<Result<T>>
Performs a PATCH request to the specified path.
override
post<T>(String path, {dynamic data, Map<String, dynamic>? queryParameters, FutureOr<T> onSuccess(dynamic data)?, FutureOr<T> onError(dynamic data)?, RestApiClientRequestOptions? options, bool cacheEnabled = false, Duration? cacheLifetimeDuration}) Future<Result<T>>
Performs a POST request to the specified path.
override
postCached<T>(String path, {dynamic data, Map<String, dynamic>? queryParameters, FutureOr<T> onSuccess(dynamic data)?, FutureOr<T> onError(dynamic data)?}) Future<Result<T>>
Retrieves a cached response for a POST request.
override
postStreamed<T>(String path, {dynamic data, Map<String, dynamic>? queryParameters, FutureOr<T> onSuccess(dynamic data)?, FutureOr<T> onError(dynamic data)?, RestApiClientRequestOptions? options, Duration? cacheLifetimeDuration}) Stream<Result<T>>
Streams POST results, yielding cached data first (if available), then network data.
override
put<T>(String path, {dynamic data, Map<String, dynamic>? queryParameters, FutureOr<T> onSuccess(dynamic data)?, FutureOr<T> onError(dynamic data)?, RestApiClientRequestOptions? options}) Future<Result<T>>
Performs a PUT request to the specified path.
override
setAcceptLanguageHeader(String languageCode) → void
Sets the Accept-Language header for localization.
override
setContentType(String contentType) → void
Sets the default Content-Type header for all requests.
override
toString() String
A string representation of this object.
inherited
unAuthorize() Future<bool>
Clears stored tokens and removes the Authorization header.
override

Operators

operator ==(Object other) bool
The equality operator.
inherited