protected virtual IDictionary <string, string> BuildParameters <TRequest>(TRequest request) where TRequest : ApiRequest { var allParameters = new ApiDictionary(request.GetParameters()); allParameters.Add(ApiConstant.PARAM_VERSION_KEY, API_VERSION); allParameters.Add(ApiConstant.PARAM_TIMESTAMP_KEY, DateTime.UtcNow); return(allParameters); }
protected override IDictionary <string, string> BuildParameters <TRequest>(TRequest request) { var allParameters = new ApiDictionary(request.GetParameters()); allParameters.Add(ApiConstant.PARAM_METHOD_KEY, ApiUtils.GetApiName(request.GetType())); allParameters.Add(ApiConstant.PARAM_VERSION_KEY, API_VERSION); allParameters.Add(ApiConstant.PARAM_APP_KEY_KEY, AppKey); allParameters.Add(ApiConstant.PARAM_FORMAT_KEY, Format); allParameters.Add(ApiConstant.PARAM_TIMESTAMP_KEY, DateTime.UtcNow); allParameters.Add(ApiConstant.PARAM_SIGN_KEY, ApiUtils.SignApiRequest(allParameters, AppSecret)); return(allParameters); }
internal static IDictionary <string, string> GetParametersInternal(string prefix, object model) { var apiDictionary = new ApiDictionary(); if (model != null) { prefix = prefix ?? string.Empty; var thisType = model.GetType(); var allPropertyInfos = thisType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.SetProperty); var parameterProperties = default(Dictionary <string, PropertyInfo>); if (!_RequestParameterPropertyMapping.TryGetValue(thisType.FullName, out parameterProperties) || parameterProperties == null) { parameterProperties = new Dictionary <string, PropertyInfo>(); foreach (var property in allPropertyInfos) { var apiParameterAttribtue = property.GetCustomAttribute <ApiRequestParameterAttribute>(); if (apiParameterAttribtue != null) { var parameterKey = property.Name; parameterProperties.Add(parameterKey, property); } } _RequestParameterPropertyMapping.AddOrUpdate(thisType.FullName, parameterProperties, (key, value) => parameterProperties); } // primative types and string... if (!parameterProperties.Any()) { apiDictionary.Add(prefix, model); } // complex types & enumerable<T> else { // ToDo, need refine this logic // Would better use deep iteration. // Support List, two level properties. foreach (var key in parameterProperties.Keys) { var valueSetSuccess = false; var propertyType = parameterProperties[key].PropertyType; var propertyValue = parameterProperties[key].GetValue(model); if (propertyType.IsGenericType) { var genericParameters = propertyType.GetGenericArguments(); if (genericParameters.Length == 1 && typeof(IEnumerable <>).MakeGenericType(genericParameters[0]).IsAssignableFrom(propertyType)) { var apiRequestParameterSubProperties = genericParameters[0].GetProperties(BindingFlags.Instance | BindingFlags.Public) .Where(c => c.GetCustomAttribute <ApiRequestParameterAttribute>() != null) .ToList(); var isGenericParameterComplexType = apiRequestParameterSubProperties.Any(); var index = 0; foreach (var item in propertyValue as IEnumerable) { var subPrefix = string.Format("{0}{1}{2}[{3}]", prefix, string.IsNullOrEmpty(prefix) ? string.Empty : ".", key, index++); var subDictionary = GetParametersInternal(subPrefix, item); apiDictionary.Merge(subDictionary); } valueSetSuccess = true; } } if (!valueSetSuccess) { var subPrefix = string.Format("{0}{1}{2}", prefix, string.IsNullOrEmpty(prefix) ? string.Empty : ".", key); var subDictionary = GetParametersInternal(subPrefix, parameterProperties[key].GetValue(model)); apiDictionary.Merge(subDictionary); } } } } return(apiDictionary); }