Пример #1
0
        private async Task <GeocodeResult> GetGeoCodeByCityNameAsync(string city)
        {
            var url = $"{ApiUrl}{Constants.ApiMockUrl}/Geocode/Get?city={city}";
            var geocodeResultJson = await HttpManager.GetAsync(url);

            return(JsonConvert.DeserializeObject <GeocodeResult>(geocodeResultJson));
        }
Пример #2
0
        /// <summary>
        /// [异步async]查询pfop操作处理结果(或状态)
        /// </summary>
        /// <param name="persistentId">持久化ID</param>
        /// <returns>操作结果</returns>
        public static async Task <HttpResult> PrefopAsync(string persistentId)
        {
            HttpResult result = new HttpResult();

            try
            {
                string prefopUrl = string.Format("{0}/status/get/prefop?id={1}", Config.ZONE.ApiHost, persistentId);

                HttpManager httpMgr = new HttpManager();
                result = await httpMgr.GetAsync(prefopUrl, null);
            }
            catch (Exception ex)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("[{0}] prefop Error:  ", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff"));
                Exception e = ex;
                while (e != null)
                {
                    sb.Append(e.Message + " ");
                    e = e.InnerException;
                }
                sb.AppendLine();

                result.RefCode  = (int)HttpCode.USER_EXCEPTION;
                result.RefText += sb.ToString();
            }

            return(result);
        }
Пример #3
0
        public async Task GetTest()
        {
            HttpManager target = new HttpManager();
            HttpResult  result = await target.GetAsync(TestURL1, null);

            //Assert.AreEqual((int)HttpCode.OK, result.Code);
            Assert.AreNotEqual((int)HttpCode.USER_EXCEPTION, result.RefCode);
        }
        /// <summary>
        ///     查询pfop操作处理结果(或状态)
        /// </summary>
        /// <param name="persistentId">持久化ID</param>
        /// <returns>操作结果</returns>
        public async Task <PrefopResult> Prefop(string persistentId)
        {
            var result = new PrefopResult();

            var scheme    = _config.UseHttps ? "https://" : "http://";
            var prefopUrl = $"{scheme}{Config.DefaultApiHost}/status/get/prefop?id={persistentId}";

            var httpMgr    = new HttpManager();
            var httpResult = await httpMgr.GetAsync(prefopUrl);

            result.Shadow(httpResult);

            return(result);
        }
Пример #5
0
        /// <summary>
        ///     获取空间文件信息
        /// </summary>
        /// <param name="bucket">空间名称</param>
        /// <param name="key">文件key</param>
        /// <returns>文件信息获取结果</returns>
        public async Task <StatResult> Stat(string bucket, string key)
        {
            var result = new StatResult();

            try
            {
                var host = await _config.RsHost(_mac.AccessKey, bucket);

                var statUrl = $"{host}{StatOp(bucket, key)}";
                var token   = _auth.CreateManageToken(statUrl);

                var hr = await _httpManager.GetAsync(statUrl, token);

                result.Shadow(hr);
            }
            catch (QiniuException ex)
            {
                var sb = new StringBuilder();
                sb.Append($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.ffff}] [stat] Error:  ");
                Exception e = ex;
                while (e != null)
                {
                    sb.Append(e.Message + " ");
                    e = e.InnerException;
                }

                sb.AppendLine();

                result.Code     = ex.HttpResult.Code;
                result.RefCode  = ex.HttpResult.Code;
                result.Text     = ex.HttpResult.Text;
                result.RefText += sb.ToString();
            }

            return(result);
        }
Пример #6
0
        /// <summary>
        /// 从uc.qbox.me查询得到回复后,解析出upHost,然后根据upHost确定Zone
        /// </summary>
        /// <param name="accessKey">AccessKek</param>
        /// <param name="bucket">空间名称</param>
        public static async Task <ZoneID> QueryZoneAsync(string accessKey, string bucket)
        {
            ZoneID zoneId = ZoneID.Invalid;

            try
            {
                // HTTP/GET https://uc.qbox.me/v1/query?ak=(AK)&bucket=(Bucket)
                string queryUrl = string.Format("https://uc.qbox.me/v1/query?ak={0}&bucket={1}", accessKey, bucket);

                HttpManager httpManager = new HttpManager();
                var         hr          = await httpManager.GetAsync(queryUrl, null);

                if (hr.Code == (int)HttpCode.OK)
                {
                    ZoneInfo zInfo = null;
                    if (JsonHelper.Deserialize(hr.Text, out zInfo))
                    {
                        string upHost = zInfo.HTTP.UP[0];
                        zoneId = ZONE_DICT[upHost];
                    }
                    else
                    {
                        throw new Exception("JSON Deserialize failed: " + hr.Text);
                    }
                }
                else
                {
                    throw new Exception("code: " + hr.Code + ", text: " + hr.Text + ", ref-text:" + hr.RefText);
                }
            }
            catch (Exception ex)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("[{0}] queryZone Error:  ", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff"));
                Exception e = ex;
                while (e != null)
                {
                    sb.Append(e.Message + " ");
                    e = e.InnerException;
                }
                sb.AppendLine();

                throw new Exception(sb.ToString());
            }

            return(zoneId);
        }
Пример #7
0
        private async Task <SMHIModel> GetWeatherDataAsync(Coordinate coordinate)
        {
            var model = new SMHIModel();

            var lon = coordinate.Longitude.ToString().Replace(",", ".");
            var lat = coordinate.Latitude.ToString().Replace(",", ".");
            var url = $"{ApiUrl}{Constants.ApiMockUrl}/Weather/Point/lon/{lon}/lat/{lat}";

            var res = await HttpManager.GetAsync(url);

            var obj = JsonConvert.DeserializeObject <SmhiGetPoint>(res);
            var earliestWeatherPrognosis     = obj.timeSeries.OrderBy(a => a.validTime).FirstOrDefault();
            var earliestWeatherPrognosisTemp = earliestWeatherPrognosis.parameters.Where(b => b.name == "t").FirstOrDefault();

            model.ValidTime   = earliestWeatherPrognosis.validTime;
            model.Temperature = decimal.Parse(earliestWeatherPrognosisTemp.values.FirstOrDefault(), CultureInfo.InvariantCulture);
            return(model);
        }
Пример #8
0
        /// <summary>
        ///     下载文件到本地
        /// </summary>
        /// <param name="url">(可访问的或者已授权的)链接</param>
        /// <param name="saveasFile">(另存为)本地文件名</param>
        /// <returns>下载资源的结果</returns>
        public async Task <HttpResult> Download(string url, string saveasFile)
        {
            var result = new HttpResult();

            try
            {
                var httpManager = new HttpManager();

                result = await httpManager.GetAsync(url, null, true);

                if (result.Code == (int)HttpCode.OK)
                {
                    using (var fs = File.Create(saveasFile, result.Data.Length))
                    {
                        fs.Write(result.Data, 0, result.Data.Length);
                        fs.Flush();
                    }

                    result.RefText += $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.ffff}] [Download] Success: (Remote file) ==> \"{saveasFile}\"\n";
                }
                else
                {
                    result.RefText += $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.ffff}] [Download] Error: code = {result.Code}\n";
                }
            }
            catch (Exception ex)
            {
                var sb = new StringBuilder();
                sb.Append($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.ffff}] [Download] Error:  ");
                var e = ex;
                while (e != null)
                {
                    sb.Append(e.Message + " ");
                    e = e.InnerException;
                }

                sb.AppendLine();

                result.RefCode  = (int)HttpCode.USER_UNDEF;
                result.RefText += sb.ToString();
            }

            return(result);
        }
Пример #9
0
        /// <summary>
        /// [异步async]下载文件到本地
        /// </summary>
        /// <param name="url">(可访问的或者已授权的)链接</param>
        /// <param name="saveasFile">(另存为)本地文件名</param>
        /// <returns>下载资源的结果</returns>
        public static async Task <HttpResult> DownloadAsync(string url, string saveasFile)
        {
            HttpResult result = new HttpResult();

            try
            {
                HttpManager httpManager = new HttpManager();

                result = await httpManager.GetAsync(url, null, true);

                if (result.Code == (int)HttpCode.OK)
                {
                    using (FileStream fs = File.Create(saveasFile, result.Data.Length))
                    {
                        fs.Write(result.Data, 0, result.Data.Length);
                        fs.Flush();
                    }
                    result.RefText += string.Format("[{0}] [Download] Success: (Remote file) ==> \"{1}\"\n",
                                                    DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff"), saveasFile);
                }
                else
                {
                    result.RefText += string.Format("[{0}] [Download] Error: code = {1}\n",
                                                    DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff"), result.Code);
                }
            }
            catch (Exception ex)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("[{0}] Download Error:  ", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff"));
                Exception e = ex;
                while (e != null)
                {
                    sb.Append(e.Message + " ");
                    e = e.InnerException;
                }
                sb.AppendLine();

                result.RefCode  = (int)HttpCode.USER_EXCEPTION;
                result.RefText += sb.ToString();
            }

            return(result);
        }
Пример #10
0
 public static async Task <ContextModel> GetContext(string domain, string accessToken, int id)
 {
     return(await HttpManager.GetAsync <ContextModel>($"{domain}{string.Format(Url.Context, id.ToString())}", accessToken, null));
 }
Пример #11
0
        public async Task <List <ProductDetail> > GetProducts()
        {
            var response = await httpManager.GetAsync <ProductDetail>(Constants.URL);

            return(response);
        }
Пример #12
0
        /// <summary>
        ///     从uc.qbox.me查询得到回复后,解析出upHost,然后根据upHost确定Zone
        /// </summary>
        /// <param name="accessKey">AccessKek</param>
        /// <param name="bucket">空间名称</param>
        public async Task <Zone> QueryZone(string accessKey, string bucket)
        {
            Zone zone = null;

            var cacheKey = $"{accessKey}:{bucket}";

            //check from cache
            lock (RwLock)
            {
                if (ZoneCache.ContainsKey(cacheKey))
                {
                    zone = ZoneCache[cacheKey];
                }
            }

            if (zone != null)
            {
                return(zone);
            }

            //query from uc api
            HttpResult hr = null;

            try
            {
                var queryUrl = $"https://uc.qbox.me/v2/query?ak={accessKey}&bucket={bucket}";
                hr = await _httpManager.GetAsync(queryUrl);

                if (hr.Code == (int)HttpCode.OK)
                {
                    var zInfo = JsonConvert.DeserializeObject <ZoneInfo>(hr.Text);
                    if (zInfo != null)
                    {
                        zone = new Zone
                        {
                            SrcUpHosts = zInfo.Up.Src.Main,
                            CdnUpHosts = zInfo.Up.Acc.Main,
                            IovipHost  = zInfo.Io.Src.Main[0]
                        };
                        if (zone.IovipHost.Contains("z1"))
                        {
                            zone.ApiHost = "api-z1.qiniu.com";
                            zone.RsHost  = "rs-z1.qiniu.com";
                            zone.RsfHost = "rsf-z1.qiniu.com";
                        }
                        else if (zone.IovipHost.Contains("z2"))
                        {
                            zone.ApiHost = "api-z2.qiniu.com";
                            zone.RsHost  = "rs-z2.qiniu.com";
                            zone.RsfHost = "rsf-z2.qiniu.com";
                        }
                        else if (zone.IovipHost.Contains("na0"))
                        {
                            zone.ApiHost = "api-na0.qiniu.com";
                            zone.RsHost  = "rs-na0.qiniu.com";
                            zone.RsfHost = "rsf-na0.qiniu.com";
                        }
                        else if (zone.IovipHost.Contains("as0"))
                        {
                            zone.ApiHost = "api-as0.qiniu.com";
                            zone.RsHost  = "rs-as0.qiniu.com";
                            zone.RsfHost = "rsf-as0.qiniu.com";
                        }
                        else
                        {
                            zone.ApiHost = "api.qiniu.com";
                            zone.RsHost  = "rs.qiniu.com";
                            zone.RsfHost = "rsf.qiniu.com";
                        }

                        lock (RwLock)
                        {
                            ZoneCache[cacheKey] = zone;
                        }
                    }
                    else
                    {
                        throw new Exception("JSON Deserialize failed: " + hr.Text);
                    }
                }
                else
                {
                    throw new Exception("code: " + hr.Code + ", text: " + hr.Text + ", ref-text:" + hr.RefText);
                }
            }
            catch (Exception ex)
            {
                var sb = new StringBuilder();
                sb.Append($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.ffff}] QueryZone Error:  ");
                var e = ex;
                while (e != null)
                {
                    sb.Append(e.Message + " ");
                    e = e.InnerException;
                }

                sb.AppendLine();

                throw new QiniuException(hr, sb.ToString());
            }

            return(zone);
        }