Added Json flag to RouteRequest and RouteResponse to control whether or not the content is Json and needs to be handled. Updated example code. Renamed ClientCodeGenerator to RestClientGenerator. Bumped package version to 1.2.2.

This commit is contained in:
MattMo 2023-02-06 09:01:06 -08:00
parent 60768b1b3e
commit ed1d10ba9d
7 changed files with 97 additions and 9 deletions

View File

@ -81,7 +81,7 @@ namespace MontoyaTech.Rest.Net.Example
var response = this.Client.HttpClient.Send(message);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
return JsonConvert.DeserializeObject<bool>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
return bool.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
else
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}

View File

@ -37,7 +37,7 @@ namespace MontoyaTech.Rest.Net.Example
new Route(HttpRequestMethod.Get, "/auth/", Json)
);
string code = ClientCodeGenerator.GenerateCSharpClient(listener.Routes);
string code = RestClientGenerator.GenerateCSharpClient(listener.Routes);
Console.WriteLine(code);
@ -91,12 +91,12 @@ namespace MontoyaTech.Rest.Net.Example
[RouteGroup("Auth")]
[RouteName("UserExists")]
[RouteResponse(typeof(bool))]
[RouteResponse(typeof(bool), Json = false)]
public static HttpListenerResponse Exists(HttpListenerContext context, string name)
{
Console.WriteLine("Auth.Exists called, name:" + name);
return context.Response.WithStatus(HttpStatusCode.OK).WithJson(true);
return context.Response.WithStatus(HttpStatusCode.OK).WithText("true");
}
[RouteGroup("Auth")]

View File

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

View File

@ -15,7 +15,7 @@ namespace MontoyaTech.Rest.Net
/// A class that can take a set of routes and generate code
/// for a client that can be used to interact with them.
/// </summary>
public class ClientCodeGenerator
public class RestClientGenerator
{
/// <summary>
/// Returns whether or not a given type belongs to DotNet.
@ -600,7 +600,12 @@ namespace MontoyaTech.Rest.Net
//Add the request content if any.
if (routeRequest != null)
writer.WriteBreak().WriteLine("message.Content = new StringContent(JsonConvert.SerializeObject(request));");
{
if (routeRequest.Json)
writer.WriteBreak().WriteLine("message.Content = new StringContent(JsonConvert.SerializeObject(request));");
else
writer.WriteBreak().WriteLine("message.Content = new StringContent(request.ToString());");
}
//Generate the response code
writer.WriteBreak().WriteLine("var response = this.Client.HttpClient.Send(message);");
@ -609,7 +614,80 @@ namespace MontoyaTech.Rest.Net
if (routeResponse != null)
{
writer.WriteBreak().WriteLine("if (response.StatusCode == System.Net.HttpStatusCode.OK)").Indent();
writer.WriteLine($"return JsonConvert.DeserializeObject<{GetTypeFullyResolvedName(routeResponse.ResponseType)}>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());");
if (routeResponse.Json)
{
writer.WriteLine($"return JsonConvert.DeserializeObject<{GetTypeFullyResolvedName(routeResponse.ResponseType)}>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());");
}
else
{
switch (Type.GetTypeCode(routeResponse.ResponseType))
{
case TypeCode.Boolean:
writer.WriteLine("return bool.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());");
break;
case TypeCode.Byte:
writer.WriteLine("return byte.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());");
break;
case TypeCode.Char:
writer.WriteLine("return response.Content.ReadAsStringAsync().GetAwaiter().GetResult()[0];");
break;
case TypeCode.DateTime:
writer.WriteLine("return DateTime.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());");
break;
case TypeCode.Decimal:
writer.WriteLine("return decimal.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());");
break;
case TypeCode.Double:
writer.WriteLine("return double.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());");
break;
case TypeCode.Int16:
writer.WriteLine("return short.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());");
break;
case TypeCode.Int32:
writer.WriteLine("return int.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());");
break;
case TypeCode.Int64:
writer.WriteLine("return long.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());");
break;
case TypeCode.SByte:
writer.WriteLine("return sbyte.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());");
break;
case TypeCode.Single:
writer.WriteLine("return float.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());");
break;
case TypeCode.String:
writer.WriteLine("return response.Content.ReadAsStringAsync().GetAwaiter().GetResult();");
break;
case TypeCode.UInt16:
writer.WriteLine("return ushort.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());");
break;
case TypeCode.UInt32:
writer.WriteLine("return uint.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());");
break;
case TypeCode.UInt64:
writer.WriteLine("return ulong.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());");
break;
case TypeCode.Object:
throw new NotSupportedException("ResponseType isn't JSON but is an object.");
}
}
writer.Outdent().WriteLine("else").Indent();
writer.WriteLine(@"throw new Exception(""Unexpected Http Response StatusCode:"" + response.StatusCode);").Outdent();
}

View File

@ -227,7 +227,7 @@ namespace MontoyaTech.Rest.Net
/// <returns></returns>
public string GenerateCSharpClient(string name = "Client")
{
return ClientCodeGenerator.GenerateCSharpClient(this, name);
return RestClientGenerator.GenerateCSharpClient(this, name);
}
}
}

View File

@ -17,6 +17,11 @@ namespace MontoyaTech.Rest.Net
/// </summary>
public Type RequestType = null;
/// <summary>
/// Whether or not the Request is Json Serialized. Default is true.
/// </summary>
public bool Json = true;
/// <summary>
/// Creates a default route request.
/// </summary>

View File

@ -17,6 +17,11 @@ namespace MontoyaTech.Rest.Net
/// </summary>
public Type ResponseType = null;
/// <summary>
/// Whether or not the response is Json Serialized, default is true.
/// </summary>
public bool Json = true;
/// <summary>
/// Creates a default route response.
/// </summary>