@@ -17,12 +17,20 @@ import 'package:json_api/src/document_builder.dart';
1717/// JSON:API client
1818class JsonApiClient {
1919 static const contentType = 'application/vnd.api+json' ;
20-
2120 final http.Client httpClient;
21+ final OnHttpCall _onHttpCall;
2222 final SimpleDocumentBuilder _build;
2323
24- const JsonApiClient (this .httpClient, {SimpleDocumentBuilder builder})
25- : _build = builder ?? const DocumentBuilder ();
24+ /// Creates an instance of JSON:API client.
25+ /// You have to create and pass an instance of the [httpClient] yourself.
26+ /// Do not forget to call [httpClient.close()] when you're done using
27+ /// the JSON:API client.
28+ /// The [onHttpCall] hook, if passed, gets called when an http response is
29+ /// received from the HTTP Client.
30+ const JsonApiClient (this .httpClient,
31+ {SimpleDocumentBuilder builder, OnHttpCall onHttpCall})
32+ : _build = builder ?? const DocumentBuilder (),
33+ _onHttpCall = onHttpCall ?? _doNothing;
2634
2735 /// Fetches a resource collection by sending a GET query to the [uri] .
2836 /// Use [headers] to pass extra HTTP headers.
@@ -161,7 +169,7 @@ class JsonApiClient {
161169 http.Request request, D decodePrimaryData (Object _)) async {
162170 final response =
163171 await http.Response .fromStream (await httpClient.send (request));
164-
172+ _onHttpCall (request, response);
165173 if (response.body.isEmpty) {
166174 return Response (response.statusCode, response.headers);
167175 }
@@ -177,3 +185,9 @@ class JsonApiClient {
177185 body == null ? null : Document .decodeJson (body, decodePrimaryData));
178186 }
179187}
188+
189+ /// Defines the hook which gets called when the HTTP response is received from
190+ /// the HTTP Client.
191+ typedef void OnHttpCall (http.Request request, http.Response response);
192+
193+ _doNothing (http.Request request, http.Response response) {}
0 commit comments