Example #1
0
 public ActionResult Quetion(int id)
 {
     try
     {
         var respons = new APIHub().GetRequest($"/2.2/questions/{id}?order=desc&sort=activity&site=stackoverflow");
         var model   = JsonConvert.DeserializeObject <Questions>(respons.Result.ToString());
         return(View(model));
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Example #2
0
 public ActionResult Index()
 {
     try
     {
         var respons = new APIHub().GetRequest("2.2/questions/?page=1&pagesize=50&order=desc&sort=activity&site=stackoverflow");
         var model   = JsonConvert.DeserializeObject <Questions>(respons.Result.ToString());
         return(View(model));
     }
     catch (Exception ex)
     {
         throw;
     }
 }
        public ActionResult Index(string methodName, string rawInput)
        {
            if (string.IsNullOrEmpty(methodName))
            {
                return(new EmptyResult());
            }

            if (Request.Browser.Crawler)
            {
                KernelContext.Log.Info("crawler:" + Request.UserAgent);

                return(Content(GenericException.Stringify(I18n.Exceptions["code_web_api_ban_search_engine_spider"],
                                                          I18n.Exceptions["text_web_api_ban_search_engine_spider"])));
            }

            // 限制 IP 访问频次 两个小时 500 次
            if (HttpRequestLimit.LimitIP())
            {
                return(Content(GenericException.Stringify(I18n.Exceptions["code_web_api_request_exceed_limit"],
                                                          I18n.Exceptions["text_web_api_request_exceed_limit"])));
            }

            DateTime timestamp = DateTime.Now;

            HttpContextBase context = this.HttpContext;

            IDictionary <string, APIMethod> dictionary = APIsConfigurationView.Instance.Configuration.APIMethods;

            string responseText = string.Empty;

            // 支持两种格式 connect/auth/authorize 和 connect.auth.authorize, 内部统一使用 connect.auth.authorize 格式
            if (methodName.IndexOf("/") > -1)
            {
                methodName = methodName.Replace("/", ".");
            }

            // 调试情况下记录输入参数
            if (context.Request.QueryString["xhr-debug"] == "1")
            {
                logger.Info("api:" + methodName + " start.");

                logger.Info(RequestHelper.Dump(context.Request, rawInput));
            }

            if (dictionary.ContainsKey(methodName))
            {
                // 优先执行 WebAPI 配置文件中设置的方法.
                responseText = APIHub.ProcessRequest(context, methodName, rawInput, logger, APIMethodInvoke);
            }
            else
            {
                // 匿名方法允许用户跳过验证直接访问
                // 应用方法信息
                ApplicationMethodInfo method = AppsContext.Instance.ApplicationMethodService.FindOneByName(methodName);

                if (method == null)
                {
                    logger.Warn(string.Format(I18n.Exceptions["text_web_api_method_not_exists"], methodName));

                    responseText = GenericException.Stringify(I18n.Exceptions["code_web_api_method_not_exists"],
                                                              string.Format(I18n.Exceptions["text_web_api_method_not_exists"], methodName));
                }
                else if (method.EffectScope == 1 || Authenticate(context, methodName))
                {
                    // 直接执行匿名方法 或者 验证需要身份验证方法

                    // 尝试执行 Application Method 中设置的方法.
                    responseText = APIHub.ProcessRequest(context, methodName, rawInput, logger, MethodInvoker.Invoke);
                }
                else
                {
                    responseText = "401";
                }
            }

            // 调试情况下记录输出参数
            if (context.Request.QueryString["xhr-debug"] == "1")
            {
                KernelContext.Log.Info("api " + methodName + " finished, timespan:" + DateHelper.GetTimeSpan(timestamp).TotalSeconds + "s.");

                KernelContext.Log.Info(responseText);
            }

            return(Content(responseText));
        }