|
1 | 1 | import 'dart:convert'; |
2 | 2 |
|
3 | 3 | import 'package:http/http.dart'; |
4 | | -import 'package:http_parser/http_parser.dart'; |
5 | 4 | import 'package:json_api/http.dart'; |
| 5 | +import 'package:json_api/src/client/message_converter.dart'; |
6 | 6 |
|
7 | 7 | /// Handler which relies on the built-in Dart HTTP client. |
8 | 8 | /// It is the developer's responsibility to instantiate the client and |
9 | 9 | /// call `close()` on it in the end pf the application lifecycle. |
10 | 10 | class PersistentHandler implements HttpHandler { |
11 | 11 | /// Creates a new instance of the handler. Do not forget to call `close()` on |
12 | 12 | /// the [client] when it's not longer needed. |
13 | | - PersistentHandler(this.client, {this.defaultEncoding = utf8}); |
| 13 | + /// |
| 14 | + /// Use [messageConverter] to fine tune the HTTP request/response conversion. |
| 15 | + PersistentHandler( |
| 16 | + this.client, |
| 17 | + {@Deprecated('Deprecated in favor of MessageConverter.' |
| 18 | + ' To be removed in version 6.0.0') |
| 19 | + this.defaultEncoding = utf8, |
| 20 | + MessageConverter? messageConverter}) |
| 21 | + : _converter = messageConverter ?? |
| 22 | + MessageConverter(defaultResponseEncoding: defaultEncoding); |
14 | 23 |
|
15 | 24 | final Client client; |
16 | 25 | final Encoding defaultEncoding; |
| 26 | + final MessageConverter _converter; |
17 | 27 |
|
18 | 28 | @override |
19 | 29 | Future<HttpResponse> handle(HttpRequest request) async { |
20 | | - final response = await Response.fromStream( |
21 | | - await client.send(Request(request.method, request.uri) |
22 | | - ..headers.addAll(request.headers) |
23 | | - ..body = request.body)); |
24 | | - final responseBody = |
25 | | - _encodingForHeaders(response.headers).decode(response.bodyBytes); |
26 | | - return HttpResponse(response.statusCode, body: responseBody) |
27 | | - ..headers.addAll(response.headers); |
28 | | - } |
29 | | - |
30 | | - /// Returns the encoding to use for a response with the given headers. |
31 | | - /// |
32 | | - /// Defaults to [defaultEncoding] if the headers don't specify a charset or if that |
33 | | - /// charset is unknown. |
34 | | - Encoding _encodingForHeaders(Map<String, String> headers) => |
35 | | - _encodingForCharset( |
36 | | - _contentTypeForHeaders(headers).parameters['charset']); |
37 | | - |
38 | | - /// Returns the [Encoding] that corresponds to [charset]. |
39 | | - /// |
40 | | - /// Returns [defaultEncoding] if [charset] is null or if no [Encoding] was found that |
41 | | - /// corresponds to [charset]. |
42 | | - Encoding _encodingForCharset(String? charset) { |
43 | | - if (charset == null) return defaultEncoding; |
44 | | - return Encoding.getByName(charset) ?? defaultEncoding; |
45 | | - } |
46 | | - |
47 | | - /// Returns the [MediaType] object for the given headers's content-type. |
48 | | - /// |
49 | | - /// Defaults to `application/octet-stream`. |
50 | | - MediaType _contentTypeForHeaders(Map<String, String> headers) { |
51 | | - final contentType = headers['content-type']; |
52 | | - if (contentType != null) return MediaType.parse(contentType); |
53 | | - return MediaType('application', 'octet-stream'); |
| 30 | + final convertedRequest = _converter.request(request); |
| 31 | + final streamedResponse = await client.send(convertedRequest); |
| 32 | + final response = await Response.fromStream(streamedResponse); |
| 33 | + return _converter.response(response); |
54 | 34 | } |
55 | 35 | } |
0 commit comments