http_extensions 0.1.2

http_extensions #

Base classes for building standard extensions for http package.

Usage #

To build an extension, you have to provide an Extension implementation :

import 'package:http/http.dart';
import 'package:http_extensions/http_extensions.dart';

class LogOptions {
  final bool isEnabled;
  const LogOptions({this.isEnabled = true});

class LogExtension extends Extension<LogOptions> {
  LogExtension([LogOptions defaultOptions = const LogOptions()])
      : super(defaultOptions: defaultOptions);

  int _requestId = 0;

  Future<StreamedResponse> sendWithOptions(
      BaseRequest request, LogOptions options) async {

    if(!options.isEnabled) {
      return await super.sendWithOptions(request, options);

    try {
      final id = _requestId++;
          "[HTTP]($id:${request.method}:${request.url}) Starting request ...");
      final result = await super.sendWithOptions(request, options);
          "[HTTP]($id:${request.method}:${request.url}) Request succeeded (statusCode: ${result.statusCode})");
      return result;
    } catch (e) {
      print("[HTTP]($id:${request.method}:${request.url}) An error occured during request : $e");

To call request using your extensions, the easiest way is to instantiate an ExtendedClient with your extensions.

import 'package:http/http.dart';
import 'package:http_extensions/http_extensions.dart';
import 'log_extension.dart';

Future main() async {
  final client = ExtendedClient(
    inner: Client(),
    extensions: [

  // Default options
  final defaultResult = await client.get("");
  print("default status code: ${defaultResult.statusCode}");

  // Custom options (not logged)
  final customResult = await client.getWithOptions("",
      options: [LogOptions(isEnabled: false)]);
  print("default status code: ${customResult.statusCode}");

Conventions #

If you create an extension package, please follow those naming conventions :

  • http_extensions_<name> : package name
  • <Name>Extension : extension class
  • <Name>Options : options class.

<name> : snake_case

<Name> : PascalCase

See a log example

Q & A

How is this compare to dio ?

Dio does a lot more, but it doesn't integrate well over web. That's why I just wanted a thin layer on top of http package, which is an officially supported package, compatible with native platforms, but also with web browsers.

0.1.2 #

  • Added helpers class to be platform independent.

0.1.0 #

  • Initial version

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  http_extensions: ^0.1.2

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:http_extensions/http_extensions.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Feb 13, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.5

Health suggestions

Fix lib/src/helpers/http_date.dart. (-16.51 points)

Analysis of lib/src/helpers/http_date.dart reported 36 hints, including:

line 8 col 1: Prefer using /// for doc comments.

line 36 col 3: Prefer using /// for doc comments.

line 42 col 24: Avoid const keyword.

line 43 col 24: Avoid const keyword.

line 59 col 23: Unnecessary new keyword.

Fix lib/src/client.dart. (-0.50 points)

Analysis of lib/src/client.dart reported 1 hint:

line 82 col 15: Unnecessary new keyword.

Format lib/example/log_extension.dart.

Run dartfmt to format lib/example/log_extension.dart.

Fix additional 6 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/helpers.dart (Run dartfmt to format lib/helpers.dart.)
  • lib/http_extensions.dart (Run dartfmt to format lib/http_extensions.dart.)
  • lib/src/buffered_request.dart (Run dartfmt to format lib/src/buffered_request.dart.)
  • lib/src/buffered_response.dart (Run dartfmt to format lib/src/buffered_response.dart.)
  • lib/src/helpers/headers.dart (Run dartfmt to format lib/src/helpers/headers.dart.)
  • lib/src/request.dart (Run dartfmt to format lib/src/request.dart.)

Maintenance suggestions

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and http_extensions.dart. Packages with multiple examples should provide example/

For more information see the pub package layout conventions.

The package description is too short. (-6 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.0 <3.0.0
http ^0.12.0+1 0.12.0+4
meta ^1.1.6 1.1.8
Transitive dependencies
async 2.4.0
charcode 1.1.3
collection 1.14.12
http_parser 3.1.3
path 1.6.4
pedantic 1.9.0
source_span 1.6.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6