{"id":16401443,"url":"https://github.com/hawkkiller/intercepted_client","last_synced_at":"2025-10-05T14:09:51.185Z","repository":{"id":220710259,"uuid":"751293903","full_name":"hawkkiller/intercepted_client","owner":"hawkkiller","description":null,"archived":false,"fork":false,"pushed_at":"2024-09-10T09:58:10.000Z","size":58,"stargazers_count":8,"open_issues_count":1,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-18T18:05:50.462Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hawkkiller.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-02-01T10:18:38.000Z","updated_at":"2024-12-08T00:13:30.000Z","dependencies_parsed_at":"2024-02-08T22:05:31.396Z","dependency_job_id":"8a58d63c-69b6-4c3b-a492-fc5044f87411","html_url":"https://github.com/hawkkiller/intercepted_client","commit_stats":null,"previous_names":["hawkkiller/intercepted_client"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hawkkiller%2Fintercepted_client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hawkkiller%2Fintercepted_client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hawkkiller%2Fintercepted_client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hawkkiller%2Fintercepted_client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hawkkiller","download_url":"https://codeload.github.com/hawkkiller/intercepted_client/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245061382,"owners_count":20554563,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T05:43:02.754Z","updated_at":"2025-10-05T14:09:51.068Z","avatar_url":"https://github.com/hawkkiller.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Intercepted Client\n\n[![codecov](https://codecov.io/gh/hawkkiller/intercepted_client/graph/badge.svg?token=lCJwHqNC1E)](https://codecov.io/gh/hawkkiller/intercepted_client)\n\nThis is a simple HTTP client that supports interceptors and concurrent requests. It is built on top of the `http` package and implements the `Client` interface.\n\n## Example\n\n```dart\nimport 'package:http/http.dart';\nimport 'package:intercepted_client/intercepted_client.dart';\n\nclass AuthInterceptor extends SequentialHttpInterceptor {\n  final String token;\n\n  AuthInterceptor(this.token);\n\n  @override\n  void interceptRequest(BaseRequest request, RequestHandler handler) {\n    request.headers['Authorization'] = 'Bearer $token';\n\n    handler.next(request);\n  }\n}\n\nFuture\u003cvoid\u003e main() async {\n  final Client client = InterceptedClient(\n    interceptors: [\n      AuthInterceptor('my-token'),\n    ],\n  );\n\n  final response = await client.get(Uri.parse('https://example.com'));\n\n  // prints 'Bearer my-token'\n  print(response.request?.headers['Authorization']);\n}\n```\n\nThe interceptor above adds an `Authorization` header to every request made by the client.\n\n## Interceptors\n\nInterceptor is a class that has access to the request, response and error objects. It can be used to modify the request, response or error, or to perform any side effect.\n\nFor example, an interceptor can be used to add an `Authorization` header to every request made by the client, print responses to the console or retry failed requests.\n\n**intercepted_client** provides two types of interceptors: `HttpInterceptor` and `SequentialHttpInterceptor`.\n\n### HttpInterceptor\n\n`HttpInterceptor` is a simple interceptor that both defines the Interceptor contract and provides a default implementation. Each request is handled independently, and the interceptor has no knowledge of the previous or next requests.\n\nThere are three methods that can be overridden:\n\n- `interceptRequest` - called before the request is sent, and can be used to modify the request, for example to add headers.\n- `interceptResponse` - called after the response is received. Note, that this method works only with StreamedResponse (not with Response). This means that you can't see the full response body in this method, but you have access to the response headers and status code.\n- `interceptError` - called when an error occurs during the request. This method can be used to retry the request or to perform any side effect.\n\nEach method receives a `RequestHandler`, `ResponseHandler` or `ErrorHandler` object, which can be used to continue, reject or resolve the request, response or error handling.\n\nEach method can be overridden independently, so you can create an interceptor that only logs requests, for example.\n\nHere you can see an example of a simple `HttpInterceptor` that logs the request and response:\n\n```dart\nclass LogInterceptor extends HttpInterceptor {\n  @override\n  void interceptRequest(BaseRequest request, RequestHandler handler) {\n    print('Request: ${request.url}');\n    handler.next(request);\n  }\n\n  @override\n  void interceptResponse(StreamedResponse response, ResponseHandler handler) {\n    print('Response: ${response.statusCode}');\n    handler.resolveResponse(response);\n  }\n\n  @override\n  void interceptError(Object error, ErrorHandler handler) {\n    print('Error: $error');\n    handler.rejectError(error);\n  }\n}\n```\n\n### SequentialHttpInterceptor\n\n`SequentialHttpInterceptor` implements the `HttpInterceptor` contract, but intercepts requests sequentially.\n\nUnder the hood, this interceptor maintains 3 queues - request, response and error. Each task in queue processed in order. It means that interceptor will wait until the previous task is completed before starting the next one.\n\nHere is an interceptor that adds a delay of 1 second to every request:\n\n```dart\nclass DelayInterceptor extends SequentialHttpInterceptor {\n  @override\n  void interceptRequest(BaseRequest request, RequestHandler handler) {\n    Future.delayed(Duration(seconds: 1), () {\n      handler.next(request);\n    });\n  }\n}\n```\n\nIf two requests are made in parallel, the second request will be delayed by 2 seconds, as it will wait for the previous request interceptor to complete.\n\nSame way it works for response and error interceptors.\n\nNote, that requests (actual packets sending) are still made in parallel, so that the application won't be blocked by the interceptor.\n\nAlso, you can create your own interceptor by implementing the `HttpInterceptor` contract.\n\n## InterceptedClient\n\n`InterceptedClient` is a class that implements the `Client` interface from `http` and supports interceptors that implement the `HttpInterceptor` contract.\n\nIt can be used as a drop-in replacement for the `http` client.\n\n```dart\nfinal Client client = InterceptedClient(\n  interceptors: [\n    LogInterceptor(),\n  ],\n);\n\nfinal response = await client.get(Uri.parse('https://example.com'));\n```\n\nThis client will log every request and response made by the client.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhawkkiller%2Fintercepted_client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhawkkiller%2Fintercepted_client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhawkkiller%2Fintercepted_client/lists"}