Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/OpenApiWeaver/ClientGenerator.SupportTypesEmitter.Cookie.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace OpenApiWeaver;

public sealed partial class ClientGenerator
{
private static partial class SupportTypesEmitter
{
private static void EmitCookieHelpers(IndentedStringBuilder writer)
{
writer.AppendLine("internal static void AppendCookieParameter(StringBuilder builder, string name, string value)");
writer.AppendLine("{");
using (writer.PushIndent())
{
writer.AppendLine("if (builder.Length > 0)");
writer.AppendLine("{");
using (writer.PushIndent())
{
writer.AppendLine("builder.Append(\"; \");");
}

writer.AppendLine("}");
writer.AppendLine();
writer.AppendLine("builder.Append(name);");
writer.AppendLine("builder.Append('=');");
writer.AppendLine("builder.Append(Uri.EscapeDataString(value));");
}

writer.AppendLine("}");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
namespace OpenApiWeaver;

public sealed partial class ClientGenerator
{
private static partial class SupportTypesEmitter
{
private static void EmitExceptionTypes(IndentedStringBuilder writer)
{
writer.Append("public class ").Append(SupportTypeNames.Exception).AppendLine(" : Exception");
writer.AppendLine("{");
using (writer.PushIndent())
{
writer.Append("public ").Append(SupportTypeNames.Exception).AppendLine("(int statusCode, string? reasonPhrase, string? contentType, string? responseContent, Exception? innerException = null)");
writer.AppendLine(": base(CreateMessage(statusCode, reasonPhrase), innerException)");
writer.AppendLine("{");
using (writer.PushIndent())
{
writer.AppendLine("StatusCode = statusCode;");
writer.AppendLine("ReasonPhrase = reasonPhrase;");
writer.AppendLine("ContentType = contentType;");
writer.AppendLine("ResponseContent = responseContent;");
}

writer.AppendLine("}");
writer.AppendLine();
writer.AppendLine("public int StatusCode { get; }");
writer.AppendLine();
writer.AppendLine("public string? ReasonPhrase { get; }");
writer.AppendLine();
writer.AppendLine("public string? ContentType { get; }");
writer.AppendLine();
writer.AppendLine("public string? ResponseContent { get; }");
writer.AppendLine();
writer.AppendLine("private static string CreateMessage(int statusCode, string? reasonPhrase)");
writer.AppendLine("{");
using (writer.PushIndent())
{
writer.AppendLine("return string.IsNullOrWhiteSpace(reasonPhrase)");
using (writer.PushIndent())
{
writer.AppendLine("? $\"The HTTP request failed with status code {statusCode}.\"");
writer.AppendLine(": $\"The HTTP request failed with status code {statusCode} ({reasonPhrase}).\";");
}
}

writer.AppendLine("}");
}

writer.AppendLine("}");
writer.AppendLine();
writer.Append("public class ").Append(SupportTypeNames.Exception).Append("<TError> : ").Append(SupportTypeNames.Exception).AppendLine();
writer.AppendLine("{");
using (writer.PushIndent())
{
writer.Append("public ").Append(SupportTypeNames.Exception).AppendLine("(int statusCode, string? reasonPhrase, string? contentType, string? responseContent, TError? error, Exception? innerException = null)");
writer.AppendLine(": base(statusCode, reasonPhrase, contentType, responseContent, innerException)");
writer.AppendLine("{");
using (writer.PushIndent())
{
writer.AppendLine("Error = error;");
}

writer.AppendLine("}");
writer.AppendLine();
writer.AppendLine("public TError? Error { get; }");
}

writer.AppendLine("}");
}
}
}
28 changes: 28 additions & 0 deletions src/OpenApiWeaver/ClientGenerator.SupportTypesEmitter.Helpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace OpenApiWeaver;

public sealed partial class ClientGenerator
{
private static partial class SupportTypesEmitter
{
private static void EmitHelperClass(IndentedStringBuilder writer)
{
writer.Append("internal static class ").Append(SupportTypeNames.ClientHelpers).AppendLine();
writer.AppendLine("{");
using (writer.PushIndent())
{
writer.Append("internal static readonly JsonSerializerOptions ").Append(SupportTypeNames.SerializerOptions).AppendLine(" = new(JsonSerializerDefaults.Web);");
writer.AppendLine();

EmitParameterFormattingHelpers(writer);
writer.AppendLine();
EmitQueryHelpers(writer);
writer.AppendLine();
EmitCookieHelpers(writer);
writer.AppendLine();
EmitResponseHelpers(writer);
}

writer.AppendLine("}");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
namespace OpenApiWeaver;

public sealed partial class ClientGenerator
{
private static partial class SupportTypesEmitter
{
private static void EmitParameterFormattingHelpers(IndentedStringBuilder writer)
{
writer.AppendLine("internal static string FormatParameter(string? value) => value ?? string.Empty;");
writer.AppendLine();
writer.AppendLine("internal static string FormatParameter(JsonElement value) => value.ToString();");
writer.AppendLine();
writer.AppendLine("internal static string FormatParameter(bool value) => value ? \"true\" : \"false\";");
writer.AppendLine();
writer.AppendLine("internal static string FormatParameter(bool? value) => value is bool actual ? (actual ? \"true\" : \"false\") : string.Empty;");
writer.AppendLine();
writer.AppendLine("internal static string FormatParameter<T>(T value) where T : struct, IFormattable");
writer.AppendLine("{");
using (writer.PushIndent())
{
writer.AppendLine("return FormatFormattable(value);");
}

writer.AppendLine("}");
writer.AppendLine();
writer.AppendLine("internal static string FormatParameter<T>(T? value) where T : struct, IFormattable");
writer.AppendLine("{");
using (writer.PushIndent())
{
writer.AppendLine("return value is T actual ? FormatFormattable(actual) : string.Empty;");
}

writer.AppendLine("}");
writer.AppendLine();
writer.AppendLine("internal static string FormatParameter(object? value)");
writer.AppendLine("{");
using (writer.PushIndent())
{
writer.AppendLine("return value switch");
writer.AppendLine("{");
using (writer.PushIndent())
{
writer.AppendLine("null => string.Empty,");
writer.AppendLine("bool boolean => boolean ? \"true\" : \"false\",");
writer.AppendLine("DateTimeOffset dateTimeOffset => dateTimeOffset.ToString(\"o\", CultureInfo.InvariantCulture),");
writer.AppendLine("DateTime dateTime => dateTime.ToString(\"o\", CultureInfo.InvariantCulture),");
writer.AppendLine("DateOnly dateOnly => dateOnly.ToString(\"yyyy-MM-dd\", CultureInfo.InvariantCulture),");
writer.AppendLine("TimeOnly timeOnly => timeOnly.ToString(\"HH:mm:ss.FFFFFFF\", CultureInfo.InvariantCulture),");
writer.AppendLine("IFormattable formattable => formattable.ToString(null, CultureInfo.InvariantCulture),");
writer.AppendLine("_ => value.ToString() ?? string.Empty");
}

writer.AppendLine("};");
}

writer.AppendLine("}");
writer.AppendLine();
writer.AppendLine("private static string FormatFormattable<T>(T value) where T : struct, IFormattable");
writer.AppendLine("{");
using (writer.PushIndent())
{
writer.AppendLine("return value switch");
writer.AppendLine("{");
using (writer.PushIndent())
{
writer.AppendLine("DateTimeOffset dateTimeOffset => dateTimeOffset.ToString(\"o\", CultureInfo.InvariantCulture),");
writer.AppendLine("DateTime dateTime => dateTime.ToString(\"o\", CultureInfo.InvariantCulture),");
writer.AppendLine("DateOnly dateOnly => dateOnly.ToString(\"yyyy-MM-dd\", CultureInfo.InvariantCulture),");
writer.AppendLine("TimeOnly timeOnly => timeOnly.ToString(\"HH:mm:ss.FFFFFFF\", CultureInfo.InvariantCulture),");
writer.AppendLine("_ => value.ToString(null, CultureInfo.InvariantCulture)");
}

writer.AppendLine("};");
}

writer.AppendLine("}");
}
}
}
76 changes: 76 additions & 0 deletions src/OpenApiWeaver/ClientGenerator.SupportTypesEmitter.Query.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
namespace OpenApiWeaver;

public sealed partial class ClientGenerator
{
private static partial class SupportTypesEmitter
{
private static void EmitQueryHelpers(IndentedStringBuilder writer)
{
writer.AppendLine("internal static void AppendQueryParameter(StringBuilder builder, ref bool hasQuery, string name, string value)");
writer.AppendLine("{");
using (writer.PushIndent())
{
writer.AppendLine("builder.Append(hasQuery ? '&' : '?');");
writer.AppendLine("hasQuery = true;");
writer.AppendLine("builder.Append(name);");
writer.AppendLine("builder.Append('=');");
writer.AppendLine("builder.Append(Uri.EscapeDataString(value));");
}

writer.AppendLine("}");
writer.AppendLine();
writer.AppendLine("internal static void AppendQueryParameters<T>(StringBuilder builder, ref bool hasQuery, string name, IEnumerable<T> values)");
writer.AppendLine("{");
using (writer.PushIndent())
{
writer.AppendLine("foreach (var value in values)");
writer.AppendLine("{");
using (writer.PushIndent())
{
writer.AppendLine("AppendQueryParameter(builder, ref hasQuery, name, FormatParameter(value));");
}

writer.AppendLine("}");
}

writer.AppendLine("}");
writer.AppendLine();
writer.AppendLine("internal static string FormatCollectionParameter<T>(IEnumerable<T>? values)");
writer.AppendLine("{");
using (writer.PushIndent())
{
writer.AppendLine("if (values is null)");
writer.AppendLine("{");
using (writer.PushIndent())
{
writer.AppendLine("return string.Empty;");
}

writer.AppendLine("}");
writer.AppendLine();
writer.AppendLine("var builder = new StringBuilder();");
writer.AppendLine("foreach (var value in values)");
writer.AppendLine("{");
using (writer.PushIndent())
{
writer.AppendLine("if (builder.Length > 0)");
writer.AppendLine("{");
using (writer.PushIndent())
{
writer.AppendLine("builder.Append(',');");
}

writer.AppendLine("}");
writer.AppendLine();
writer.AppendLine("builder.Append(FormatParameter(value));");
}

writer.AppendLine("}");
writer.AppendLine();
writer.AppendLine("return builder.ToString();");
}

writer.AppendLine("}");
}
}
}
77 changes: 77 additions & 0 deletions src/OpenApiWeaver/ClientGenerator.SupportTypesEmitter.Response.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
namespace OpenApiWeaver;

public sealed partial class ClientGenerator
{
private static partial class SupportTypesEmitter
{
private static void EmitResponseHelpers(IndentedStringBuilder writer)
{
writer.AppendLine("internal static bool HasJsonContentType(string? contentType)");
writer.AppendLine("{");
using (writer.PushIndent())
{
writer.AppendLine("return !string.IsNullOrWhiteSpace(contentType) && contentType.Contains(\"json\", StringComparison.OrdinalIgnoreCase);");
}

writer.AppendLine("}");
writer.AppendLine();
writer.AppendLine("internal static bool ResponseMatchesStatusCode(int statusCode, string statusCodePattern)");
writer.AppendLine("{");
using (writer.PushIndent())
{
writer.AppendLine("if (string.Equals(statusCodePattern, \"default\", StringComparison.OrdinalIgnoreCase))");
writer.AppendLine("{");
using (writer.PushIndent())
{
writer.AppendLine("return true;");
}

writer.AppendLine("}");
writer.AppendLine();
writer.AppendLine("if (statusCodePattern.Length == 3");
using (writer.PushIndent())
{
writer.AppendLine("&& char.IsDigit(statusCodePattern[0])");
writer.AppendLine("&& statusCodePattern[1] is 'X' or 'x'");
writer.AppendLine("&& statusCodePattern[2] is 'X' or 'x')");
}

writer.AppendLine("{");
using (writer.PushIndent())
{
writer.AppendLine("var prefix = statusCodePattern[0] - '0';");
writer.AppendLine("return statusCode >= prefix * 100 && statusCode < (prefix + 1) * 100;");
}

writer.AppendLine("}");
writer.AppendLine();
writer.AppendLine("return int.TryParse(statusCodePattern, out var expectedStatusCode) && statusCode == expectedStatusCode;");
}

writer.AppendLine("}");
writer.AppendLine();
writer.AppendLine("internal static T? DeserializeResponseContent<T>(string? responseContent)");
writer.AppendLine("{");
using (writer.PushIndent())
{
writer.Append("return DeserializeResponseContent<T>(responseContent, ").Append(SupportTypeNames.SerializerOptions).AppendLine(");");
}

writer.AppendLine("}");
writer.AppendLine();
writer.AppendLine("internal static T? DeserializeResponseContent<T>(string? responseContent, JsonSerializerOptions options)");
writer.AppendLine("{");
using (writer.PushIndent())
{
writer.AppendLine("return string.IsNullOrWhiteSpace(responseContent)");
using (writer.PushIndent())
{
writer.AppendLine("? default");
writer.AppendLine(": JsonSerializer.Deserialize<T>(responseContent, options);");
}
}

writer.AppendLine("}");
}
}
}
Loading