private static IAsyncResult QueryItemFromServerAsync(string key, ConfigType type, Action<ConfigItem, Exception> callback)
        {
            string url = new UrlBuilder()
                .SetPath("/ActiveConfig/v1/GetKey")
                .AddParam("appid", _appKey)
                .AddParam("secretkey", _appSecret)
                .AddParam("key", key + ":" + (int)type)
                .GetUrl();

            return Http.GetStringAsync(url, (s, e) =>
            {
                ConfigItem result = null;
                if (e == null)
                {
                    result = null;

                    try
                    {
                        var dict = JSON.Parse(s);
                        if (dict != null)
                        {
                            string code = dict["code"].Value;
                            string msg = dict["msg"].Value;
                            string data = dict["data"].Value;

                            if (code != "0")
                            {
                                Debug.WriteLine("Server return error {0}: {1}", code, msg);
                                e = new ActiveConfigException(msg);
                            }
                            else
                            {
                                if (dict["data"].Count == 1)
                                {
                                    var item = dict["data"][0];
                                    result = new ConfigItem();
                                    result.Key = item["key"].Value;
                                    result.Value = item["value"].Value;
                                    result.Type = (ConfigType)Enum.Parse(typeof(ConfigType), item["type"].Value, true);
                                    result.ExpireTime = Helper.ConvertTime(item["endtime"].Value);
                                    result.MD5 = item["md5"].Value;
                                    result.ID = string.Format("{0}:{1}", (int)result.Type, result.Key);
                                    if (result.Status == ItemStatus.KeyNotFound)
                                    {
                                        e = new KeyNotFoundException(key);
                                    }
                                }
                                else
                                {
                                    e = new KeyNotFoundException(key);
                                }
                            }
                        }
                        else
                        {
                            e = new ActiveConfigException("Parsing json failed");
                        }
                    }
                    catch (Exception err)
                    {
                        e = new ActiveConfigException("Parsing json failed", err);
                    }
                }

                callback(result, e);
            });
        }
Esempio n. 2
0
        private static IAsyncResult QueryAll(ConfigItem[] keys, Action <ConfigItem[], Exception> callback)
        {
            string url = new UrlBuilder()
                         .SetPath("/ActiveConfig/v1/CheckUpdate")
                         .AddParam("appid", _appKey)
                         .AddParam("secretkey", _appSecret)
                         .AddParam("key", string.Join(";", keys.Select(x => x.Key + "," + x.MD5 + "," + (int)x.Type)))
                         .GetUrl();

            return(Http.GetStringAsync(url, (s, e) =>
            {
                List <ConfigItem> results = null;

                if (e == null)
                {
                    results = new List <ConfigItem>();
                    var dict = JSON.Parse(s);
                    if (dict != null)
                    {
                        string code = dict["code"].Value;
                        string msg = dict["msg"].Value;

                        if (code != "0")
                        {
                            Debug.WriteLine("Server return error {0}: {1}", code, msg);
                            e = new ActiveConfigException(msg);
                        }
                        else
                        {
                            foreach (var item in dict["data"].Childs)
                            {
                                ConfigItem c = new ConfigItem();
                                c.Key = item["key"].Value;
                                c.Type = (ConfigType)Enum.Parse(typeof(ConfigType), item["type"].Value, true);

                                var serverStatus = item["status"].Value != "" ? (ServerItemStatus)Enum.Parse(typeof(ServerItemStatus), item["status"].Value, true) : ServerItemStatus.Success;

                                if (serverStatus == ServerItemStatus.NoUpdate)
                                {
                                    continue;
                                }
                                else if (serverStatus == ServerItemStatus.Invalid)
                                {
                                    c.Status = ItemStatus.KeyNotFound;
                                }
                                else
                                {
                                    c.Value = item["value"].Value;
                                    c.ExpireTime = Helper.ConvertTime(item["endtime"].Value);
                                    c.MD5 = item["md5"].Value;
                                    c.Status = ItemStatus.OK;
                                }

                                results.Add(c);
                            }
                        }
                    }
                    else
                    {
                        e = new ActiveConfigException("Parsing json failed");
                    }
                }

                callback(results.ToArray(), e);
            }));
        }
        private static IAsyncResult QueryAll(ConfigItem[] keys, Action<ConfigItem[], Exception> callback)
        {
            string url = new UrlBuilder()
                .SetPath("/ActiveConfig/v1/CheckUpdate")
                .AddParam("appid", _appKey)
                .AddParam("secretkey", _appSecret)
                .AddParam("key", string.Join(";", keys.Select(x => x.Key + "," + x.MD5 + "," + (int)x.Type)))
                .GetUrl();

            return Http.GetStringAsync(url, (s, e) =>
            {
                List<ConfigItem> results = null;

                if (e == null)
                {
                    results = new List<ConfigItem>();
                    var dict = JSON.Parse(s);
                    if (dict != null)
                    {
                        string code = dict["code"].Value;
                        string msg = dict["msg"].Value;

                        if (code != "0")
                        {
                            Debug.WriteLine("Server return error {0}: {1}", code, msg);
                            e = new ActiveConfigException(msg);
                        }
                        else
                        {
                            foreach (var item in dict["data"].Childs)
                            {
                                ConfigItem c = new ConfigItem();
                                c.Key = item["key"].Value;
                                c.Type = (ConfigType)Enum.Parse(typeof(ConfigType), item["type"].Value, true);

                                var serverStatus = item["status"].Value != "" ? (ServerItemStatus)Enum.Parse(typeof(ServerItemStatus), item["status"].Value, true) : ServerItemStatus.Success;

                                if (serverStatus == ServerItemStatus.NoUpdate)
                                {
                                    continue;
                                }
                                else if (serverStatus == ServerItemStatus.Invalid)
                                {
                                    c.Status = ItemStatus.KeyNotFound;
                                }
                                else
                                {
                                    c.Value = item["value"].Value;
                                    c.ExpireTime = Helper.ConvertTime(item["endtime"].Value);
                                    c.MD5 = item["md5"].Value;
                                    c.Status = ItemStatus.OK;
                                }

                                results.Add(c);
                            }
                        }
                    }
                    else
                    {
                        e = new ActiveConfigException("Parsing json failed");
                    }
                }

                callback(results.ToArray(), e);
            });
        }
Esempio n. 4
0
        private static IAsyncResult QueryItemFromServerAsync(string key, ConfigType type, Action <ConfigItem, Exception> callback)
        {
            string url = new UrlBuilder()
                         .SetPath("/ActiveConfig/v1/GetKey")
                         .AddParam("appid", _appKey)
                         .AddParam("secretkey", _appSecret)
                         .AddParam("key", key + ":" + (int)type)
                         .GetUrl();

            return(Http.GetStringAsync(url, (s, e) =>
            {
                ConfigItem result = null;
                if (e == null)
                {
                    result = null;

                    try
                    {
                        var dict = JSON.Parse(s);
                        if (dict != null)
                        {
                            string code = dict["code"].Value;
                            string msg = dict["msg"].Value;
                            string data = dict["data"].Value;

                            if (code != "0")
                            {
                                Debug.WriteLine("Server return error {0}: {1}", code, msg);
                                e = new ActiveConfigException(msg);
                            }
                            else
                            {
                                if (dict["data"].Count == 1)
                                {
                                    var item = dict["data"][0];
                                    result = new ConfigItem();
                                    result.Key = item["key"].Value;
                                    result.Value = item["value"].Value;
                                    result.Type = (ConfigType)Enum.Parse(typeof(ConfigType), item["type"].Value, true);
                                    result.ExpireTime = Helper.ConvertTime(item["endtime"].Value);
                                    result.MD5 = item["md5"].Value;
                                    result.ID = string.Format("{0}:{1}", (int)result.Type, result.Key);
                                    if (result.Status == ItemStatus.KeyNotFound)
                                    {
                                        e = new KeyNotFoundException(key);
                                    }
                                }
                                else
                                {
                                    e = new KeyNotFoundException(key);
                                }
                            }
                        }
                        else
                        {
                            e = new ActiveConfigException("Parsing json failed");
                        }
                    }
                    catch (Exception err)
                    {
                        e = new ActiveConfigException("Parsing json failed", err);
                    }
                }

                callback(result, e);
            }));
        }