示例#1
0
        public bool Parse(ref string requestString, JsonPropertyAttribute attribute, PropertyInfo property, object propertyValue, object propertyParent)
        {
            if (property.PropertyType != typeof(StripeDateFilter))
            {
                return(false);
            }
            StripeDateFilter stripeDateFilter = (StripeDateFilter)propertyValue;

            if (stripeDateFilter.EqualTo.HasValue)
            {
                RequestStringBuilder.ApplyParameterToRequestString(ref requestString, attribute.PropertyName, stripeDateFilter.EqualTo.Value.ConvertDateTimeToEpoch().ToString());
            }
            if (stripeDateFilter.LessThan.HasValue)
            {
                RequestStringBuilder.ApplyParameterToRequestString(ref requestString, attribute.PropertyName + "[lt]", stripeDateFilter.LessThan.Value.ConvertDateTimeToEpoch().ToString());
            }
            if (stripeDateFilter.LessThanOrEqual.HasValue)
            {
                RequestStringBuilder.ApplyParameterToRequestString(ref requestString, attribute.PropertyName + "[lte]", stripeDateFilter.LessThanOrEqual.Value.ConvertDateTimeToEpoch().ToString());
            }
            if (stripeDateFilter.GreaterThan.HasValue)
            {
                RequestStringBuilder.ApplyParameterToRequestString(ref requestString, attribute.PropertyName + "[gt]", stripeDateFilter.GreaterThan.Value.ConvertDateTimeToEpoch().ToString());
            }
            if (stripeDateFilter.GreaterThanOrEqual.HasValue)
            {
                RequestStringBuilder.ApplyParameterToRequestString(ref requestString, attribute.PropertyName + "[gte]", stripeDateFilter.GreaterThanOrEqual.Value.ConvertDateTimeToEpoch().ToString());
            }
            return(true);
        }
示例#2
0
        public void BuildRequestStringFromJObject()
        {
            JObject jObject = JObject.Parse(@"{ 'CPU': 'Intel'}");

            var response = RequestStringBuilder.BuildRequestStringFromJObject(jObject);

            Assert.NotNull(response);
        }
        public static string ApplyAllParameters <T>(this Service <T> service, BaseOptions obj, string url, bool isListMethod = false)
            where T : IStripeEntity
        {
            // store the original url from the service call into requestString (e.g. https://api.stripe.com/v1/accounts/account_id)
            // before the stripe attributes get applied. all of the attributes that will get passed to stripe will be applied to this string,
            // don't worry - if the request is a post, the Requestor will take care of moving the attributes to the post body
            var requestString = url;

            // obj = the options object passed from the service
            if (obj != null)
            {
                RequestStringBuilder.CreateQuery(ref requestString, obj);

                foreach (KeyValuePair <string, string> pair in obj.ExtraParams)
                {
                    var key = WebUtility.UrlEncode(pair.Key);
                    RequestStringBuilder.ApplyParameterToRequestString(ref requestString, key, pair.Value);
                }

                foreach (var value in obj.Expand)
                {
                    RequestStringBuilder.ApplyParameterToRequestString(ref requestString, "expand[]", value);
                }
            }

            if (service != null)
            {
                // expandable properties
                var propertiesToExpand = service.GetType()
                                         .GetRuntimeProperties()
                                         .Where(p => p.Name.StartsWith("Expand") && p.PropertyType == typeof(bool))
                                         .Where(p => (bool)p.GetValue(service, null))
                                         .Select(p => p.Name);

                foreach (var propertyName in propertiesToExpand)
                {
                    string expandPropertyName = propertyName.Substring("Expand".Length);
                    expandPropertyName = Regex.Replace(expandPropertyName, "([a-z])([A-Z])", "$1_$2").ToLower();

                    if (isListMethod)
                    {
                        expandPropertyName = "data." + expandPropertyName;
                    }

                    requestString = ApplyParameterToUrl(requestString, "expand[]", expandPropertyName);

                    // note: I had no idea you could expand properties beyond the first level (up to 4 before stripe throws an exception).
                    // something to consider adding to the project.
                    //
                    // example:
                    // requestString = ApplyParameterToUrl(requestString, "expand[]", "data.charge.dispute.charge.dispute.charge.dispute");
                }
            }

            return(requestString);
        }
示例#4
0
        private static void ApplyNestedObjectProperties(ref string requestString, object nestedObject)
        {
            foreach (var property in nestedObject.GetType().GetRuntimeProperties())
            {
                var value = property.GetValue(nestedObject, null);
                if (value == null)
                {
                    continue;
                }

                foreach (var attribute in property.GetCustomAttributes <JsonPropertyAttribute>())
                {
                    RequestStringBuilder.ProcessPlugins(ref requestString, attribute, property, value, nestedObject);
                }
            }
        }
        public bool Parse(ref string requestString, JsonPropertyAttribute attribute, PropertyInfo property, object propertyValue, object propertyParent)
        {
            if (!attribute.PropertyName !.Contains("metadata") && !attribute.PropertyName !.Contains("fraud_details"))
            {
                return(false);
            }
            Dictionary <string, string> dictionary = (Dictionary <string, string>)propertyValue;

            if (dictionary == null)
            {
                return(true);
            }
            foreach (string key in dictionary.Keys)
            {
                RequestStringBuilder.ApplyParameterToRequestString(ref requestString, attribute.PropertyName + "[" + key + "]", dictionary[key]);
            }
            return(true);
        }
示例#6
0
        public void ApplyParameterToRequestString()
        {
            string requestString = "https://example.com";
            var    pairs         = new Dictionary <string, string>()
            {
                { "a", "x" },
                { "b", "y" },
                { "c", "z" },
            };

            foreach (KeyValuePair <string, string> pair in pairs)
            {
                //var key = WebUtility.UrlEncode(pair.Key);
                RequestStringBuilder.ApplyParameterToRequestString(ref requestString, pair.Key, pair.Value);
            }

            var expected = "https://example.com?a=x&b=y&c=z";

            Assert.Equal(expected, requestString);
        }
示例#7
0
        public static string ApplyParameterToUrl(string url, string argument, string value)
        {
            RequestStringBuilder.ApplyParameterToRequestString(ref url, argument, value);

            return(url);
        }
示例#8
0
        public static string ApplyAllParameters(this StripeService service, object obj, string url, bool isListMethod = false)
        {
            // store the original url from the service call into requestString (e.g. https://api.stripe.com/v1/accounts/account_id)
            // before the stripe attributes get applied. all of the attributes that will get passed to stripe will be applied to this string,
            // don't worry - if the request is a post, the Requestor will take care of moving the attributes to the post body
            var requestString = url;

            // obj = the options object passed from the service
            if (obj != null)
            {
                foreach (var property in obj.GetType().GetRuntimeProperties())
                {
                    var value = property.GetValue(obj, null);
                    if (value == null)
                    {
                        continue;
                    }

                    foreach (var attribute in property.GetCustomAttributes <JsonPropertyAttribute>())
                    {
                        if (value is INestedOptions)
                        {
                            ApplyNestedObjectProperties(ref requestString, value);
                        }
                        else
                        {
                            RequestStringBuilder.ProcessPlugins(ref requestString, attribute, property, value, obj);
                        }
                    }
                }
            }

            if (service != null)
            {
                // expandable properties
                var propertiesToExpand = service.GetType()
                                         .GetRuntimeProperties()
                                         .Where(p => p.Name.StartsWith("Expand") && p.PropertyType == typeof(bool))
                                         .Where(p => (bool)p.GetValue(service, null))
                                         .Select(p => p.Name);

                foreach (var propertyName in propertiesToExpand)
                {
                    string expandPropertyName = propertyName.Substring("Expand".Length);
                    expandPropertyName = Regex.Replace(expandPropertyName, "([a-z])([A-Z])", "$1_$2").ToLower();

                    if (isListMethod)
                    {
                        expandPropertyName = "data." + expandPropertyName;
                    }

                    requestString = ApplyParameterToUrl(requestString, "expand[]", expandPropertyName);

                    // note: I had no idea you could expand properties beyond the first level (up to 4 before stripe throws an exception).
                    // something to consider adding to the project.
                    //
                    // example:
                    // requestString = ApplyParameterToUrl(requestString, "expand[]", "data.charge.dispute.charge.dispute.charge.dispute");
                }
            }

            return(requestString);
        }
        /// <summary>
        /// ApplyAllParameters
        /// </summary>
        /// <typeparam name="T">T</typeparam>
        /// <param name="service">service</param>
        /// <param name="obj">obj</param>
        /// <param name="url">url</param>
        /// <param name="isListMethod">isListMethod</param>
        /// <returns>requestString</returns>
        public static string ApplyAllParameters <T>(this Service <T> service, BaseOptions obj, string url, bool isListMethod = false)
            where T : ITelnyxEntity
        {
            // store the original url from the service call into requestString
            // before the Telnyx attributes get applied. all of the attributes that will get passed to Telnyx will be applied to this string,
            // don't worry - if the request is a post, the Requestor will take care of moving the attributes to the post body
            var requestString = url;

            // obj = the options object passed from the service
            if (obj != null)
            {
                // Normalize extra params for json serialization
                if (obj.ExtraParams != null && !obj.ExtraParams.Any())
                {
                    obj.ExtraParams = null;
                }

                // Normalize expand for json serialization
                if (obj.Expand != null && !obj.Expand.Any())
                {
                    obj.Expand = null;
                }

                RequestStringBuilder.CreateQuery(ref requestString, obj);

                if (obj.ExtraParams != null)
                {
                    foreach (KeyValuePair <string, string> pair in obj.ExtraParams)
                    {
                        var key = WebUtility.UrlEncode(pair.Key);
                        RequestStringBuilder.ApplyParameterToRequestString(ref requestString, key, pair.Value);
                    }
                }

                if (obj.Expand != null)
                {
                    foreach (var value in obj.Expand)
                    {
                        RequestStringBuilder.ApplyParameterToRequestString(ref requestString, "expand[]", value);
                    }
                }
            }

            if (service != null)
            {
                // expandable properties
                var propertiesToExpand = service.GetType()
                                         .GetRuntimeProperties()
                                         .Where(p => p.Name.StartsWith("Expand") && p.PropertyType == typeof(bool))
                                         .Where(p => (bool)p.GetValue(service, null))
                                         .Select(p => p.Name);

                foreach (var propertyName in propertiesToExpand)
                {
                    string expandPropertyName = propertyName.Substring("Expand".Length);
                    expandPropertyName = Regex.Replace(expandPropertyName, "([a-z])([A-Z])", "$1_$2").ToLower();

                    if (isListMethod)
                    {
                        expandPropertyName = "data." + expandPropertyName;
                    }

                    requestString = ApplyParameterToUrl(requestString, "expand[]", expandPropertyName);
                }
            }

            return(requestString);
        }