コード例 #1
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
                });
            }
        }
コード例 #2
0
ファイル: ApiManager.cs プロジェクト: outputlog/Kooboo
        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);
            }

            // check permission
            if (apiProvider.CheckAccess != null)
            {
                if (!apiProvider.CheckAccess(call.Context, apimethod))
                {
                    var result = new JsonResponse()
                    {
                        Success = false
                    };
                    result.Messages.Add(Hardcoded.GetValue("Unauthorized access", 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.Instance.Exception.Write(ex.Message + "\r\n" + ex.StackTrace + "\r\n" + ex.Source);

                return(result);
            }
        }
コード例 #3
0
 public static string GetHelper(ApiCall request)
 {
     return(null);
 }
コード例 #4
0
        public async Task Invoke(RenderContext context)
        {
            context.Request.Channel = RequestChannel.API;

            if (!string.IsNullOrEmpty(this.Prefix) && !context.Request.RawRelativeUrl.ToLower().StartsWith(this.Prefix))
            {
                await Next.Invoke(context); return;
            }
            ApiCommand command;

            if (!ApiRoute.ApiRouting.TryParseCommand(context.Request.RelativeUrl, context.Request.Method, out command, this.BeforeApi))
            {
                context.Response.StatusCode = 500;
                context.Response.Body       = System.Text.Encoding.UTF8.GetBytes("Invalid Api command");

                if (Log != null)
                {
                    Log(context, null);
                }

                return;
            }

            ApiCall apirequest = new ApiCall()
            {
                Command = command, Context = context
            };

            var response = ApiManager.Execute(apirequest, this.ApiProvider);

            if (Log != null)
            {
                Log(context, response);
            }

            if (response is MetaResponse)
            {
                var dataresponse = response as MetaResponse;
                foreach (var item in dataresponse.Headers)
                {
                    context.Response.Headers.Add(item.Key, string.Join(null, item.Value));
                }

                foreach (var item in dataresponse.DeletedCookieNames)
                {
                    context.Response.DeleteCookie(item);
                }
                foreach (var item in dataresponse.AppendedCookies)
                {
                    context.Response.AddCookie(item);
                }
                dataresponse.DeletedCookieNames.Clear();
                dataresponse.AppendedCookies.Clear();
                dataresponse.Headers.Clear();

                context.Response.StatusCode = dataresponse.StatusCode;
                if (!string.IsNullOrEmpty(dataresponse.RedirectUrl))
                {
                    context.Response.Redirect(302, dataresponse.RedirectUrl);
                    return;
                }
            }

            if (response is BinaryResponse)
            {
                RenderBinary(context, response as BinaryResponse);
            }
            else if (response is NoResponse)
            {
                // do nothing.
            }
            else if (response is PlainResponse)
            {
                RenderPlainResponse(context, response as PlainResponse);
            }
            else
            {
                MultilingualService.EnsureLangText(response, context);
                var jsonbody = Lib.Helper.JsonHelper.Serialize(response);
                context.Response.ContentType = "application/json";
                context.Response.Body        = System.Text.Encoding.UTF8.GetBytes(jsonbody);

                context.Response.StatusCode = 200;
            }

            // k.response.setHeader("Access-Control-Allow-Origin", "*");
            //  k.response.setHeader("Access-Control-Allow-Headers", "*");

            foreach (var item in DefaultHeaders)
            {
                if (!context.Response.Headers.ContainsKey(item.Key))
                {
                    context.Response.Headers.Add(item.Key, item.Value);
                }
            }
        }
コード例 #5
0
 public virtual void OnActionExecuted(ApiCall ApiCall)
 {
 }
コード例 #6
0
 public virtual IResponse OnActionExecuting(ApiCall ApiCall)
 {
     return(null);
 }
コード例 #7
0
ファイル: ApiManager.cs プロジェクト: stantoxt/Kooboo
        public static IResponse Execute(ApiCall call, IApiProvider apiProvider)
        {
            var apiobject = apiProvider.Get(call.Command.ObjectType);

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

            var apimethod = Methods.ApiMethodManager.Get(apiobject, call.Command.Method);

            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 (!ValidateRequirement(call.Command, call.Context.WebSite, call.Context.User, 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);
            }

            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);
            }
        }