예제 #1
0
        public HandlerContext(RestRequest request, ReadOnlyDictionary <String, String> env, MethodInfo code)
        {
            Request = request.AssertNotNull();
            Env     = env.AssertNotNull();
            Code    = code.AssertNotNull();

            // todo:
            // 1) allow to return void, json, string, byte[], stream and any other object (the latter will then be serialized to json)
            // 2) allow non-statics
            // 3) allow RestRequest, RestResponse, RestContext, RestHints, Query, RequestHeaders, ResponseHeaders, Cookies, dynamic/Json (Data), Stream (output stream), TextWriter (response)
            // 4) allow config types
            // 5) deserialize when binding
            // 6) bind to fields as well
            // 7) comprehensive logging
            // 8) try to bind everything, rather than stop at first error

            Debug.EnsureBlankLine();
            Debug.WriteLine("    * Env: {0}", Env.Select(kvp => String.Format("{0} = {1}", kvp.Key, kvp.Value)).StringJoin());
            Debug.WriteLine("    * Query: {0}", request.Query.Select(kvp => String.Format("{0} = {1}", kvp.Key, (String)kvp.Value)).StringJoin().Fluent(s => s.IsEmpty() ? "<empty>" : s));
            Debug.WriteLine("    * Data: {0}", ((Json)request.Data).ToCompactString().Fluent(s => s.IsEmpty() ? "<empty>" : s));
            // todo. after that log:
            // Write("    * field foo <= ")
            // do something, then write either:
            // 1) WriteLine("Data.foo") or
            // 2) WriteLine("FAIL") or even
            // 3) exception.... [will be traced by RestGateway]
        }
예제 #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)
                }
            });
        }