csharp-functions.ClientUtils.mustache Maven / Gradle / Ivy
{{>partial_header}}
using System;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
{{#useCompareNetObjects}}
using KellermanSoftware.CompareNetObjects;
{{/useCompareNetObjects}}
namespace {{packageName}}.Client
{
///
/// Utility functions providing some benefit to API client consumers.
///
public static class ClientUtils
{
{{#useCompareNetObjects}}
///
/// An instance of CompareLogic.
///
public static CompareLogic compareLogic;
///
/// Static constructor to initialise compareLogic.
///
static ClientUtils()
{
compareLogic = new CompareLogic();
}
{{/useCompareNetObjects}}
///
/// 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;
}
///
/// Convert params to key/value pairs.
/// Use collectionFormat to properly format lists and collections.
///
/// The swagger-supported collection format, one of: csv, tsv, ssv, pipes, multi
/// Key name.
/// Value object.
/// A multimap of keys with 1..n associated values.
public static Multimap ParameterToMultiMap(string collectionFormat, string name, object value)
{
var parameters = new Multimap();
if (value is ICollection collection && collectionFormat == "multi")
{
foreach (var item in collection)
{
parameters.Add(name, ParameterToString(item));
}
}
else if (value is IDictionary dictionary)
{
if(collectionFormat == "deepObject") {
foreach (DictionaryEntry entry in dictionary)
{
parameters.Add(name + "[" + entry.Key + "]", ParameterToString(entry.Value));
}
}
else {
foreach (DictionaryEntry entry in dictionary)
{
parameters.Add(entry.Key.ToString(), ParameterToString(entry.Value));
}
}
}
else
{
parameters.Add(name, ParameterToString(value));
}
return parameters;
}
///
/// 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).
/// An optional configuration instance, providing formatting options used in processing.
/// Formatted string.
public static string ParameterToString(object obj, IReadableConfiguration configuration = null)
{
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((configuration ?? GlobalConfiguration.Instance).DateTimeFormat);
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((configuration ?? GlobalConfiguration.Instance).DateTimeFormat);
if (obj is bool boolean)
return boolean ? "true" : "false";
if (obj is ICollection collection)
return string.Join(",", collection.Cast
© 2015 - 2024 Weber Informatics LLC | Privacy Policy