121 lines
4.1 KiB
C#
121 lines
4.1 KiB
C#
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; }
|
|
}
|
|
} |