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;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
@ -52,10 +50,19 @@ public class StaticClient
var response = StaticClient.HttpClient.Send(message); var response = StaticClient.HttpClient.Send(message);
if (response.StatusCode == HttpStatusCode.OK) if (response.IsSuccessStatusCode)
return JsonConvert.DeserializeObject<string>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult()); {
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
if (string.IsNullOrEmpty(content))
return default;
return JsonConvert.DeserializeObject<string>(content);
}
else else
{
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode); throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
} }
public static string Add(double a, double b) public static string Add(double a, double b)
@ -64,10 +71,61 @@ public class StaticClient
var response = StaticClient.HttpClient.Send(message); var response = StaticClient.HttpClient.Send(message);
if (response.StatusCode == HttpStatusCode.OK) if (response.IsSuccessStatusCode)
return JsonConvert.DeserializeObject<string>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult()); {
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
if (string.IsNullOrEmpty(content))
return default;
return JsonConvert.DeserializeObject<string>(content);
}
else else
{
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode); 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); var response = StaticClient.HttpClient.Send(message);
if (response.StatusCode == HttpStatusCode.OK) if (response.IsSuccessStatusCode)
return bool.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult()); {
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
if (string.IsNullOrEmpty(content))
return default;
return bool.Parse(content);
}
else else
{
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode); throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
} }
public static void Signup(User request) public static void Signup(User request)
@ -93,7 +160,7 @@ public class StaticClient
var response = StaticClient.HttpClient.Send(message); var response = StaticClient.HttpClient.Send(message);
if (response.StatusCode != HttpStatusCode.OK) if (!response.IsSuccessStatusCode)
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode); throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
} }
@ -103,14 +170,49 @@ public class StaticClient
var response = StaticClient.HttpClient.Send(message); var response = StaticClient.HttpClient.Send(message);
if (response.StatusCode == HttpStatusCode.OK) if (response.IsSuccessStatusCode)
return JsonConvert.DeserializeObject<User>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult()); {
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
if (string.IsNullOrEmpty(content))
return default;
return JsonConvert.DeserializeObject<User>(content);
}
else else
{
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode); 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; public string Name;
@ -118,4 +220,36 @@ public class StaticClient
public ulong Property { get; set; } 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;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
@ -67,10 +65,19 @@ public class Client
var response = this.Client.HttpClient.Send(message); var response = this.Client.HttpClient.Send(message);
if (response.StatusCode == HttpStatusCode.OK) if (response.IsSuccessStatusCode)
return JsonConvert.DeserializeObject<string>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult()); {
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
if (string.IsNullOrEmpty(content))
return default;
return JsonConvert.DeserializeObject<string>(content);
}
else else
{
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode); throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
} }
public string Add(double a, double b) public string Add(double a, double b)
@ -79,10 +86,61 @@ public class Client
var response = this.Client.HttpClient.Send(message); var response = this.Client.HttpClient.Send(message);
if (response.StatusCode == HttpStatusCode.OK) if (response.IsSuccessStatusCode)
return JsonConvert.DeserializeObject<string>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult()); {
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
if (string.IsNullOrEmpty(content))
return default;
return JsonConvert.DeserializeObject<string>(content);
}
else else
{
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode); 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); var response = this.Client.HttpClient.Send(message);
if (response.StatusCode == HttpStatusCode.OK) if (response.IsSuccessStatusCode)
return bool.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult()); {
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
if (string.IsNullOrEmpty(content))
return default;
return bool.Parse(content);
}
else else
{
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode); throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
} }
public void Signup(User request) public void Signup(User request)
@ -115,7 +182,7 @@ public class Client
var response = this.Client.HttpClient.Send(message); var response = this.Client.HttpClient.Send(message);
if (response.StatusCode != HttpStatusCode.OK) if (!response.IsSuccessStatusCode)
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode); throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
} }
@ -125,14 +192,49 @@ public class Client
var response = this.Client.HttpClient.Send(message); var response = this.Client.HttpClient.Send(message);
if (response.StatusCode == HttpStatusCode.OK) if (response.IsSuccessStatusCode)
return JsonConvert.DeserializeObject<User>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult()); {
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
if (string.IsNullOrEmpty(content))
return default;
return JsonConvert.DeserializeObject<User>(content);
}
else else
{
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode); 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; public string Name;
@ -140,4 +242,36 @@ public class Client
public ulong Property { get; set; } 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(); string code = listener.GenerateCSharpClient();
File.WriteAllText("Client.cs", code);
Console.WriteLine(code); Console.WriteLine(code);
Console.WriteLine(); Console.WriteLine();
string staticCode = listener.GenerateCSharpClient("StaticClient", staticCode: true); string staticCode = listener.GenerateCSharpClient("StaticClient", staticCode: true);
File.WriteAllText("Client.Static.cs", staticCode);
Console.WriteLine(staticCode); Console.WriteLine(staticCode);
listener.RequestPreProcessEvent += (HttpListenerContext context) => { listener.RequestPreProcessEvent += (HttpListenerContext context) => {

View File

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

View File

@ -426,74 +426,78 @@ namespace MontoyaTech.Rest.Net
//Handle the response //Handle the response
if (routeResponse != null) 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) 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 else
{ {
switch (Type.GetTypeCode(routeResponse.ResponseType)) switch (Type.GetTypeCode(routeResponse.ResponseType))
{ {
case TypeCode.Boolean: case TypeCode.Boolean:
writer.WriteLine("return bool.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());"); writer.WriteBreak().WriteLine("return bool.Parse(content);");
break; break;
case TypeCode.Byte: case TypeCode.Byte:
writer.WriteLine("return byte.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());"); writer.WriteBreak().WriteLine("return byte.Parse(content);");
break; break;
case TypeCode.Char: case TypeCode.Char:
writer.WriteLine("return response.Content.ReadAsStringAsync().GetAwaiter().GetResult()[0];"); writer.WriteBreak().WriteLine("return content[0];");
break; break;
case TypeCode.DateTime: case TypeCode.DateTime:
writer.WriteLine("return DateTime.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());"); writer.WriteBreak().WriteLine("return DateTime.Parse(content);");
break; break;
case TypeCode.Decimal: case TypeCode.Decimal:
writer.WriteLine("return decimal.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());"); writer.WriteBreak().WriteLine("return decimal.Parse(content);");
break; break;
case TypeCode.Double: case TypeCode.Double:
writer.WriteLine("return double.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());"); writer.WriteBreak().WriteLine("return double.Parse(content);");
break; break;
case TypeCode.Int16: case TypeCode.Int16:
writer.WriteLine("return short.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());"); writer.WriteBreak().WriteLine("return short.Parse(content);");
break; break;
case TypeCode.Int32: case TypeCode.Int32:
writer.WriteLine("return int.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());"); writer.WriteBreak().WriteLine("return int.Parse(content);");
break; break;
case TypeCode.Int64: case TypeCode.Int64:
writer.WriteLine("return long.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());"); writer.WriteBreak().WriteLine("return long.Parse(content);");
break; break;
case TypeCode.SByte: case TypeCode.SByte:
writer.WriteLine("return sbyte.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());"); writer.WriteBreak().WriteLine("return sbyte.Parse(content);");
break; break;
case TypeCode.Single: case TypeCode.Single:
writer.WriteLine("return float.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());"); writer.WriteBreak().WriteLine("return float.Parse(content);");
break; break;
case TypeCode.String: case TypeCode.String:
writer.WriteLine("return response.Content.ReadAsStringAsync().GetAwaiter().GetResult();"); writer.WriteBreak().WriteLine("return content;");
break; break;
case TypeCode.UInt16: case TypeCode.UInt16:
writer.WriteLine("return ushort.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());"); writer.WriteBreak().WriteLine("return ushort.Parse(content);");
break; break;
case TypeCode.UInt32: case TypeCode.UInt32:
writer.WriteLine("return uint.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());"); writer.WriteBreak().WriteLine("return uint.Parse(content);");
break; break;
case TypeCode.UInt64: case TypeCode.UInt64:
writer.WriteLine("return ulong.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());"); writer.WriteBreak().WriteLine("return ulong.Parse(content);");
break; break;
case TypeCode.Object: case TypeCode.Object:
@ -501,12 +505,13 @@ namespace MontoyaTech.Rest.Net
} }
} }
writer.Outdent().WriteLine("else").Indent(); writer.Outdent().WriteLine("}").WriteLine("else").WriteLine("{").Indent();
writer.WriteLine(@"throw new Exception(""Unexpected Http Response StatusCode:"" + response.StatusCode);").Outdent(); writer.WriteLine(@"throw new Exception(""Unexpected Http Response StatusCode:"" + response.StatusCode);");
writer.Outdent().WriteLine("}");
} }
else 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(); writer.WriteLine(@"throw new Exception(""Unexpected Http Response StatusCode:"" + response.StatusCode);").Outdent();
} }