Skip to content

Commit 5183da9

Browse files
release 3.1.164 source code
1 parent 78da677 commit 5183da9

File tree

1,318 files changed

+96513
-25408
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,318 files changed

+96513
-25408
lines changed

CHANGELOG.md

Lines changed: 577 additions & 0 deletions
Large diffs are not rendered by default.

CHANGELOG_CN.md

Lines changed: 577 additions & 0 deletions
Large diffs are not rendered by default.

Core/Utils/HttpUtils.cs

Lines changed: 11 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@
2222
using System;
2323
using System.Collections;
2424
using System.Collections.Generic;
25-
using System.Diagnostics;
2625
using System.Linq;
2726
using System.Net.Http;
2827
using System.Reflection;
29-
using System.Runtime.Serialization;
3028
using System.Text;
3129

3230
namespace HuaweiCloud.SDK.Core
@@ -44,57 +42,17 @@ public static class HttpUtils
4442
private const string HttpHeaders = "HttpHeaders";
4543
private const string SetStream = "SetStream";
4644

47-
private static readonly List<string> HttpContentHeadersList = new List<string>
48-
{
49-
ContentType,
50-
ContentLength,
51-
"Allow",
52-
"Content-Disposition",
53-
"Content-Encoding",
54-
"Content-Language",
55-
"Content-Location",
56-
"Content-MD5",
57-
"Content-Range",
58-
"Expires",
59-
"Last-Modified",
60-
};
61-
6245
public static string AddUrlPath(string path, Dictionary<string, string> pathParams)
6346
{
6447
return pathParams.Aggregate(path,
6548
(current, keyValuePair) => current.Replace("{" + keyValuePair.Key + "}",
6649
keyValuePair.Value));
6750
}
6851

69-
private static string ConvertToString(object value)
70-
{
71-
if (value is bool boolean)
72-
{
73-
return Convert.ToString(boolean).ToLowerInvariant();
74-
}
75-
if (value is Enum enumValue)
76-
{
77-
var type = enumValue.GetType();
78-
var name = Enum.GetName(type, enumValue);
79-
if (name == null) return string.Empty;
80-
81-
var field = type.GetField(name);
82-
if (field == null) return string.Empty;
83-
84-
var attribute = field.GetCustomAttribute<EnumMemberAttribute>();
85-
return attribute?.Value;
86-
}
87-
88-
return Convert.ToString(value);
89-
}
90-
9152
private static StringBuilder BuildQueryStringParameter(string key, object value)
9253
{
9354
var sb = new StringBuilder();
94-
var str = ConvertToString(value);
95-
if (string.IsNullOrEmpty(str)) return sb;
96-
97-
return sb.Append(key).Append("=").Append(str).Append("&");
55+
return StringUtils.TryConvertToNonEmptyString(value, out var str) ? sb.Append(key).Append("=").Append(str).Append("&") : sb;
9856
}
9957

10058
private static StringBuilder BuildQueryListParameter(string key, IList list)
@@ -231,23 +189,12 @@ private static void ProcessRequestParams(object obj, SdkRequest request, string
231189
var value = propertyInfo.GetValue(obj, null);
232190
if (value == null) continue;
233191

234-
if (sdkPropertyAttribute.IsCname)
235-
{
236-
var cname = Convert.ToString(value);
237-
if (!string.IsNullOrEmpty(cname)) request.Cname = cname;
238-
}
239-
else if (sdkPropertyAttribute.IsQuery)
240-
{
241-
ProcessQueryParams(querySb, sdkPropertyAttribute.PropertyName, value);
242-
}
243-
else if (sdkPropertyAttribute.IsHeader)
192+
if (sdkPropertyAttribute.IsQuery) ProcessQueryParams(querySb, sdkPropertyAttribute.PropertyName, value);
193+
else if (sdkPropertyAttribute.IsBody) ProcessRequestBody(request, value, contentType);
194+
else if (StringUtils.TryConvertToNonEmptyString(value, out var strVal))
244195
{
245-
var strVal = Convert.ToString(value);
246-
if (!string.IsNullOrEmpty(strVal)) headers.Add(sdkPropertyAttribute.PropertyName, strVal);
247-
}
248-
else if (sdkPropertyAttribute.IsBody)
249-
{
250-
ProcessRequestBody(request, value, contentType);
196+
if (sdkPropertyAttribute.IsHeader) headers.Add(sdkPropertyAttribute.PropertyName, strVal);
197+
else if (sdkPropertyAttribute.IsCname) request.Cname = strVal;
251198
}
252199
}
253200

@@ -298,7 +245,7 @@ public static T DeSerializeStream<T>(HttpResponseMessage message)
298245
var t = Activator.CreateInstance<T>();
299246
t.GetType().GetProperty(HttpStatusCode)?.SetValue(t, (int)message.StatusCode, null);
300247
t.GetType().GetProperty(HttpHeaders)?.SetValue(t, message.Headers.ToString(), null);
301-
var flag = BindingFlags.Public | BindingFlags.Instance;
248+
const BindingFlags flag = BindingFlags.Public | BindingFlags.Instance;
302249
t.GetType().GetMethod(SetStream)
303250
?.Invoke(t, flag, Type.DefaultBinder,
304251
new object[]
@@ -343,7 +290,6 @@ public static T DeSerializeStream<R, T>(R progressRequest, HttpResponseMessage m
343290
}
344291

345292
streamResponse.SetStream(stream);
346-
347293
return streamResponse;
348294
}
349295

@@ -362,46 +308,11 @@ public static void SetResponseHeaders<T>(HttpResponseMessage message, T obj)
362308

363309
foreach (var property in properties)
364310
{
365-
string oriAttrName = null;
366-
var customAttrs = property.GetCustomAttributes(typeof(SDKPropertyAttribute), true);
367-
if (customAttrs.Length > 0)
368-
{
369-
SDKPropertyAttribute sdkPropertyAttribute = null;
370-
foreach (var customAttr in customAttrs)
371-
{
372-
if (customAttr is SDKPropertyAttribute propertyAttribute)
373-
{
374-
sdkPropertyAttribute = propertyAttribute;
375-
}
376-
377-
if (sdkPropertyAttribute == null || !sdkPropertyAttribute.IsHeader)
378-
{
379-
continue;
380-
}
381-
382-
oriAttrName = sdkPropertyAttribute.PropertyName;
383-
}
384-
}
385-
386-
if (string.IsNullOrEmpty(oriAttrName))
387-
{
388-
continue;
389-
}
311+
var propertyAttribute = GetSdkPropertyAttribute(property);
312+
if (propertyAttribute == null || !propertyAttribute.IsHeader || string.IsNullOrEmpty(propertyAttribute.PropertyName)) continue;
390313

391-
if (HttpContentHeadersList.Contains(oriAttrName))
392-
{
393-
if (message.Content.Headers.Contains(oriAttrName))
394-
{
395-
property.SetValue(obj, message.Content.Headers.GetValues(oriAttrName).First());
396-
}
397-
}
398-
else
399-
{
400-
if (message.Headers.Contains(oriAttrName))
401-
{
402-
property.SetValue(obj, message.Headers.GetValues(oriAttrName).First());
403-
}
404-
}
314+
if (message.Headers.TryGetValues(propertyAttribute.PropertyName, out var values)) property.SetValue(obj, values.First());
315+
else if (message.Content.Headers.TryGetValues(propertyAttribute.PropertyName, out values)) property.SetValue(obj, values.First());
405316
}
406317
}
407318
}

Core/Utils/StringUtils.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,17 @@
1919
* under the License.
2020
*/
2121

22+
using System;
2223
using System.Linq;
24+
using System.Reflection;
25+
using System.Runtime.Serialization;
2326

2427
namespace HuaweiCloud.SDK.Core
2528
{
2629
public class StringUtils
2730
{
31+
private const string LowerTrue = "true";
32+
private const string LowerFalse = "false";
2833
public static string ToSnakeCase(string str)
2934
{
3035
return string.Concat(str.Select((x, i) => i > 0 && char.IsUpper(x) ? "_" + x : x.ToString()))
@@ -42,5 +47,33 @@ public static string UnderscoreToCamel(string str)
4247
}
4348
return str;
4449
}
50+
51+
public static bool TryConvertToNonEmptyString(object value, out string result)
52+
{
53+
result = string.Empty;
54+
if (value is bool boolean)
55+
{
56+
result = boolean ? LowerTrue : LowerFalse;
57+
return true;
58+
}
59+
if (value is Enum enumValue)
60+
{
61+
var type = enumValue.GetType();
62+
var name = Enum.GetName(type, enumValue);
63+
if (name == null) return false;
64+
65+
var field = type.GetField(name);
66+
if (field == null) return false;
67+
68+
var attribute = field.GetCustomAttribute<EnumMemberAttribute>();
69+
if (attribute == null) return false;
70+
71+
result = attribute.Value;
72+
return true;
73+
}
74+
75+
result = Convert.ToString(value);
76+
return !string.IsNullOrEmpty(result);
77+
}
4578
}
4679
}

0 commit comments

Comments
 (0)