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:
@ -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; }
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user