Skip to content

Commit 8463ca9

Browse files
committed
CollectionRequest, AutoCompleteRequest models binding, HttpPut and HttpDelete support
1 parent fc6a228 commit 8463ca9

File tree

98 files changed

+33501
-30
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+33501
-30
lines changed

NetCoreStack.Proxy.sln

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26228.4
4+
VisualStudioVersion = 15.0.26403.7
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{9754E8AD-F544-4C44-939D-95164DC29671}"
77
EndProject
@@ -17,11 +17,17 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCoreStack.Proxy", "src\N
1717
EndProject
1818
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCoreStack.Proxy.Tests", "test\NetCoreStack.Proxy.Tests\NetCoreStack.Proxy.Tests.csproj", "{00CEA1AF-05CC-4D39-9118-267683DEDCD1}"
1919
EndProject
20-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCoreStack.Proxy.ServerApp", "test\NetCoreStack.Proxy.ServerApp\NetCoreStack.Proxy.ServerApp.csproj", "{89018E44-AA73-4EB2-8B3D-B96FBFEF96A4}"
21-
EndProject
2220
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCoreStack.Proxy.Test.Contracts", "test\NetCoreStack.Proxy.Test.Contracts\NetCoreStack.Proxy.Test.Contracts.csproj", "{65CF1A10-0E97-479E-9D8A-2C7741BF608C}"
2321
EndProject
24-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCoreStack.Proxy.WebClient", "test\NetCoreStack.Proxy.WebClient\NetCoreStack.Proxy.WebClient.csproj", "{9E156265-88E0-4506-8584-0644DCD9D205}"
22+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCoreStack.Api.Hosting", "test\NetCoreStack.Api.Hosting\NetCoreStack.Api.Hosting.csproj", "{8BD1F907-921F-44BD-A06A-C77F58F82B8F}"
23+
EndProject
24+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCoreStack.WebClient.Hosting", "test\NetCoreStack.WebClient.Hosting\NetCoreStack.WebClient.Hosting.csproj", "{7C186FB4-EA93-470F-9552-CDF2A0C5F20D}"
25+
EndProject
26+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCoreStack.Domain.Contracts", "test\NetCoreStack.Domain.Contracts\NetCoreStack.Domain.Contracts.csproj", "{E0D0DB64-3A8F-46A8-95BE-C2A559041BD8}"
27+
EndProject
28+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCoreStack.Proxy.ServerApp", "test\NetCoreStack.Proxy.ServerApp\NetCoreStack.Proxy.ServerApp.csproj", "{0847A55D-050B-4F99-A694-6D3F640B99EE}"
29+
EndProject
30+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCoreStack.Proxy.WebClient", "test\NetCoreStack.Proxy.WebClient\NetCoreStack.Proxy.WebClient.csproj", "{EE267E71-A7A8-44C2-B0A1-7E093D702A5F}"
2531
EndProject
2632
Global
2733
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -37,27 +43,42 @@ Global
3743
{00CEA1AF-05CC-4D39-9118-267683DEDCD1}.Debug|Any CPU.Build.0 = Debug|Any CPU
3844
{00CEA1AF-05CC-4D39-9118-267683DEDCD1}.Release|Any CPU.ActiveCfg = Release|Any CPU
3945
{00CEA1AF-05CC-4D39-9118-267683DEDCD1}.Release|Any CPU.Build.0 = Release|Any CPU
40-
{89018E44-AA73-4EB2-8B3D-B96FBFEF96A4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
41-
{89018E44-AA73-4EB2-8B3D-B96FBFEF96A4}.Debug|Any CPU.Build.0 = Debug|Any CPU
42-
{89018E44-AA73-4EB2-8B3D-B96FBFEF96A4}.Release|Any CPU.ActiveCfg = Release|Any CPU
43-
{89018E44-AA73-4EB2-8B3D-B96FBFEF96A4}.Release|Any CPU.Build.0 = Release|Any CPU
4446
{65CF1A10-0E97-479E-9D8A-2C7741BF608C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
4547
{65CF1A10-0E97-479E-9D8A-2C7741BF608C}.Debug|Any CPU.Build.0 = Debug|Any CPU
4648
{65CF1A10-0E97-479E-9D8A-2C7741BF608C}.Release|Any CPU.ActiveCfg = Release|Any CPU
4749
{65CF1A10-0E97-479E-9D8A-2C7741BF608C}.Release|Any CPU.Build.0 = Release|Any CPU
48-
{9E156265-88E0-4506-8584-0644DCD9D205}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
49-
{9E156265-88E0-4506-8584-0644DCD9D205}.Debug|Any CPU.Build.0 = Debug|Any CPU
50-
{9E156265-88E0-4506-8584-0644DCD9D205}.Release|Any CPU.ActiveCfg = Release|Any CPU
51-
{9E156265-88E0-4506-8584-0644DCD9D205}.Release|Any CPU.Build.0 = Release|Any CPU
50+
{8BD1F907-921F-44BD-A06A-C77F58F82B8F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
51+
{8BD1F907-921F-44BD-A06A-C77F58F82B8F}.Debug|Any CPU.Build.0 = Debug|Any CPU
52+
{8BD1F907-921F-44BD-A06A-C77F58F82B8F}.Release|Any CPU.ActiveCfg = Release|Any CPU
53+
{8BD1F907-921F-44BD-A06A-C77F58F82B8F}.Release|Any CPU.Build.0 = Release|Any CPU
54+
{7C186FB4-EA93-470F-9552-CDF2A0C5F20D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
55+
{7C186FB4-EA93-470F-9552-CDF2A0C5F20D}.Debug|Any CPU.Build.0 = Debug|Any CPU
56+
{7C186FB4-EA93-470F-9552-CDF2A0C5F20D}.Release|Any CPU.ActiveCfg = Release|Any CPU
57+
{7C186FB4-EA93-470F-9552-CDF2A0C5F20D}.Release|Any CPU.Build.0 = Release|Any CPU
58+
{E0D0DB64-3A8F-46A8-95BE-C2A559041BD8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
59+
{E0D0DB64-3A8F-46A8-95BE-C2A559041BD8}.Debug|Any CPU.Build.0 = Debug|Any CPU
60+
{E0D0DB64-3A8F-46A8-95BE-C2A559041BD8}.Release|Any CPU.ActiveCfg = Release|Any CPU
61+
{E0D0DB64-3A8F-46A8-95BE-C2A559041BD8}.Release|Any CPU.Build.0 = Release|Any CPU
62+
{0847A55D-050B-4F99-A694-6D3F640B99EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
63+
{0847A55D-050B-4F99-A694-6D3F640B99EE}.Debug|Any CPU.Build.0 = Debug|Any CPU
64+
{0847A55D-050B-4F99-A694-6D3F640B99EE}.Release|Any CPU.ActiveCfg = Release|Any CPU
65+
{0847A55D-050B-4F99-A694-6D3F640B99EE}.Release|Any CPU.Build.0 = Release|Any CPU
66+
{EE267E71-A7A8-44C2-B0A1-7E093D702A5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
67+
{EE267E71-A7A8-44C2-B0A1-7E093D702A5F}.Debug|Any CPU.Build.0 = Debug|Any CPU
68+
{EE267E71-A7A8-44C2-B0A1-7E093D702A5F}.Release|Any CPU.ActiveCfg = Release|Any CPU
69+
{EE267E71-A7A8-44C2-B0A1-7E093D702A5F}.Release|Any CPU.Build.0 = Release|Any CPU
5270
EndGlobalSection
5371
GlobalSection(SolutionProperties) = preSolution
5472
HideSolutionNode = FALSE
5573
EndGlobalSection
5674
GlobalSection(NestedProjects) = preSolution
5775
{80583F01-4793-4A33-9945-09397C1FCBA8} = {9754E8AD-F544-4C44-939D-95164DC29671}
5876
{00CEA1AF-05CC-4D39-9118-267683DEDCD1} = {E65756E9-13C3-4F24-8818-A6CD2E5E99D8}
59-
{89018E44-AA73-4EB2-8B3D-B96FBFEF96A4} = {E65756E9-13C3-4F24-8818-A6CD2E5E99D8}
6077
{65CF1A10-0E97-479E-9D8A-2C7741BF608C} = {E65756E9-13C3-4F24-8818-A6CD2E5E99D8}
61-
{9E156265-88E0-4506-8584-0644DCD9D205} = {E65756E9-13C3-4F24-8818-A6CD2E5E99D8}
78+
{8BD1F907-921F-44BD-A06A-C77F58F82B8F} = {E65756E9-13C3-4F24-8818-A6CD2E5E99D8}
79+
{7C186FB4-EA93-470F-9552-CDF2A0C5F20D} = {E65756E9-13C3-4F24-8818-A6CD2E5E99D8}
80+
{E0D0DB64-3A8F-46A8-95BE-C2A559041BD8} = {E65756E9-13C3-4F24-8818-A6CD2E5E99D8}
81+
{0847A55D-050B-4F99-A694-6D3F640B99EE} = {E65756E9-13C3-4F24-8818-A6CD2E5E99D8}
82+
{EE267E71-A7A8-44C2-B0A1-7E093D702A5F} = {E65756E9-13C3-4F24-8818-A6CD2E5E99D8}
6283
EndGlobalSection
6384
EndGlobal

src/NetCoreStack.Proxy/DefaultProxyContentStreamProvider.cs

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
using NetCoreStack.Proxy.Extensions;
1+
using Microsoft.AspNetCore.WebUtilities;
2+
using NetCoreStack.Contracts;
3+
using NetCoreStack.Proxy.Extensions;
4+
using NetCoreStack.Proxy.Internal;
25
using Newtonsoft.Json;
36
using System;
7+
using System.Collections.Generic;
8+
using System.IO;
49
using System.Linq;
510
using System.Net.Http;
611
using System.Text;
@@ -10,28 +15,79 @@ namespace NetCoreStack.Proxy
1015
{
1116
public class DefaultProxyContentStreamProvider : IProxyContentStreamProvider
1217
{
18+
protected virtual byte[] Serialize(object value)
19+
{
20+
return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(value));
21+
}
22+
23+
protected virtual StringContent SerializeToString(object value)
24+
{
25+
return new StringContent(JsonConvert.SerializeObject(value), Encoding.UTF8, "application/json");
26+
}
27+
28+
protected virtual HttpContent CreateHttpContent(object value)
29+
{
30+
HttpContent content;
31+
32+
if (value.GetType() == typeof(byte[]))
33+
{
34+
content = new ByteArrayContent((value as byte[]) ?? new byte[0]);
35+
}
36+
else if (value.GetType() == typeof(Stream))
37+
{
38+
content = new StreamContent((value as Stream) ?? new MemoryStream());
39+
}
40+
else
41+
{
42+
content = new ByteArrayContent(Serialize(value));
43+
}
44+
45+
return content;
46+
}
47+
1348
public async Task CreateRequestContentAsync(RequestContext requestContext,
1449
HttpRequestMessage request,
15-
ProxyMethodDescriptor descriptor,
50+
ProxyMethodDescriptor descriptor,
1651
UriBuilder uriBuilder)
1752
{
53+
await Task.CompletedTask;
54+
1855
var argsDic = descriptor.Resolve(requestContext.Args);
56+
var argsCount = argsDic.Count;
57+
var keys = new List<string>(argsDic.Keys);
1958
if (descriptor.HttpMethod == HttpMethod.Post)
2059
{
21-
// TODO Streaming
22-
await Task.Run(() =>
23-
{
24-
if (argsDic.Count == 1)
25-
request.Content = new StringContent(JsonConvert.SerializeObject(argsDic.First().Value), Encoding.UTF8, "application/json");
26-
else
27-
request.Content = new StringContent(JsonConvert.SerializeObject(argsDic), Encoding.UTF8, "application/json");
28-
});
60+
if (argsCount == 1)
61+
request.Content = SerializeToString(argsDic.First().Value);
62+
else
63+
request.Content = SerializeToString(argsDic);
64+
65+
return;
2966
}
67+
else if(descriptor.HttpMethod == HttpMethod.Put)
68+
{
69+
if (argsCount == 1)
70+
{
71+
request.Content = SerializeToString(argsDic.First().Value);
72+
}
73+
else if(argsCount == 2)
74+
{
75+
var firstParameter = argsDic[keys[0]];
76+
var secondParameter = argsDic[keys[1]];
3077

31-
if (descriptor.HttpMethod == HttpMethod.Get)
78+
// PUT Request first parameter should be Id or Key
79+
if (firstParameter.GetType().IsPrimitive())
80+
{
81+
uriBuilder.Query += string.Format("&{0}={1}", keys[0], firstParameter);
82+
}
83+
84+
request.RequestUri = uriBuilder.Uri;
85+
request.Content = SerializeToString(secondParameter);
86+
}
87+
}
88+
if (descriptor.HttpMethod == HttpMethod.Get || descriptor.HttpMethod == HttpMethod.Delete)
3289
{
33-
uriBuilder = new UriBuilder(uriBuilder.Uri.ToQueryString(argsDic));
34-
request.RequestUri = uriBuilder.Uri;
90+
request.RequestUri = QueryStringResolver.Parse(uriBuilder, argsDic);
3591
}
3692
}
3793
}

src/NetCoreStack.Proxy/Extensions/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public static void AddNetCoreProxy(this IServiceCollection services,
4242
services.TryAdd(ServiceDescriptor.Singleton<IHeaderProvider, DefaultHeaderProvider>());
4343
services.TryAdd(ServiceDescriptor.Singleton<IProxyContentStreamProvider, DefaultProxyContentStreamProvider>());
4444
services.TryAdd(ServiceDescriptor.Singleton<IProxyEndpointManager, DefaultProxyEndpointManager>());
45+
4546
services.TryAddSingleton<RoundRobinManager>();
4647

4748
var proxyBuilderOptions = new ProxyBuilderOptions();

src/NetCoreStack.Proxy/Internal/DefaultProxyTypeManager.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ private IList<ProxyDescriptor> GetProxyDescriptors()
7272
{
7373
if (httpMethodAttribute is HttpPostMarkerAttribute)
7474
proxyMethodDescriptor.HttpMethod = HttpMethod.Post;
75+
else if(httpMethodAttribute is HttpPutMarkerAttribute)
76+
proxyMethodDescriptor.HttpMethod = HttpMethod.Put;
77+
else if (httpMethodAttribute is HttpDeleteMarkerAttribute)
78+
proxyMethodDescriptor.HttpMethod = HttpMethod.Delete;
7579
}
7680
else
7781
{
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using NetCoreStack.Contracts;
2+
using NetCoreStack.Proxy.Extensions;
3+
using System;
4+
using System.Collections;
5+
using System.Collections.Generic;
6+
using System.Reflection;
7+
8+
namespace NetCoreStack.Proxy.Internal
9+
{
10+
internal static class QueryStringResolver
11+
{
12+
internal static Uri Parse(UriBuilder uriBuilder, IDictionary<string, object> argsDic)
13+
{
14+
var referenceArgs = new Dictionary<string, object>();
15+
var keys = new List<string>(argsDic.Keys);
16+
17+
foreach (string key in keys)
18+
{
19+
if (argsDic[key] == null)
20+
continue;
21+
22+
var type = argsDic[key].GetType();
23+
24+
if (typeof(string) == type || type.IsPrimitive())
25+
continue;
26+
27+
referenceArgs.Add(key, argsDic[key]);
28+
29+
// remove reference type key
30+
argsDic.Remove(key);
31+
}
32+
33+
uriBuilder = new UriBuilder(uriBuilder.Uri.ToQueryString(argsDic));
34+
35+
// Resolve reference type args
36+
keys = new List<string>(referenceArgs.Keys);
37+
foreach (string key in keys)
38+
{
39+
var type = referenceArgs[key].GetType();
40+
41+
if (typeof(IEnumerable<Column>).IsAssignableFrom(type))
42+
{
43+
List<string> values = new List<string>();
44+
var data = referenceArgs[key] as IEnumerable<Column>;
45+
var index = 0;
46+
foreach (var item in data)
47+
{
48+
values.Add($"{key}[{index}][{nameof(Column.Data)}][_]={item.Data}");
49+
values.Add($"{key}[{index}][{nameof(Column.Data)}][{nameof(Column.Meta)}]={item.Meta}");
50+
values.Add($"{key}[{index}][{nameof(Column.Data)}][{nameof(Column.Composer)}]={item.Composer}");
51+
values.Add($"{key}[{index}][name]=");
52+
values.Add($"{key}[{index}][{nameof(Column.Searchable)}]={item.Searchable}");
53+
values.Add($"{key}[{index}][{nameof(Column.Orderable)}]={item.Orderable}");
54+
values.Add($"{key}[{index}][{nameof(Column.Search)}][value]={item.Search?.Value}");
55+
values.Add($"{key}[{index}][{nameof(Column.Search)}][regex]={item.Search?.IsRegex}");
56+
57+
index++;
58+
}
59+
60+
uriBuilder.Query += "&" + string.Join("&", values);
61+
continue;
62+
}
63+
64+
if (typeof(IEnumerable<OrderDescriptor>).IsAssignableFrom(type))
65+
{
66+
List<string> values = new List<string>();
67+
var data = referenceArgs[key] as IEnumerable<OrderDescriptor>;
68+
var index = 0;
69+
foreach (var item in data)
70+
{
71+
values.Add($"{key}[{index}][{nameof(Column)}]={item.ColumnIndex}");
72+
values.Add($"{key}[{index}][dir]={item.Direction}");
73+
index++;
74+
}
75+
76+
uriBuilder.Query += "&" + string.Join("&", values);
77+
continue;
78+
}
79+
80+
if (typeof(IEnumerable).IsAssignableFrom(type))
81+
{
82+
List<string> values = new List<string>();
83+
var data = referenceArgs[key] as IEnumerable;
84+
var index = 0;
85+
foreach (var item in data)
86+
{
87+
values.Add($"{key}[{index}]={item}");
88+
index++;
89+
}
90+
91+
uriBuilder.Query += "&" + string.Join("&", values);
92+
continue;
93+
}
94+
}
95+
96+
return uriBuilder.Uri;
97+
}
98+
}
99+
}

src/NetCoreStack.Proxy/NetCoreStack.Proxy.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
1616
<PackageReference Include="Microsoft.Extensions.Configuration" Version="1.1.1" />
1717
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.1" />
18-
<PackageReference Include="NetCoreStack.Contracts" Version="1.0.0-preview-173" />
18+
<PackageReference Include="NetCoreStack.Contracts" Version="1.0.0" />
1919
<PackageReference Include="NetCoreStack.DispatchProxyAsync" Version="1.1.0" />
2020
</ItemGroup>
2121

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using NetCoreStack.Data.Helpers;
3+
using NetCoreStack.Data.Interfaces;
4+
using NetCoreStack.Domain.Contracts;
5+
using System;
6+
using System.Linq;
7+
8+
namespace NetCoreStack.Api.Hosting
9+
{
10+
public static class BsonDataInitializer
11+
{
12+
const string imgUrl = "http://placehold.it/200x100";
13+
14+
public static void InitializeMusicStoreMongoDb(IServiceProvider serviceProvider)
15+
{
16+
using (var db = serviceProvider.GetService<IMongoDbDataContext>())
17+
{
18+
var albums = BsonSampleData.GetAlbums(imgUrl, BsonSampleData.Genres, BsonSampleData.Artists);
19+
if (!albums.Any())
20+
{
21+
db.MongoDatabase.DropCollection(BsonCollectionHelper.GetCollectionName<AlbumBson>());
22+
db.Collection<AlbumBson>().InsertMany(albums);
23+
}
24+
}
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)