using System; using System.Net; using System.Net.Http; using Newtonsoft.Json; public class Client { public string BaseUrl; public CookieContainer CookieContainer; public HttpClientHandler ClientHandler; public HttpClient HttpClient; public TestApi Test; public AuthApi Auth; public Client(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); this.BaseUrl = baseUrl; this.CookieContainer = new CookieContainer(); this.ClientHandler = new HttpClientHandler() { 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 string Status() { var message = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/status"); var response = this.Client.HttpClient.Send(message); if (response.IsSuccessStatusCode) { var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); if (string.IsNullOrEmpty(content)) return default; return JsonConvert.DeserializeObject(content); } 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.IsSuccessStatusCode) { var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); if (string.IsNullOrEmpty(content)) return default; return JsonConvert.DeserializeObject(content); } else { throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode); } } public string Compress() { var message = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/compress"); var response = this.Client.HttpClient.Send(message); if (response.IsSuccessStatusCode) { var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); if (string.IsNullOrEmpty(content)) return default; return JsonConvert.DeserializeObject(content); } else { throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode); } } public string CompressFile() { var message = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/file/compress"); var response = this.Client.HttpClient.Send(message); if (response.IsSuccessStatusCode) { var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); if (string.IsNullOrEmpty(content)) return default; return JsonConvert.DeserializeObject(content); } 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.IsSuccessStatusCode) { var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); if (string.IsNullOrEmpty(content)) return default; return bool.Parse(content); } 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.IsSuccessStatusCode) 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.IsSuccessStatusCode) { var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); if (string.IsNullOrEmpty(content)) return default; return JsonConvert.DeserializeObject(content); } else { throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode); } } public UserRole GetRole() { var message = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/auth/role"); var response = this.Client.HttpClient.Send(message); if (response.IsSuccessStatusCode) { var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); if (string.IsNullOrEmpty(content)) return default; return JsonConvert.DeserializeObject(content); } else { throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode); } } } public class IncludedType { public int Test; } public class User : BaseUser { public string Name; public System.Collections.Generic.List List; public ulong Property { get; set; } } public class BaseUser { public string Id; public System.Collections.Generic.List Permissions; public UserRole Role { get; set; } public class Permission { public string Name; public Types Type; public enum Types : int { Read = 0, Write = 1, } } } public enum UserRole : byte { Unknown = 0, Admin = 2, User = 1, } }