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:
MattMo 2023-03-24 07:33:35 -07:00
parent 1345e326a8
commit ff7c356655
5 changed files with 324 additions and 47 deletions

View File

@ -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,
}
}

View File

@ -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,
}
}

View File

@ -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) => {

View File

@ -17,7 +17,7 @@
<AssemblyName>MontoyaTech.Rest.Net</AssemblyName>
<RootNamespace>MontoyaTech.Rest.Net</RootNamespace>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<Version>1.4.1</Version>
<Version>1.4.2</Version>
<PackageReleaseNotes></PackageReleaseNotes>
<PackageIcon>Logo_Symbol_Black_Outline.png</PackageIcon>
</PropertyGroup>

View File

@ -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();
}