Skip to content

Commit 2ebbbf6

Browse files
committed
simple uri builder, api route path, template keys
1 parent 085b799 commit 2ebbbf6

File tree

15 files changed

+111
-83
lines changed

15 files changed

+111
-83
lines changed

src/NetCoreStack.Proxy/DefaultProxyContentStreamProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ public DefaultProxyContentStreamProvider(IServiceProvider serviceProvider,
2323
public async Task CreateRequestContentAsync(RequestDescriptor requestContext,
2424
HttpRequestMessage request,
2525
ProxyMethodDescriptor descriptor,
26-
ProxyUriDefinition proxyUriDefinition)
26+
UriBuilder uriBuilder)
2727
{
2828
await Task.CompletedTask;
2929

3030
var httpMethod = descriptor.HttpMethod;
3131
var contentModelBinder = ContentBinderFactory.GetContentModelBinder(_serviceProvider, httpMethod, descriptor.ContentType);
3232

33-
var bindingContext = new ContentModelBindingContext(httpMethod, descriptor, proxyUriDefinition)
33+
var bindingContext = new ContentModelBindingContext(httpMethod, descriptor, uriBuilder)
3434
{
3535
ContentType = descriptor.ContentType,
3636
ModelContentResolver = ContentResolver,

src/NetCoreStack.Proxy/DefaultProxyEndpointManager.cs

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,62 @@ public DefaultProxyEndpointManager(RoundRobinManager roundRobinManager)
1212
RoundRobinManager = roundRobinManager;
1313
}
1414

15-
public ProxyUriDefinition CreateUriDefinition(ProxyMethodDescriptor methodDescriptor, string route, string regionKey, string targetMethodName)
15+
private string ConcatRoute(string route, string methodName)
1616
{
17-
var uriBuilder = RoundRobinManager.RoundRobinUri(regionKey);
18-
if (uriBuilder == null)
17+
string path = string.Empty;
18+
if (string.IsNullOrEmpty(route))
1919
{
20-
throw new ArgumentNullException(nameof(uriBuilder));
20+
path = methodName;
21+
}
22+
else
23+
{
24+
path = $"{route}/{methodName}";
2125
}
2226

23-
var uriDefinition = new ProxyUriDefinition(uriBuilder);
27+
return path;
28+
}
2429

25-
if (!string.IsNullOrEmpty(route))
30+
private void ResolveTemplate(UriBuilder uriBuilder, ProxyMethodDescriptor methodDescriptor, string route, string targetMethodName)
31+
{
32+
var path = uriBuilder.Path ?? string.Empty;
33+
if (methodDescriptor.RouteTemplate != null)
2634
{
27-
if (targetMethodName.ToLower() == HttpMethod.Get.Method.ToLower())
35+
if (methodDescriptor.RouteTemplate.Parameters.Count > 0)
2836
{
29-
var path = uriDefinition.UriBuilder.Path ?? string.Empty;
30-
path += $"{route}/";
31-
uriDefinition.UriBuilder.Path = path;
37+
var methodName = string.Join("/", methodDescriptor.TemplateKeys);
38+
path += ConcatRoute(route, methodName);
39+
uriBuilder.Path = path;
40+
return;
3241
}
33-
else
34-
{
35-
if (targetMethodName.StartsWith("/"))
36-
targetMethodName = targetMethodName.Substring(1);
42+
}
3743

38-
uriDefinition.ResolveTemplate(methodDescriptor, route, targetMethodName);
39-
}
44+
path += ConcatRoute(route, targetMethodName);
45+
uriBuilder.Path = path;
46+
}
47+
48+
public UriBuilder CreateUriBuilder(ProxyMethodDescriptor methodDescriptor, string route, string regionKey, string targetMethodName)
49+
{
50+
var uriBuilder = RoundRobinManager.RoundRobinUri(regionKey);
51+
if (uriBuilder == null)
52+
{
53+
throw new ArgumentNullException(nameof(uriBuilder));
54+
}
55+
56+
if (targetMethodName.ToLower() == HttpMethod.Get.Method.ToLower())
57+
{
58+
var path = uriBuilder.Path ?? string.Empty;
59+
path += string.IsNullOrEmpty(route) ? "" : $"{route}/";
60+
uriBuilder.Path = path;
61+
}
62+
else
63+
{
64+
if (targetMethodName.StartsWith("/"))
65+
targetMethodName = targetMethodName.Substring(1);
66+
67+
ResolveTemplate(uriBuilder, methodDescriptor, route, targetMethodName);
4068
}
4169

42-
return uriDefinition;
70+
return uriBuilder;
4371
}
4472
}
4573
}

src/NetCoreStack.Proxy/Extensions/ContentModelBindingContextExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ public static void TrySetContent(this ContentModelBindingContext bindingContext,
1616

1717
public static void TryUpdateUri(this ContentModelBindingContext bindingContext, Dictionary<string, string> dictionary)
1818
{
19-
if (bindingContext.UriDefinition != null)
19+
if (bindingContext.UriBuilder != null)
2020
{
2121
var uriBuilder = new UriBuilder(bindingContext.Uri.ToQueryString(dictionary));
22-
bindingContext.UriDefinition.UriBuilder = uriBuilder;
22+
bindingContext.UriBuilder = uriBuilder;
2323
}
2424
}
2525
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Net.Http;
1+
using System;
2+
using System.Net.Http;
23
using System.Threading.Tasks;
34

45
namespace NetCoreStack.Proxy
@@ -8,6 +9,6 @@ public interface IProxyContentStreamProvider
89
Task CreateRequestContentAsync(RequestDescriptor requestDescriptor,
910
HttpRequestMessage request,
1011
ProxyMethodDescriptor descriptor,
11-
ProxyUriDefinition proxyUriDefinition);
12+
UriBuilder uriBuilder);
1213
}
1314
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
namespace NetCoreStack.Proxy
1+
using System;
2+
3+
namespace NetCoreStack.Proxy
24
{
35
public interface IProxyEndpointManager
46
{
5-
ProxyUriDefinition CreateUriDefinition(ProxyMethodDescriptor methodDescriptor, string route, string regionKey, string targetMethodName);
7+
UriBuilder CreateUriBuilder(ProxyMethodDescriptor methodDescriptor, string route, string regionKey, string targetMethodName);
68
}
79
}

src/NetCoreStack.Proxy/Internal/DefaultProxyTypeManager.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ private IList<ProxyDescriptor> GetProxyDescriptors()
3838
throw new ProxyException($"Specify the \"{nameof(pathAttr.RegionKey)}\"!");
3939

4040
var route = proxyType.Name.GetApiRootPath(pathAttr.RouteTemplate);
41+
if (route.StartsWith("/"))
42+
route = route.Substring(1);
4143

4244
ProxyDescriptor descriptor = new ProxyDescriptor(proxyType, pathAttr.RegionKey, route);
4345

src/NetCoreStack.Proxy/Internal/ProxyManager.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,10 @@ public async Task<RequestContext> CreateRequestAsync(RequestDescriptor descripto
117117
if (methodDescriptor.MethodMarkerTemplate.HasValue())
118118
methodPath = methodDescriptor.MethodMarkerTemplate;
119119

120-
ProxyUriDefinition proxyUriDefinition = _endpointManager.CreateUriDefinition(methodDescriptor, proxyDescriptor.Route, regionKey, methodPath);
120+
UriBuilder uriBuilder = _endpointManager.CreateUriBuilder(methodDescriptor, proxyDescriptor.Route, regionKey, methodPath);
121121
TimeSpan? timeout = methodDescriptor.Timeout;
122-
123-
request.RequestUri = proxyUriDefinition.UriBuilder.Uri;
124-
await _streamProvider.CreateRequestContentAsync(descriptor, request, methodDescriptor, proxyUriDefinition);
122+
123+
await _streamProvider.CreateRequestContentAsync(descriptor, request, methodDescriptor, uriBuilder);
125124

126125
return new RequestContext(request,
127126
methodDescriptor,

src/NetCoreStack.Proxy/Types/ContentModelBindingContext.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ public bool HasAnyTemplateParameterKey
2424
return TemplateParameterKeys.Any();
2525
}
2626
}
27-
28-
public UriBuilder UriBuilder => UriDefinition.UriBuilder;
27+
2928
public Uri Uri => UriBuilder.Uri;
3029
public string MethodMarkerTemplate => MethodDescriptor.MethodMarkerTemplate;
3130
public List<TemplatePart> TemplateParts => MethodDescriptor.TemplateParts;
@@ -36,14 +35,14 @@ public bool HasAnyTemplateParameterKey
3635
public object[] Args { get; set; }
3736
public IModelContentResolver ModelContentResolver { get; set; }
3837
public HttpMethod HttpMethod { get; }
39-
public ProxyUriDefinition UriDefinition { get; }
38+
public UriBuilder UriBuilder { get; set; }
4039
public ProxyMethodDescriptor MethodDescriptor { get; }
4140

42-
public ContentModelBindingContext(HttpMethod httpMethod, ProxyMethodDescriptor methodDescriptor, ProxyUriDefinition proxyUriDefinition)
41+
public ContentModelBindingContext(HttpMethod httpMethod, ProxyMethodDescriptor methodDescriptor, UriBuilder uriBuilder)
4342
{
4443
HttpMethod = httpMethod ?? throw new ArgumentNullException(nameof(httpMethod));
4544
MethodDescriptor = methodDescriptor ?? throw new ArgumentNullException(nameof(methodDescriptor));
46-
UriDefinition = proxyUriDefinition ?? throw new ArgumentNullException(nameof(proxyUriDefinition));
45+
UriBuilder = uriBuilder ?? throw new ArgumentNullException(nameof(uriBuilder));
4746
}
4847
}
4948
}

src/NetCoreStack.Proxy/Types/ProxyUriDefinition.cs

Lines changed: 0 additions & 49 deletions
This file was deleted.

test/NetCoreStack.Proxy.Mvc.Hosting/Controllers/GuidelineController.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,5 +153,10 @@ public Task TaskActionBarSimpleXml(BarSimple model)
153153
{
154154
throw new NotImplementedException();
155155
}
156+
157+
public Task UploadAsync(FileProxyUploadContext context)
158+
{
159+
throw new NotImplementedException();
160+
}
156161
}
157162
}

0 commit comments

Comments
 (0)