Rest.Net/Rest.Net.Example/StaticClient.cs

369 lines
11 KiB
C#
Raw Normal View History

//Generated using MontoyaTech.Rest.Net
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 HttpMessageHandler MessageHandler;
public static HttpClient HttpClient;
public static void Init(string baseUrl, HttpMessageHandler handler = null)
{
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();
if (handler == null)
{
handler = new HttpClientHandler()
{
AllowAutoRedirect = true,
UseCookies = true,
CookieContainer = StaticClient.CookieContainer,
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
}
StaticClient.MessageHandler = handler;
StaticClient.HttpClient = new HttpClient(handler);
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, HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
if (string.IsNullOrEmpty(content))
return default;
return JsonConvert.DeserializeObject<string>(content);
}
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, HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
if (string.IsNullOrEmpty(content))
return default;
return JsonConvert.DeserializeObject<string>(content);
}
else
{
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
}
public static string Compress()
{
var message = new HttpRequestMessage(HttpMethod.Get, $"{StaticClient.BaseUrl}/compress");
var response = StaticClient.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
if (string.IsNullOrEmpty(content))
return default;
return JsonConvert.DeserializeObject<string>(content);
}
else
{
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
}
public static string CompressFile()
{
var message = new HttpRequestMessage(HttpMethod.Get, $"{StaticClient.BaseUrl}/file/compress");
var response = StaticClient.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
if (string.IsNullOrEmpty(content))
return default;
return JsonConvert.DeserializeObject<string>(content);
}
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, HttpCompletionOption.ResponseHeadersRead);
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 static void Signup(UserDto request)
{
var message = new HttpRequestMessage(HttpMethod.Post, $"{StaticClient.BaseUrl}/auth/signup");
message.Content = new StringContent(JsonConvert.SerializeObject(request));
var response = StaticClient.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
if (!response.IsSuccessStatusCode)
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
public static UserDto Get()
{
var message = new HttpRequestMessage(HttpMethod.Get, $"{StaticClient.BaseUrl}/auth");
var response = StaticClient.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
if (string.IsNullOrEmpty(content))
return default;
return JsonConvert.DeserializeObject<UserDto>(content);
}
else
{
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
}
public static UserRole GetRole()
{
var message = new HttpRequestMessage(HttpMethod.Get, $"{StaticClient.BaseUrl}/auth/role");
var response = StaticClient.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
if (string.IsNullOrEmpty(content))
return default;
return JsonConvert.DeserializeObject<UserRole>(content);
}
else
{
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
}
}
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, HttpCompletionOption.ResponseHeadersRead);
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, HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
var stream = new System.IO.MemoryStream();
response.Content.CopyToAsync(stream).GetAwaiter().GetResult();
return stream;
}
else
{
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
}
}
public class IncludedType
{
public int Test;
public IncludedType() { }
public IncludedType(IncludedType instance)
{
this.Test = instance.Test;
}
public IncludedType(int Test = 0)
{
this.Test = Test;
}
}
public class UserDto : BaseUser
{
public System.PlatformID MachineType;
public string Name;
public System.Collections.Generic.List<string> List;
public System.String[] Array;
public ulong Property { get; set; }
public UserDto() { }
public UserDto(UserDto instance)
{
this.MachineType = instance.MachineType;
this.Name = instance.Name;
this.List = instance.List;
this.Array = instance.Array;
this.Property = instance.Property;
}
public UserDto(System.PlatformID MachineType = 0, string Name = null, System.Collections.Generic.List<string> List = null, System.String[] Array = null, ulong Property = 0)
{
this.MachineType = MachineType;
this.Name = Name;
this.List = List;
this.Array = Array;
this.Property = Property;
}
}
public class BaseUser
{
public string Id;
public char FirstInitial;
public System.Collections.Generic.List<Permission> Permissions;
public UserRole Role { get; set; }
public BaseUser() { }
public BaseUser(BaseUser instance)
{
this.Id = instance.Id;
this.FirstInitial = instance.FirstInitial;
this.Permissions = instance.Permissions;
this.Role = instance.Role;
}
public BaseUser(string Id = null, char FirstInitial = '\0', System.Collections.Generic.List<Permission> Permissions = null, UserRole Role = 0)
{
this.Id = Id;
this.FirstInitial = FirstInitial;
this.Permissions = Permissions;
this.Role = Role;
}
public class Permission
{
public string Name;
public Types Type;
public Permission() { }
public Permission(Permission instance)
{
this.Name = instance.Name;
this.Type = instance.Type;
}
public Permission(string Name = null, Types Type = 0)
{
this.Name = Name;
this.Type = Type;
}
public enum Types : int
{
Read = 0,
Write = 1,
}
}
}
public enum UserRole : byte
{
Unknown = 0,
Admin = 2,
User = 1,
}
}