http_client_dio 0.1.1
http_client_dio: ^0.1.1 copied to clipboard
Dio adapter for the http_client_contracts contract.
http_client_dio #
dio adapter for
http_client_contracts.
Use this package when your app should depend on the transport-agnostic
HttpClient contract, but you want requests to run through dio.
When To Use #
- Your app already uses
dioor wantsdioconfiguration, interceptors, and transport behavior. - Keep feature/business code decoupled from
dioby depending on the transport-agnosticHttpClientcontract fromhttp_client_contracts. - The concrete adapter should be created only in composition/infrastructure.
Import http_client_contracts directly wherever you need the shared
HttpClient types.
Usage #
Create the adapter in your composition root and pass it to code that expects the contract:
import 'package:dio/dio.dart';
import 'package:http_client_contracts/http_client_contracts.dart';
import 'package:http_client_dio/http_client_dio.dart';
final dio = Dio(
BaseOptions(
headers: {'Authorization': 'Bearer token'},
),
);
final HttpClient client = DioHttpClient(dio: dio);
final feedRepository = FeedRepository(client: client); // expects HttpClient
Use the contract in app/business code:
import 'package:http_client_contracts/http_client_contracts.dart';
class FeedRepository {
FeedRepository({required this.client});
final HttpClient client;
Future<List<Object?>> loadFeed() async {
final response = await client.get(
Uri.parse('https://api.example.com/feed'),
timeout: const Duration(seconds: 8),
);
if (!response.isSuccess) {
throw StateError('Feed request failed with ${response.statusCode}.');
}
return response.bodyAsJson<List<Object?>>();
}
}
Related Packages #
http_client_contracts: sharedHttpClientinterface, request/response models, and exceptions.http_client_http:package:httpadapter for the same contract.