Improved route function generator to handle status codes better and check to see if json content was null or empty and return default. Bumped package version to 1.4.2
This commit is contained in:
@ -1,5 +1,3 @@
|
||||
namespace MontoyaTech.Rest.Net.Example;
|
||||
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
@ -52,10 +50,19 @@ public class StaticClient
|
||||
|
||||
var response = StaticClient.HttpClient.Send(message);
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.OK)
|
||||
return JsonConvert.DeserializeObject<string>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
|
||||
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)
|
||||
@ -64,10 +71,61 @@ public class StaticClient
|
||||
|
||||
var response = StaticClient.HttpClient.Send(message);
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.OK)
|
||||
return JsonConvert.DeserializeObject<string>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -79,10 +137,19 @@ public class StaticClient
|
||||
|
||||
var response = StaticClient.HttpClient.Send(message);
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.OK)
|
||||
return bool.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
|
||||
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(User request)
|
||||
@ -93,7 +160,7 @@ public class StaticClient
|
||||
|
||||
var response = StaticClient.HttpClient.Send(message);
|
||||
|
||||
if (response.StatusCode != HttpStatusCode.OK)
|
||||
if (!response.IsSuccessStatusCode)
|
||||
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||
}
|
||||
|
||||
@ -103,14 +170,49 @@ public class StaticClient
|
||||
|
||||
var response = StaticClient.HttpClient.Send(message);
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.OK)
|
||||
return JsonConvert.DeserializeObject<User>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
|
||||
|
||||
if (string.IsNullOrEmpty(content))
|
||||
return default;
|
||||
|
||||
return JsonConvert.DeserializeObject<User>(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);
|
||||
|
||||
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 User
|
||||
public class IncludedType
|
||||
{
|
||||
public int Test;
|
||||
}
|
||||
|
||||
public class User : BaseUser
|
||||
{
|
||||
public string Name;
|
||||
|
||||
@ -118,4 +220,36 @@ public class StaticClient
|
||||
|
||||
public ulong Property { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
public class BaseUser
|
||||
{
|
||||
public string Id;
|
||||
|
||||
public System.Collections.Generic.List<Permission> 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,
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,3 @@
|
||||
namespace MontoyaTech.Rest.Net.Example;
|
||||
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
@ -67,10 +65,19 @@ public class Client
|
||||
|
||||
var response = this.Client.HttpClient.Send(message);
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.OK)
|
||||
return JsonConvert.DeserializeObject<string>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
|
||||
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 string Add(double a, double b)
|
||||
@ -79,10 +86,61 @@ public class Client
|
||||
|
||||
var response = this.Client.HttpClient.Send(message);
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.OK)
|
||||
return JsonConvert.DeserializeObject<string>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
|
||||
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 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<string>(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<string>(content);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -101,10 +159,19 @@ public class Client
|
||||
|
||||
var response = this.Client.HttpClient.Send(message);
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.OK)
|
||||
return bool.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
|
||||
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)
|
||||
@ -115,7 +182,7 @@ public class Client
|
||||
|
||||
var response = this.Client.HttpClient.Send(message);
|
||||
|
||||
if (response.StatusCode != HttpStatusCode.OK)
|
||||
if (!response.IsSuccessStatusCode)
|
||||
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||
}
|
||||
|
||||
@ -125,14 +192,49 @@ public class Client
|
||||
|
||||
var response = this.Client.HttpClient.Send(message);
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.OK)
|
||||
return JsonConvert.DeserializeObject<User>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
|
||||
|
||||
if (string.IsNullOrEmpty(content))
|
||||
return default;
|
||||
|
||||
return JsonConvert.DeserializeObject<User>(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<UserRole>(content);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class User
|
||||
public class IncludedType
|
||||
{
|
||||
public int Test;
|
||||
}
|
||||
|
||||
public class User : BaseUser
|
||||
{
|
||||
public string Name;
|
||||
|
||||
@ -140,4 +242,36 @@ public class Client
|
||||
|
||||
public ulong Property { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
public class BaseUser
|
||||
{
|
||||
public string Id;
|
||||
|
||||
public System.Collections.Generic.List<Permission> 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,
|
||||
}
|
||||
}
|
||||
|
@ -77,11 +77,15 @@ namespace MontoyaTech.Rest.Net.Example
|
||||
|
||||
string code = listener.GenerateCSharpClient();
|
||||
|
||||
File.WriteAllText("Client.cs", code);
|
||||
|
||||
Console.WriteLine(code);
|
||||
Console.WriteLine();
|
||||
|
||||
string staticCode = listener.GenerateCSharpClient("StaticClient", staticCode: true);
|
||||
|
||||
File.WriteAllText("Client.Static.cs", staticCode);
|
||||
|
||||
Console.WriteLine(staticCode);
|
||||
|
||||
listener.RequestPreProcessEvent += (HttpListenerContext context) => {
|
||||
|
Reference in New Issue
Block a user