http_client_dio 0.1.1 copy "http_client_dio: ^0.1.1" to clipboard
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 dio or wants dio configuration, interceptors, and transport behavior.
  • Keep feature/business code decoupled from dio by depending on the transport-agnostic HttpClient contract from http_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?>>();
  }
}
0
likes
130
points
81
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Dio adapter for the http_client_contracts contract.

License

MIT (license)

Dependencies

dio, http_client_contracts

More

Packages that depend on http_client_dio