csharp.libraries.generichost.ClientUtils.mustache Maven / Gradle / Ivy
{{>partial_header}}
{{#nrt}}
#nullable enable
{{/nrt}}
using System;
using System.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;{{#useCompareNetObjects}}
using KellermanSoftware.CompareNetObjects;{{/useCompareNetObjects}}
{{#models}}
{{#-first}}
using {{packageName}}.{{modelPackage}};
{{/-first}}
{{/models}}
using System.Runtime.CompilerServices;
{{>Assembly}}namespace {{packageName}}.{{clientPackage}}
{
///
/// Utility functions providing some benefit to API client consumers.
///
{{>visibility}} static class ClientUtils
{
{{#useCompareNetObjects}}
///
/// An instance of CompareLogic.
///
public static CompareLogic compareLogic;
///
/// Static constructor to initialise compareLogic.
///
static ClientUtils()
{
{{#equatable}}
ComparisonConfig comparisonConfig = new{{^net70OrLater}} ComparisonConfig{{/net70OrLater}}();
comparisonConfig.UseHashCodeIdentifier = true;
{{/equatable}}
compareLogic = new{{^net70OrLater}} CompareLogic{{/net70OrLater}}({{#equatable}}comparisonConfig{{/equatable}});
}
{{/useCompareNetObjects}}
///
/// A delegate for events.
///
///
///
///
///
public delegate void EventHandler(object sender, T e) where T : EventArgs;
{{#hasApiKeyMethods}}
///
/// An enum of headers
///
public enum ApiKeyHeader
{
{{#apiKeyMethods}}
///
/// The {{keyParamName}} header
///
{{#lambda.titlecase}}{{#lambda.alphabet_or_underscore}}{{keyParamName}}{{/lambda.alphabet_or_underscore}}{{/lambda.titlecase}}{{^-last}},{{/-last}}
{{/apiKeyMethods}}
}
///
/// Converte an ApiKeyHeader to a string
///
///
///
///
{{>visibility}} static string ApiKeyHeaderToString(ApiKeyHeader value)
{
{{#net80OrLater}}
return value switch
{
{{#apiKeyMethods}}
ApiKeyHeader.{{#lambda.titlecase}}{{#lambda.alphabet_or_underscore}}{{keyParamName}}{{/lambda.alphabet_or_underscore}}{{/lambda.titlecase}} => "{{keyParamName}}",
{{/apiKeyMethods}}
_ => throw new System.ComponentModel.InvalidEnumArgumentException(nameof(value), (int)value, typeof(ApiKeyHeader)),
};
{{/net80OrLater}}
{{^net80OrLater}}
switch(value)
{
{{#apiKeyMethods}}
case ApiKeyHeader.{{#lambda.titlecase}}{{#lambda.alphabet_or_underscore}}{{keyParamName}}{{/lambda.alphabet_or_underscore}}{{/lambda.titlecase}}:
return "{{keyParamName}}";
{{/apiKeyMethods}}
default:
throw new System.ComponentModel.InvalidEnumArgumentException(nameof(value), (int)value, typeof(ApiKeyHeader));
}
{{/net80OrLater}}
}
{{/hasApiKeyMethods}}
///
/// Returns true when deserialization succeeds.
///
///
///
///
///
///
public static bool TryDeserialize(string json, JsonSerializerOptions options, {{#net60OrLater}}[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] {{/net60OrLater}}out T{{#nrt}}{{#net60OrLater}}?{{/net60OrLater}}{{/nrt}} result)
{
try
{
result = JsonSerializer.Deserialize(json, options);
return result != null;
}
catch (Exception)
{
result = default;
return false;
}
}
///
/// Returns true when deserialization succeeds.
///
///
///
///
///
///
public static bool TryDeserialize(ref Utf8JsonReader reader, JsonSerializerOptions options, {{#net60OrLater}}[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] {{/net60OrLater}}out T{{#nrt}}{{#net60OrLater}}?{{/net60OrLater}}{{/nrt}} result)
{
try
{
result = JsonSerializer.Deserialize(ref reader, options);
return result != null;
}
catch (Exception)
{
result = default;
return false;
}
}
///
/// Sanitize filename by removing the path
///
/// Filename
/// Filename
public static string SanitizeFilename(string filename)
{
Match match = Regex.Match(filename, @".*[/\\](.*)$");
return match.Success ? match.Groups[1].Value : filename;
}
///
/// If parameter is DateTime, output in a formatted string (default ISO 8601), customizable with Configuration.DateTime.
/// If parameter is a list, join the list with ",".
/// Otherwise just return the string.
///
/// The parameter (header, path, query, form).
/// The DateTime serialization format.
/// Formatted string.
public static string{{nrt?}} ParameterToString(object{{nrt?}} obj, string{{nrt?}} format = ISO8601_DATETIME_FORMAT)
{
if (obj is DateTime dateTime)
// Return a formatted date string - Can be customized with Configuration.DateTimeFormat
// Defaults to an ISO 8601, using the known as a Round-trip date/time pattern ("o")
// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8
// For example: 2009-06-15T13:45:30.0000000
return dateTime.ToString(format);
if (obj is DateTimeOffset dateTimeOffset)
// Return a formatted date string - Can be customized with Configuration.DateTimeFormat
// Defaults to an ISO 8601, using the known as a Round-trip date/time pattern ("o")
// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8
// For example: 2009-06-15T13:45:30.0000000
return dateTimeOffset.ToString(format);
{{#net60OrLater}}
if (obj is DateOnly dateOnly)
return dateOnly.ToString(format);
{{/net60OrLater}}
if (obj is bool boolean)
return boolean
? "true"
: "false";
{{#models}}
{{#model}}
{{#isEnum}}
if (obj is {{classname}} {{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}})
{{! below has #isNumeric as a work around but should probably have ^isString instead https://github.com/OpenAPITools/openapi-generator/issues/15038}}
return {{classname}}ValueConverter.{{#isInnerEnum}}{{classname}}.{{/isInnerEnum}}{{{datatypeWithEnum}}}ToJsonValue({{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}){{#isNumeric}}.ToString(){{/isNumeric}};
{{/isEnum}}
{{^isEnum}}
{{#vars}}
{{#items.isEnum}}
{{#items}}
{{^complexType}}
if (obj is {{#isInnerEnum}}{{classname}}.{{/isInnerEnum}}{{{datatypeWithEnum}}} {{#lambda.camelcase_sanitize_param}}{{classname}}{{{datatypeWithEnum}}}{{/lambda.camelcase_sanitize_param}})
{{! below has #isNumeric as a work around but should probably have ^isString instead https://github.com/OpenAPITools/openapi-generator/issues/15038}}
return {{#isInnerEnum}}{{classname}}.{{/isInnerEnum}}{{{datatypeWithEnum}}}ToJsonValue({{#lambda.camelcase_sanitize_param}}{{classname}}{{{datatypeWithEnum}}}{{/lambda.camelcase_sanitize_param}}){{#isNumeric}}.ToString(){{/isNumeric}};
{{/complexType}}
{{/items}}
{{/items.isEnum}}
{{#isEnum}}
{{^complexType}}
if (obj is {{#isInnerEnum}}{{classname}}.{{/isInnerEnum}}{{{datatypeWithEnum}}} {{#lambda.camelcase_sanitize_param}}{{classname}}{{{datatypeWithEnum}}}{{/lambda.camelcase_sanitize_param}})
{{! below has #isNumeric as a work around but should probably have ^isString instead https://github.com/OpenAPITools/openapi-generator/issues/15038}}
return {{#isInnerEnum}}{{classname}}.{{/isInnerEnum}}{{{datatypeWithEnum}}}ToJsonValue({{#lambda.camelcase_sanitize_param}}{{classname}}{{{datatypeWithEnum}}}{{/lambda.camelcase_sanitize_param}}){{#isNumeric}}.ToString(){{/isNumeric}};
{{/complexType}}
{{/isEnum}}
{{/vars}}
{{/isEnum}}
{{/model}}
{{/models}}
if (obj is ICollection collection)
{
List entries = new{{^net70OrLater}} List{{/net70OrLater}}();
foreach (var entry in collection)
entries.Add(ParameterToString(entry));
return string.Join(",", entries);
}
return Convert.ToString(obj, System.Globalization.CultureInfo.InvariantCulture);
}
///
/// URL encode a string
/// Credit/Ref: https://github.com/restsharp/RestSharp/blob/master/RestSharp/Extensions/StringExtensions.cs#L50
///
/// string to be URL encoded
/// Byte array
public static string UrlEncode(string input)
{
const int maxLength = 32766;
if (input == null)
{
throw new ArgumentNullException("input");
}
if (input.Length <= maxLength)
{
return Uri.EscapeDataString(input);
}
StringBuilder sb = new StringBuilder(input.Length * 2);
int index = 0;
while (index < input.Length)
{
int length = Math.Min(input.Length - index, maxLength);
string subString = input.Substring(index, length);
sb.Append(Uri.EscapeDataString(subString));
index += subString.Length;
}
return sb.ToString();
}
///
/// Encode string in base64 format.
///
/// string to be encoded.
/// Encoded string.
public static string Base64Encode(string text)
{
return Convert.ToBase64String(global::System.Text.Encoding.UTF8.GetBytes(text));
}
///
/// Convert stream to byte array
///
/// Input stream to be converted
/// Byte array
public static byte[] ReadAsBytes(Stream inputStream)
{
using (var ms = new MemoryStream())
{
inputStream.CopyTo(ms);
return ms.ToArray();
}
}
///
/// Select the Content-Type header's value from the given content-type array:
/// if JSON type exists in the given array, use it;
/// otherwise use the first one defined in 'consumes'
///
/// The Content-Type array to select from.
/// The Content-Type header to use.
public static string{{nrt?}} SelectHeaderContentType(string[] contentTypes)
{
if (contentTypes.Length == 0)
return null;
foreach (var contentType in contentTypes)
{
if (IsJsonMime(contentType))
return contentType;
}
return contentTypes[0]; // use the first content type specified in 'consumes'
}
///
/// Select the Accept header's value from the given accepts array:
/// if JSON exists in the given array, use it;
/// otherwise use all of them (joining into a string)
///
/// The accepts array to select from.
/// The Accept header to use.
public static string{{nrt?}} SelectHeaderAccept(string[] accepts)
{
if (accepts.Length == 0)
return null;
if (accepts.Contains("application/json", StringComparer.OrdinalIgnoreCase))
return "application/json";
return string.Join(",", accepts);
}
///
/// Provides a case-insensitive check that a provided content type is a known JSON-like content type.
///
public static readonly Regex JsonRegex = new Regex("(?i)^(application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(;.*)?$");
///
/// Check if the given MIME is a JSON MIME.
/// JSON MIME examples:
/// application/json
/// application/json; charset=UTF8
/// APPLICATION/JSON
/// application/vnd.company+json
///
/// MIME
/// Returns True if MIME type is json.
public static bool IsJsonMime(string mime)
{
if (string.IsNullOrWhiteSpace(mime)) return false;
return JsonRegex.IsMatch(mime) || mime.Equals("application/json-patch+json");
}
///
/// Get the discriminator
///
///
///
///
///
public static string{{nrt?}} GetDiscriminator(Utf8JsonReader utf8JsonReader, string discriminator)
{
int currentDepth = utf8JsonReader.CurrentDepth;
if (utf8JsonReader.TokenType != JsonTokenType.StartObject && utf8JsonReader.TokenType != JsonTokenType.StartArray)
throw new JsonException();
JsonTokenType startingTokenType = utf8JsonReader.TokenType;
while (utf8JsonReader.Read())
{
if (startingTokenType == JsonTokenType.StartObject && utf8JsonReader.TokenType == JsonTokenType.EndObject && currentDepth == utf8JsonReader.CurrentDepth)
break;
if (startingTokenType == JsonTokenType.StartArray && utf8JsonReader.TokenType == JsonTokenType.EndArray && currentDepth == utf8JsonReader.CurrentDepth)
break;
if (utf8JsonReader.TokenType == JsonTokenType.PropertyName && currentDepth == utf8JsonReader.CurrentDepth - 1)
{
string{{nrt?}} localVarJsonPropertyName = utf8JsonReader.GetString();
utf8JsonReader.Read();
if (localVarJsonPropertyName != null && localVarJsonPropertyName.Equals(discriminator))
return utf8JsonReader.GetString();
}
}
throw new JsonException("The specified discriminator was not found.");
}
///
/// The base path of the API
///
public const string BASE_ADDRESS = "{{{basePath}}}";
///
/// The scheme of the API
///
public const string SCHEME = "{{{scheme}}}";
///
/// The context path of the API
///
public const string CONTEXT_PATH = "{{contextPath}}";
///
/// The host of the API
///
public const string HOST = "{{{host}}}";
///
/// The format to use for DateTime serialization
///
public const string ISO8601_DATETIME_FORMAT = "o";
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy