Example #1
0
        private static string GetQueryParameters(Object obj)
        {
            var sb = new StringBuilder();
            var t  = obj.GetType();
            var pi = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (var p in pi)
            {
                var attributes = p.GetCustomAttributes(typeof(SDKPropertyAttribute), true);
                SDKPropertyAttribute sdkPropertyAttribute = null;

                if (attributes.Length == 0)
                {
                    continue;
                }

                foreach (var a in attributes)
                {
                    if (a is SDKPropertyAttribute propertyAttribute)
                    {
                        sdkPropertyAttribute = propertyAttribute;
                    }
                }

                if (sdkPropertyAttribute == null || !sdkPropertyAttribute.IsQuery)
                {
                    continue;
                }

                var value = p.GetValue(obj, null);
                if (value == null || string.IsNullOrEmpty(Convert.ToString(value)))
                {
                    continue;
                }

                if (value is IList list)
                {
                    var qs = new StringBuilder();
                    foreach (var item in list)
                    {
                        qs.Append(sdkPropertyAttribute.PropertyName).Append("=").Append(Convert.ToString(item)).Append("&");
                    }

                    sb.Append(qs);
                }
                else
                {
                    sb.Append(sdkPropertyAttribute.PropertyName).Append("=").Append(Convert.ToString(value)).Append("&");
                }
            }

            var strIndex = sb.Length;

            if (!string.IsNullOrEmpty(sb.ToString()))
            {
                sb.Remove(strIndex - 1, 1);
            }

            return(sb.ToString());
        }
Example #2
0
        private static string GetRequestBody(object obj)
        {
            var t  = obj.GetType();
            var pi = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            var sdkPropertyList = new List <object>();

            foreach (var p in pi)
            {
                var attributes = p.GetCustomAttributes(typeof(SDKPropertyAttribute), true);
                SDKPropertyAttribute sdkPropertyAttribute = null;

                if (attributes.Length == 0)
                {
                    continue;
                }

                foreach (var a in attributes)
                {
                    if (a is SDKPropertyAttribute propertyAttribute)
                    {
                        sdkPropertyAttribute = propertyAttribute;
                    }
                }

                if (sdkPropertyAttribute == null || !sdkPropertyAttribute.IsBody)
                {
                    continue;
                }

                var value = p.GetValue(obj, null);
                if (value == null)
                {
                    continue;
                }

                sdkPropertyList.Add(value);
            }

            if (sdkPropertyList.Count == 1)
            {
                foreach (var elem in sdkPropertyList)
                {
                    return(JsonUtils.Serialize(elem));
                }
            }

            return("");
        }
Example #3
0
        private static Dictionary <string, string> GetRequestHeader(object obj)
        {
            var t  = obj.GetType();
            var pi = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            var element = new Dictionary <string, string>();

            foreach (var p in pi)
            {
                var attributes = p.GetCustomAttributes(typeof(SDKPropertyAttribute), true);
                SDKPropertyAttribute sdkPropertyAttribute = null;

                if (attributes.Length == 0)
                {
                    continue;
                }

                foreach (var a in attributes)
                {
                    if (a is SDKPropertyAttribute propertyAttribute)
                    {
                        sdkPropertyAttribute = propertyAttribute;
                    }
                }

                if (sdkPropertyAttribute == null || !sdkPropertyAttribute.IsHeader)
                {
                    continue;
                }

                var value = p.GetValue(obj, null);
                if (value == null || string.IsNullOrEmpty(Convert.ToString(value)))
                {
                    continue;
                }

                element.Add(sdkPropertyAttribute.PropertyName, Convert.ToString(value));
            }

            return(element);
        }
Example #4
0
        private static void SetResponseHeaders <T>(HttpResponseMessage message, T jsonObject)
        {
            const BindingFlags instanceBindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

            var properties = jsonObject.GetType().GetProperties(instanceBindFlags);

            foreach (var property in properties)
            {
                var oriAttrName = "";
                var customAttrs = property.GetCustomAttributes(typeof(SDKPropertyAttribute), true);
                if (customAttrs.Length > 0)
                {
                    SDKPropertyAttribute sdkPropertyAttribute = null;
                    foreach (var customAttr in customAttrs)
                    {
                        if (customAttr is SDKPropertyAttribute propertyAttribute)
                        {
                            sdkPropertyAttribute = propertyAttribute;
                        }

                        if (sdkPropertyAttribute == null || !sdkPropertyAttribute.IsHeader)
                        {
                            continue;
                        }

                        oriAttrName = sdkPropertyAttribute.PropertyName;
                    }
                }

                if (string.IsNullOrEmpty(oriAttrName))
                {
                    continue;
                }

                var isContentHeader = HttpContentHeadersList.Contains(oriAttrName);
                property.SetValue(jsonObject,
                                  isContentHeader
                        ? message.Content.Headers.GetValues(oriAttrName).First()
                        : message.Headers.GetValues(oriAttrName).First());
            }
        }