csharp-refactor.ClientUtils.mustache Maven / Gradle / Ivy
{{>partial_header}}
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace {{packageName}}.Client
{
///
/// Utility functions providing some benefit to API client consumers.
///
public static class ClientUtils
{
///
/// 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 (IsCollection(value) && collectionFormat == "multi")
{
var valueCollection = value as IEnumerable;
if (valueCollection != null)
{
foreach (var item in valueCollection)
{
parameters.Add(name, ParameterToString(item));
}
}
}
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)
// 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)obj).ToString((configuration ?? GlobalConfiguration.Instance).DateTimeFormat);
else if (obj is 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)obj).ToString((configuration ?? GlobalConfiguration.Instance).DateTimeFormat);
else
{
if (obj is IList)
{
var list = obj as IList;
var flattenedString = new StringBuilder();
foreach (var param in list)
{
if (flattenedString.Length > 0)
flattenedString.Append(",");
flattenedString.Append(param);
}
return flattenedString.ToString();
}
return Convert.ToString (obj);
}
}
///
/// Check if generic object is a collection.
///
///
/// True if object is a collection type
private static bool IsCollection(object value)
{
return value is IList || value is ICollection;
}
///
/// 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 System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(text));
}
///
/// Convert stream to byte array
///
/// Input stream to be converted
/// Byte array
public static byte[] ReadAsBytes(Stream inputStream)
{
byte[] buf = new byte[16*1024];
using (MemoryStream ms = new MemoryStream())
{
int count;
while ((count = inputStream.Read(buf, 0, buf.Length)) > 0)
{
ms.Write(buf, 0, count);
}
return ms.ToArray();
}
}
///
/// Dynamically cast the object into target type.
///
/// Object to be casted
/// Target type
/// Casted object
{{#supportsAsync}}
public static dynamic ConvertType(dynamic fromObject, Type toObject)
{{/supportsAsync}}
{{^supportsAsync}}
public static object ConvertType(T fromObject, Type toObject) where T : class
{{/supportsAsync}}
{
return Convert.ChangeType(fromObject, toObject);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy