コード例 #1
0
        public RequestBody(RestValue content, ContentType contentType)
            : base(contentType)
        {
            content.AssertNotNull(nameof(content));

            Content = content;
        }
コード例 #2
0
        private Maybe <RestValue> GetPropertyValue(RestValue sourceValue, BindingPath bindingPath, int pathIndex)
        {
            if (sourceValue.IsNull)
            {
                return(Maybe.Nothing <RestValue>());
            }

            var propertyName = bindingPath[pathIndex];
            var propertyInfo = sourceValue.Type.GetProperty(propertyName);

            if (propertyInfo == null)
            {
                throw new ArgumentException($"Invalid property name '{propertyName}'", nameof(bindingPath));
            }

            var value      = propertyInfo.GetValue(sourceValue.Value, null);
            var modelValue = new RestValue(value, propertyInfo.PropertyType);

            if (pathIndex + 1 == bindingPath.Length)
            {
                return(modelValue);
            }

            return(GetPropertyValue(modelValue, bindingPath, pathIndex + 1));
        }
コード例 #3
0
        string IRestValueFormatter.Serialize(RestValue value, CultureInfo cultureInfo)
        {
            var valueType = value.Type;

            if (!typeof(T).IsAssignableFrom(valueType) || valueType.IsInstanceOfType(value))
            {
                throw new ArgumentException("Invalid value type"); //TODO msg
            }
            return(Serialize((T)value.Value, cultureInfo));
        }
コード例 #4
0
        public override string Serialize(RestValue value, CultureInfo cultureInfo)
        {
            var formatter = ValueFormatters.FirstOrDefault(x => x.CanSerialize(value));

            if (formatter == null)
            {
                throw new RestException(RestExceptionKind.Serialization, $"Value '{value.Type}' formatter not found"); //TODO msg
            }
            return(formatter.Serialize(value, cultureInfo));
        }
コード例 #5
0
        byte[] IRestContentFormatter.Serialize(ContentType contentType, RestValue value)
        {
            var valueType = value.Type;

            if (!typeof(T).IsAssignableFrom(valueType) || valueType.IsInstanceOfType(value))
            {
                throw new ArgumentException("Invalid value type"); //TODO msg
            }
            return(Serialize(contentType, (T)value.Value));
        }
コード例 #6
0
        public RequestFormField(string name, RestValue content, MimeType mediaType)
        {
            content.AssertNotNull(nameof(content));
            name.AssertNotNullOrEmpty(nameof(name));
            mediaType.AssertNotNull(nameof(mediaType));

            Name      = name;
            Content   = content;
            MediaType = mediaType;
        }
コード例 #7
0
        private string FormatQueryValue(RestValue value, CultureInfo cultureInfo)
        {
            var stringValue = RestSerializer.SerializeQueryValue(value, cultureInfo);

            if (stringValue == null)
            {
                return(null);
            }

            return(Uri.EscapeDataString(stringValue));
        }
コード例 #8
0
        public virtual byte[] SerializeContent(ContentType contentType, RestValue value)
        {
            var formatter = ContentFormatters.FirstOrDefault(x => x.CanSerialize(contentType, value));

            if (formatter == null)
            {
                throw new RestException(RestExceptionKind.Serialization, $"Content '{value.Type}' formatter not found for media type '{contentType.MediaType}'");
            }

            return(formatter.Serialize(contentType, value));
        }
コード例 #9
0
        public virtual string SerializeRouteValue(RestValue value, CultureInfo cultureInfo)
        {
            var formatter = RequestRouteValueFormatters.FirstOrDefault(x => x.CanSerialize(value));

            if (formatter == null)
            {
                throw new RestException(RestExceptionKind.Serialization, $"Route value '{value.Type}' formatter not found");
            }

            return(formatter.Serialize(value, cultureInfo));
        }
コード例 #10
0
        public void AddValue(string sourceName, RestValue value)
        {
            sourceName.AssertNotNull(nameof(sourceName));
            value.AssertNotNull(nameof(value));

            if (_values == null)
            {
                _values = new Dictionary <string, RestValue>();
            }

            _values.Add(sourceName, value);
        }
コード例 #11
0
        public string Serialize(RestValue value, CultureInfo cultureInfo)
        {
            var type      = Nullable.GetUnderlyingType(value.Type);
            var typeValue = new RestValue(value.Value, type);
            var formatter = ValueFormatters.FirstOrDefault(x => x.CanSerialize(typeValue));

            if (formatter == null)
            {
                throw new RestException(RestExceptionKind.Serialization, $"Value '{value.Type}' formatter not found"); //TODO msg
            }
            return(formatter.Serialize(typeValue, cultureInfo));
        }
コード例 #12
0
        public bool CanSerialize(RestValue value)
        {
            var type = Nullable.GetUnderlyingType(value.Type);

            if (type == null)
            {
                return(false);
            }

            var typeValue = new RestValue(value.Value, type);

            return(ValueFormatters.Any(x => x.CanSerialize(typeValue)));
        }
コード例 #13
0
        private IRestRequest GetRequest(MethodMetadata methodMetadata, object[] args)
        {
            var bindingSource = new ModelBindingSource();

            foreach (var parameter in methodMetadata.MethodInfo.GetParameters())
            {
                var parameterValue = new RestValue(args[parameter.Position], parameter.ParameterType);

                bindingSource.AddValue(parameter.Name, parameterValue);
            }

            return(_modelMapper.MapRequest(methodMetadata.Request, bindingSource));
        }
コード例 #14
0
        public string Serialize(RestValue value, CultureInfo cultureInfo)
        {
            var v = value.Value;

            if (v == null)
            {
                return(null);
            }
            else if (v is string s)
            {
                return(s);
            }
            else if (v is IFormattable formattable)
            {
                return(formattable.ToString(null, cultureInfo));
            }

            return(v.ToString());
        }
コード例 #15
0
        public bool TryGetValue(string sourceName, BindingPath propertyPath, out RestValue value)
        {
            sourceName.AssertNotNull(nameof(sourceName));

            if (_values != null && _values.TryGetValue(sourceName, out var entry))
            {
                if (propertyPath.IsEmpty)
                {
                    value = entry;
                    return(true);
                }

                var propertyValue = GetPropertyValue(entry, propertyPath, 0);
                if (propertyValue.HasValue)
                {
                    value = propertyValue.Value;
                    return(true);
                }
            }

            value = null;
            return(false);
        }
コード例 #16
0
 public abstract string Serialize(RestValue value, CultureInfo cultureInfo);
コード例 #17
0
 public bool CanSerialize(RestValue value)
 {
     return(true);
 }
コード例 #18
0
 bool IRestContentFormatter.CanSerialize(ContentType contentType, RestValue value)
 => value.Type == typeof(T) && CanSerialize(contentType, (T)value.Value);
コード例 #19
0
        private static IEnumerable <T> Bind <T>(RequestParameterMetadata parameter, RestValue source, Func <string, RestValue, T> factory)
        {
            var name = parameter.ParameterName;

            if (source.Value == null)
            {
                yield break;
            }

            if (source.Value is T t)
            {
                if (parameter.EmitDefault.GetValueOrDefault() || !Equals(t, default(T)))
                {
                    yield return(t);
                }
            }
            else if (source.Value is IEnumerable <T> tList)
            {
                foreach (var value in tList)
                {
                    if (parameter.EmitDefault.GetValueOrDefault() || !Equals(value, default(T)))
                    {
                        yield return(value);
                    }
                }
            }
            else if (source.Value is KeyValuePair <string, string> pair)
            {
                //TODO check for not null name
                yield return(factory(name ?? pair.Key, RestValue.Create(pair.Value)));
            }
            else if (source.Value is IEnumerable <KeyValuePair <string, string> > pairs)
            {
                var result = pairs.Select(x => factory(name ?? pair.Key, RestValue.Create(x.Value)));
                foreach (var value in result)
                {
                    yield return(value);
                }
            }
            else if (source.Value is NameValueCollection nmcol)
            {
                var result = nmcol.Keys.OfType <string>().SelectMany(n => nmcol.GetValues(n).Select(v => factory(n, RestValue.Create(v))));
                foreach (var value in result)
                {
                    yield return(value);
                }
            }
            else if (source.Value is string str)
            {
                if (parameter.EmitDefault.GetValueOrDefault() || str != null)
                {
                    yield return(factory(name, RestValue.Create(str)));
                }
            }
            else if (source.Value is IEnumerable <string> strList)
            {
                foreach (var p in strList.Select(x => factory(name, RestValue.Create(x))))
                {
                    yield return(p);
                }
            }
            else if (source.Type.IsArray)
            {
                var elementType = source.Type.GetElementType();

                foreach (var el in (Array)source.Value)
                {
                    yield return(factory(name, new RestValue(el, elementType)));
                }
            }
            else
            {
                //var value = source.Value == null ? null : ConvertToString(parameter, source.Value);
                //if (parameter.EmitDefault.GetValueOrDefault() || value != null)
                yield return(factory(name, source));
            }
        }
コード例 #20
0
 public override bool CanSerialize(RestValue value)
 {
     return(ValueFormatters.Any(x => x.CanSerialize(value)));
 }
コード例 #21
0
        protected virtual void BindParameter(IRequestBindingContext bindingContext, RequestParameterMetadata param, RestValue sourceValue)
        {
            if (param.ParameterKind == RequestParameterKind.Route)
            {
                var segments = Bind(param, sourceValue, (n, v) => new RequestRouteSegment(n, v));
                foreach (var segment in segments)
                {
                    bindingContext.AddRouteSegment(segment);
                }
            }
            else if (param.ParameterKind == RequestParameterKind.Header)
            {
                var headers = Bind(param, sourceValue, (n, v) => new RequestHeader(n, v));
                foreach (var header in headers)
                {
                    bindingContext.AddHeader(header);
                }
            }
            else if (param.ParameterKind == RequestParameterKind.Query)
            {
                var queries = Bind(param, sourceValue, (n, v) => new RequestQuery(n, v));

                if (param.StringFormat != null)                                  //TODO костыль
                {
                    foreach (var query in queries.Where(x => !x.Content.IsNull)) //TODO filter at source level
                    {
                        var value          = (IFormattable)query.Content.Value;
                        var formattedValue = value.ToString(param.StringFormat, bindingContext.CultureInfo);

                        bindingContext.AddQuery(new RequestQuery(query.Name, RestValue.Create(formattedValue)));
                    }
                }
                else
                {
                    foreach (var query in queries.Where(x => !x.Content.IsNull)) //TODO filter at source level
                    {
                        bindingContext.AddQuery(query);
                    }
                }
            }
            else if (param.ParameterKind.In(RequestParameterKind.Body, RequestParameterKind.FormField, RequestParameterKind.FormFile))
            {
                if (bindingContext.BindingSource.TryGetValue(param.BindingSourceName, param.BindingPath, out var contentValue))
                {
                    if (param.ParameterKind == RequestParameterKind.Body)
                    {
                        var contentMediaType = param.MediaType ?? MimeTypes.Application.Json;                               //TODO
                        var contentType      = new ContentType(contentMediaType, DefaultEncodingProvider.Encoding.WebName); //TODO charset

                        bindingContext.Body = new RequestBody(contentValue, contentType);
                    }
                    else if (param.ParameterKind == RequestParameterKind.FormField)
                    {
                        var field = new RequestFormField(param.ParameterName, contentValue, param.MediaType ?? DefaultMediaTypeProvider.MediaType);

                        bindingContext.AddFormField(field);
                    }
                    else if (param.ParameterKind == RequestParameterKind.FormFile)
                    {
                        throw new NotImplementedException();
                        //var file = new RequestFormFile(contentValue, param.ParameterName)
                        //{
                        //    FileName = param.FileName,
                        //};

                        //bindingContext.AddFormField(field);
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }
                }
                else if (param.IsRequired)
                {
                    throw new RestException(RestExceptionKind.Mapping, "req param missed");//TODO msg
                }
            }
            else
            {
                throw new NotImplementedException(param.ParameterKind.ToString()); //TODO msg
            }
            //TODO mark parameter as valid
        }
コード例 #22
0
 public abstract bool CanSerialize(RestValue value);
コード例 #23
0
 bool IRestValueFormatter.CanSerialize(RestValue value)
 => value.Type == typeof(T) && CanSerialize((T)value.Value);