intercepted_client 0.0.1 copy "intercepted_client: ^0.0.1" to clipboard
intercepted_client: ^0.0.1 copied to clipboard

Interceptors for http package that support concurrent requests.

example/main.dart

import 'package:http/http.dart';
import 'package:intercepted_client/intercepted_client.dart';
import 'package:intercepted_client/src/intercepted_client.dart';

class AuthInterceptor extends SequentialHttpInterceptor {
  final String token;

  AuthInterceptor(this.token);

  @override
  void interceptRequest(BaseRequest request, RequestHandler handler) {
    request.headers['Authorization'] = 'Bearer $token';

    handler.next(request);
  }
}

class LogInterceptor extends HttpInterceptor {
  @override
  void interceptRequest(BaseRequest request, RequestHandler handler) {
    print('Request: ${request.url}');
    handler.next(request);
  }

  @override
  void interceptResponse(StreamedResponse response, ResponseHandler handler) {
    print('Request: ${handler.request.url} - Status: ${response.statusCode}');
    handler.resolveResponse(response);
  }

  @override
  void interceptError(Object error, ErrorHandler handler) {
    print('Error: $error');
    handler.rejectError(error);
  }
}

Future<void> main() async {
  final Client client = InterceptedClient(
    interceptors: [
      AuthInterceptor('my-token'),
      LogInterceptor(),
    ],
  );

  final response = await client.get(Uri.parse('https://example.com'));

  // prints 'Bearer my-token'
  print(response.request?.headers['Authorization']);
}
3
likes
150
pub points
84%
popularity

Publisher

verified publisherlazebny.io

Interceptors for http package that support concurrent requests.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

collection, http, stack_trace

More

Packages that depend on intercepted_client