Skip to content

Commit 75f5043

Browse files
committed
binding model, content resolvers
1 parent 11d4b31 commit 75f5043

16 files changed

+108
-37
lines changed

src/NetCoreStack.Proxy/Binders/HttpDeleteContentBinder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public override void BindContent(ContentModelBindingContext bindingContext)
1313
{
1414
ModelDictionaryResult result = bindingContext.GetResolvedContentResult();
1515
List<string> keys = result.Dictionary.Keys.ToList();
16-
EnsureTemplate(bindingContext.MethodMarkerTemplate, bindingContext.Args, bindingContext.UriDefinition, result.Dictionary, keys);
16+
EnsureTemplate(bindingContext.MethodMarkerTemplate, bindingContext.UriDefinition, result.Dictionary, keys);
1717

1818
bindingContext.TryUpdateUri(result.Dictionary);
1919
}

src/NetCoreStack.Proxy/Binders/HttpGetContentBinder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public override void BindContent(ContentModelBindingContext bindingContext)
1313
{
1414
ModelDictionaryResult result = bindingContext.GetResolvedContentResult();
1515
List<string> keys = result.Dictionary.Keys.ToList();
16-
EnsureTemplate(bindingContext.MethodMarkerTemplate, bindingContext.Args, bindingContext.UriDefinition, result.Dictionary, keys);
16+
EnsureTemplate(bindingContext.MethodMarkerTemplate, bindingContext.UriDefinition, result.Dictionary, keys);
1717

1818
bindingContext.TryUpdateUri(result.Dictionary);
1919
}

src/NetCoreStack.Proxy/Binders/HttpPutContentBinder.cs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Net.Http;
5-
using NetCoreStack.Contracts;
65

76
namespace NetCoreStack.Proxy
87
{
@@ -12,31 +11,29 @@ public class HttpPutContentBinder : BodyContentBinder
1211

1312
public override void BindContent(ContentModelBindingContext bindingContext)
1413
{
14+
var isMultiPartFormData = bindingContext.IsMultiPartFormData;
15+
var templateKeys = bindingContext.UriDefinition.TemplateKeys;
16+
17+
// bindingContext.Parameters.Select(p => p.PropertyName);
18+
19+
//if (bindingContext.ArgsLength == 1)
20+
//{
21+
22+
//}
23+
1524
ModelDictionaryResult result = bindingContext.GetResolvedContentResult();
1625
List<string> keys = result.Dictionary.Keys.ToList();
17-
EnsureTemplate(bindingContext.MethodMarkerTemplate, bindingContext.Args, bindingContext.UriDefinition, result.Dictionary, keys);
18-
if (bindingContext.ArgsLength == 1)
19-
{
20-
bindingContext.ContentResult = ContentModelBindingResult.Success(SerializeToString(bindingContext.Args[0]));
26+
EnsureTemplate(bindingContext.MethodMarkerTemplate, bindingContext.UriDefinition, result.Dictionary, keys);
27+
if (isMultiPartFormData)
28+
{
29+
var content = GetMultipartFormDataContent(result);
30+
bindingContext.ContentResult = ContentModelBindingResult.Success(content);
2131
return;
22-
// request.Content = SerializeToString(argsDic.First().Value);
2332
}
24-
else if (bindingContext.ArgsLength == 2)
25-
{
26-
var firstParameter = result.Dictionary[keys[0]];
27-
var secondParameter = result.Dictionary[keys[1]];
2833

29-
// PUT Request first parameter should be Id or Key
30-
if (firstParameter.GetType().IsPrimitive())
31-
{
32-
bindingContext.UriBuilder.Query += string.Format("&{0}={1}", keys[0], firstParameter);
33-
}
34-
35-
// request.RequestUri = uriBuilder.Uri;
36-
// request.Content = SerializeToString(secondParameter);
37-
bindingContext.ContentResult = ContentModelBindingResult.Success(SerializeToString(secondParameter));
38-
return;
39-
}
34+
// PUT Request first parameter should be Id or Key then will be removed from the result dictionary
35+
// The remaining parameters belong to the body
36+
bindingContext.ContentResult = ContentModelBindingResult.Success(SerializeToString(result.Dictionary));
4037
}
4138
}
4239
}

src/NetCoreStack.Proxy/DefaultModelContentResolver.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,16 @@ private void ResolveInternal(ProxyModelMetadata modelMetadata, ModelDictionaryRe
176176
{
177177
var key = isTopLevelObject ? string.Empty : modelMetadata.PropertyName;
178178
if (!string.IsNullOrEmpty(prefix))
179-
key = $"{prefix}.{key}";
179+
{
180+
if (string.IsNullOrEmpty(key))
181+
{
182+
key = prefix;
183+
}
184+
else
185+
{
186+
key = $"{prefix}.{key}";
187+
}
188+
}
180189

181190
if (modelMetadata.IsFormFile || (modelMetadata.IsEnumerableType && modelMetadata.ElementType.IsFormFile))
182191
{

src/NetCoreStack.Proxy/DefaultProxyContentStreamProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public async Task CreateRequestContentAsync(RequestDescriptor requestContext,
2828

2929
var bindingContext = new ContentModelBindingContext
3030
{
31+
HttpMethod = httpMethod,
3132
UriDefinition = proxyUriDefinition,
3233
MethodMarkerTemplate = descriptor.MethodMarkerTemplate,
3334
IsMultiPartFormData = isMultiPartFormData,

src/NetCoreStack.Proxy/Internal/DefaultProxyTypeManager.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ private IList<ProxyDescriptor> GetProxyDescriptors()
6767
var httpMethodAttribute = method.GetCustomAttributes(inherit: true)
6868
.OfType<HttpMethodMarkerAttribute>().FirstOrDefault();
6969

70+
var httpHeaders = method.GetCustomAttribute<HttpHeadersAttribute>();
71+
if (httpHeaders != null && httpHeaders.Headers != null)
72+
{
73+
foreach (var header in httpHeaders.Headers)
74+
{
75+
var token = header.Split(':');
76+
if (token.Length > 1)
77+
{
78+
proxyMethodDescriptor.Headers[token[0].Trim()] = token[1].Trim();
79+
}
80+
}
81+
}
82+
7083
if (httpMethodAttribute != null)
7184
{
7285
if (httpMethodAttribute.Template.HasValue())
@@ -88,7 +101,6 @@ private IList<ProxyDescriptor> GetProxyDescriptors()
88101
}
89102

90103
bool isMultipartFormData = false;
91-
proxyMethodDescriptor.Parameters = new List<ProxyModelMetadata>();
92104
foreach (var parameter in method.GetParameters())
93105
{
94106
var modelMetadata = MetadataProvider.GetMetadataForParameter(parameter);

src/NetCoreStack.Proxy/Internal/ProxyManager.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,25 @@ public HttpClient HttpClient
8181
public bool HasFilter { get; }
8282
public List<IProxyRequestFilter> RequestFilters { get; }
8383

84-
private HttpRequestMessage CreateHttpRequest()
84+
private HttpRequestMessage CreateHttpRequest(ProxyMethodDescriptor methodDescriptor)
8585
{
8686
HttpRequestMessage requestMessage = new HttpRequestMessage();
8787

8888
foreach (KeyValuePair<string, string> entry in _headerProvider.Headers)
8989
{
9090
requestMessage.Headers.Add(entry.Key, entry.Value);
91-
}
91+
}
92+
93+
foreach (KeyValuePair<string, string> entry in methodDescriptor.Headers)
94+
{
95+
requestMessage.Headers.Add(entry.Key, entry.Value);
96+
}
9297

9398
return requestMessage;
9499
}
95100

96101
public async Task<RequestContext> CreateRequestAsync(RequestDescriptor descriptor)
97102
{
98-
HttpRequestMessage request = CreateHttpRequest();
99103
var proxyDescriptor = _typeManager.ProxyDescriptors.FirstOrDefault(x => x.ProxyType == descriptor.ProxyType);
100104

101105
if (proxyDescriptor == null)
@@ -107,6 +111,7 @@ public async Task<RequestContext> CreateRequestAsync(RequestDescriptor descripto
107111
if (!proxyDescriptor.Methods.TryGetValue(descriptor.TargetMethod, out methodDescriptor))
108112
throw new ArgumentOutOfRangeException("Method (Action) info could not be found!");
109113

114+
HttpRequestMessage request = CreateHttpRequest(methodDescriptor);
110115
request.Method = methodDescriptor.HttpMethod;
111116
var methodPath = descriptor.TargetMethod.Name;
112117
if (methodDescriptor.MethodMarkerTemplate.HasValue())

src/NetCoreStack.Proxy/Types/ContentModelBinder.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ namespace NetCoreStack.Proxy
66
public abstract class ContentModelBinder : IContentModelBinder
77
{
88
protected virtual void EnsureTemplate(string methodMarkerTemplate,
9-
object[] args,
109
ProxyUriDefinition proxyUriDefinition,
1110
Dictionary<string, string> argsDic,
1211
List<string> keys)
@@ -18,8 +17,11 @@ protected virtual void EnsureTemplate(string methodMarkerTemplate,
1817
for (int i = 0; i < proxyUriDefinition.ParameterParts.Count; i++)
1918
{
2019
var key = keys[i];
21-
proxyUriDefinition.UriBuilder.Path += ($"/{WebUtility.UrlEncode(args[i]?.ToString())}");
22-
argsDic.Remove(key);
20+
if(argsDic.TryGetValue(key, out string value))
21+
{
22+
proxyUriDefinition.UriBuilder.Path += ($"/{WebUtility.UrlEncode(value)}");
23+
argsDic.Remove(key);
24+
}
2325
}
2426
}
2527
}

src/NetCoreStack.Proxy/Types/ContentModelBindingContext.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Net.Http;
34

45
namespace NetCoreStack.Proxy
56
{
@@ -22,5 +23,6 @@ public int ArgsLength
2223
public List<ProxyModelMetadata> Parameters { get; set; }
2324
public ContentModelBindingResult ContentResult { get; set; }
2425
public ProxyUriDefinition UriDefinition { get; set; }
26+
public HttpMethod HttpMethod { get; set; }
2527
}
26-
}
28+
}

src/NetCoreStack.Proxy/Types/ProxyMethodDescriptor.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,25 @@ public class ProxyMethodDescriptor
2323

2424
public bool IsMultiPartFormData { get; set; }
2525

26-
public List<ProxyModelMetadata> Parameters { get; set; }
26+
public List<ProxyModelMetadata> Parameters { get; }
2727

2828
public bool IsVoidReturn { get; }
2929

3030
public bool IsTaskReturn { get; }
3131

3232
public bool IsGenericTaskReturn { get; }
3333

34+
public Dictionary<string, string> Headers { get; }
35+
3436
public ProxyMethodDescriptor(MethodInfo methodInfo)
3537
{
3638
MethodInfo = methodInfo;
3739
ReturnType = methodInfo.ReturnType;
3840
IsVoidReturn = ReturnType == typeof(void);
3941
IsTaskReturn = ReturnType.IsAssignableFrom(typeof(Task)) ? true : false;
4042
IsGenericTaskReturn = ReturnType.IsGenericTask() ? true : false;
43+
Headers = new Dictionary<string, string>(StringComparer.Ordinal);
44+
Parameters = new List<ProxyModelMetadata>();
4145

4246
if (IsGenericTaskReturn)
4347
{

0 commit comments

Comments
 (0)