Skip to content

Commit 71488a7

Browse files
committed
Adding fluent formatter API
1 parent 25b559d commit 71488a7

File tree

5 files changed

+219
-3
lines changed

5 files changed

+219
-3
lines changed

src/ScriptCs.WebApi/Formatter.cs

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.IO;
3+
using System.Net;
4+
using System.Net.Http;
5+
using System.Net.Http.Formatting;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
9+
namespace ScriptCs.WebApi
10+
{
11+
internal class Formatter : MediaTypeFormatter
12+
{
13+
private Func<Type, bool> _canReadType;
14+
private Func<Type, bool> _canWriteType;
15+
private Func<Type, Stream, HttpContent, IFormatterLogger, Task<object>> _readFromStream;
16+
private Func<Type, object, Stream, HttpContent, TransportContext, CancellationToken, Task> _writeToStream;
17+
18+
public Formatter(
19+
Func<Type, bool> canReadType,
20+
Func<Type, bool> canWriteType,
21+
Func<Type, Stream, HttpContent, IFormatterLogger, Task<object>> readFromStream,
22+
Func<Type, object, Stream, HttpContent, TransportContext, CancellationToken, Task> writeToStream
23+
)
24+
{
25+
_canReadType = canReadType;
26+
_canWriteType = canWriteType;
27+
_readFromStream = readFromStream;
28+
_writeToStream = writeToStream;
29+
}
30+
31+
public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
32+
{
33+
return _readFromStream(type, readStream, content, formatterLogger);
34+
}
35+
36+
37+
public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content,
38+
TransportContext transportContext, CancellationToken cancellationToken)
39+
{
40+
return _writeToStream(type, value, writeStream, content, transportContext, cancellationToken);
41+
}
42+
43+
public override bool CanReadType(Type type)
44+
{
45+
return _canReadType(type);
46+
}
47+
48+
public override bool CanWriteType(Type type)
49+
{
50+
return _canWriteType(type);
51+
}
52+
}
53+
}
+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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+
}

src/ScriptCs.WebApi/Properties/AssemblyInfo.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414

1515
[assembly: Guid("be92b3c5-a23a-4234-8856-162fb14ec2b1")]
1616

17-
[assembly: AssemblyVersion("0.1.0")]
18-
[assembly: AssemblyFileVersion("0.1.0")]
19-
[assembly: AssemblyInformationalVersion("0.1.0")]
17+
[assembly: AssemblyVersion("1.0.0")]
18+
[assembly: AssemblyFileVersion("1.0.0")]
19+
[assembly: AssemblyInformationalVersion("1.0.0")]

src/ScriptCs.WebApi/ScriptCs.WebApi.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@
7979
</ItemGroup>
8080
<ItemGroup>
8181
<Compile Include="ControllerResolver.cs" />
82+
<Compile Include="Formatter.cs" />
8283
<Compile Include="IControllerTypeManager.cs" />
8384
<Compile Include="Properties\AssemblyInfo.cs" />
85+
<Compile Include="FormatterBuilder.cs" />
8486
<Compile Include="ScriptPack.cs" />
8587
<Compile Include="ControllerTypeManager.cs" />
8688
<Compile Include="WebApi.cs" />

src/ScriptCs.WebApi/WebApi.cs

+5
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ public IDisposable Start(string baseAddress)
103103
});
104104
}
105105

106+
public FormatterBuilder NewFormatter()
107+
{
108+
return new FormatterBuilder();
109+
}
110+
106111
private void ApplyDefaultConfiguration(HttpConfiguration config,
107112
IEnumerable<Type> controllerTypes)
108113
{

0 commit comments

Comments
 (0)