Exemplo n.º 1
0
        internal static HttpContent CreateContent(IRequestParameter parameter, IRestClient client)
        {
            if (parameter is IRequestBody &&
                ((IRequestBody)parameter).IsMultiparted())
            {
                return(CreateMultipartContent((IMultipartBody)parameter, client));
            }

            HttpContent content;

            if (parameter.IsBody())
            {
                if (parameter.IsSimpleValue())
                {
                    content = parameter.ValueAsString(client);
                }
                else
                {
                    var format = parameter is IRequestBody ? ((IRequestBody)parameter).Format : ContentFormat.Default;

                    if (format == ContentFormat.Default)
                    {
                        format = client.Settings.DefaultFormat;
                    }

                    if (format == ContentFormat.Default)
                    {
                        throw new NotSupportedException("Invalid content format setup: the body fallbacks to default that was not set");
                    }

                    var serializer = client.Settings.Serializers.FirstOrDefault(x => x.SupportedFormat == format);
                    if (serializer == null)
                    {
                        throw new NotSupportedException(
                                  $"Unable to define a serializer for content of format {format.ToString().ToUpperInvariant()}");
                    }

                    content = SerializeValue(client, parameter, serializer);
                }
            }
            else if (parameter.IsFile())
            {
                if (!(parameter is RequestFile))
                {
                    throw new NotSupportedException();
                }

                content = ((RequestFile)parameter).CreateFileContent(client);
            }
            else
            {
                throw new NotSupportedException();
            }

            return(content);
        }
Exemplo n.º 2
0
        public IRequestBuilder With(IRequestParameter parameter)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException(nameof(parameter));
            }

            if (parameters.Any(x => x.Name == parameter.Name &&
                               x.Type == parameter.Type))
            {
                throw new ArgumentException($"The parameter {parameter.Name} of type {parameter.Type} already set");
            }

            if (parameter.IsBody())
            {
                if (parameters.Any(x => x.IsBody()))
                {
                    throw new ArgumentException();
                }
            }

            parameters.Add(parameter);
            return(this);
        }