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 = t => true ;
18
+ private Func < Type , bool > _canWriteType = t => true ;
19
+ private Func < ReadFromStreamArgs , Task < object > > _readFromStream ;
20
+ private Func < WriteToStreamArgs , 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
+ _supportedEncodings = new List < Encoding > ( ) ;
30
+ }
31
+
32
+ public FormatterBuilder CanReadType ( Func < Type , bool > condition )
33
+ {
34
+ _canReadType = condition ;
35
+ return this ;
36
+ }
37
+
38
+ public FormatterBuilder CanWriteType ( Func < Type , bool > condition )
39
+ {
40
+ _canWriteType = condition ;
41
+ return this ;
42
+ }
43
+
44
+ public FormatterBuilder ReadFromStream (
45
+ Func < ReadFromStreamArgs , Task < object > > readFromStream )
46
+ {
47
+ _readFromStream = readFromStream ;
48
+ return this ;
49
+ }
50
+
51
+ public FormatterBuilder WriteToStream (
52
+ Func < WriteToStreamArgs , Task > writeToStream )
53
+ {
54
+ _writeToStream = writeToStream ;
55
+ return this ;
56
+ }
57
+
58
+ public FormatterBuilder SupportMediaType ( MediaTypeHeaderValue mediaType )
59
+ {
60
+ _supportedMediaTypes . Add ( mediaType ) ;
61
+ return this ;
62
+ }
63
+
64
+ public FormatterBuilder SupportMediaType ( string mediaType )
65
+ {
66
+ _supportedMediaTypes . Add ( new MediaTypeHeaderValue ( mediaType ) ) ;
67
+ return this ;
68
+ }
69
+
70
+ public FormatterBuilder SupportEncoding ( Encoding encoding )
71
+ {
72
+ _supportedEncodings . Add ( encoding ) ;
73
+ return this ;
74
+ }
75
+
76
+ public FormatterBuilder MapQueryString (
77
+ string parameterName ,
78
+ string parameterValue ,
79
+ MediaTypeHeaderValue mediaType )
80
+ {
81
+ _mappings . Add ( new QueryStringMapping ( parameterName , parameterValue , mediaType ) ) ;
82
+ return this ;
83
+ }
84
+
85
+ public FormatterBuilder MapQueryString (
86
+ string parameterName ,
87
+ string parameterValue ,
88
+ string mediaType )
89
+ {
90
+ _mappings . Add ( new QueryStringMapping ( parameterName , parameterValue , mediaType ) ) ;
91
+ return this ;
92
+ }
93
+
94
+ public FormatterBuilder MapRequestHeader (
95
+ string headerName ,
96
+ string headerValue ,
97
+ System . StringComparison valueComparison ,
98
+ bool isValueSubstring ,
99
+ string mediaType )
100
+ {
101
+ _mappings . Add ( new RequestHeaderMapping ( headerName , headerValue , valueComparison , isValueSubstring , mediaType ) ) ;
102
+ return this ;
103
+ }
104
+
105
+ public FormatterBuilder MapRequestHeader (
106
+ string headerName ,
107
+ string headerValue ,
108
+ System . StringComparison valueComparison ,
109
+ bool isValueSubstring ,
110
+ MediaTypeHeaderValue mediaType
111
+ )
112
+ {
113
+ _mappings . Add ( new RequestHeaderMapping ( headerName , headerValue , valueComparison , isValueSubstring , mediaType ) ) ;
114
+ return this ;
115
+ }
116
+
117
+ public FormatterBuilder MapUriExtension (
118
+ string extension ,
119
+ string mediaType
120
+ )
121
+ {
122
+ _mappings . Add ( new UriPathExtensionMapping ( extension , mediaType ) ) ;
123
+ return this ;
124
+ }
125
+
126
+ public FormatterBuilder MapUriExtension (
127
+ string extension ,
128
+ MediaTypeHeaderValue mediaType
129
+ )
130
+ {
131
+ _mappings . Add ( new UriPathExtensionMapping ( extension , mediaType ) ) ;
132
+ return this ;
133
+ }
134
+
135
+ public MediaTypeFormatter Build ( )
136
+ {
137
+ var formatter = new Formatter ( _canReadType , _canWriteType , _readFromStream , _writeToStream ) ;
138
+
139
+ foreach ( var mediaType in _supportedMediaTypes )
140
+ {
141
+ formatter . SupportedMediaTypes . Add ( mediaType ) ;
142
+ }
143
+
144
+ foreach ( var mapping in _mappings )
145
+ {
146
+ formatter . MediaTypeMappings . Add ( mapping ) ;
147
+ }
148
+
149
+ foreach ( var encoding in _supportedEncodings )
150
+ {
151
+ formatter . SupportedEncodings . Add ( encoding ) ;
152
+ }
153
+
154
+ return formatter ;
155
+ }
156
+ }
157
+ }
0 commit comments