Example #1
0
        /// <summary>
        /// 从服务器中获取数据。
        /// </summary>
        /// <param name="dataUriType">要获取的数据的类型。</param>
        /// <param name="args">一个参数数组,其中的内容用于替换格式符号。</param>
        public static async Task <JToken> GetData(DataUriType dataUriType, params object[] args)
        {
            var json = await NetworkHelper.GetJson(string.Format(GetUriStringTemplate(dataUriType), args));

            JToken token = null;
            var    o     = JObject.Parse(json);

            if (!string.IsNullOrEmpty(json) &&
                !o.TryGetValue("data", out token) &&
                o.TryGetValue("message", out JToken value))
            {
                throw new Exceptions.ServerException($"{value}");
            }
            return(token);
        }
Example #2
0
        public static async Task <T> GetDataAsync <T>(DataUriType dataUriType, bool forceRefresh = false, params object[] args)
        {
            string uri    = string.Format(GetUriStringTemplate(dataUriType), args);
            string json   = string.Empty;
            T      result = default;

            if (forceRefresh || !responseCache.ContainsKey(uri))
            {
                json = await NetworkHelper.GetJson(uri);

                result = await Task.Run(() => JsonConvert.DeserializeObject <T>(json, new JsonSerializerSettings
                {
                    Error = (_, e) =>
                    {
                        if (!string.IsNullOrEmpty(json))
                        {
                            var o = JsonConvert.DeserializeObject <Models.Json.MessageModel.Rootobject>(json, new JsonSerializerSettings {
                                Error = (__, ee) => ee.ErrorContext.Handled = true
                            });
                            if (o != null)
                            {
                                throw new CoolapkMessageException(o.Message);
                            }
                        }
                        e.ErrorContext.Handled = true;
                    }
                }));

                if (responseCache.ContainsKey(uri))
                {
                    responseCache[uri] = result;
                }
                else
                {
                    responseCache.Add(uri, result);
                }
            }
            else
            {
                result = (T)responseCache[uri];
            }

            return(result);
        }
Example #3
0
        /// <summary>
        ///     从服务器中获取数据。
        /// </summary>
        /// <param name="type">
        ///     要获取的数据的类型。
        /// </param>
        /// <param name="args">
        ///     一个参数数组,其中的内容用于替换格式符号。
        /// </param>
        public static async Task <JToken> GetDataAsync(DataUriType type, params object[] args)
        {
            bool   forceRefresh = false;
            string uri          = string.Format(GetUriStringTemplate(type), args);
            string json;

            if (forceRefresh || !responseCache.ContainsKey(uri))
            {
                json = await NetworkHelper.GetJson(uri);

                if (responseCache.ContainsKey(uri))
                {
                    responseCache[uri] = json;
                }
                else
                {
                    responseCache.Add(uri, json);
                }
            }
            else
            {
                json = responseCache[uri] as string;
            }
            var    o     = JObject.Parse(json);
            JToken token = null;

            if (!string.IsNullOrEmpty(json) &&
                !o.TryGetValue("data", out token) &&
                o.TryGetValue("message", out JToken value))
            {
                throw new CoolapkMessageException(value);
            }
            else
            {
                return(token);
            }
        }