Improved the csharp client generator to check if the base url is null or whitespace and to remove trailing / characters. Fixed some bugs with different client names. Generator also now sets up cookie support.
This commit is contained in:
121
Rest.Net.Example/Client.Static.cs
Normal file
121
Rest.Net.Example/Client.Static.cs
Normal file
@ -0,0 +1,121 @@
|
||||
namespace MontoyaTech.Rest.Net.Example;
|
||||
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
public class StaticClient
|
||||
{
|
||||
public static string BaseUrl;
|
||||
|
||||
public static CookieContainer CookieContainer;
|
||||
|
||||
public static HttpClientHandler ClientHandler;
|
||||
|
||||
public static HttpClient HttpClient;
|
||||
|
||||
public static void Init(string baseUrl)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(baseUrl))
|
||||
throw new ArgumentException("baseUrl must not be null or whitespace.");
|
||||
|
||||
if (baseUrl.EndsWith('/'))
|
||||
baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
|
||||
|
||||
StaticClient.BaseUrl = baseUrl;
|
||||
|
||||
StaticClient.CookieContainer = new CookieContainer();
|
||||
|
||||
StaticClient.ClientHandler = new HttpClientHandler()
|
||||
{
|
||||
AllowAutoRedirect = true,
|
||||
UseCookies = true,
|
||||
CookieContainer = StaticClient.CookieContainer,
|
||||
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
|
||||
};
|
||||
|
||||
StaticClient.HttpClient = new HttpClient(StaticClient.ClientHandler);
|
||||
|
||||
StaticClient.HttpClient.DefaultRequestHeaders.Add("Accept", "*/*");
|
||||
|
||||
StaticClient.HttpClient.DefaultRequestHeaders.Add("Connection", "keep-alive");
|
||||
|
||||
StaticClient.HttpClient.DefaultRequestHeaders.Add("Accept-Encoding", "identity");
|
||||
}
|
||||
|
||||
public class Test
|
||||
{
|
||||
public static string Status()
|
||||
{
|
||||
var message = new HttpRequestMessage(HttpMethod.Get, $"{StaticClient.BaseUrl}/status");
|
||||
|
||||
var response = StaticClient.HttpClient.Send(message);
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.OK)
|
||||
return JsonConvert.DeserializeObject<string>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
|
||||
else
|
||||
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||
}
|
||||
|
||||
public static string Add(double a, double b)
|
||||
{
|
||||
var message = new HttpRequestMessage(HttpMethod.Post, $"{StaticClient.BaseUrl}/add/{a}/{b}");
|
||||
|
||||
var response = StaticClient.HttpClient.Send(message);
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.OK)
|
||||
return JsonConvert.DeserializeObject<string>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
|
||||
else
|
||||
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||
}
|
||||
}
|
||||
|
||||
public class Auth
|
||||
{
|
||||
public static bool UserExists(string name)
|
||||
{
|
||||
var message = new HttpRequestMessage(HttpMethod.Get, $"{StaticClient.BaseUrl}/auth/{name}");
|
||||
|
||||
var response = StaticClient.HttpClient.Send(message);
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.OK)
|
||||
return bool.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
|
||||
else
|
||||
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||
}
|
||||
|
||||
public static void Signup(User request)
|
||||
{
|
||||
var message = new HttpRequestMessage(HttpMethod.Post, $"{StaticClient.BaseUrl}/auth/signup");
|
||||
|
||||
message.Content = new StringContent(JsonConvert.SerializeObject(request));
|
||||
|
||||
var response = StaticClient.HttpClient.Send(message);
|
||||
|
||||
if (response.StatusCode != HttpStatusCode.OK)
|
||||
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||
}
|
||||
|
||||
public static User Get()
|
||||
{
|
||||
var message = new HttpRequestMessage(HttpMethod.Get, $"{StaticClient.BaseUrl}/auth");
|
||||
|
||||
var response = StaticClient.HttpClient.Send(message);
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.OK)
|
||||
return JsonConvert.DeserializeObject<User>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
|
||||
else
|
||||
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||
}
|
||||
}
|
||||
|
||||
public class User
|
||||
{
|
||||
public string Name;
|
||||
|
||||
public System.Collections.Generic.List<string> List;
|
||||
|
||||
public ulong Property { get; set; }
|
||||
}
|
||||
}
|
@ -1,123 +1,143 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
namespace MontoyaTech.Rest.Net.Example;
|
||||
|
||||
namespace MontoyaTech.Rest.Net.Example
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
public class Client
|
||||
{
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using Newtonsoft.Json;
|
||||
public string BaseUrl;
|
||||
|
||||
public class Client
|
||||
public CookieContainer CookieContainer;
|
||||
|
||||
public HttpClientHandler ClientHandler;
|
||||
|
||||
public HttpClient HttpClient;
|
||||
|
||||
public TestApi Test;
|
||||
|
||||
public AuthApi Auth;
|
||||
|
||||
public Client(string baseUrl)
|
||||
{
|
||||
public string BaseUrl;
|
||||
if (string.IsNullOrWhiteSpace(baseUrl))
|
||||
throw new ArgumentException("baseUrl must not be null or whitespace.");
|
||||
|
||||
public HttpClient HttpClient;
|
||||
if (baseUrl.EndsWith('/'))
|
||||
baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
|
||||
|
||||
public TestApi Test;
|
||||
this.BaseUrl = baseUrl;
|
||||
|
||||
public AuthApi Auth;
|
||||
this.CookieContainer = new CookieContainer();
|
||||
|
||||
public Client(string baseUrl)
|
||||
this.ClientHandler = new HttpClientHandler()
|
||||
{
|
||||
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");
|
||||
this.HttpClient.DefaultRequestHeaders.Add("Accept-Encoding", "identity");
|
||||
AllowAutoRedirect = true,
|
||||
UseCookies = true,
|
||||
CookieContainer = this.CookieContainer,
|
||||
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
|
||||
};
|
||||
|
||||
this.HttpClient = new HttpClient(this.ClientHandler);
|
||||
|
||||
this.HttpClient.DefaultRequestHeaders.Add("Accept", "*/*");
|
||||
|
||||
this.HttpClient.DefaultRequestHeaders.Add("Connection", "keep-alive");
|
||||
|
||||
this.HttpClient.DefaultRequestHeaders.Add("Accept-Encoding", "identity");
|
||||
|
||||
this.Test = new TestApi(this);
|
||||
|
||||
this.Auth = new AuthApi(this);
|
||||
}
|
||||
|
||||
public class TestApi
|
||||
{
|
||||
public Client Client;
|
||||
|
||||
public TestApi(Client client)
|
||||
{
|
||||
this.Client = client;
|
||||
}
|
||||
|
||||
public class TestApi
|
||||
public string Status()
|
||||
{
|
||||
public Client Client;
|
||||
var message = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/status");
|
||||
|
||||
public TestApi(Client client)
|
||||
{
|
||||
this.Client = client;
|
||||
}
|
||||
var response = this.Client.HttpClient.Send(message);
|
||||
|
||||
public string Status()
|
||||
{
|
||||
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 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 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 JsonConvert.DeserializeObject<string>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
|
||||
else
|
||||
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||
}
|
||||
if (response.StatusCode == HttpStatusCode.OK)
|
||||
return JsonConvert.DeserializeObject<string>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
|
||||
else
|
||||
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||
}
|
||||
|
||||
public class AuthApi
|
||||
public string Add(double a, double b)
|
||||
{
|
||||
public Client Client;
|
||||
var message = new HttpRequestMessage(HttpMethod.Post, $"{this.Client.BaseUrl}/add/{a}/{b}");
|
||||
|
||||
public AuthApi(Client client)
|
||||
{
|
||||
this.Client = client;
|
||||
}
|
||||
var response = this.Client.HttpClient.Send(message);
|
||||
|
||||
public bool UserExists(string name)
|
||||
{
|
||||
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 bool.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
|
||||
else
|
||||
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||
}
|
||||
|
||||
public void Signup(User request)
|
||||
{
|
||||
var message = new HttpRequestMessage(HttpMethod.Post, $"{this.Client.BaseUrl}/auth/signup");
|
||||
|
||||
message.Content = new StringContent(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 JsonConvert.DeserializeObject<User>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
|
||||
else
|
||||
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||
}
|
||||
}
|
||||
|
||||
public class User
|
||||
{
|
||||
public string Name;
|
||||
|
||||
public System.Collections.Generic.List<string> List;
|
||||
|
||||
public ulong Property { get; set; }
|
||||
if (response.StatusCode == HttpStatusCode.OK)
|
||||
return 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 bool UserExists(string name)
|
||||
{
|
||||
var message = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/auth/{name}");
|
||||
|
||||
var response = this.Client.HttpClient.Send(message);
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.OK)
|
||||
return bool.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
|
||||
else
|
||||
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||
}
|
||||
|
||||
public void Signup(User request)
|
||||
{
|
||||
var message = new HttpRequestMessage(HttpMethod.Post, $"{this.Client.BaseUrl}/auth/signup");
|
||||
|
||||
message.Content = new StringContent(JsonConvert.SerializeObject(request));
|
||||
|
||||
var response = this.Client.HttpClient.Send(message);
|
||||
|
||||
if (response.StatusCode != 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 == HttpStatusCode.OK)
|
||||
return JsonConvert.DeserializeObject<User>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
|
||||
else
|
||||
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||
}
|
||||
}
|
||||
|
||||
public class User
|
||||
{
|
||||
public string Name;
|
||||
|
||||
public System.Collections.Generic.List<string> List;
|
||||
|
||||
public ulong Property { get; set; }
|
||||
}
|
||||
}
|
@ -40,6 +40,11 @@ namespace MontoyaTech.Rest.Net.Example
|
||||
string code = listener.GenerateCSharpClient();
|
||||
|
||||
Console.WriteLine(code);
|
||||
Console.WriteLine();
|
||||
|
||||
string staticCode = listener.GenerateCSharpClient("StaticClient", staticCode: true);
|
||||
|
||||
Console.WriteLine(staticCode);
|
||||
|
||||
listener.RequestPreProcessEvent += (HttpListenerContext context) => {
|
||||
Console.WriteLine($"[{context.Request.HttpMethod}] Request start: " + context.Request.RawUrl);
|
||||
@ -64,6 +69,10 @@ namespace MontoyaTech.Rest.Net.Example
|
||||
|
||||
var result = client.Auth.Get();
|
||||
|
||||
StaticClient.Init(listener.BaseUrl);
|
||||
|
||||
var result2 = StaticClient.Auth.Get();
|
||||
|
||||
listener.Block();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user