private object ExcuteApi(ConfiguredAPI api, HttpContext context, JToken param)
        {
            if (!api.implemented)
            {
                if (api.mock == null)
                {
                    throw new Exception("No mock data configured.");
                }

                var data = ResolveMock(param, api.mock);
                return(data);
            }

            var identity = ApiAuthorizeFilter.OnAuthorization(context, api.allowAnonymous);

            context.Items.Add("identity", identity);

            var cacheKey = context.Request.Path + (context.Request.QueryString.HasValue ? context.Request.QueryString.Value : "");

            if (api.cache?.enabled == true)
            {
                var data = CacheProviderFactory.Get(api.cache.type, cacheKey);

                if (data != null)
                {
                    return(SetStandardResult(context, data));
                }
            }

            object result;

            if (api.implementation.type == "csi")
            {
                DbRESTFulRepository repository = new DbRESTFulRepository()
                {
                    HttpContext = context, CurrentUser = identity
                };
                result = repository.Invoke(api.implementation.name, param);
            }
            else if (api.implementation.type == "repository")
            {
                result = RepositoryInvoker.Call(api.implementation.name, param, identity, context);
            }
            else if (api.implementation.type == "cri")
            {
                result = RedisInvoker.Call(api.implementation.name, param);
            }
            else
            {
                throw new Exception(string.Format("Unsupported implementation type '{0}'", api.implementation.type));
            }

            if (api.cache?.enabled == true)
            {
                CacheProviderFactory.Set(api.cache.type, cacheKey, result, api.cache.expiration);
            }

            return(SetStandardResult(context, result));
        }
示例#2
0
        public static bool TryMapRoute(string path, out ConfiguredAPI api)
        {
            var key = UrlCaseInsensitive ? path.ToLowerInvariant() : path;

            bool matched = APILoader.Map.ContainsKey(key);

            api = matched ? APILoader.Map[key] : null;

            return(matched);
        }