http_client_http 0.1.1 copy "http_client_http: ^0.1.1" to clipboard
http_client_http: ^0.1.1 copied to clipboard

package:http adapter for the http_client_contracts contract.

http_client_http #

package:http 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 package:http.

When To Use #

  • You want a small adapter around the official Dart package:http client.
  • Keep feature/business code decoupled from package:http 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:http/http.dart' as http;
import 'package:http_client_contracts/http_client_contracts.dart';
import 'package:http_client_http/http_client_http.dart';

final HttpClient client = HttpPackageClient(
  innerClient: http.Client(),
);
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
63
downloads

Documentation

API reference

Publisher

verified publisherapptify.co.uk

Weekly Downloads

package:http adapter for the http_client_contracts contract.

License

MIT (license)

Dependencies

http, http_client_contracts

More

Packages that depend on http_client_http