2222using System ;
2323using System . Collections ;
2424using System . Collections . Generic ;
25- using System . Diagnostics ;
2625using System . Linq ;
2726using System . Net . Http ;
2827using System . Reflection ;
29- using System . Runtime . Serialization ;
3028using System . Text ;
3129
3230namespace 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 }
0 commit comments