Ejemplo n.º 1
0
        public static bool ValideAssignModel(ApiMethod method, ApiCall call, Action <string> callback)
        {
            bool IsSuccess = true;

            if (method.RequireModelType != null)
            {
                string json = call.Context.Request.Body;

                if (!string.IsNullOrEmpty(json))
                {
                    try
                    {
                        call.Context.Request.Model = Lib.Helper.JsonHelper.Deserialize(json, method.RequireModelType);
                    }
                    catch (Exception ex)
                    {
                        IsSuccess = false;
                        callback.Invoke(ex.Message);
                    }
                }
                else
                {
                    var req = call.Context.Request;
                    Dictionary <string, string> values = new Dictionary <string, string>();

                    foreach (var item in req.Forms.AllKeys)
                    {
                        var value = req.Forms[item];
                        values[item] = value;
                    }

                    foreach (var item in req.QueryString.AllKeys)
                    {
                        var value = req.QueryString[item];
                        values[item] = value;
                    }

                    if (values.Count() == 0)
                    {
                        callback.Invoke(Hardcoded.GetValue("required model type not provided", call.Context) + ": " + method.RequireModelType.Name);
                        IsSuccess = false;
                    }
                    else
                    {
                        string dictjson = Lib.Helper.JsonHelper.Serialize(values);
                        try
                        {
                            call.Context.Request.Model = Lib.Helper.JsonHelper.Deserialize(dictjson, method.RequireModelType);
                        }
                        catch (Exception ex)
                        {
                            IsSuccess = false;
                            callback.Invoke(ex.Message);
                        }
                    }
                }
            }
            return(IsSuccess);
        }
Ejemplo n.º 2
0
        private static IResponse ExecuteMethod(ApiCall call, ApiMethod apimethod)
        {
            var response = Methods.ApiMethodManager.Execute(apimethod, call);

            if (apimethod.IsVoid)
            {
                return(new JsonResponse()
                {
                    Success = true, DataChange = true
                });
            }

            if (response != null && response.GetType() == typeof(bool))
            {
                var ok     = (bool)response;
                var result = new JsonResponse()
                {
                    Success = ok
                };
                if (!ok)
                {
                    result.Messages.Add(Hardcoded.GetValue("Api method define a bool return type and return false", call.Context));
                }
                return(result);
            }

            if (response == null)
            {
                var result = new JsonResponse()
                {
                    Success = false
                };
                result.Messages.Add(Hardcoded.GetValue("method return null for required object type", call.Context) + " :" + apimethod.ReturnType.ToString());
                return(result);
            }

            if (response is IResponse)
            {
                return(response as IResponse);
                //TODO: set the response message to multiple lingual.
            }
            else
            {
                return(new JsonResponse(response)
                {
                    Success = true
                });
            }
        }
Ejemplo n.º 3
0
        public static IResponse Execute(ApiCall call, IApiProvider apiProvider)
        {
            ApiMethod apimethod = null;

            var apiobject = apiProvider.Get(call.Command.ObjectType);

            if (apiobject != null)
            {
                apimethod = Methods.ApiMethodManager.Get(apiobject, call.Command.Method);
            }

            if (apimethod == null && apiProvider.GetMethod != null)
            {
                apimethod = apiProvider.GetMethod(call);
            }

            if (apimethod == null)
            {
                var result = new JsonResponse()
                {
                    Success = false
                };
                result.Messages.Add(Hardcoded.GetValue("Api method Not Found", call.Context));
                return(result);
            }


            if (call.IsFake)
            {
                var fakedata = Lib.Development.FakeData.GetFakeValue(apimethod.ReturnType);
                return(new JsonResponse(fakedata)
                {
                    Success = true
                });
            }
            if (apiobject != null)
            {
                if (!ValidateRequirement(call.Command, call.Context, apiProvider))
                {
                    var result = new JsonResponse()
                    {
                        Success = false
                    };
                    result.Messages.Add(Hardcoded.GetValue("User or website not valid", call.Context));
                    return(result);
                }
            }

            List <string> errors = new List <string>();

            if (!ValideAssignModel(apimethod, call, errors.Add))
            {
                var result = new JsonResponse()
                {
                    Success = false
                };
                result.Messages.AddRange(errors);
                return(result);
            }

            if (!ValideParameters(apimethod, call, errors.Add))
            {
                var result = new JsonResponse()
                {
                    Success = false
                };
                result.Messages.AddRange(errors);
                return(result);
            }


            // ValidateKModel()


            try
            {
                return(ExecuteMethod(call, apimethod));
            }
            catch (Exception ex)
            {
                var result = new JsonResponse()
                {
                    Success = false
                };
                result.Messages.Add(ex.Message);

                Kooboo.Data.Log.ExceptionLog.Write(ex.Message + "\r\n" + ex.StackTrace + "\r\n" + ex.Source);

                return(result);
            }
        }
Ejemplo n.º 4
0
        private static IResponse ExecuteMethod(ApiCall call, ApiMethod apimethod)
        {
            object response = null;

            if (apimethod.ClassInstance is Api)
            {
                var instance = Activator.CreateInstance(apimethod.DeclareType) as Api;

                try
                {
                    var ok = instance.OnActionExecuting(call);
                    if (ok)
                    {
                        response = Methods.ApiMethodManager.Execute(apimethod, call);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    instance.OnActionExecuted(call);
                }
            }
            else
            {
                response = Methods.ApiMethodManager.Execute(apimethod, call);
            }


            if (apimethod.IsVoid)
            {
                return(new JsonResponse()
                {
                    Success = true, DataChange = true
                });
            }

            if (response != null && response.GetType() == typeof(bool))
            {
                var ok     = (bool)response;
                var result = new JsonResponse()
                {
                    Success = ok
                };
                if (!ok)
                {
                    result.Messages.Add(Hardcoded.GetValue("Api method define a bool return type and return false", call.Context));
                }
                return(result);
            }

            if (response == null)
            {
                var result = new JsonResponse()
                {
                    Success = false
                };
                result.Messages.Add(Hardcoded.GetValue("method return null for required object type", call.Context) + " :" + apimethod.ReturnType.ToString());
                return(result);
            }

            if (response is IResponse)
            {
                return(response as IResponse);
                //TODO: set the response message to multiple lingual.
            }
            else
            {
                return(new JsonResponse(response)
                {
                    Success = true
                });
            }
        }