Bumped package version to 1.4.7. Added support for Stream requests and MemoryStream responses.
This commit is contained in:
@ -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");
|
||||
|
||||
@ -164,7 +164,7 @@ public class StaticClient
|
||||
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");
|
||||
|
||||
@ -177,7 +177,7 @@ public class StaticClient
|
||||
if (string.IsNullOrEmpty(content))
|
||||
return default;
|
||||
|
||||
return JsonConvert.DeserializeObject<User>(content);
|
||||
return JsonConvert.DeserializeObject<UserDto>(content);
|
||||
}
|
||||
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 int Test;
|
||||
}
|
||||
|
||||
public class User : BaseUser
|
||||
public class UserDto : BaseUser
|
||||
{
|
||||
public System.PlatformID MachineType;
|
||||
|
||||
public string Name;
|
||||
|
||||
public System.Collections.Generic.List<string> List;
|
||||
|
@ -17,6 +17,8 @@ public class Client
|
||||
|
||||
public AuthApi Auth;
|
||||
|
||||
public StreamApi Stream;
|
||||
|
||||
public Client(string baseUrl)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(baseUrl))
|
||||
@ -48,6 +50,8 @@ public class Client
|
||||
this.Test = new TestApi(this);
|
||||
|
||||
this.Auth = new AuthApi(this);
|
||||
|
||||
this.Stream = new StreamApi(this);
|
||||
}
|
||||
|
||||
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");
|
||||
|
||||
@ -186,7 +190,7 @@ public class Client
|
||||
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");
|
||||
|
||||
@ -199,7 +203,7 @@ public class Client
|
||||
if (string.IsNullOrEmpty(content))
|
||||
return default;
|
||||
|
||||
return JsonConvert.DeserializeObject<User>(content);
|
||||
return JsonConvert.DeserializeObject<UserDto>(content);
|
||||
}
|
||||
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 int Test;
|
||||
}
|
||||
|
||||
public class User : BaseUser
|
||||
public class UserDto : BaseUser
|
||||
{
|
||||
public System.PlatformID MachineType;
|
||||
|
||||
public string Name;
|
||||
|
||||
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(HttpRequestMethod.Post, "/auth/signup", Signup),
|
||||
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();
|
||||
@ -110,6 +112,24 @@ namespace MontoyaTech.Rest.Net.Example
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
@ -182,5 +202,23 @@ namespace MontoyaTech.Rest.Net.Example
|
||||
{
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user