1
1
import 'dart:convert' ;
2
- import 'dart:typed_data' ;
3
2
4
3
import 'package:built_value/serializer.dart' ;
5
4
import 'package:dynamite_runtime/http_client.dart' ;
6
5
import 'package:http/http.dart' as http;
6
+ import 'package:http_parser/http_parser.dart' ;
7
+ import 'package:logging/logging.dart' ;
7
8
import 'package:meta/meta.dart' ;
8
9
9
10
/// Response returned by operations of a `DynamiteClient` .
@@ -70,6 +71,8 @@ final class ResponseConverter<B, H> with Converter<http.Response, DynamiteRespon
70
71
/// The serializer to convert the raw header and body into [B] and [H] .
71
72
final DynamiteSerializer <B , H > serializer;
72
73
74
+ static final Logger _logger = Logger ('ResponseConverter' );
75
+
73
76
@override
74
77
DynamiteResponse <B , H > convert (http.Response input) {
75
78
final rawHeaders = input.headers;
@@ -81,12 +84,25 @@ final class ResponseConverter<B, H> with Converter<http.Response, DynamiteRespon
81
84
82
85
final headers = _deserialize <H >(rawHeaders, serializer.serializers, serializer.headersType);
83
86
84
- final rawBody = switch (serializer.bodyType) {
85
- const FullType (Uint8List ) => input.bodyBytes,
86
- const FullType (String ) => input.body,
87
- _ => json.decode (input.body),
87
+ final contentType = rawHeaders['content-type' ];
88
+ MediaType ? mediaType;
89
+ if (contentType != null ) {
90
+ try {
91
+ mediaType = MediaType .parse (contentType);
92
+ } on FormatException catch (error, stackTrace) {
93
+ _logger.warning ('Could not parse $contentType ' , error, stackTrace);
94
+ }
95
+ }
96
+
97
+ final body = switch (mediaType) {
98
+ MediaType (type: 'text' ) || MediaType (type: 'application' , subtype: 'javascript' ) => input.body,
99
+ MediaType (type: 'application' , subtype: 'json' ) => _deserialize <B >(
100
+ json.decode (input.body),
101
+ serializer.serializers,
102
+ serializer.bodyType,
103
+ ),
104
+ _ => input.bodyBytes,
88
105
};
89
- final body = _deserialize <B >(rawBody, serializer.serializers, serializer.bodyType);
90
106
91
107
return DynamiteResponse <B , H >(
92
108
statusCode,
0 commit comments