Skip to content

Commit dd67b04

Browse files
committed
Cleaning up fluent formatter syntax and adding sample with formatter
1 parent 71488a7 commit dd67b04

9 files changed

+143
-15
lines changed

samples/formatter.csx

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Dynamic;
2+
using System.Web.Http;
3+
using System.Web.Http.Routing;
4+
5+
public class TestController : ApiController
6+
{
7+
public dynamic Get() {
8+
dynamic obj = new ExpandoObject();
9+
obj.message = "Hello from Web Api";
10+
return obj;
11+
}
12+
}
13+
14+
var webapi = Require<WebApi>();
15+
16+
var formatter = webapi.NewFormatter().
17+
SupportMediaType("application/vnd.foo+json").
18+
MapUriExtension(".foo", "application/vnd.foo+json").
19+
WriteToStream(async (args) => {
20+
var writer = new StreamWriter(args.Stream);
21+
await writer.WriteLineAsync("{\"foo\":\"foo\"}");
22+
await writer.FlushAsync();
23+
}).
24+
Build();
25+
26+
var config = new HttpConfiguration();
27+
28+
webapi.
29+
UseJsonOnly().
30+
Configure(config, typeof(TestController));
31+
32+
config.Formatters.Insert(0, formatter);
33+
34+
config.Routes.Clear();
35+
config.Routes.MapHttpRoute(name: "Extension",
36+
routeTemplate: "api/{controller}.{extension}/{id}",
37+
defaults: new {id = RouteParameter.Optional}
38+
);
39+
40+
var server = webapi.Start("http://localhost:8080");
41+
42+
Console.WriteLine("Listening...");
43+
Console.ReadLine();
44+
server.Dispose();
45+
46+

samples/packages.config

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.0" targetFramework="net45" />
4+
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.0" targetFramework="net45" />
5+
<package id="Microsoft.AspNet.WebApi.Owin" version="5.2.0" targetFramework="net45" />
6+
<package id="Microsoft.AspNet.WebApi.OwinSelfHost" version="5.2.0" />
7+
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
8+
<package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net40" />
9+
<package id="Microsoft.Net.Http" version="2.2.22" targetFramework="net45" />
10+
<package id="Microsoft.Owin" version="2.0.2" targetFramework="net45" />
11+
<package id="Microsoft.Owin.Host.HttpListener" version="2.0.2" targetFramework="net45" />
12+
<package id="Microsoft.Owin.Hosting" version="2.0.2" targetFramework="net45" />
13+
<package id="Newtonsoft.Json" version="6.0.2" targetFramework="net45" />
14+
<package id="Owin" version="1.0" targetFramework="net40" />
15+
<package id="ScriptCs.Contracts" version="0.10.0" targetFramework="net45" />
16+
<package id="ScriptCs.WebApi2" version="1.0.0" targetFramework="net45" />
17+
</packages>

src/ScriptCs.WebApi/Formatter.cs

+6-7
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ internal class Formatter : MediaTypeFormatter
1212
{
1313
private Func<Type, bool> _canReadType;
1414
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;
15+
private Func<ReadFromStreamArgs, Task<object>> _readFromStream;
16+
private Func<WriteToStreamArgs, Task> _writeToStream;
1717

1818
public Formatter(
1919
Func<Type, bool> canReadType,
2020
Func<Type, bool> canWriteType,
21-
Func<Type, Stream, HttpContent, IFormatterLogger, Task<object>> readFromStream,
22-
Func<Type, object, Stream, HttpContent, TransportContext, CancellationToken, Task> writeToStream
21+
Func<ReadFromStreamArgs, Task<object>> readFromStream,
22+
Func<WriteToStreamArgs, Task> writeToStream
2323
)
2424
{
2525
_canReadType = canReadType;
@@ -30,14 +30,13 @@ Func<Type, object, Stream, HttpContent, TransportContext, CancellationToken, Tas
3030

3131
public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
3232
{
33-
return _readFromStream(type, readStream, content, formatterLogger);
33+
return _readFromStream(new ReadFromStreamArgs(type, readStream, content, formatterLogger));
3434
}
3535

36-
3736
public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content,
3837
TransportContext transportContext, CancellationToken cancellationToken)
3938
{
40-
return _writeToStream(type, value, writeStream, content, transportContext, cancellationToken);
39+
return _writeToStream(new WriteToStreamArgs(type, value, writeStream, content, transportContext, cancellationToken));
4140
}
4241

4342
public override bool CanReadType(Type type)

src/ScriptCs.WebApi/FormatterBuilder.cs

+7-6
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ namespace ScriptCs.WebApi
1414
{
1515
public class FormatterBuilder
1616
{
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;
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;
2121
private readonly IList<MediaTypeMapping> _mappings;
2222
private readonly IList<MediaTypeHeaderValue> _supportedMediaTypes;
2323
private readonly IList<Encoding> _supportedEncodings;
@@ -26,6 +26,7 @@ public FormatterBuilder()
2626
{
2727
_mappings = new List<MediaTypeMapping>();
2828
_supportedMediaTypes = new List<MediaTypeHeaderValue>();
29+
_supportedEncodings = new List<Encoding>();
2930
}
3031

3132
public FormatterBuilder CanReadType(Func<Type, bool> condition)
@@ -41,14 +42,14 @@ public FormatterBuilder CanWriteType(Func<Type, bool> condition)
4142
}
4243

4344
public FormatterBuilder ReadFromStream(
44-
Func<Type, Stream, HttpContent, IFormatterLogger, Task<object>> readFromStream)
45+
Func<ReadFromStreamArgs, Task<object>> readFromStream)
4546
{
4647
_readFromStream = readFromStream;
4748
return this;
4849
}
4950

5051
public FormatterBuilder WriteToStream(
51-
Func<Type, object, Stream, HttpContent,TransportContext, CancellationToken, Task> writeToStream)
52+
Func<WriteToStreamArgs, Task> writeToStream)
5253
{
5354
_writeToStream = writeToStream;
5455
return this;
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.IO;
3+
using System.Net.Http;
4+
using System.Net.Http.Formatting;
5+
6+
namespace ScriptCs.WebApi
7+
{
8+
public class ReadFromStreamArgs
9+
{
10+
public ReadFromStreamArgs(Type type, Stream stream, HttpContent content, IFormatterLogger logger)
11+
{
12+
Type = type;
13+
Stream = stream;
14+
Content = content;
15+
Logger = logger;
16+
}
17+
18+
public Type Type { get; private set; }
19+
public Stream Stream { get; private set; }
20+
public Object Instance { get; private set; }
21+
public HttpContent Content { get; private set; }
22+
public IFormatterLogger Logger { get; private set; }
23+
}
24+
}

src/ScriptCs.WebApi/ScriptCs.WebApi.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,11 @@
8383
<Compile Include="IControllerTypeManager.cs" />
8484
<Compile Include="Properties\AssemblyInfo.cs" />
8585
<Compile Include="FormatterBuilder.cs" />
86+
<Compile Include="ReadFromStreamArgs.cs" />
8687
<Compile Include="ScriptPack.cs" />
8788
<Compile Include="ControllerTypeManager.cs" />
8889
<Compile Include="WebApi.cs" />
90+
<Compile Include="WriteToStreamArgs.cs" />
8991
</ItemGroup>
9092
<ItemGroup>
9193
<None Include="packages.config">

src/ScriptCs.WebApi/ScriptPack.cs

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ void IScriptPack.Initialize(IScriptPackSession session)
3333
var namespaces = new[]
3434
{
3535
"System.Web.Http",
36+
"System.Web.Http.Routing",
3637
"System.Net.Http",
3738
"System.Net.Http.Headers",
3839
"Owin"

src/ScriptCs.WebApi/WebApi.cs

+11-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Reflection;
88
using System.Web.Http;
99
using System.Web.Http.Dispatcher;
10+
using System.Web.Http.Routing;
1011
using Common.Logging;
1112
using Microsoft.Owin.Builder;
1213
using Microsoft.SqlServer.Server;
@@ -70,6 +71,7 @@ public WebApi Configure(Action<IAppBuilder> startupAction, HttpConfiguration con
7071

7172
_controllerTypes = controllerTypes;
7273
_config = config;
74+
ApplyDefaultConfiguration(_config, _controllerTypes);
7375
return this;
7476
}
7577

@@ -79,6 +81,11 @@ public WebApi Configure(Action<IAppBuilder> startupAction, HttpConfiguration con
7981
return Configure(startupAction, config, _typeManager.GetControllerTypes(controllerAssemblies).ToArray());
8082
}
8183

84+
public WebApi Configure(HttpConfiguration config, params Type[] controllerTypes)
85+
{
86+
return Configure(null, config, controllerTypes);
87+
}
88+
8289
public WebApi UseJsonOnly()
8390
{
8491
_useJsonOnly = true;
@@ -98,8 +105,10 @@ public IDisposable Start(string baseAddress)
98105
return WebApp.Start(baseAddress, appBuilder =>
99106
{
100107
appBuilder.UseWebApi(_config);
101-
ApplyDefaultConfiguration(_config, _controllerTypes);
102-
_startupAction(appBuilder);
108+
if (_startupAction != null)
109+
{
110+
_startupAction(appBuilder);
111+
}
103112
});
104113
}
105114

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.IO;
3+
using System.Net;
4+
using System.Net.Http;
5+
using System.Threading;
6+
7+
namespace ScriptCs.WebApi
8+
{
9+
public class WriteToStreamArgs
10+
{
11+
public WriteToStreamArgs(Type type, object instance, Stream stream, HttpContent content, TransportContext context, CancellationToken token)
12+
{
13+
Type = type;
14+
Instance = instance;
15+
Stream = stream;
16+
Content = content;
17+
Context = context;
18+
Token = token;
19+
20+
}
21+
22+
public Type Type { get; private set; }
23+
public object Instance { get; private set; }
24+
public Stream Stream { get; private set; }
25+
public HttpContent Content { get; private set; }
26+
public TransportContext Context { get; private set; }
27+
public CancellationToken Token { get; private set; }
28+
}
29+
}

0 commit comments

Comments
 (0)