Bumped package version to 1.4.7. Added support for Stream requests and MemoryStream responses.
This commit is contained in:
parent
fe99ba9b9d
commit
cf477522c0
@ -152,7 +152,7 @@ public class StaticClient
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Signup(User request)
|
public static void Signup(UserDto request)
|
||||||
{
|
{
|
||||||
var message = new HttpRequestMessage(HttpMethod.Post, $"{StaticClient.BaseUrl}/auth/signup");
|
var message = new HttpRequestMessage(HttpMethod.Post, $"{StaticClient.BaseUrl}/auth/signup");
|
||||||
|
|
||||||
@ -164,7 +164,7 @@ public class StaticClient
|
|||||||
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static User Get()
|
public static UserDto Get()
|
||||||
{
|
{
|
||||||
var message = new HttpRequestMessage(HttpMethod.Get, $"{StaticClient.BaseUrl}/auth");
|
var message = new HttpRequestMessage(HttpMethod.Get, $"{StaticClient.BaseUrl}/auth");
|
||||||
|
|
||||||
@ -177,7 +177,7 @@ public class StaticClient
|
|||||||
if (string.IsNullOrEmpty(content))
|
if (string.IsNullOrEmpty(content))
|
||||||
return default;
|
return default;
|
||||||
|
|
||||||
return JsonConvert.DeserializeObject<User>(content);
|
return JsonConvert.DeserializeObject<UserDto>(content);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -207,13 +207,54 @@ public class StaticClient
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class Stream
|
||||||
|
{
|
||||||
|
public static void Upload(System.IO.MemoryStream request)
|
||||||
|
{
|
||||||
|
var message = new HttpRequestMessage(HttpMethod.Post, $"{StaticClient.BaseUrl}/upload");
|
||||||
|
|
||||||
|
request.Seek(0, System.IO.SeekOrigin.Begin);
|
||||||
|
|
||||||
|
message.Content = new StreamContent(request);
|
||||||
|
|
||||||
|
var response = StaticClient.HttpClient.Send(message);
|
||||||
|
|
||||||
|
if (!response.IsSuccessStatusCode)
|
||||||
|
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static System.IO.MemoryStream Download()
|
||||||
|
{
|
||||||
|
var message = new HttpRequestMessage(HttpMethod.Get, $"{StaticClient.BaseUrl}/download");
|
||||||
|
|
||||||
|
var response = StaticClient.HttpClient.Send(message);
|
||||||
|
|
||||||
|
if (response.IsSuccessStatusCode)
|
||||||
|
{
|
||||||
|
var stream = new System.IO.MemoryStream();
|
||||||
|
|
||||||
|
response.Content.ReadAsStream().CopyTo(stream);
|
||||||
|
|
||||||
|
stream.Seek(0, System.IO.SeekOrigin.Begin);
|
||||||
|
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public class IncludedType
|
public class IncludedType
|
||||||
{
|
{
|
||||||
public int Test;
|
public int Test;
|
||||||
}
|
}
|
||||||
|
|
||||||
public class User : BaseUser
|
public class UserDto : BaseUser
|
||||||
{
|
{
|
||||||
|
public System.PlatformID MachineType;
|
||||||
|
|
||||||
public string Name;
|
public string Name;
|
||||||
|
|
||||||
public System.Collections.Generic.List<string> List;
|
public System.Collections.Generic.List<string> List;
|
||||||
|
@ -17,6 +17,8 @@ public class Client
|
|||||||
|
|
||||||
public AuthApi Auth;
|
public AuthApi Auth;
|
||||||
|
|
||||||
|
public StreamApi Stream;
|
||||||
|
|
||||||
public Client(string baseUrl)
|
public Client(string baseUrl)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(baseUrl))
|
if (string.IsNullOrWhiteSpace(baseUrl))
|
||||||
@ -48,6 +50,8 @@ public class Client
|
|||||||
this.Test = new TestApi(this);
|
this.Test = new TestApi(this);
|
||||||
|
|
||||||
this.Auth = new AuthApi(this);
|
this.Auth = new AuthApi(this);
|
||||||
|
|
||||||
|
this.Stream = new StreamApi(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class TestApi
|
public class TestApi
|
||||||
@ -174,7 +178,7 @@ public class Client
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Signup(User request)
|
public void Signup(UserDto request)
|
||||||
{
|
{
|
||||||
var message = new HttpRequestMessage(HttpMethod.Post, $"{this.Client.BaseUrl}/auth/signup");
|
var message = new HttpRequestMessage(HttpMethod.Post, $"{this.Client.BaseUrl}/auth/signup");
|
||||||
|
|
||||||
@ -186,7 +190,7 @@ public class Client
|
|||||||
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
public User Get()
|
public UserDto Get()
|
||||||
{
|
{
|
||||||
var message = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/auth");
|
var message = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/auth");
|
||||||
|
|
||||||
@ -199,7 +203,7 @@ public class Client
|
|||||||
if (string.IsNullOrEmpty(content))
|
if (string.IsNullOrEmpty(content))
|
||||||
return default;
|
return default;
|
||||||
|
|
||||||
return JsonConvert.DeserializeObject<User>(content);
|
return JsonConvert.DeserializeObject<UserDto>(content);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -229,13 +233,61 @@ public class Client
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class StreamApi
|
||||||
|
{
|
||||||
|
public Client Client;
|
||||||
|
|
||||||
|
public StreamApi(Client client)
|
||||||
|
{
|
||||||
|
this.Client = client;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Upload(System.IO.MemoryStream request)
|
||||||
|
{
|
||||||
|
var message = new HttpRequestMessage(HttpMethod.Post, $"{this.Client.BaseUrl}/upload");
|
||||||
|
|
||||||
|
request.Seek(0, System.IO.SeekOrigin.Begin);
|
||||||
|
|
||||||
|
message.Content = new StreamContent(request);
|
||||||
|
|
||||||
|
var response = this.Client.HttpClient.Send(message);
|
||||||
|
|
||||||
|
if (!response.IsSuccessStatusCode)
|
||||||
|
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
public System.IO.MemoryStream Download()
|
||||||
|
{
|
||||||
|
var message = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/download");
|
||||||
|
|
||||||
|
var response = this.Client.HttpClient.Send(message);
|
||||||
|
|
||||||
|
if (response.IsSuccessStatusCode)
|
||||||
|
{
|
||||||
|
var stream = new System.IO.MemoryStream();
|
||||||
|
|
||||||
|
response.Content.ReadAsStream().CopyTo(stream);
|
||||||
|
|
||||||
|
stream.Seek(0, System.IO.SeekOrigin.Begin);
|
||||||
|
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public class IncludedType
|
public class IncludedType
|
||||||
{
|
{
|
||||||
public int Test;
|
public int Test;
|
||||||
}
|
}
|
||||||
|
|
||||||
public class User : BaseUser
|
public class UserDto : BaseUser
|
||||||
{
|
{
|
||||||
|
public System.PlatformID MachineType;
|
||||||
|
|
||||||
public string Name;
|
public string Name;
|
||||||
|
|
||||||
public System.Collections.Generic.List<string> List;
|
public System.Collections.Generic.List<string> List;
|
||||||
|
@ -75,7 +75,9 @@ namespace MontoyaTech.Rest.Net.Example
|
|||||||
new Route<string>(HttpRequestMethod.Get, "/auth/{username}", Exists),
|
new Route<string>(HttpRequestMethod.Get, "/auth/{username}", Exists),
|
||||||
new Route(HttpRequestMethod.Post, "/auth/signup", Signup),
|
new Route(HttpRequestMethod.Post, "/auth/signup", Signup),
|
||||||
new Route(HttpRequestMethod.Get, "/auth/", Json),
|
new Route(HttpRequestMethod.Get, "/auth/", Json),
|
||||||
new Route(HttpRequestMethod.Get, "/auth/role", GetRole)
|
new Route(HttpRequestMethod.Get, "/auth/role", GetRole),
|
||||||
|
new Route(HttpRequestMethod.Post, "/upload", Upload),
|
||||||
|
new Route(HttpRequestMethod.Get, "/download", Download)
|
||||||
);
|
);
|
||||||
|
|
||||||
string code = listener.GenerateCSharpClient();
|
string code = listener.GenerateCSharpClient();
|
||||||
@ -110,6 +112,24 @@ namespace MontoyaTech.Rest.Net.Example
|
|||||||
|
|
||||||
Console.WriteLine($"Rest api server running at {listener.BaseUrl}");
|
Console.WriteLine($"Rest api server running at {listener.BaseUrl}");
|
||||||
|
|
||||||
|
StaticClient.Init(listener.BaseUrl);
|
||||||
|
|
||||||
|
using (var stream = new MemoryStream())
|
||||||
|
{
|
||||||
|
var bytes = Encoding.UTF8.GetBytes("hello world!");
|
||||||
|
|
||||||
|
stream.Write(bytes, 0, bytes.Length);
|
||||||
|
|
||||||
|
StaticClient.Stream.Upload(stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
using (var stream = StaticClient.Stream.Download())
|
||||||
|
{
|
||||||
|
var str = Encoding.UTF8.GetString(stream.ToArray());
|
||||||
|
|
||||||
|
Console.WriteLine("Download output:" + str);
|
||||||
|
}
|
||||||
|
|
||||||
listener.Block();
|
listener.Block();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -182,5 +202,23 @@ namespace MontoyaTech.Rest.Net.Example
|
|||||||
{
|
{
|
||||||
return context.Response.WithStatus(HttpStatusCode.OK).WithJson(new User("Rest.Net"));
|
return context.Response.WithStatus(HttpStatusCode.OK).WithJson(new User("Rest.Net"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[RouteGroup("Stream")]
|
||||||
|
[RouteRequest(typeof(MemoryStream))]
|
||||||
|
public static HttpListenerResponse Upload(HttpListenerContext context)
|
||||||
|
{
|
||||||
|
var content = context.Request.ReadAsString();
|
||||||
|
|
||||||
|
Console.WriteLine("Uploaded:" + content);
|
||||||
|
|
||||||
|
return context.Response.WithStatus(HttpStatusCode.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
[RouteGroup("Stream")]
|
||||||
|
[RouteResponse(typeof(MemoryStream))]
|
||||||
|
public static HttpListenerResponse Download(HttpListenerContext context)
|
||||||
|
{
|
||||||
|
return context.Response.WithStatus(HttpStatusCode.OK).WithText("Hello world");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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.6</Version>
|
<Version>1.4.7</Version>
|
||||||
<PackageReleaseNotes></PackageReleaseNotes>
|
<PackageReleaseNotes></PackageReleaseNotes>
|
||||||
<PackageIcon>Logo_Symbol_Black_Outline.png</PackageIcon>
|
<PackageIcon>Logo_Symbol_Black_Outline.png</PackageIcon>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
@ -3,8 +3,10 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.IO;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Xml.Linq;
|
using System.Xml.Linq;
|
||||||
|
using System.IO.Pipes;
|
||||||
|
|
||||||
namespace MontoyaTech.Rest.Net
|
namespace MontoyaTech.Rest.Net
|
||||||
{
|
{
|
||||||
@ -413,10 +415,20 @@ namespace MontoyaTech.Rest.Net
|
|||||||
//Add the request content if any.
|
//Add the request content if any.
|
||||||
if (routeRequest != null)
|
if (routeRequest != null)
|
||||||
{
|
{
|
||||||
if (routeRequest.Json)
|
if (routeRequest.RequestType.IsAssignableTo(typeof(Stream)))
|
||||||
|
{
|
||||||
|
writer.WriteBreak().WriteLine("request.Seek(0, System.IO.SeekOrigin.Begin);");
|
||||||
|
|
||||||
|
writer.WriteBreak().WriteLine("message.Content = new StreamContent(request);");
|
||||||
|
}
|
||||||
|
else if (routeRequest.Json)
|
||||||
|
{
|
||||||
writer.WriteBreak().WriteLine("message.Content = new StringContent(JsonConvert.SerializeObject(request));");
|
writer.WriteBreak().WriteLine("message.Content = new StringContent(JsonConvert.SerializeObject(request));");
|
||||||
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
writer.WriteBreak().WriteLine("message.Content = new StringContent(request.ToString());");
|
writer.WriteBreak().WriteLine("message.Content = new StringContent(request.ToString());");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Generate the response code
|
//Generate the response code
|
||||||
@ -430,80 +442,93 @@ namespace MontoyaTech.Rest.Net
|
|||||||
{
|
{
|
||||||
writer.WriteBreak().WriteLine("if (response.IsSuccessStatusCode)").WriteLine("{").Indent();
|
writer.WriteBreak().WriteLine("if (response.IsSuccessStatusCode)").WriteLine("{").Indent();
|
||||||
|
|
||||||
writer.WriteBreak().WriteLine("var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();");
|
if (routeResponse.ResponseType.IsEquivalentTo(typeof(MemoryStream)))
|
||||||
|
|
||||||
writer.WriteBreak().WriteLine("if (string.IsNullOrEmpty(content))").Indent().WriteLine("return default;").Outdent();
|
|
||||||
|
|
||||||
if (routeResponse.Json)
|
|
||||||
{
|
{
|
||||||
writer.WriteBreak().WriteLine($"return JsonConvert.DeserializeObject<{this.GetTypeFullyResolvedName(routeResponse.ResponseType)}>(content);");
|
writer.WriteBreak().WriteLine($"var stream = new {this.GetTypeFullyResolvedName(routeResponse.ResponseType)}();");
|
||||||
|
|
||||||
|
writer.WriteBreak().WriteLine("response.Content.ReadAsStream().CopyTo(stream);");
|
||||||
|
|
||||||
|
writer.WriteBreak().WriteLine("stream.Seek(0, System.IO.SeekOrigin.Begin);");
|
||||||
|
|
||||||
|
writer.WriteBreak().WriteLine("return stream;");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
switch (Type.GetTypeCode(routeResponse.ResponseType))
|
writer.WriteBreak().WriteLine("var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();");
|
||||||
|
|
||||||
|
writer.WriteBreak().WriteLine("if (string.IsNullOrEmpty(content))").Indent().WriteLine("return default;").Outdent();
|
||||||
|
|
||||||
|
if (routeResponse.Json)
|
||||||
{
|
{
|
||||||
case TypeCode.Boolean:
|
writer.WriteBreak().WriteLine($"return JsonConvert.DeserializeObject<{this.GetTypeFullyResolvedName(routeResponse.ResponseType)}>(content);");
|
||||||
writer.WriteBreak().WriteLine("return bool.Parse(content);");
|
}
|
||||||
break;
|
else
|
||||||
|
{
|
||||||
|
switch (Type.GetTypeCode(routeResponse.ResponseType))
|
||||||
|
{
|
||||||
|
case TypeCode.Boolean:
|
||||||
|
writer.WriteBreak().WriteLine("return bool.Parse(content);");
|
||||||
|
break;
|
||||||
|
|
||||||
case TypeCode.Byte:
|
case TypeCode.Byte:
|
||||||
writer.WriteBreak().WriteLine("return byte.Parse(content);");
|
writer.WriteBreak().WriteLine("return byte.Parse(content);");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case TypeCode.Char:
|
case TypeCode.Char:
|
||||||
writer.WriteBreak().WriteLine("return content[0];");
|
writer.WriteBreak().WriteLine("return content[0];");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case TypeCode.DateTime:
|
case TypeCode.DateTime:
|
||||||
writer.WriteBreak().WriteLine("return DateTime.Parse(content);");
|
writer.WriteBreak().WriteLine("return DateTime.Parse(content);");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case TypeCode.Decimal:
|
case TypeCode.Decimal:
|
||||||
writer.WriteBreak().WriteLine("return decimal.Parse(content);");
|
writer.WriteBreak().WriteLine("return decimal.Parse(content);");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case TypeCode.Double:
|
case TypeCode.Double:
|
||||||
writer.WriteBreak().WriteLine("return double.Parse(content);");
|
writer.WriteBreak().WriteLine("return double.Parse(content);");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case TypeCode.Int16:
|
case TypeCode.Int16:
|
||||||
writer.WriteBreak().WriteLine("return short.Parse(content);");
|
writer.WriteBreak().WriteLine("return short.Parse(content);");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case TypeCode.Int32:
|
case TypeCode.Int32:
|
||||||
writer.WriteBreak().WriteLine("return int.Parse(content);");
|
writer.WriteBreak().WriteLine("return int.Parse(content);");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case TypeCode.Int64:
|
case TypeCode.Int64:
|
||||||
writer.WriteBreak().WriteLine("return long.Parse(content);");
|
writer.WriteBreak().WriteLine("return long.Parse(content);");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case TypeCode.SByte:
|
case TypeCode.SByte:
|
||||||
writer.WriteBreak().WriteLine("return sbyte.Parse(content);");
|
writer.WriteBreak().WriteLine("return sbyte.Parse(content);");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case TypeCode.Single:
|
case TypeCode.Single:
|
||||||
writer.WriteBreak().WriteLine("return float.Parse(content);");
|
writer.WriteBreak().WriteLine("return float.Parse(content);");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case TypeCode.String:
|
case TypeCode.String:
|
||||||
writer.WriteBreak().WriteLine("return content;");
|
writer.WriteBreak().WriteLine("return content;");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case TypeCode.UInt16:
|
case TypeCode.UInt16:
|
||||||
writer.WriteBreak().WriteLine("return ushort.Parse(content);");
|
writer.WriteBreak().WriteLine("return ushort.Parse(content);");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case TypeCode.UInt32:
|
case TypeCode.UInt32:
|
||||||
writer.WriteBreak().WriteLine("return uint.Parse(content);");
|
writer.WriteBreak().WriteLine("return uint.Parse(content);");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case TypeCode.UInt64:
|
case TypeCode.UInt64:
|
||||||
writer.WriteBreak().WriteLine("return ulong.Parse(content);");
|
writer.WriteBreak().WriteLine("return ulong.Parse(content);");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case TypeCode.Object:
|
case TypeCode.Object:
|
||||||
throw new NotSupportedException("ResponseType isn't JSON but is an object.");
|
throw new NotSupportedException("ResponseType isn't JSON but is an object.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user