Ejemplo n.º 1
0
        private IEnumerable <ResponseParameterMetadata> GetResponseParameters(Type targetType, MetadataValueProvider provider)
        {
            targetType.AssertNotNull(nameof(targetType));
            provider.AssertNotNull(nameof(provider));

            if (TypeDescriptor.GetProperties(targetType).OfType <PropertyDescriptor>().SelectMany(x => x.Attributes.OfType <IParameterDefinition>()).Any())
            {
                foreach (var property in TypeDescriptor.GetProperties(targetType).OfType <PropertyDescriptor>())
                {
                    var paramDef = property.Attributes.OfType <IParameterDefinition>().FirstOrDefault();
                    if (paramDef == null)
                    {
                        continue;
                    }

                    var propertyValues   = provider.Nested(property.Attributes);
                    var propertyMetadata = GetResponseParameter(property.PropertyType, paramDef.ParameterKind, propertyValues.Nested(TypeDescriptor.GetAttributes(targetType)));
                    propertyMetadata.ModelPropertyName = property.Name;
                    propertyMetadata.TypeConverter     = property.Converter;

                    yield return(propertyMetadata);
                }
            }
            else
            {
                var bodyParam = GetResponseParameter(targetType, RequestParameterKind.Body, provider);

                yield return(bodyParam);
            }
        }
Ejemplo n.º 2
0
        private MethodMetadata GetMethodMetadata(MethodInfo method, MetadataValueProvider provider, IEnumerable <IRestValueFormatter> valueFormatters)
        {
            method.AssertNotNull(nameof(method));
            provider.AssertNotNull(nameof(provider));

            var values = provider.Nested(method.GetCustomAttributes());

            if (!values.TryGetRequestMethod(out var requestMethod))
            {
                throw new Exception("method not setted");//TODO msg
            }
            var parameters = method.GetParameters().Where(x => !IsCancellationToken(x));
            var namePrefix = values.GetParameterPrefix();

            var methodValueFormatters = valueFormatters.Union(method.GetCustomAttributes <Attribute>().OfType <IRestValueFormatter>()).ToArray();

            return(new MethodMetadata(method)
            {
                Request = new RequestMetadata(requestMethod)
                {
                    Route = values.GetRequestRoutePrefix() + values.GetRequestRoute(),
                    Timeout = values.GetRequestTimeout(),
                    ContentKind = values.GetRequestContentKind(),
                    Parameters = parameters.SelectMany(x => GetRequestParameters(x.Name, namePrefix, x.ParameterType, x.Name, methodValueFormatters, values.Nested(x.GetCustomAttributes()))).ToArray()
                },
                Response = new ResponseMetadata
                {
                    Parameters = GetResponseParameters(method.ReturnParameter, values).ToArray(),
                    IsAsync = typeof(Task).IsAssignableFrom(method.ReturnType),
                    ResultType = GetResultType(method.ReturnType)
                }
            });
        }
Ejemplo n.º 3
0
        public EndpointMetadata GetEndpointMetadata(Type contractType, MetadataValueProvider provider)
        {
            contractType.AssertNotNull(nameof(contractType));
            provider.AssertNotNull(nameof(provider));

            var values          = provider.Nested(TypeDescriptor.GetAttributes(contractType));
            var methods         = contractType.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
            var valueFormatters = contractType.GetCustomAttributes <Attribute>().OfType <IRestValueFormatter>().ToArray();

            return(new EndpointMetadata
            {
                Methods = methods.Select(x => GetMethodMetadata(x, values, valueFormatters)).ToArray()
            });
        }