コード例 #1
0
        public static MoodleRestResponse InvokeMethod(string methodName, HttpContextBase context, params KeyValuePair <string, string>[] requestParams)
        {
            var cache = getCachedResponse(methodName, context);

            if (cache != null)
            {
                return(cache);
            }
            RestClient client  = new RestClient(ConfigurationManager.AppSettings["MoodleBaseAddress"]);
            var        request = new RestRequest(ConfigurationManager.AppSettings["RestAddress"]);

            request.AddParameter("wstoken", ConfigurationManager.AppSettings["SecretKey"]);
            request.AddParameter("wsfunction", methodName);
            if (requestParams != null && requestParams.Length > 0)
            {
                object[] criteriaParams = new object[requestParams.Length];
                for (int i = 0; i < requestParams.Length; i++)
                {
                    criteriaParams[i] = new { key = requestParams[i].Key, value = requestParams[i].Value }
                }
                ;
                request.AddParameter("criteria", criteriaParams, ParameterType.RequestBody);
            }
            RestResponse <MoodleRestResponse> resp = (RestResponse <MoodleRestResponse>)client.Execute <MoodleRestResponse>(request);
            XmlSerializer      ser = new XmlSerializer(typeof(MoodleRestResponse));
            MoodleRestResponse res = (MoodleRestResponse)ser.Deserialize(new StringReader(resp.Content));

            setCachedResponse(methodName, res, context);
            return(res);
        }
    }
コード例 #2
0
        public static void setCachedResponse(string methodName, MoodleRestResponse resp, HttpContextBase context)
        {
            CacheItemPolicy policy = new CacheItemPolicy();

            policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(60);
            context.Cache.Add(methodName, resp, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, 30, 0), System.Web.Caching.CacheItemPriority.Normal, null);
        }