ApplyParameterToRequestString() public static method

public static ApplyParameterToRequestString ( string &requestString, string argument, string value ) : void
requestString string
argument string
value string
return void
Example #1
0
        /// <summary>
        /// Creates the HTTP query string for a given options class.
        /// </summary>
        /// <param name="requestString">The string to which the query string will be appended.</param>
        /// <param name="options">The options class for which to create the query string.</param>
        public static void CreateQuery(ref string requestString, INestedOptions options)
        {
            List <Parameter> flatParams = FlattenParams(options);

            foreach (Parameter param in flatParams)
            {
                RequestStringBuilder.ApplyParameterToRequestString(ref requestString, param.Key, param.Value);
            }
        }
Example #2
0
        public bool Parse(ref string requestString, JsonPropertyAttribute attribute, PropertyInfo property, object propertyValue, object propertyParent)
        {
            if (attribute.PropertyName != "tiers_array")
            {
                return(false);
            }

            var items = ((List <StripePlanTierOptions>)property.GetValue(propertyParent, null));

            var itemIndex = 0;

            foreach (var item in items)
            {
                var properties = item.GetType().GetRuntimeProperties();

                foreach (var prop in properties)
                {
                    var value = prop.GetValue(item, null);
                    if (value == null)
                    {
                        continue;
                    }

                    // it must have a json attribute matching stripe's key, and only one
                    var attr = prop.GetCustomAttributes <JsonPropertyAttribute>().SingleOrDefault();
                    if (attr == null)
                    {
                        continue;
                    }

                    string valueRep;
                    if (value is StripePlanTierOptions.UpToInf)
                    {
                        valueRep = "inf";
                    }
                    else if (value is StripePlanTierOptions.UpToBound)
                    {
                        StripePlanTierOptions.UpToBound valueBound = (StripePlanTierOptions.UpToBound)value;
                        valueRep = valueBound.Bound.ToString();
                    }
                    else
                    {
                        valueRep = value.ToString();
                    }

                    RequestStringBuilder.ApplyParameterToRequestString(ref requestString,
                                                                       $"tiers[{itemIndex}][{attr.PropertyName}]", valueRep);
                }

                itemIndex++;
            }

            return(true);
        }
Example #3
0
        public bool Parse(ref string requestString, JsonPropertyAttribute attribute, PropertyInfo property, object propertyValue, object propertyParent)
        {
            if (!attribute.PropertyName.Contains("include_total_count"))
            {
                return(false);
            }

            var doInclude = (bool)propertyValue;

            if (doInclude)
            {
                RequestStringBuilder.ApplyParameterToRequestString(ref requestString, $"include[]", "total_count");
            }

            return(true);
        }
        public bool Parse(ref string requestString, JsonPropertyAttribute attribute, PropertyInfo property, object propertyValue, object propertyParent)
        {
            if (attribute.PropertyName != "legal_entity[additional_owners]")
            {
                return(false);
            }

            var owners = ((List <StripeAccountAdditionalOwner>)property.GetValue(propertyParent, null));

            if (owners.Count == 0)
            {
                RequestStringBuilder.ApplyParameterToRequestString(ref requestString, attribute.PropertyName, "");
                return(true);
            }

            var ownerIndex = 0;

            foreach (var owner in owners)
            {
                var properties = owner.GetType().GetRuntimeProperties();

                foreach (var prop in properties)
                {
                    var value = prop.GetValue(owner, null);
                    if (value == null)
                    {
                        continue;
                    }

                    // it must have a json attribute matching stripe's key, and only one
                    var attr = prop.GetCustomAttributes <JsonPropertyAttribute>().SingleOrDefault();
                    if (attr == null)
                    {
                        continue;
                    }

                    RequestStringBuilder.ApplyParameterToRequestString(ref requestString,
                                                                       $"{attribute.PropertyName}[{ownerIndex}]{attr.PropertyName}",
                                                                       value.ToString());
                }

                ownerIndex++;
            }

            return(true);
        }
Example #5
0
        public bool Parse(ref string requestString, JsonPropertyAttribute attribute, PropertyInfo property, object propertyValue, object propertyParent)
        {
            if (!attribute.PropertyName.Contains("array:"))
            {
                return(false);
            }

            var values = ((IEnumerable)propertyValue).Cast <object>().Select(x => x.ToString()).ToArray();

            var key = attribute.PropertyName.Replace("array:", "") + "[]";

            foreach (var value in values)
            {
                RequestStringBuilder.ApplyParameterToRequestString(ref requestString, key, value);
            }

            return(true);
        }
Example #6
0
        public bool Parse(ref string requestString, JsonPropertyAttribute attribute, PropertyInfo property, object propertyValue, object propertyParent)
        {
            // Check if the property is a Dictionary
            var type = property.PropertyType;

            if (!type.GetTypeInfo().IsGenericType)
            {
                return(false);
            }

            if (type.GetTypeInfo().GetGenericTypeDefinition() != typeof(Dictionary <,>))
            {
                return(false);
            }

            // Ensure that key and value types are both string
            var keyType = type.GetTypeInfo().GenericTypeArguments[0];

            if (keyType != typeof(string))
            {
                throw new System.ArgumentException($"Expected {typeof(string).ToString()} as dictionary key type, got {keyType.ToString()}");
            }

            var valueType = type.GetTypeInfo().GenericTypeArguments[1];

            if (valueType != typeof(string))
            {
                throw new System.ArgumentException($"Expected {typeof(string).ToString()} as dictionary value type, got {valueType.ToString()}");
            }

            var dictionary = (Dictionary <string, string>)propertyValue;

            if (dictionary == null)
            {
                return(true);
            }

            foreach (var key in dictionary.Keys)
            {
                RequestStringBuilder.ApplyParameterToRequestString(ref requestString, $"{attribute.PropertyName}[{WebUtility.UrlEncode(key)}]", dictionary[key]);
            }

            return(true);
        }
Example #7
0
        public bool Parse(ref string requestString, JsonPropertyAttribute attribute, PropertyInfo property, object propertyValue, object propertyParent)
        {
            // Check if the property is an array
            var type = property.PropertyType;

            if (!type.GetTypeInfo().IsArray)
            {
                return(false);
            }

            var values = ((IEnumerable)propertyValue).Cast <object>().Select(x => x.ToString()).ToArray();

            foreach (var value in values)
            {
                RequestStringBuilder.ApplyParameterToRequestString(ref requestString, $"{attribute.PropertyName}[]", value);
            }

            return(true);
        }
Example #8
0
        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);
            }

            var dictionary = (Dictionary <string, string>)propertyValue;

            if (dictionary == null)
            {
                return(true);
            }

            foreach (var key in dictionary.Keys)
            {
                RequestStringBuilder.ApplyParameterToRequestString(ref requestString, $"{attribute.PropertyName}[{key}]", dictionary[key]);
            }

            return(true);
        }
Example #9
0
        public bool Parse(ref string requestString, JsonPropertyAttribute attribute, PropertyInfo property, object propertyValue, object propertyParent)
        {
            // Check if the property is an Enum or a Nullable<Enum>
            var type = property.PropertyType;

            if (type.GetTypeInfo().IsGenericType&& type.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                type = Nullable.GetUnderlyingType(type);
            }
            if (!type.GetTypeInfo().IsEnum)
            {
                return(false);
            }

            // Use JsonConvert to grab the EnumMemberAttribute's Value for the enum element
            string value = JsonConvert.SerializeObject(propertyValue).Trim('"');

            RequestStringBuilder.ApplyParameterToRequestString(ref requestString, $"{attribute.PropertyName}", value);

            return(true);
        }
Example #10
0
        public bool Parse(ref string requestString, JsonPropertyAttribute attribute, PropertyInfo property, object propertyValue, object propertyParent)
        {
            if (attribute.PropertyName != "subscription_items_array_updated")
            {
                return(false);
            }

            var items = ((List <StripeSubscriptionItemUpdateOption>)property.GetValue(propertyParent, null));

            var itemIndex = 0;

            foreach (var item in items)
            {
                var properties = item.GetType().GetRuntimeProperties();

                foreach (var prop in properties)
                {
                    var value = prop.GetValue(item, null);
                    if (value == null)
                    {
                        continue;
                    }

                    // it must have a json attribute matching stripe's key, and only one
                    var attr = prop.GetCustomAttributes <JsonPropertyAttribute>().SingleOrDefault();
                    if (attr == null)
                    {
                        continue;
                    }

                    RequestStringBuilder.ApplyParameterToRequestString(ref requestString,
                                                                       $"items[{itemIndex}][{attr.PropertyName}]", value.ToString());
                }

                itemIndex++;
            }

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

            var filter = (StripeDateFilter)propertyValue;

            if (filter.EqualTo.HasValue)
            {
                RequestStringBuilder.ApplyParameterToRequestString(ref requestString, attribute.PropertyName, filter.EqualTo.Value.ToUniversalTime().ConvertDateTimeToEpoch().ToString());
            }

            if (filter.LessThan.HasValue)
            {
                RequestStringBuilder.ApplyParameterToRequestString(ref requestString, attribute.PropertyName + "[lt]", filter.LessThan.Value.ToUniversalTime().ConvertDateTimeToEpoch().ToString());
            }

            if (filter.LessThanOrEqual.HasValue)
            {
                RequestStringBuilder.ApplyParameterToRequestString(ref requestString, attribute.PropertyName + "[lte]", filter.LessThanOrEqual.Value.ToUniversalTime().ConvertDateTimeToEpoch().ToString());
            }

            if (filter.GreaterThan.HasValue)
            {
                RequestStringBuilder.ApplyParameterToRequestString(ref requestString, attribute.PropertyName + "[gt]", filter.GreaterThan.Value.ToUniversalTime().ConvertDateTimeToEpoch().ToString());
            }

            if (filter.GreaterThanOrEqual.HasValue)
            {
                RequestStringBuilder.ApplyParameterToRequestString(ref requestString, attribute.PropertyName + "[gte]", filter.GreaterThanOrEqual.Value.ToUniversalTime().ConvertDateTimeToEpoch().ToString());
            }

            return(true);
        }
Example #12
0
        public bool Parse(
            ref string requestString,
            JsonPropertyAttribute attribute,
            PropertyInfo property,
            object propertyValue,
            object propertyParent)
        {
            // Check if the property is a List
            var type = property.PropertyType;

            if (!type.GetTypeInfo().IsGenericType)
            {
                return(false);
            }

            if (type.GetTypeInfo().GetGenericTypeDefinition() != typeof(List <>))
            {
                return(false);
            }

            // Cast to List<object>
            var items = ((IEnumerable)propertyValue).Cast <object>().ToList();

            // If the list is empty, just send the parameter's name with an empty string as the
            // value to tell Stripe's API to empty the parameter.
            if (items.Count == 0)
            {
                RequestStringBuilder.ApplyParameterToRequestString(ref requestString, attribute.PropertyName, string.Empty);
                return(true);
            }

            // Encode each item
            var itemIndex = 0;

            foreach (var item in items)
            {
                var itemType = item.GetType();

                if (itemType.GetTypeInfo().IsPrimitive || (itemType == typeof(string)))
                {
                    // Primitive type encoding (string counts as a primitive type)
                    RequestStringBuilder.ApplyParameterToRequestString(
                        ref requestString,
                        $"{attribute.PropertyName}[{itemIndex}]",
                        item.ToString());
                }
                else
                {
                    // Complex type encoding
                    var itemProperties = item.GetType().GetRuntimeProperties();

                    foreach (var itemProp in itemProperties)
                    {
                        var itemAttr = itemProp.GetCustomAttributes <JsonPropertyAttribute>().SingleOrDefault();
                        if (itemAttr == null)
                        {
                            // Skip attributes not tagged with JsonPropertyAttribute
                            continue;
                        }

                        var itemValue = itemProp.GetValue(item, null);
                        if (itemValue == null)
                        {
                            // Ignore null elements
                            continue;
                        }

                        // A JsonPropertyAttribute is required to supply a property name to parser plugins.
                        var key = $"{attribute.PropertyName}[{itemIndex}][{itemAttr.PropertyName}]";
                        var indexedItemAttribute = new JsonPropertyAttribute(key);
                        RequestStringBuilder.ProcessPlugins(ref requestString, indexedItemAttribute, itemProp, itemValue, propertyParent);
                    }
                }

                itemIndex++;
            }

            return(true);
        }