|
1 | 1 | import 'dart:async'; |
2 | | -import 'dart:convert'; |
3 | 2 |
|
| 3 | +import 'package:aha_client/src/api/util/content_type_extractor.dart'; |
4 | 4 | import 'package:chopper/chopper.dart'; |
| 5 | +import 'package:meta/meta.dart'; |
5 | 6 | import 'package:xml/xml.dart'; |
6 | 7 |
|
7 | | -class XmlConverter implements Converter { |
8 | | - static const contentTypeHeader = 'Content-Type'; |
9 | | - static const xmlContentType = 'text/xml'; |
10 | | - static const xmlAltContentType = 'application/xml'; |
| 8 | +import 'combined_converter.dart'; |
| 9 | + |
| 10 | +abstract class XmlTypeConverter<T> { |
| 11 | + const XmlTypeConverter._(); |
| 12 | + |
| 13 | + XmlDocument toXml(T data); |
| 14 | + T fromXml(XmlDocument xml); |
| 15 | +} |
| 16 | + |
| 17 | +abstract class SimpleXmlTypeConverter<T> implements XmlTypeConverter<T> { |
| 18 | + const SimpleXmlTypeConverter(); |
| 19 | + |
| 20 | + @override |
| 21 | + T fromXml(XmlDocument xml) => parseXml(xml.rootElement); |
| 22 | + |
| 23 | + @override |
| 24 | + XmlDocument toXml(T data) { |
| 25 | + final builder = XmlBuilder()..processing('xml', 'version="1.0"'); |
| 26 | + buildXml(data, builder); |
| 27 | + return builder.buildDocument(); |
| 28 | + } |
| 29 | + |
| 30 | + @protected |
| 31 | + void buildXml(T data, XmlBuilder builder); |
| 32 | + |
| 33 | + @protected |
| 34 | + T parseXml(XmlElement element); |
| 35 | +} |
| 36 | + |
| 37 | +class XmlConverter with ContentTypeExtractor implements Converter { |
| 38 | + static const _xmlContentType = 'text/xml'; |
| 39 | + static const _allowedXmlContentTypes = [_xmlContentType, 'application/xml']; |
| 40 | + |
| 41 | + final _typeConverters = <Type, XmlTypeConverter>{ |
| 42 | + XmlDocument: const _XmlDocumentConverter(), |
| 43 | + }; |
| 44 | + |
| 45 | + void registerConverter<T>(XmlTypeConverter<T> converter) => |
| 46 | + _typeConverters[T] = converter; |
11 | 47 |
|
12 | 48 | @override |
13 | 49 | FutureOr<Request> convertRequest(Request request) { |
14 | | - final rawBody = request.body; |
15 | | - if (rawBody is XmlDocument) { |
16 | | - return request.copyWith( |
17 | | - body: rawBody.toXmlString(), |
18 | | - headers: { |
19 | | - ...request.headers, |
20 | | - contentTypeHeader: xmlContentType, |
21 | | - }, |
22 | | - ); |
23 | | - } else { |
24 | | - return request; |
25 | | - } |
| 50 | + final converter = _findConverter(request.body.runtimeType); |
| 51 | + return request.copyWith( |
| 52 | + body: converter.toXml(request.body).toXmlString(), |
| 53 | + headers: { |
| 54 | + ...request.headers, |
| 55 | + ContentTypeExtractor.contentTypeHeader: _xmlContentType, |
| 56 | + }, |
| 57 | + ); |
26 | 58 | } |
27 | 59 |
|
28 | 60 | @override |
29 | 61 | FutureOr<Response<BodyType>> convertResponse<BodyType, InnerType>( |
30 | 62 | Response response, |
31 | 63 | ) { |
32 | | - if (BodyType == XmlDocument) { |
33 | | - final contentType = |
34 | | - response.headers[contentTypeHeader.toLowerCase()]?.split(';').first; |
35 | | - if (contentType != xmlContentType && contentType != xmlAltContentType) { |
36 | | - throw Exception( |
37 | | - 'Cannot decode a response without the XML content type', |
38 | | - ); |
39 | | - } |
40 | | - |
41 | | - return response.copyWith( |
42 | | - body: XmlDocument.parse(response.bodyString), |
43 | | - ) as Response<BodyType>; |
44 | | - } else { |
45 | | - return response.copyWith(); |
| 64 | + _checkContentType(response); |
| 65 | + |
| 66 | + final converter = _findConverter<BodyType>(); |
| 67 | + return response.copyWith( |
| 68 | + body: converter.fromXml(XmlDocument.parse(response.bodyString)), |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + void _checkContentType(Response response) { |
| 73 | + final contentType = getContentType(response.headers); |
| 74 | + if (!_allowedXmlContentTypes.contains(contentType)) { |
| 75 | + throw ConversionNotSupportedException( |
| 76 | + runtimeType.toString(), |
| 77 | + 'Content-Type "$contentType" is not supported', |
| 78 | + ); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + XmlTypeConverter<T> _findConverter<T>([Type? t]) { |
| 83 | + final converter = _typeConverters[t ?? T] as XmlTypeConverter<T>?; |
| 84 | + if (converter == null) { |
| 85 | + throw ConversionNotSupportedException( |
| 86 | + runtimeType.toString(), |
| 87 | + 'Type has not been registered for XML conversion', |
| 88 | + ); |
46 | 89 | } |
| 90 | + |
| 91 | + return converter; |
47 | 92 | } |
48 | 93 | } |
| 94 | + |
| 95 | +class _XmlDocumentConverter implements XmlTypeConverter<XmlDocument> { |
| 96 | + const _XmlDocumentConverter(); |
| 97 | + |
| 98 | + @override |
| 99 | + XmlDocument fromXml(XmlDocument xml) => xml; |
| 100 | + |
| 101 | + @override |
| 102 | + XmlDocument toXml(XmlDocument data) => data; |
| 103 | +} |
0 commit comments