Skip to content

Commit 58b9f42

Browse files
committed
byte array result content stream, template key check
1 parent 2ebbbf6 commit 58b9f42

File tree

7 files changed

+40
-2
lines changed

7 files changed

+40
-2
lines changed

src/NetCoreStack.Proxy/Binders/ContentModelBinder.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Linq;
1+
using System;
2+
using System.Linq;
23
using System.Net;
34
using System.Net.Http;
45

@@ -22,6 +23,11 @@ protected virtual EnsureTemplateResult EnsureTemplate(ContentModelBindingContext
2223
{
2324
var keyParameter = bindingContext.TemplateParameterKeys[i];
2425
var keyModelMetadata = bindingContext.Parameters.FirstOrDefault(x => x.PropertyName == keyParameter);
26+
if (keyModelMetadata == null)
27+
{
28+
throw new ArgumentOutOfRangeException("Key parameter name does not match the template key. Please check template key(s) of the method.");
29+
}
30+
2531
var value = bindingContext.ModelContentResolver.ResolveParameter(keyModelMetadata, bindingContext.Args[i], false);
2632
bindingContext.UriBuilder.Path += ($"/{WebUtility.UrlEncode(value)}");
2733
parameterOffset++;

src/NetCoreStack.Proxy/Internal/ProxyResultExecutor.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ public static async Task<ResponseContext> ExecuteAsync(HttpResponseMessage respo
3333
throw new ProxyException(message, null);
3434
}
3535

36+
if (methodDescriptor.IsByteArrayReturn)
37+
{
38+
context.Value = await response.Content.ReadAsByteArrayAsync();
39+
return context;
40+
}
41+
3642
if (methodDescriptor.IsVoidReturn)
3743
return context;
3844

src/NetCoreStack.Proxy/Types/ProxyMethodDescriptor.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class ProxyMethodDescriptor
4444
public bool HasAnyTemplateParameterKey => TemplateParameterKeys.Any();
4545

4646
public Dictionary<string, string> Headers { get; }
47+
public bool IsByteArrayReturn { get; }
4748

4849
public ProxyMethodDescriptor(MethodInfo methodInfo)
4950
{
@@ -61,6 +62,17 @@ public ProxyMethodDescriptor(MethodInfo methodInfo)
6162
if (IsGenericTaskReturn)
6263
{
6364
UnderlyingReturnType = ReturnType.GetGenericArguments()[0];
65+
if (typeof(byte[]).IsAssignableFrom(UnderlyingReturnType))
66+
{
67+
IsByteArrayReturn = true;
68+
}
69+
}
70+
else
71+
{
72+
if (typeof(byte[]).IsAssignableFrom(ReturnType))
73+
{
74+
IsByteArrayReturn = true;
75+
}
6476
}
6577
}
6678
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,10 @@ public Task UploadAsync(FileProxyUploadContext context)
158158
{
159159
throw new NotImplementedException();
160160
}
161+
162+
public Task<byte[]> GetFileAsync(string id, string fileName)
163+
{
164+
throw new NotImplementedException();
165+
}
161166
}
162167
}

test/NetCoreStack.Proxy.Test.Contracts/IFileProxyApi.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ namespace NetCoreStack.Proxy.Test.Contracts
66
[ApiRoute("/", regionKey: "Main")]
77
public interface IFileProxyApi : IApiContract
88
{
9-
Task GetFilesAsync(params string[] fileNames);
9+
[HttpGetMarker(Template = "proxy-fs/{id}/{filename}")]
10+
Task<byte[]> GetFileAsync(string id, string filename);
1011

1112
[HttpPostMarker(Template = "upload")]
1213
Task UploadAsync(FileProxyUploadContext context);

test/NetCoreStack.Proxy.Tests/ProxyCreationTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ public async Task GetCollectionStreamTest()
144144
Assert.True(collection.Data.Count() == 5);
145145
}
146146

147+
[Fact]
148+
public async Task GetSingleFileWithKeyTemplate()
149+
{
150+
var guidelineApi = Resolver.GetService<IFileProxyApi>();
151+
byte[] bytes = await guidelineApi.GetFileAsync("5a6ee0791653ff2348f1cd32", "pdf-sample.pdf");
152+
Assert.True(true);
153+
}
154+
147155
[Fact]
148156
public async Task TaskSingleFileModel()
149157
{
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)