| | 1 | | /* |
| | 2 | | * WeShare API |
| | 3 | | * |
| | 4 | | * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) |
| | 5 | | * |
| | 6 | | * The version of the OpenAPI document: 1.0 |
| | 7 | | * Generated by: https://github.com/openapitools/openapi-generator.git |
| | 8 | | */ |
| | 9 | |
|
| | 10 | |
|
| | 11 | | using System; |
| | 12 | | using System.Collections; |
| | 13 | | using System.Globalization; |
| | 14 | | using System.IO; |
| | 15 | | using System.Linq; |
| | 16 | | using System.Text; |
| | 17 | | using System.Text.RegularExpressions; |
| | 18 | |
|
| | 19 | | namespace Applications.WeShare.Swagger.Client |
| | 20 | | { |
| | 21 | | /// <summary> |
| | 22 | | /// Utility functions providing some benefit to API client consumers. |
| | 23 | | /// </summary> |
| | 24 | | public static class ClientUtils |
| | 25 | | { |
| | 26 | | /// <summary> |
| | 27 | | /// Sanitize filename by removing the path |
| | 28 | | /// </summary> |
| | 29 | | /// <param name="filename">Filename</param> |
| | 30 | | /// <returns>Filename</returns> |
| | 31 | | public static string SanitizeFilename(string filename) |
| 0 | 32 | | { |
| 0 | 33 | | Match match = Regex.Match(filename, @".*[/\\](.*)$"); |
| 0 | 34 | | return match.Success ? match.Groups[1].Value : filename; |
| 0 | 35 | | } |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Convert params to key/value pairs. |
| | 39 | | /// Use collectionFormat to properly format lists and collections. |
| | 40 | | /// </summary> |
| | 41 | | /// <param name="collectionFormat">The swagger-supported collection format, one of: csv, tsv, ssv, pipes, multi< |
| | 42 | | /// <param name="name">Key name.</param> |
| | 43 | | /// <param name="value">Value object.</param> |
| | 44 | | /// <returns>A multimap of keys with 1..n associated values.</returns> |
| | 45 | | public static Multimap<string, string> ParameterToMultiMap(string collectionFormat, string name, object value) |
| 0 | 46 | | { |
| 0 | 47 | | var parameters = new Multimap<string, string>(); |
| | 48 | |
|
| 0 | 49 | | if (value is ICollection collection && collectionFormat == "multi") |
| 0 | 50 | | { |
| 0 | 51 | | foreach (var item in collection) |
| 0 | 52 | | { |
| 0 | 53 | | parameters.Add(name, ParameterToString(item)); |
| 0 | 54 | | } |
| 0 | 55 | | } |
| 0 | 56 | | else if (value is IDictionary dictionary) |
| 0 | 57 | | { |
| 0 | 58 | | if(collectionFormat == "deepObject") { |
| 0 | 59 | | foreach (DictionaryEntry entry in dictionary) |
| 0 | 60 | | { |
| 0 | 61 | | parameters.Add(name + "[" + entry.Key + "]", ParameterToString(entry.Value)); |
| 0 | 62 | | } |
| 0 | 63 | | } |
| 0 | 64 | | else { |
| 0 | 65 | | foreach (DictionaryEntry entry in dictionary) |
| 0 | 66 | | { |
| 0 | 67 | | parameters.Add(entry.Key.ToString(), ParameterToString(entry.Value)); |
| 0 | 68 | | } |
| 0 | 69 | | } |
| 0 | 70 | | } |
| | 71 | | else |
| 0 | 72 | | { |
| 0 | 73 | | parameters.Add(name, ParameterToString(value)); |
| 0 | 74 | | } |
| | 75 | |
|
| 0 | 76 | | return parameters; |
| 0 | 77 | | } |
| | 78 | |
|
| | 79 | | /// <summary> |
| | 80 | | /// If parameter is DateTime, output in a formatted string (default ISO 8601), customizable with Configuration.D |
| | 81 | | /// If parameter is a list, join the list with ",". |
| | 82 | | /// Otherwise just return the string. |
| | 83 | | /// </summary> |
| | 84 | | /// <param name="obj">The parameter (header, path, query, form).</param> |
| | 85 | | /// <param name="configuration">An optional configuration instance, providing formatting options used in process |
| | 86 | | /// <returns>Formatted string.</returns> |
| | 87 | | public static string ParameterToString(object obj, IReadableConfiguration configuration = null) |
| 126 | 88 | | { |
| 126 | 89 | | if (obj is DateTime dateTime) |
| | 90 | | // Return a formatted date string - Can be customized with Configuration.DateTimeFormat |
| | 91 | | // Defaults to an ISO 8601, using the known as a Round-trip date/time pattern ("o") |
| | 92 | | // https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8 |
| | 93 | | // For example: 2009-06-15T13:45:30.0000000 |
| 0 | 94 | | return dateTime.ToString((configuration ?? GlobalConfiguration.Instance).DateTimeFormat); |
| 126 | 95 | | if (obj is DateTimeOffset dateTimeOffset) |
| | 96 | | // Return a formatted date string - Can be customized with Configuration.DateTimeFormat |
| | 97 | | // Defaults to an ISO 8601, using the known as a Round-trip date/time pattern ("o") |
| | 98 | | // https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8 |
| | 99 | | // For example: 2009-06-15T13:45:30.0000000 |
| 0 | 100 | | return dateTimeOffset.ToString((configuration ?? GlobalConfiguration.Instance).DateTimeFormat); |
| 126 | 101 | | if (obj is bool boolean) |
| 0 | 102 | | return boolean ? "true" : "false"; |
| 126 | 103 | | if (obj is ICollection collection) |
| 0 | 104 | | return string.Join(",", collection.Cast<object>()); |
| | 105 | |
|
| 126 | 106 | | return Convert.ToString(obj, CultureInfo.InvariantCulture); |
| 126 | 107 | | } |
| | 108 | |
|
| | 109 | | /// <summary> |
| | 110 | | /// Serializes the given object when not null. Otherwise return null. |
| | 111 | | /// </summary> |
| | 112 | | /// <param name="obj">The object to serialize.</param> |
| | 113 | | /// <returns>Serialized string.</returns> |
| | 114 | | public static string Serialize(object obj) |
| 0 | 115 | | { |
| 0 | 116 | | return obj != null ? Newtonsoft.Json.JsonConvert.SerializeObject(obj) : null; |
| 0 | 117 | | } |
| | 118 | |
|
| | 119 | | /// <summary> |
| | 120 | | /// Encode string in base64 format. |
| | 121 | | /// </summary> |
| | 122 | | /// <param name="text">string to be encoded.</param> |
| | 123 | | /// <returns>Encoded string.</returns> |
| | 124 | | public static string Base64Encode(string text) |
| 0 | 125 | | { |
| 0 | 126 | | return Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(text)); |
| 0 | 127 | | } |
| | 128 | |
|
| | 129 | | /// <summary> |
| | 130 | | /// Convert stream to byte array |
| | 131 | | /// </summary> |
| | 132 | | /// <param name="inputStream">Input stream to be converted</param> |
| | 133 | | /// <returns>Byte array</returns> |
| | 134 | | public static byte[] ReadAsBytes(Stream inputStream) |
| 0 | 135 | | { |
| 0 | 136 | | using (var ms = new MemoryStream()) |
| 0 | 137 | | { |
| 0 | 138 | | inputStream.CopyTo(ms); |
| 0 | 139 | | return ms.ToArray(); |
| | 140 | | } |
| 0 | 141 | | } |
| | 142 | |
|
| | 143 | | /// <summary> |
| | 144 | | /// Select the Content-Type header's value from the given content-type array: |
| | 145 | | /// if JSON type exists in the given array, use it; |
| | 146 | | /// otherwise use the first one defined in 'consumes' |
| | 147 | | /// </summary> |
| | 148 | | /// <param name="contentTypes">The Content-Type array to select from.</param> |
| | 149 | | /// <returns>The Content-Type header to use.</returns> |
| | 150 | | public static string SelectHeaderContentType(string[] contentTypes) |
| 34 | 151 | | { |
| 34 | 152 | | if (contentTypes.Length == 0) |
| 27 | 153 | | return null; |
| | 154 | |
|
| 28 | 155 | | foreach (var contentType in contentTypes) |
| 7 | 156 | | { |
| 7 | 157 | | if (IsJsonMime(contentType)) |
| 7 | 158 | | return contentType; |
| 0 | 159 | | } |
| | 160 | |
|
| 0 | 161 | | return contentTypes[0]; // use the first content type specified in 'consumes' |
| 34 | 162 | | } |
| | 163 | |
|
| | 164 | | /// <summary> |
| | 165 | | /// Select the Accept header's value from the given accepts array: |
| | 166 | | /// if JSON exists in the given array, use it; |
| | 167 | | /// otherwise use all of them (joining into a string) |
| | 168 | | /// </summary> |
| | 169 | | /// <param name="accepts">The accepts array to select from.</param> |
| | 170 | | /// <returns>The Accept header to use.</returns> |
| | 171 | | public static string SelectHeaderAccept(string[] accepts) |
| 34 | 172 | | { |
| 34 | 173 | | if (accepts.Length == 0) |
| 2 | 174 | | return null; |
| | 175 | |
|
| 32 | 176 | | if (accepts.Contains("application/json", StringComparer.OrdinalIgnoreCase)) |
| 32 | 177 | | return "application/json"; |
| | 178 | |
|
| 0 | 179 | | return string.Join(",", accepts); |
| 34 | 180 | | } |
| | 181 | |
|
| | 182 | | /// <summary> |
| | 183 | | /// Provides a case-insensitive check that a provided content type is a known JSON-like content type. |
| | 184 | | /// </summary> |
| 1 | 185 | | public static readonly Regex JsonRegex = new Regex("(?i)^(application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(;.* |
| | 186 | |
|
| | 187 | | /// <summary> |
| | 188 | | /// Check if the given MIME is a JSON MIME. |
| | 189 | | /// JSON MIME examples: |
| | 190 | | /// application/json |
| | 191 | | /// application/json; charset=UTF8 |
| | 192 | | /// APPLICATION/JSON |
| | 193 | | /// application/vnd.company+json |
| | 194 | | /// </summary> |
| | 195 | | /// <param name="mime">MIME</param> |
| | 196 | | /// <returns>Returns True if MIME type is json.</returns> |
| | 197 | | public static bool IsJsonMime(string mime) |
| 7 | 198 | | { |
| 7 | 199 | | if (string.IsNullOrWhiteSpace(mime)) return false; |
| | 200 | |
|
| 7 | 201 | | return JsonRegex.IsMatch(mime) || mime.Equals("application/json-patch+json"); |
| 7 | 202 | | } |
| | 203 | | } |
| | 204 | | } |