1
+ using System ;
2
+ using System . Collections ;
3
+ using System . Collections . Generic ;
4
+ using System . IO ;
5
+ using System . Net ;
6
+ using System . Net . Http ;
7
+ using System . Net . Http . Formatting ;
8
+ using System . Net . Http . Headers ;
9
+ using System . Text ;
10
+ using System . Threading ;
11
+ using System . Threading . Tasks ;
12
+
13
+ namespace ScriptCs . WebApi
14
+ {
15
+ public class FormatterBuilder
16
+ {
17
+ private Func < Type , bool > _canReadType ;
18
+ private Func < Type , bool > _canWriteType ;
19
+ private Func < Type , Stream , HttpContent , IFormatterLogger , Task < object > > _readFromStream ;
20
+ private Func < Type , object , Stream , HttpContent , TransportContext , CancellationToken , Task > _writeToStream ;
21
+ private readonly IList < MediaTypeMapping > _mappings ;
22
+ private readonly IList < MediaTypeHeaderValue > _supportedMediaTypes ;
23
+ private readonly IList < Encoding > _supportedEncodings ;
24
+
25
+ public FormatterBuilder ( )
26
+ {
27
+ _mappings = new List < MediaTypeMapping > ( ) ;
28
+ _supportedMediaTypes = new List < MediaTypeHeaderValue > ( ) ;
29
+ }
30
+
31
+ public FormatterBuilder CanReadType ( Func < Type , bool > condition )
32
+ {
33
+ _canReadType = condition ;
34
+ return this ;
35
+ }
36
+
37
+ public FormatterBuilder CanWriteType ( Func < Type , bool > condition )
38
+ {
39
+ _canWriteType = condition ;
40
+ return this ;
41
+ }
42
+
43
+ public FormatterBuilder ReadFromStream (
44
+ Func < Type , Stream , HttpContent , IFormatterLogger , Task < object > > readFromStream )
45
+ {
46
+ _readFromStream = readFromStream ;
47
+ return this ;
48
+ }
49
+
50
+ public FormatterBuilder WriteToStream (
51
+ Func < Type , object , Stream , HttpContent , TransportContext , CancellationToken , Task > writeToStream )
52
+ {
53
+ _writeToStream = writeToStream ;
54
+ return this ;
55
+ }
56
+
57
+ public FormatterBuilder SupportMediaType ( MediaTypeHeaderValue mediaType )
58
+ {
59
+ _supportedMediaTypes . Add ( mediaType ) ;
60
+ return this ;
61
+ }
62
+
63
+ public FormatterBuilder SupportMediaType ( string mediaType )
64
+ {
65
+ _supportedMediaTypes . Add ( new MediaTypeHeaderValue ( mediaType ) ) ;
66
+ return this ;
67
+ }
68
+
69
+ public FormatterBuilder SupportEncoding ( Encoding encoding )
70
+ {
71
+ _supportedEncodings . Add ( encoding ) ;
72
+ return this ;
73
+ }
74
+
75
+ public FormatterBuilder MapQueryString (
76
+ string parameterName ,
77
+ string parameterValue ,
78
+ MediaTypeHeaderValue mediaType )
79
+ {
80
+ _mappings . Add ( new QueryStringMapping ( parameterName , parameterValue , mediaType ) ) ;
81
+ return this ;
82
+ }
83
+
84
+ public FormatterBuilder MapQueryString (
85
+ string parameterName ,
86
+ string parameterValue ,
87
+ string mediaType )
88
+ {
89
+ _mappings . Add ( new QueryStringMapping ( parameterName , parameterValue , mediaType ) ) ;
90
+ return this ;
91
+ }
92
+
93
+ public FormatterBuilder MapRequestHeader (
94
+ string headerName ,
95
+ string headerValue ,
96
+ System . StringComparison valueComparison ,
97
+ bool isValueSubstring ,
98
+ string mediaType )
99
+ {
100
+ _mappings . Add ( new RequestHeaderMapping ( headerName , headerValue , valueComparison , isValueSubstring , mediaType ) ) ;
101
+ return this ;
102
+ }
103
+
104
+ public FormatterBuilder MapRequestHeader (
105
+ string headerName ,
106
+ string headerValue ,
107
+ System . StringComparison valueComparison ,
108
+ bool isValueSubstring ,
109
+ MediaTypeHeaderValue mediaType
110
+ )
111
+ {
112
+ _mappings . Add ( new RequestHeaderMapping ( headerName , headerValue , valueComparison , isValueSubstring , mediaType ) ) ;
113
+ return this ;
114
+ }
115
+
116
+ public FormatterBuilder MapUriExtension (
117
+ string extension ,
118
+ string mediaType
119
+ )
120
+ {
121
+ _mappings . Add ( new UriPathExtensionMapping ( extension , mediaType ) ) ;
122
+ return this ;
123
+ }
124
+
125
+ public FormatterBuilder MapUriExtension (
126
+ string extension ,
127
+ MediaTypeHeaderValue mediaType
128
+ )
129
+ {
130
+ _mappings . Add ( new UriPathExtensionMapping ( extension , mediaType ) ) ;
131
+ return this ;
132
+ }
133
+
134
+ public MediaTypeFormatter Build ( )
135
+ {
136
+ var formatter = new Formatter ( _canReadType , _canWriteType , _readFromStream , _writeToStream ) ;
137
+
138
+ foreach ( var mediaType in _supportedMediaTypes )
139
+ {
140
+ formatter . SupportedMediaTypes . Add ( mediaType ) ;
141
+ }
142
+
143
+ foreach ( var mapping in _mappings )
144
+ {
145
+ formatter . MediaTypeMappings . Add ( mapping ) ;
146
+ }
147
+
148
+ foreach ( var encoding in _supportedEncodings )
149
+ {
150
+ formatter . SupportedEncodings . Add ( encoding ) ;
151
+ }
152
+
153
+ return formatter ;
154
+ }
155
+ }
156
+ }
0 commit comments