| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for |
| | 3 | | // license information. |
| | 4 | |
|
| | 5 | | namespace Microsoft.Azure.HDInsight.Job.Models |
| | 6 | | { |
| | 7 | | using System; |
| | 8 | | using System.Collections.Generic; |
| | 9 | | using System.Linq; |
| | 10 | | using System.Threading.Tasks; |
| | 11 | |
|
| | 12 | | internal static class ModelHelper |
| | 13 | | { |
| 2 | 14 | | internal static int MaxStringSizeForUrlEscapeData = 32766; |
| | 15 | |
|
| | 16 | | internal static string GetStatusDirectory(string status) |
| | 17 | | { |
| 38 | 18 | | var statusDir = status; |
| 38 | 19 | | if (string.IsNullOrWhiteSpace(statusDir)) |
| | 20 | | { |
| | 21 | | // Format of status directory: 2009-06-15T13-45-30-<guid> |
| 32 | 22 | | statusDir = DateTime.UtcNow.ToString("s", System.Globalization.CultureInfo.InvariantCulture); |
| | 23 | |
|
| | 24 | | // Can't create a directory with char ":" in azure storage |
| 32 | 25 | | statusDir = statusDir.Replace(':', '-'); |
| 32 | 26 | | statusDir = string.Format("{0}-{1}", statusDir, Guid.NewGuid().ToString()); |
| | 27 | | } |
| | 28 | |
|
| 38 | 29 | | return statusDir; |
| | 30 | | } |
| | 31 | |
|
| | 32 | | internal static string ConvertItemsToString(IEnumerable<KeyValuePair<string, string>> args) |
| | 33 | | { |
| 38 | 34 | | var strings = (from kvp in args |
| 170 | 35 | | select kvp.Key.EscapeDataString() + "=" + kvp.Value.EscapeDataString()).ToList(); |
| 38 | 36 | | return string.Join("&", strings); |
| | 37 | | } |
| | 38 | |
|
| | 39 | | internal static string EscapeDataString(this string inputValue) |
| | 40 | | { |
| 264 | 41 | | if (string.IsNullOrEmpty(inputValue)) |
| | 42 | | { |
| 0 | 43 | | return string.Empty; |
| | 44 | | } |
| | 45 | |
|
| 264 | 46 | | var escapeString = string.Empty; |
| 264 | 47 | | int i = 0; |
| | 48 | |
|
| 268 | 49 | | while (i < inputValue.Length / MaxStringSizeForUrlEscapeData) |
| | 50 | | { |
| 4 | 51 | | escapeString += Uri.EscapeDataString(inputValue.Substring(MaxStringSizeForUrlEscapeData * i, MaxStringSi |
| 4 | 52 | | i++; |
| | 53 | | } |
| | 54 | |
|
| 264 | 55 | | escapeString += Uri.EscapeDataString(inputValue.Substring(MaxStringSizeForUrlEscapeData * i)); |
| | 56 | |
|
| 264 | 57 | | return escapeString; |
| | 58 | | } |
| | 59 | |
|
| | 60 | | internal static IEnumerable<KeyValuePair<string, string>> BuildNameValueList(string paramName, IEnumerable<KeyVa |
| | 61 | | { |
| 8 | 62 | | List<KeyValuePair<string, string>> retval = new List<KeyValuePair<string, string>>(); |
| | 63 | |
|
| 68 | 64 | | foreach (var kvp in nameValuePairs) |
| | 65 | | { |
| 26 | 66 | | retval.Add(new KeyValuePair<string, string>(paramName, kvp.Key + "=" + kvp.Value)); |
| | 67 | | } |
| 8 | 68 | | return retval; |
| | 69 | | } |
| | 70 | |
|
| | 71 | | internal static IEnumerable<KeyValuePair<string, string>> BuildList(string type, IEnumerable<string> args) |
| | 72 | | { |
| 6 | 73 | | List<KeyValuePair<string, string>> retval = new List<KeyValuePair<string, string>>(); |
| | 74 | |
|
| 36 | 75 | | foreach (var arg in args) |
| | 76 | | { |
| 12 | 77 | | retval.Add(new KeyValuePair<string, string>(type, arg)); |
| | 78 | | } |
| 6 | 79 | | return retval; |
| | 80 | | } |
| | 81 | |
|
| | 82 | | internal static string BuildListToCommaSeparatedString(IEnumerable<string> input) |
| | 83 | | { |
| 0 | 84 | | return string.Join(",", input.ToArray()); |
| | 85 | | } |
| | 86 | |
|
| | 87 | | internal static void Delay(TimeSpan duration) |
| | 88 | | { |
| 380 | 89 | | Task.Delay(duration).Wait(); |
| 380 | 90 | | } |
| | 91 | | } |
| | 92 | | } |