From ff7c3566551b6feba70eb73debe577d73691df6e Mon Sep 17 00:00:00 2001 From: MattMo Date: Fri, 24 Mar 2023 07:33:35 -0700 Subject: [PATCH] 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 --- Rest.Net.Example/Client.Static.cs | 160 +++++++++++++++++++++++--- Rest.Net.Example/Client.cs | 160 +++++++++++++++++++++++--- Rest.Net.Example/Program.cs | 4 + Rest.Net/Rest.Net.csproj | 2 +- Rest.Net/RestCSharpClientGenerator.cs | 45 ++++---- 5 files changed, 324 insertions(+), 47 deletions(-) diff --git a/Rest.Net.Example/Client.Static.cs b/Rest.Net.Example/Client.Static.cs index dae3b15..fe0d38c 100644 --- a/Rest.Net.Example/Client.Static.cs +++ b/Rest.Net.Example/Client.Static.cs @@ -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(response.Content.ReadAsStringAsync().GetAwaiter().GetResult()); + 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 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(response.Content.ReadAsStringAsync().GetAwaiter().GetResult()); + 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 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(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(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(response.Content.ReadAsStringAsync().GetAwaiter().GetResult()); + 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 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(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; } } -} \ No newline at end of file + + 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, + } +} diff --git a/Rest.Net.Example/Client.cs b/Rest.Net.Example/Client.cs index f7d24e1..ecc88ef 100644 --- a/Rest.Net.Example/Client.cs +++ b/Rest.Net.Example/Client.cs @@ -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(response.Content.ReadAsStringAsync().GetAwaiter().GetResult()); + 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) @@ -79,10 +86,61 @@ public class Client var response = this.Client.HttpClient.Send(message); - if (response.StatusCode == HttpStatusCode.OK) - return JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().GetAwaiter().GetResult()); + 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); + } } } @@ -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(response.Content.ReadAsStringAsync().GetAwaiter().GetResult()); + 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 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; } } -} \ No newline at end of file + + 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, + } +} diff --git a/Rest.Net.Example/Program.cs b/Rest.Net.Example/Program.cs index b51bb5b..a94e4c7 100644 --- a/Rest.Net.Example/Program.cs +++ b/Rest.Net.Example/Program.cs @@ -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) => { diff --git a/Rest.Net/Rest.Net.csproj b/Rest.Net/Rest.Net.csproj index b1e7c77..0f72126 100644 --- a/Rest.Net/Rest.Net.csproj +++ b/Rest.Net/Rest.Net.csproj @@ -17,7 +17,7 @@ MontoyaTech.Rest.Net MontoyaTech.Rest.Net True - 1.4.1 + 1.4.2 Logo_Symbol_Black_Outline.png diff --git a/Rest.Net/RestCSharpClientGenerator.cs b/Rest.Net/RestCSharpClientGenerator.cs index 343ecd0..7c81342 100644 --- a/Rest.Net/RestCSharpClientGenerator.cs +++ b/Rest.Net/RestCSharpClientGenerator.cs @@ -426,74 +426,78 @@ namespace MontoyaTech.Rest.Net //Handle the response if (routeResponse != null) { - writer.WriteBreak().WriteLine("if (response.StatusCode == HttpStatusCode.OK)").Indent(); + writer.WriteBreak().WriteLine("if (response.IsSuccessStatusCode)").WriteLine("{").Indent(); + + writer.WriteBreak().WriteLine("var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();"); + + writer.WriteBreak().WriteLine("if (string.IsNullOrEmpty(content))").Indent().WriteLine("return default;").Outdent(); if (routeResponse.Json) { - writer.WriteLine($"return JsonConvert.DeserializeObject<{this.GetTypeFullyResolvedName(routeResponse.ResponseType)}>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());"); + writer.WriteBreak().WriteLine($"return JsonConvert.DeserializeObject<{this.GetTypeFullyResolvedName(routeResponse.ResponseType)}>(content);"); } else { switch (Type.GetTypeCode(routeResponse.ResponseType)) { case TypeCode.Boolean: - writer.WriteLine("return bool.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());"); + writer.WriteBreak().WriteLine("return bool.Parse(content);"); break; case TypeCode.Byte: - writer.WriteLine("return byte.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());"); + writer.WriteBreak().WriteLine("return byte.Parse(content);"); break; case TypeCode.Char: - writer.WriteLine("return response.Content.ReadAsStringAsync().GetAwaiter().GetResult()[0];"); + writer.WriteBreak().WriteLine("return content[0];"); break; case TypeCode.DateTime: - writer.WriteLine("return DateTime.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());"); + writer.WriteBreak().WriteLine("return DateTime.Parse(content);"); break; case TypeCode.Decimal: - writer.WriteLine("return decimal.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());"); + writer.WriteBreak().WriteLine("return decimal.Parse(content);"); break; case TypeCode.Double: - writer.WriteLine("return double.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());"); + writer.WriteBreak().WriteLine("return double.Parse(content);"); break; case TypeCode.Int16: - writer.WriteLine("return short.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());"); + writer.WriteBreak().WriteLine("return short.Parse(content);"); break; case TypeCode.Int32: - writer.WriteLine("return int.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());"); + writer.WriteBreak().WriteLine("return int.Parse(content);"); break; case TypeCode.Int64: - writer.WriteLine("return long.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());"); + writer.WriteBreak().WriteLine("return long.Parse(content);"); break; case TypeCode.SByte: - writer.WriteLine("return sbyte.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());"); + writer.WriteBreak().WriteLine("return sbyte.Parse(content);"); break; case TypeCode.Single: - writer.WriteLine("return float.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());"); + writer.WriteBreak().WriteLine("return float.Parse(content);"); break; case TypeCode.String: - writer.WriteLine("return response.Content.ReadAsStringAsync().GetAwaiter().GetResult();"); + writer.WriteBreak().WriteLine("return content;"); break; case TypeCode.UInt16: - writer.WriteLine("return ushort.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());"); + writer.WriteBreak().WriteLine("return ushort.Parse(content);"); break; case TypeCode.UInt32: - writer.WriteLine("return uint.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());"); + writer.WriteBreak().WriteLine("return uint.Parse(content);"); break; case TypeCode.UInt64: - writer.WriteLine("return ulong.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());"); + writer.WriteBreak().WriteLine("return ulong.Parse(content);"); break; case TypeCode.Object: @@ -501,12 +505,13 @@ namespace MontoyaTech.Rest.Net } } - writer.Outdent().WriteLine("else").Indent(); - writer.WriteLine(@"throw new Exception(""Unexpected Http Response StatusCode:"" + response.StatusCode);").Outdent(); + writer.Outdent().WriteLine("}").WriteLine("else").WriteLine("{").Indent(); + writer.WriteLine(@"throw new Exception(""Unexpected Http Response StatusCode:"" + response.StatusCode);"); + writer.Outdent().WriteLine("}"); } else { - writer.WriteBreak().WriteLine("if (response.StatusCode != HttpStatusCode.OK)").Indent(); + writer.WriteBreak().WriteLine("if (!response.IsSuccessStatusCode)").Indent(); writer.WriteLine(@"throw new Exception(""Unexpected Http Response StatusCode:"" + response.StatusCode);").Outdent(); }