convert method
Converts input
and returns the result of the conversion.
Implementation
@override
DynamiteResponse<B, H> convert(http.Response input) {
final rawHeaders = input.headers;
final statusCode = input.statusCode;
final validStatuses = serializer.validStatuses;
if (validStatuses != null && !validStatuses.contains(statusCode)) {
throw DynamiteStatusCodeException(input);
}
final headers = _deserialize<H>(rawHeaders, serializer.serializers, serializer.headersType);
final contentType = rawHeaders['content-type'];
MediaType? mediaType;
if (contentType != null) {
try {
mediaType = MediaType.parse(contentType);
} on FormatException catch (error, stackTrace) {
_logger.warning('Could not parse $contentType', error, stackTrace);
}
}
final body = switch (mediaType) {
MediaType(type: 'text') || MediaType(type: 'application', subtype: 'javascript') => input.body,
MediaType(type: 'application', subtype: 'json') => _deserialize<B>(
json.decode(input.body),
serializer.serializers,
serializer.bodyType,
),
_ => input.bodyBytes,
};
return DynamiteResponse<B, H>(
statusCode,
body as B,
headers as H,
);
}