Added RouteName and Request/Response handling to the code generator. More work is needed.
This commit is contained in:
@ -13,14 +13,17 @@ namespace MontoyaTech.Rest.Net.Example
|
||||
{
|
||||
public string BaseUrl;
|
||||
|
||||
private HttpClient HttpClient;
|
||||
public HttpClient HttpClient;
|
||||
|
||||
public TestApi Test;
|
||||
|
||||
public AuthApi Auth;
|
||||
|
||||
public Client(string baseUrl)
|
||||
{
|
||||
this.BaseUrl = baseUrl;
|
||||
this.Test = new TestApi(this);
|
||||
this.Auth = new AuthApi(this);
|
||||
this.HttpClient = new HttpClient();
|
||||
this.HttpClient.DefaultRequestHeaders.Add("Accept", "*/*");
|
||||
this.HttpClient.DefaultRequestHeaders.Add("Connection", "keep-alive");
|
||||
@ -38,26 +41,72 @@ namespace MontoyaTech.Rest.Net.Example
|
||||
|
||||
public string Status()
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/status");
|
||||
return default;
|
||||
var message = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/status");
|
||||
|
||||
var response = this.Client.HttpClient.Send(message);
|
||||
|
||||
if (response.StatusCode == System.Net.HttpStatusCode.OK)
|
||||
return Newtonsoft.Json.JsonConvert.DeserializeObject<string>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
|
||||
else
|
||||
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||
}
|
||||
|
||||
public string Add(double a, double b)
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, $"{this.Client.BaseUrl}/add/{a}/{b}");
|
||||
return default;
|
||||
var message = new HttpRequestMessage(HttpMethod.Post, $"{this.Client.BaseUrl}/add/{a}/{b}");
|
||||
|
||||
var response = this.Client.HttpClient.Send(message);
|
||||
|
||||
if (response.StatusCode == System.Net.HttpStatusCode.OK)
|
||||
return Newtonsoft.Json.JsonConvert.DeserializeObject<string>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
|
||||
else
|
||||
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||
}
|
||||
}
|
||||
|
||||
public class AuthApi
|
||||
{
|
||||
public Client Client;
|
||||
|
||||
public AuthApi(Client client)
|
||||
{
|
||||
this.Client = client;
|
||||
}
|
||||
|
||||
public string Signup(User user)
|
||||
public bool UserExists(string name)
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, $"{this.Client.BaseUrl}/signup/{user}");
|
||||
return default;
|
||||
var message = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/auth/{name}");
|
||||
|
||||
var response = this.Client.HttpClient.Send(message);
|
||||
|
||||
if (response.StatusCode == System.Net.HttpStatusCode.OK)
|
||||
return Newtonsoft.Json.JsonConvert.DeserializeObject<bool>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
|
||||
else
|
||||
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||
}
|
||||
|
||||
public User Json()
|
||||
public void Signup(User request)
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/json");
|
||||
return default;
|
||||
var message = new HttpRequestMessage(HttpMethod.Post, $"{this.Client.BaseUrl}/auth/signup");
|
||||
|
||||
message.Content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(request));
|
||||
|
||||
var response = this.Client.HttpClient.Send(message);
|
||||
|
||||
if (response.StatusCode == System.Net.HttpStatusCode.OK)
|
||||
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||
}
|
||||
|
||||
public User Get()
|
||||
{
|
||||
var message = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/auth");
|
||||
|
||||
var response = this.Client.HttpClient.Send(message);
|
||||
|
||||
if (response.StatusCode == System.Net.HttpStatusCode.OK)
|
||||
return Newtonsoft.Json.JsonConvert.DeserializeObject<User>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
|
||||
else
|
||||
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5,6 +5,7 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Net;
|
||||
using MontoyaTech.Rest.Net;
|
||||
using System.Net.Mime;
|
||||
|
||||
namespace MontoyaTech.Rest.Net.Example
|
||||
{
|
||||
@ -18,15 +19,12 @@ namespace MontoyaTech.Rest.Net.Example
|
||||
|
||||
public ulong Property { get; set; }
|
||||
|
||||
public User() { }
|
||||
|
||||
public User(string name)
|
||||
{
|
||||
this.Name = name;
|
||||
}
|
||||
|
||||
public static explicit operator User(string input)
|
||||
{
|
||||
return new User(input.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public static void Main(string[] args)
|
||||
@ -34,24 +32,23 @@ namespace MontoyaTech.Rest.Net.Example
|
||||
var listener = new RouteListener(8080,
|
||||
new Route(HttpRequestMethod.Get, "/status", Status),
|
||||
new Route<double, double>(HttpRequestMethod.Post, "/add/{a}/{b}", Add),
|
||||
new Route<User>(HttpRequestMethod.Post, "/signup/{username}", Signup),
|
||||
new Route(HttpRequestMethod.Get, "/json", Json)
|
||||
new Route<string>(HttpRequestMethod.Get, "/auth/{username}", Exists),
|
||||
new Route(HttpRequestMethod.Post, "/auth/signup", Signup),
|
||||
new Route(HttpRequestMethod.Get, "/auth/", Json)
|
||||
);
|
||||
|
||||
string code = CodeGenerator.GenerateCSharpClient(listener.Routes);
|
||||
|
||||
Console.WriteLine(code);
|
||||
|
||||
Console.ReadLine();
|
||||
|
||||
listener.RequestPreProcessEvent += (HttpListenerContext context) => {
|
||||
Console.WriteLine("Request start: " + context.Request.RawUrl);
|
||||
Console.WriteLine($"[{context.Request.HttpMethod}] Request start: " + context.Request.RawUrl);
|
||||
return true;
|
||||
};
|
||||
|
||||
listener.RequestPostProcessEvent += (HttpListenerContext context) =>
|
||||
{
|
||||
Console.WriteLine("Request end: " + context.Request.RawUrl);
|
||||
Console.WriteLine($"[{context.Request.HttpMethod}] Request end: " + context.Request.RawUrl);
|
||||
};
|
||||
|
||||
listener.Start();
|
||||
@ -61,7 +58,11 @@ namespace MontoyaTech.Rest.Net.Example
|
||||
foreach (var route in listener.Routes)
|
||||
Console.WriteLine($"- [{route.Method}] {route.Syntax}");
|
||||
|
||||
Console.WriteLine($"Rest api server running at http://localhost:{listener.Port}");
|
||||
Console.WriteLine($"Rest api server running at {listener.BaseUrl}");
|
||||
|
||||
var client = new Client(listener.BaseUrl);
|
||||
|
||||
var result = client.Auth.Get();
|
||||
|
||||
listener.Block();
|
||||
}
|
||||
@ -80,12 +81,6 @@ namespace MontoyaTech.Rest.Net.Example
|
||||
return context.Response.WithStatus(HttpStatusCode.OK).WithText((a + b).ToString());
|
||||
}
|
||||
|
||||
[RouteGroup("Test")]
|
||||
[RouteResponse(typeof(string))]
|
||||
public static HttpListenerResponse Signup(HttpListenerContext context, User user)
|
||||
{
|
||||
return context.Response.WithStatus(HttpStatusCode.OK).WithText("User:" + user.Name);
|
||||
}
|
||||
|
||||
[RouteGroup("Test")]
|
||||
[RouteRequest(typeof(User))]
|
||||
@ -94,7 +89,25 @@ namespace MontoyaTech.Rest.Net.Example
|
||||
return context.Response.WithStatus(HttpStatusCode.OK);
|
||||
}
|
||||
|
||||
[RouteGroup("Test")]
|
||||
[RouteGroup("Auth")]
|
||||
[RouteName("UserExists")]
|
||||
[RouteResponse(typeof(bool))]
|
||||
public static HttpListenerResponse Exists(HttpListenerContext context, string name)
|
||||
{
|
||||
Console.WriteLine("Auth.Exists called, name:" + name);
|
||||
|
||||
return context.Response.WithStatus(HttpStatusCode.OK).WithJson(true);
|
||||
}
|
||||
|
||||
[RouteGroup("Auth")]
|
||||
[RouteRequest(typeof(User))]
|
||||
public static HttpListenerResponse Signup(HttpListenerContext context)
|
||||
{
|
||||
return context.Response.WithStatus(HttpStatusCode.OK);
|
||||
}
|
||||
|
||||
[RouteGroup("Auth")]
|
||||
[RouteName("Get")]
|
||||
[RouteResponse(typeof(User))]
|
||||
public static HttpListenerResponse Json(HttpListenerContext context)
|
||||
{
|
||||
|
Reference in New Issue
Block a user