// This file is auto-generated, don't edit it. Thanks. using System; using System.IO; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.Linq; using System.Reflection; using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; using System.Runtime.InteropServices; using Newtonsoft.Json; using Darabonba; using Darabonba.Utils; using Tea; namespace AlibabaCloud.OpenApiClient { /// Description: /// /// This is for OpenApi Util /// public class Utils { public static Dictionary StringifyMapValue(Dictionary map) { if (map == null) { return null; } Dictionary result = new Dictionary(); foreach (var entry in map) { result.Add(entry.Key, entry.Value.ToSafeString()); } return result; } internal static readonly string SEPARATOR = "&"; internal static readonly string PEM_BEGIN = "-----BEGIN RSA PRIVATE KEY-----\n"; internal static readonly string PEM_END = "\n-----END RSA PRIVATE KEY-----"; /// Description: /// /// Convert all params of body other than type of readable into content /// /// /// /// source Model /// /// /// target Model /// /// /// /// void /// public static void Convert(TeaModel body, TeaModel content) { if (body == null || content == null) { return; } Dictionary dict = body.ToMap(); dict = (Dictionary)ExceptStream(dict); Model.ToObject(dict, content); } internal static object ExceptStream(object obj) { if (typeof(IList).IsAssignableFrom(obj.GetType()) && !typeof(Array).IsAssignableFrom(obj.GetType())) { List array = new List(); foreach (var temp in (IList)obj) { if (temp != null) { object item = ExceptStream(temp); if (item != null) { array.Add(item); } } else { array.Add(temp); } } return array; } else if (typeof(IDictionary).IsAssignableFrom(obj.GetType())) { Dictionary dict = ((IDictionary)obj).Keys.Cast() .ToDictionary(key => key, key => ((IDictionary)obj)[key]); Dictionary result = new Dictionary(); foreach (var keypair in dict) { if (keypair.Value != null) { object item = ExceptStream(keypair.Value); if (item != null) { result.Add(keypair.Key, item); } } else { result.Add(keypair.Key, keypair.Value); } } return result; } else if (typeof(Stream).IsAssignableFrom(obj.GetType())) { return null; } return obj; } /// Description: /// /// If endpointType is internal, use internal endpoint /// If serverUse is true and endpointType is accelerate, use accelerate endpoint /// Default return endpoint /// /// /// /// whether use accelerate endpoint /// /// /// value must be internal or accelerate /// /// /// /// the final endpoint /// public static string GetEndpoint(string endpoint, bool? useAccelerate, string endpointType) { if (endpointType == "internal") { string[] strs = endpoint.Split('.'); strs[0] += "-internal"; endpoint = string.Join(".", strs); } if (useAccelerate == true && endpointType == "accelerate") { return "oss-accelerate.aliyuncs.com"; } return endpoint; } /// Description: /// /// Hash the raw data with signatureAlgorithm /// /// /// /// hashing data /// /// /// the autograph method /// /// /// /// hashed bytes /// public static byte[] Hash(byte[] raw, string signatureAlgorithm) { if (signatureAlgorithm == "ACS3-HMAC-SHA256" || signatureAlgorithm == "ACS3-RSA-SHA256") { byte[] signData; using (SHA256 sha256 = new SHA256Managed()) { signData = sha256.ComputeHash(raw); } return signData; } return null; } public static void ApplyRetryHeaders(Dictionary headers, int retriesAttempted, int backoffDelay) { if (retriesAttempted <= 0 || headers == null) { return; } headers["x-acs-retry-attempts"] = retriesAttempted.ToString(); headers["x-acs-retry-delay"] = backoffDelay.ToString(); } public static long? GetThrottlingTimeLeft(Dictionary headers) { string retryAfter = headers.ContainsKey("x-acs-retry-after") ? headers["x-acs-retry-after"] : null; if (string.IsNullOrEmpty(retryAfter)) { return null; } long timeLeftValue; // Only a positive wait time is valid; missing/empty/invalid/<=0 → null // so callers' null check won't treat 0 as a usable backoff. if (!long.TryParse(retryAfter, out timeLeftValue) || timeLeftValue <= 0) { return null; } return timeLeftValue; } /// Description: /// /// Get throttling param /// /// /// /// response headers /// /// /// /// time left /// // public static Dictionary FlatMap(Dictionary params_, string prefix) // { // throw new NotImplementedException(); // } /// Description: /// /// Generate a nonce string /// /// /// /// the nonce string /// public static string GetNonce() { return Guid.NewGuid().ToString(); } /// Description: /// /// Get the string to be signed according to request /// /// /// /// which contains signed messages /// /// /// /// the signed string /// public static string GetStringToSign(Request request) { string method = request.Method; string pathname = request.Pathname; Dictionary headers = request.Headers; Dictionary query = request.Query; string accept = headers.ContainsKey("accept") ? headers["accept"] : ""; string contentMD5 = headers.ContainsKey("content-md5") ? headers["content-md5"] : ""; string contentType = headers.ContainsKey("content-type") ? headers["content-type"] : ""; string date = headers.ContainsKey("date") ? headers["date"] : ""; string header = method + "\n" + accept + "\n" + contentMD5 + "\n" + contentType + "\n" + date + "\n"; string canonicalizedHeaders = GetCanonicalizedHeaders(headers); string canonicalizedResource = GetCanonicalizedResource(pathname, query); string stringToSign = header + canonicalizedHeaders + canonicalizedResource; return stringToSign; } internal static string GetCanonicalizedHeaders(Dictionary headers) { string prefix = "x-acs-"; List canonicalizedKeys = new List(); canonicalizedKeys = headers.Where(p => p.Key.StartsWith(prefix)) .Select(p => p.Key).ToList(); canonicalizedKeys.Sort(StringComparer.Ordinal); StringBuilder result = new StringBuilder(); for (int i = 0; i < canonicalizedKeys.Count; i++) { string key = canonicalizedKeys[i]; result.Append(key); result.Append(":"); result.Append(headers[key].Trim()); result.Append("\n"); } return result.ToString(); } internal static string GetCanonicalizedResource(string pathname, Dictionary query) { if (query == null || query.Count <= 0) { return pathname; } List keys = query.Keys.ToList(); keys.Sort(StringComparer.Ordinal); string key; List result = new List(); for (int i = 0; i < keys.Count; i++) { key = keys[i]; if (query[key] == null) { continue; } if (query[key] == "") { result.Add(key); } else { result.Add(key + "=" + query[key]); } } return pathname + "?" + string.Join("&", result); } /// Description: /// /// Get signature according to stringToSign, secret /// /// /// /// the signed string /// /// /// accesskey secret /// /// /// /// the signature /// public static string GetROASignature(string stringToSign, string secret) { byte[] signData; using (KeyedHashAlgorithm algorithm = CryptoConfig.CreateFromName("HMACSHA1") as KeyedHashAlgorithm) { algorithm.Key = Encoding.UTF8.GetBytes(secret); signData = algorithm.ComputeHash(Encoding.UTF8.GetBytes(stringToSign.ToCharArray())); } return System.Convert.ToBase64String(signData); } /// Description: /// /// Parse filter into a form string /// /// /// /// object /// /// /// /// the string /// public static string ToForm(Dictionary filter) { if (filter == null) { return string.Empty; } Dictionary dict = filter.Keys.Cast().ToDictionary(key => key, key => filter[key]); Dictionary outDict = new Dictionary(); TileDict(outDict, dict); List listStr = new List(); foreach (var keypair in outDict) { if (string.IsNullOrWhiteSpace(keypair.Value)) { continue; } listStr.Add(PercentEncode(keypair.Key) + "=" + PercentEncode(keypair.Value)); } return string.Join("&", listStr); } internal static string PercentEncode(string value) { if (value == null) { return null; } var stringBuilder = new StringBuilder(); var text = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~"; var bytes = Encoding.UTF8.GetBytes(value); foreach (char c in bytes) { if (text.IndexOf(c) >= 0) { stringBuilder.Append(c); } else { stringBuilder.Append("%").Append(string.Format(CultureInfo.InvariantCulture, "{0:X2}", (int)c)); } } return stringBuilder.ToString().Replace("+", "%20") .Replace("*", "%2A").Replace("%7E", "~"); } internal static object ToJsonObject(object obj) { if (obj == null) { return null; } if (typeof(IDictionary).IsAssignableFrom(obj.GetType())) { Dictionary dicIn = ((IDictionary)obj).Keys.Cast() .ToDictionary(key => key, key => ((IDictionary)obj)[key]); Dictionary result = new Dictionary(); foreach (var keypair in dicIn) { if (keypair.Value == null) { continue; } result.Add(keypair.Key, ToJsonObject(keypair.Value)); } return result; } else if (typeof(Model).IsAssignableFrom(obj.GetType())) { Dictionary dicIn = ((Dictionary)((Model)obj).ToMap()); return ToJsonObject(dicIn); } else if (typeof(IList).IsAssignableFrom(obj.GetType()) && !typeof(Array).IsAssignableFrom(obj.GetType())) { List array = new List(); foreach (var temp in (IList)obj) { array.Add(ToJsonObject(temp)); } return array; } return obj; } /// Description: /// /// Get timestamp /// /// /// /// the timestamp string /// public static string GetTimestamp() { return DateTime.UtcNow.ToString("yyyy-MM-dd'T'HH:mm:ss'Z'", CultureInfo.InvariantCulture); } /// Description: /// /// Get UTC string /// /// /// /// the UTC string /// public static string GetDateUTCString() { return DateTime.UtcNow.ToUniversalTime().GetDateTimeFormats('r')[0]; } /// Description: /// /// Parse filter into a object which's type is map[string]string /// /// /// /// query param /// /// /// /// the object /// public static Dictionary Query(IDictionary filter) { Dictionary outDict = new Dictionary(); TileDict(outDict, filter); return outDict; } internal static void TileDict(Dictionary dicOut, object obj, string parentKey = "") { if (obj == null) { return; } if (typeof(IDictionary).IsAssignableFrom(obj.GetType())) { Dictionary dicIn = ((IDictionary)obj).Keys.Cast() .ToDictionary(key => key, key => ((IDictionary)obj)[key]); foreach (var keypair in dicIn) { string keyName = parentKey + "." + keypair.Key; if (keypair.Value == null) { continue; } TileDict(dicOut, keypair.Value, keyName); } } else if (typeof(Model).IsAssignableFrom(obj.GetType())) { Dictionary dicIn = ((Dictionary)((Model)obj).ToMap()); foreach (var keypair in dicIn) { string keyName = parentKey + "." + keypair.Key; if (keypair.Value == null) { continue; } TileDict(dicOut, keypair.Value, keyName); } } else if (typeof(IList).IsAssignableFrom(obj.GetType()) && !typeof(Array).IsAssignableFrom(obj.GetType())) { int index = 1; foreach (var temp in (IList)obj) { TileDict(dicOut, temp, parentKey + "." + index.ToSafeString()); index++; } } else { if (obj.GetType() == typeof(byte[])) { dicOut.Add(parentKey.TrimStart('.'), Encoding.UTF8.GetString((byte[])obj)); } else { dicOut.Add(parentKey.TrimStart('.'), obj.ToSafeString("")); } } } /// Description: /// /// Get signature according to signedParams, method and secret /// /// /// /// params which need to be signed /// /// /// http method e.g. GET /// /// /// AccessKeySecret /// /// /// /// the signature /// public static string GetRPCSignature(Dictionary signedParams, string method, string secret) { List sortedKeys = signedParams.Keys.ToList(); sortedKeys.Sort(StringComparer.Ordinal); StringBuilder canonicalizedQueryString = new StringBuilder(); foreach (string key in sortedKeys) { if (signedParams[key] != null) { canonicalizedQueryString.Append("&") .Append(PercentEncode(key)).Append("=") .Append(PercentEncode(signedParams[key])); } } StringBuilder stringToSign = new StringBuilder(); stringToSign.Append(method); stringToSign.Append(SEPARATOR); stringToSign.Append(PercentEncode("/")); stringToSign.Append(SEPARATOR); stringToSign.Append(PercentEncode( canonicalizedQueryString.ToString().Substring(1))); System.Diagnostics.Debug.WriteLine("GetRPCSignature:stringToSign is " + stringToSign.ToString()); byte[] signData; using (KeyedHashAlgorithm algorithm = CryptoConfig.CreateFromName("HMACSHA1") as KeyedHashAlgorithm) { algorithm.Key = Encoding.UTF8.GetBytes(secret + SEPARATOR); signData = algorithm.ComputeHash(Encoding.UTF8.GetBytes(stringToSign.ToString().ToCharArray())); } string signedStr = System.Convert.ToBase64String(signData); return signedStr; } /// Description: /// /// Parse array into a string with specified style /// /// /// /// the array /// /// /// the prefix string /// /// /// /// the string /// public static string ArrayToStringWithSpecifiedStyle(object array, string prefix, string style) { if (array == null) { return string.Empty; } switch (style.ToLower()) { case "repeatlist": Dictionary map = new Dictionary(); map.Add(prefix, array); return ToForm(map); case "simple": case "spacedelimited": case "pipedelimited": return FlatArray((IList)array, style); case "json": return JsonConvert.SerializeObject(ToJsonObject(array)); default: return string.Empty; } } /// Description: /// /// Parse map with flat style /// /// /// /// the input /// /// /// /// any /// // public static object MapToFlatStyle(object input) // { // throw new NotImplementedException(); // } /// Description: /// /// Transform input as map. /// public static Dictionary ParseToMap(object input) { if (input == null) { return null; } Type type = input.GetType(); var map = (Dictionary)ModelExtensions.ToMapFactory(type, input); return map; } /// Description: /// /// Get the authorization /// /// /// /// request params /// /// /// the autograph method /// /// /// the hashed request /// /// /// the accesskey string /// /// /// the accessKeySecret string /// /// /// /// authorization string /// public static string GetAuthorization(Request request, string signatureAlgorithm, string payload, string accesskey, string accessKeySecret) { string canonicalURI = request.Pathname.ToSafeString("") == "" ? "/" : request.Pathname.Replace("+", "%20").Replace("*", "%2A").Replace("%7E", "~"); string method = request.Method; string canonicalQueryString = GetAuthorizationQueryString(request.Query); Tuple> tuple = GetAuthorizationHeaders(request.Headers); string canonicalheaders = tuple.Item1; var signedHeaders = tuple.Item2; string canonicalRequest = method + "\n" + canonicalURI + "\n" + canonicalQueryString + "\n" + canonicalheaders + "\n" + string.Join(";", signedHeaders) + "\n" + payload; byte[] raw = Encoding.UTF8.GetBytes(canonicalRequest); string StringToSign = signatureAlgorithm + "\n" + BytesUtils.ToHex(Hash(raw, signatureAlgorithm)); System.Diagnostics.Debug.WriteLine("GetAuthorization:stringToSign is " + StringToSign); var signature = BytesUtils.ToHex(SignatureMethod(accessKeySecret, StringToSign, signatureAlgorithm)); string auth = signatureAlgorithm + " Credential=" + accesskey + ",SignedHeaders=" + string.Join(";", signedHeaders) + ",Signature=" + signature; return auth; } internal static byte[] SignatureMethod(string secret, string source, string signatureAlgorithm) { if (signatureAlgorithm == "ACS3-HMAC-SHA256") { byte[] signData; using (KeyedHashAlgorithm algorithm = CryptoConfig.CreateFromName("HMACSHA256") as KeyedHashAlgorithm) { algorithm.Key = Encoding.UTF8.GetBytes(secret); signData = algorithm.ComputeHash(Encoding.UTF8.GetBytes(source.ToSafeString().ToCharArray())); } return signData; } return null; } internal static string GetAuthorizationQueryString(Dictionary query) { string canonicalQueryString = string.Empty; if (query == null || query.Count <= 0) { return canonicalQueryString; } var hs = query.OrderBy(p => p.Key, StringComparer.Ordinal).ToDictionary(p => p.Key, p => p.Value); foreach (var keypair in hs) { if (keypair.Value != null) { canonicalQueryString += string.Format("&{0}={1}", keypair.Key, PercentEncode(keypair.Value)); } } if (!string.IsNullOrEmpty(canonicalQueryString)) { canonicalQueryString = canonicalQueryString.TrimStart('&'); } return canonicalQueryString; } internal static Tuple> GetAuthorizationHeaders(Dictionary headers) { string canonicalheaders = string.Empty; var tmp = new Dictionary>(); foreach (var keypair in headers) { var lowerKey = keypair.Key.ToLower(); if (lowerKey.StartsWith("x-acs-") || lowerKey == "host" || lowerKey == "content-type") { if (tmp.ContainsKey(lowerKey)) { tmp[lowerKey].Add(keypair.Value.ToSafeString().Trim()); } else { tmp[lowerKey] = new List { keypair.Value.ToSafeString().Trim() }; } } } var hs = tmp.OrderBy(p => p.Key, StringComparer.Ordinal).ToDictionary(p => p.Key, p => p.Value); foreach (var keypair in hs) { var listSort = new List(keypair.Value); listSort.Sort(StringComparer.Ordinal); canonicalheaders += string.Format("{0}:{1}\n", keypair.Key, string.Join(", ", listSort)); } return new Tuple>(canonicalheaders, hs.Keys.ToList()); } public static string GetUserAgent(string userAgent) { var defaultUserAgent = GetDefaultUserAgent(); if (!string.IsNullOrEmpty(userAgent)) { return defaultUserAgent + " " + userAgent; } return defaultUserAgent; } internal static string GetDefaultUserAgent() { string OSVersion = Environment.OSVersion.ToString(); string ClientVersion = GetRuntimeRegexValue(RuntimeEnvironment.GetRuntimeDirectory()); string CoreVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString(); string fileVersion = GetFileVersion(); return string.Format("AlibabaCloud ({0}) {1} Core/{2} TeaDSL/2", OSVersion, ClientVersion, fileVersion); } private static string GetFileVersion() { var versionInfo = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location); return string.Format("{0}.{1}.{2}", versionInfo.FileMajorPart, versionInfo.FileMinorPart, versionInfo.FileBuildPart); } internal static string FlatArray(IList array, string sty) { List strs = new List(); for (int i = 0; i < array.Count; i++) { strs.Add(array[i].ToSafeString()); } if (sty.ToSafeString().ToLower() == "simple".ToLower()) { return string.Join(",", strs); } else if (sty.ToSafeString().ToLower() == "spaceDelimited".ToLower()) { return string.Join(" ", strs); } else { return string.Join("|", strs); } } internal static string GetRuntimeRegexValue(string value) { var rx = new Regex(@"(\.NET).*(\\|\/).*(\d)", RegexOptions.Compiled | RegexOptions.IgnoreCase); var matches = rx.Match(value); char[] separator = { '\\', '/' }; if (matches.Success) { var clientValueArray = matches.Value.Split(separator); return BuildClientVersion(clientValueArray); } return "RuntimeNotFound"; } internal static string BuildClientVersion(string[] value) { var finalValue = ""; for (var i = 0; i < value.Length - 1; ++i) { finalValue += value[i].Replace(".", "").ToLower(); } finalValue += "/" + value[value.Length - 1]; return finalValue; } public static List> ToArray(object input) { try { var listModel = (IList)input; var listResult = new List>(); foreach (var model in listModel) { if (model != null) { listResult.Add(((Model)model).ToMap()); } } return listResult; } catch { return null; } } public static string GetEndpointRules(string product, string regionId, string endpointType, string network, string suffix) { string result; if (!string.IsNullOrEmpty(network) && network != "public") { network = "-" + network; } else { network = ""; } suffix = suffix ?? ""; if (suffix.Length > 0) { suffix = "-" + suffix; } if (endpointType == "regional") { if (string.IsNullOrEmpty(regionId)) { throw new ArgumentException("RegionId is empty, please set a valid RegionId"); } result = string.Format("{0}{1}{2}.{3}.aliyuncs.com", product, suffix, network, regionId); } else { result = string.Format("{0}{1}{2}.aliyuncs.com", product, suffix, network); } return result; } /// Description: /// /// Transform a map to a flat style map where keys are prefixed with length info. /// Map keys are transformed from "key" to "#length#key" format. /// /// /// /// the input object (can be a TeaModel, List, Dictionary, or other types) /// /// /// /// the transformed object /// public static object MapToFlatStyle(object obj) { if (obj == null) { return obj; } if (typeof(IList).IsAssignableFrom(obj.GetType()) && !typeof(Array).IsAssignableFrom(obj.GetType())) { List list = new List(); foreach (var item in (IList)obj) { list.Add(MapToFlatStyle(item)); } return list; } else if (typeof(TeaModel).IsAssignableFrom(obj.GetType())) { // Modify the original TeaModel object's fields TeaModel model = (TeaModel)obj; FieldInfo[] fields = model.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance); foreach (FieldInfo field in fields) { object value = field.GetValue(model); if (value != null && typeof(IDictionary).IsAssignableFrom(value.GetType())) { // This is a dictionary, apply flat style to keys Dictionary flatMap = new Dictionary(); Dictionary valueDict = ((IDictionary)value).Keys.Cast() .ToDictionary(key => key, key => ((IDictionary)value)[key]); foreach (var entry in valueDict) { flatMap.Add(string.Format("#{0}#{1}", entry.Key.Length, entry.Key), MapToFlatStyle(entry.Value)); } field.SetValue(model, flatMap); } else { // Recursively process other fields field.SetValue(model, MapToFlatStyle(value)); } } return model; // Return the modified original TeaModel } else if (typeof(IDictionary).IsAssignableFrom(obj.GetType())) { Dictionary flatMap = new Dictionary(); Dictionary dicIn = ((IDictionary)obj).Keys.Cast() .ToDictionary(key => key, key => ((IDictionary)obj)[key]); foreach (var keypair in dicIn) { flatMap.Add(string.Format("#{0}#{1}", keypair.Key.Length, keypair.Key), MapToFlatStyle(keypair.Value)); } return flatMap; } return obj; } } }