Exemplo n.º 1
0
    private void UpgradeSync(string name, string path, Func <dynamic, object> value)
    {
        switch (_httpMethod)
        {
        case HttpMethod.Get:
            _module.Get(path, value, null, name);
            break;

        case HttpMethod.Put:
            _module.Put(path, value, null, name);
            break;

        case HttpMethod.Post:
            _module.Post(path, value, null, name);
            break;

        case HttpMethod.Delete:
            _module.Delete(path, value, null, name);
            break;

        case HttpMethod.Head:
            _module.Head(path, value, null, name);
            break;

        case HttpMethod.Patch:
            _module.Patch(path, value, null, name);
            break;

        case HttpMethod.Options:
            _module.Options(path, value, null, name);
            break;
        }
    }
Exemplo n.º 2
0
 public static void Post <TIn>(this NancyModule module, string name, string path, Func <TIn, Task <object> > handler)
 => module.Post(path, _ => RunHandlerAsync(module, handler), name: name);
Exemplo n.º 3
0
 public static void Post <TIn, TOut>(this NancyModule module, string path, Func <TIn, TOut> handler)
 => module.Post(path, path, handler);
Exemplo n.º 4
0
 public static void Post <TIn, TOut>(this NancyModule module, string name, string path, Func <TIn, TOut> handler)
 => module.Post(path, _ => RunHandler(module, handler), name: name);
        static void RegisterRoute <TBase>(object implementation, MethodInfo method, NancyModule module, CableRouteSchema routeSchema)
        {
            var typeName = typeof(TBase).Name;
            var url      = urlMapper(typeName, method.Name);

            Logger($"Registered Route: {url}");
            var isReturningTask = method.ReturnType.BaseType == typeof(Task);

            routeSchema.Routes.Add(new CableRoute
            {
                Method         = method.Name,
                ParameterTypes = method.GetParameters().Select(p => p.ParameterType).Select(TypeName).ToList(),
                ReturnType     = TypeName(method.ReturnType),
                Route          = url
            });

            module.Post(url, async(ctx, token) =>
            {
                try
                {
                    using (var reader = new StreamReader(module.Request.Body))
                    {
                        var incomingJson = await reader.ReadToEndAsync();

                        var inputParameters = JObject.Parse(incomingJson);
                        var inputDict       = inputParameters.ToObject <Dictionary <string, object> >();
                        var keys            = inputDict.Keys.ToList();
                        //var jsonParameters = inputParameters["Value"] as JArray;
                        var parameterTypes = method.GetParameters().Select(p => p.ParameterType).ToArray();

                        object[] parameters = new object[parameterTypes.Length];

                        for (var i = 0; i < parameters.Length; i++)
                        {
                            var type = parameterTypes[i];
                            // deserialize each parameter to it's respective type
                            //var json = jsonParameters[i].ToString();
                            //var json = inputDict[keys[i]].ToString();
                            parameters[i] = inputDict[keys[i]];// JsonConvert.DeserializeObject(json, type);
                        }

                        object result = null;
                        if (isReturningTask)
                        {
                            dynamic task = method.Invoke(implementation, parameters);
                            result       = await task;
                        }
                        else
                        {
                            result = method.Invoke(implementation, parameters);
                        }


                        return(JsonConvert.SerializeObject(result));
                    }
                }
                catch (Exception ex)
                {
                    var errorData                  = new Dictionary <string, object>();
                    errorData["$exception"]        = true;
                    errorData["$exceptionMessage"] = ex.InnerException.Message;
                    return(JsonConvert.SerializeObject(errorData));
                }
            });

            //module.Post[url, true] = async (ctx, token) =>
            //{
            //    try
            //    {
            //        using (var reader = new StreamReader(module.Request.Body))
            //        {
            //            var incomingJson = await reader.ReadToEndAsync();

            //            var inputParameters = JObject.Parse(incomingJson);

            //            var jsonParameters = inputParameters["Value"] as JArray;
            //            var parameterTypes = method.GetParameters().Select(p => p.ParameterType).ToArray();

            //            object[] parameters = new object[parameterTypes.Length];

            //            for (var i = 0; i < parameters.Length; i++)
            //            {
            //                var type = parameterTypes[i];
            //                // deserialize each parameter to it's respective type
            //                var json = jsonParameters[i].ToString();
            //                parameters[i] = Json.Deserialize(json, type);
            //            }

            //            object result = null;
            //            if (isReturningTask)
            //            {
            //                dynamic task = method.Invoke(implementation, parameters);
            //                result = await task;
            //            }
            //            else
            //            {
            //                result = method.Invoke(implementation, parameters);
            //            }


            //            return Json.Serialize(result);
            //        }
            //    }
            //    catch (Exception ex)
            //    {
            //        var errorData = new Dictionary<string, object>();
            //        errorData["$exception"] = true;
            //        errorData["$exceptionMessage"] = ex.InnerException.Message;
            //        return JsonConvert.SerializeObject(errorData);
            //    }
            //};
        }