Modified ClientGenerator to support responses as a parameter input. Improved streams support. Bumped package version to 1.4.8

This commit is contained in:
MattMo 2023-03-31 09:19:22 -07:00
parent b9e1f2ca7d
commit 6a7f6ce096
4 changed files with 27 additions and 9 deletions

View File

@ -215,7 +215,7 @@ namespace MontoyaTech.Rest.Net.Example
} }
[RouteGroup("Stream")] [RouteGroup("Stream")]
[RouteResponse(typeof(MemoryStream))] [RouteResponse(typeof(FileStream), Parameter = true)]
public static HttpListenerResponse Download(HttpListenerContext context) public static HttpListenerResponse Download(HttpListenerContext context)
{ {
return context.Response.WithStatus(HttpStatusCode.OK).WithText("Hello world"); return context.Response.WithStatus(HttpStatusCode.OK).WithText("Hello world");

View File

@ -17,7 +17,7 @@
<AssemblyName>MontoyaTech.Rest.Net</AssemblyName> <AssemblyName>MontoyaTech.Rest.Net</AssemblyName>
<RootNamespace>MontoyaTech.Rest.Net</RootNamespace> <RootNamespace>MontoyaTech.Rest.Net</RootNamespace>
<GenerateDocumentationFile>True</GenerateDocumentationFile> <GenerateDocumentationFile>True</GenerateDocumentationFile>
<Version>1.4.7</Version> <Version>1.4.8</Version>
<PackageReleaseNotes></PackageReleaseNotes> <PackageReleaseNotes></PackageReleaseNotes>
<PackageIcon>Logo_Symbol_Black_Outline.png</PackageIcon> <PackageIcon>Logo_Symbol_Black_Outline.png</PackageIcon>
</PropertyGroup> </PropertyGroup>

View File

@ -342,6 +342,12 @@ namespace MontoyaTech.Rest.Net
writer.Write(this.GetTypeFullyResolvedName(routeRequest.RequestType)).Write(" request"); writer.Write(this.GetTypeFullyResolvedName(routeRequest.RequestType)).Write(" request");
} }
if (routeResponse != null && routeResponse.Parameter)
{
writer.WriteSeparator();
writer.Write(this.GetTypeFullyResolvedName(routeResponse.ResponseType)).Write(" input");
}
writer.WriteLine(")").WriteLine("{").Indent(); writer.WriteLine(")").WriteLine("{").Indent();
//Generate the message code //Generate the message code
@ -433,24 +439,31 @@ namespace MontoyaTech.Rest.Net
//Generate the response code //Generate the response code
if (this.StaticCode) if (this.StaticCode)
writer.WriteBreak().WriteLine($"var response = {this.ClientName}.HttpClient.Send(message);"); writer.WriteBreak().WriteLine($"var response = {this.ClientName}.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);");
else else
writer.WriteBreak().WriteLine("var response = this.Client.HttpClient.Send(message);"); writer.WriteBreak().WriteLine("var response = this.Client.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);");
//Handle the response //Handle the response
if (routeResponse != null) if (routeResponse != null)
{ {
writer.WriteBreak().WriteLine("if (response.IsSuccessStatusCode)").WriteLine("{").Indent(); writer.WriteBreak().WriteLine("if (response.IsSuccessStatusCode)").WriteLine("{").Indent();
if (routeResponse.ResponseType.IsEquivalentTo(typeof(MemoryStream))) if (routeResponse.ResponseType.IsAssignableTo(typeof(Stream)))
{ {
writer.WriteBreak().WriteLine($"var stream = new {this.GetTypeFullyResolvedName(routeResponse.ResponseType)}();"); if (routeResponse.Parameter)
{
writer.WriteBreak().WriteLine("response.Content.CopyToAsync(input).GetAwaiter().GetResult();");
writer.WriteBreak().WriteLine("response.Content.ReadAsStream().CopyTo(stream);"); writer.WriteBreak().WriteLine("return input;");
}
else
{
writer.WriteBreak().WriteLine($"var stream = new {this.GetTypeFullyResolvedName(routeResponse.ResponseType)}();");
writer.WriteBreak().WriteLine("stream.Seek(0, System.IO.SeekOrigin.Begin);"); writer.WriteBreak().WriteLine("response.Content.CopyToAsync(stream).GetAwaiter().GetResult();");
writer.WriteBreak().WriteLine("return stream;"); writer.WriteBreak().WriteLine("return stream;");
}
} }
else else
{ {

View File

@ -22,6 +22,11 @@ namespace MontoyaTech.Rest.Net
/// </summary> /// </summary>
public bool Json = true; public bool Json = true;
/// <summary>
/// Whether or not the response is passed as a parameter to the route function.
/// </summary>
public bool Parameter = false;
/// <summary> /// <summary>
/// Creates a default route response. /// Creates a default route response.
/// </summary> /// </summary>