Skip to content

Commit 350526b

Browse files
committed
update v2/query to v4/query
1 parent 0794f2b commit 350526b

File tree

2 files changed

+96
-119
lines changed

2 files changed

+96
-119
lines changed

src/Qiniu/Storage/ZoneHelper.cs

Lines changed: 62 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -53,76 +53,79 @@ public static Zone QueryZone(string accessKey, string bucket, string scheme = "h
5353
HttpResult hr = null;
5454
try
5555
{
56-
string queryUrl = string.Format("{0}{1}/v2/query?ak={2}&bucket={3}",
56+
string queryUrl = string.Format("{0}{1}/v4/query?ak={2}&bucket={3}",
5757
scheme,
5858
Config.DefaultUcHost,
5959
accessKey,
6060
bucket
6161
);
6262
HttpManager httpManager = new HttpManager();
6363
hr = httpManager.Get(queryUrl, null);
64-
if (hr.Code == (int)HttpCode.OK && !string.IsNullOrEmpty(hr.Text))
64+
if (hr.Code != (int) HttpCode.OK || string.IsNullOrEmpty(hr.Text))
6565
{
66-
ZoneInfo zInfo = JsonConvert.DeserializeObject<ZoneInfo>(hr.Text);
67-
if (zInfo != null)
68-
{
69-
zone = new Zone();
70-
zone.SrcUpHosts = zInfo.Up.Src.Main;
71-
zone.CdnUpHosts = zInfo.Up.Acc.Main;
72-
73-
if (!string.IsNullOrEmpty(zInfo.Io.Src.Main[0]))
74-
{
75-
zone.IovipHost = zInfo.Io.Src.Main[0];
76-
}
77-
else
78-
{
79-
zone.IovipHost = Config.DefaultIoHost;
80-
}
81-
82-
if (!string.IsNullOrEmpty(zInfo.Api.Acc.Main[0]))
83-
{
84-
zone.ApiHost = zInfo.Api.Acc.Main[0];
85-
}
86-
else
87-
{
88-
zone.ApiHost = Config.DefaultApiHost;
89-
}
90-
91-
if (!string.IsNullOrEmpty(zInfo.Rs.Acc.Main[0]))
92-
{
93-
zone.RsHost = zInfo.Rs.Acc.Main[0];
94-
}
95-
else
96-
{
97-
zone.RsHost = Config.DefaultRsHost;
98-
}
99-
100-
if (!string.IsNullOrEmpty(zInfo.Rsf.Acc.Main[0]))
101-
{
102-
zone.RsfHost = zInfo.Rsf.Acc.Main[0];
103-
}
104-
else
105-
{
106-
zone.RsfHost = Config.DefaultRsfHost;
107-
}
108-
109-
lock (rwLock)
110-
{
111-
zoneCacheValue = new ZoneCacheValue();
112-
TimeSpan ttl = TimeSpan.FromSeconds(zInfo.Ttl);
113-
zoneCacheValue.Deadline = DateTime.Now.Add(ttl);
114-
zoneCacheValue.Zone = zone;
115-
zoneCache[cacheKey] = zoneCacheValue;
116-
}
117-
}
118-
else
119-
{
120-
throw new Exception("JSON Deserialize failed: " + hr.Text);
121-
}
66+
throw new Exception("code: " + hr.Code + ", text: " + hr.Text + ", ref-text:" + hr.RefText);
67+
}
68+
69+
ZoneInfo zInfo = JsonConvert.DeserializeObject<ZoneInfo>(hr.Text);
70+
if (zInfo == null)
71+
{
72+
throw new Exception("JSON Deserialize failed: " + hr.Text);
73+
}
74+
75+
if (zInfo.Hosts.Length == 0)
76+
{
77+
throw new Exception("There are no hosts available: " + hr.Text);
78+
}
79+
80+
ZoneHost zHost = zInfo.Hosts[0];
81+
82+
zone = new Zone();
83+
zone.SrcUpHosts = zHost.Up.Domains;
84+
zone.CdnUpHosts = zHost.Up.Domains;
85+
86+
if (!string.IsNullOrEmpty(zHost.Io.Domains[0]))
87+
{
88+
zone.IovipHost = zHost.Io.Domains[0];
12289
}
12390
else
12491
{
125-
throw new Exception("code: " + hr.Code + ", text: " + hr.Text + ", ref-text:" + hr.RefText);
92+
zone.IovipHost = Config.DefaultIoHost;
93+
}
94+
95+
if (!string.IsNullOrEmpty(zHost.Api.Domains[0]))
96+
{
97+
zone.ApiHost = zHost.Api.Domains[0];
98+
}
99+
else
100+
{
101+
zone.ApiHost = Config.DefaultApiHost;
102+
}
103+
104+
if (!string.IsNullOrEmpty(zHost.Rs.Domains[0]))
105+
{
106+
zone.RsHost = zHost.Rs.Domains[0];
107+
}
108+
else
109+
{
110+
zone.RsHost = Config.DefaultRsHost;
111+
}
112+
113+
if (!string.IsNullOrEmpty(zHost.Rsf.Domains[0]))
114+
{
115+
zone.RsfHost = zHost.Rsf.Domains[0];
116+
}
117+
else
118+
{
119+
zone.RsfHost = Config.DefaultRsfHost;
120+
}
121+
122+
lock (rwLock)
123+
{
124+
zoneCacheValue = new ZoneCacheValue();
125+
TimeSpan ttl = TimeSpan.FromSeconds(zHost.Ttl);
126+
zoneCacheValue.Deadline = DateTime.Now.Add(ttl);
127+
zoneCacheValue.Zone = zone;
128+
zoneCache[cacheKey] = zoneCacheValue;
126129
}
127130
}
128131
catch (Exception ex)

src/Qiniu/Storage/ZoneInfo.cs

Lines changed: 34 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,82 +6,56 @@ namespace Qiniu.Storage
66
/// 从uc.qbox.me返回的消息
77
/// </summary>
88
internal class ZoneInfo
9+
{
10+
[JsonProperty("hosts")]
11+
public ZoneHost[] Hosts { get; set; }
12+
}
13+
14+
internal class ZoneHost
915
{
1016
/// <summary>
1117
/// 过期时间,单位:秒
1218
/// </summary>
1319
[JsonProperty("ttl")]
1420
public int Ttl { get; set; }
15-
21+
22+
[JsonProperty("region")]
23+
public string Region { get; set; }
24+
1625
[JsonProperty("io")]
17-
public Io Io { set; get; }
18-
26+
public ServiceDomains Io { get; set; }
27+
28+
[JsonProperty("io_src")]
29+
public ServiceDomains IoSrc { get; set; }
30+
1931
[JsonProperty("up")]
20-
public Up Up { set; get; }
32+
public ServiceDomains Up { get; set; }
2133

2234
[JsonProperty("api")]
23-
public Api Api { set; get; }
35+
public ServiceDomains Api { get; set; }
2436

2537
[JsonProperty("rs")]
26-
public Rs Rs { set; get; }
38+
public ServiceDomains Rs { get; set; }
2739

2840
[JsonProperty("rsf")]
29-
public Rsf Rsf { set; get; }
30-
}
31-
32-
internal class Io
33-
{
34-
[JsonProperty("src")]
35-
public Src Src { set; get; }
36-
}
37-
38-
internal class Src
39-
{
40-
[JsonProperty("main")]
41-
public string[] Main { set; get; }
42-
}
43-
44-
internal class Up
45-
{
46-
[JsonProperty("acc")]
47-
public DomainInfo Acc { set; get; }
48-
49-
[JsonProperty("old_acc")]
50-
public DomainInfo OldAcc { set; get; }
51-
52-
[JsonProperty("src")]
53-
public DomainInfo Src { set; get; }
41+
public ServiceDomains Rsf { get; set; }
5442

55-
[JsonProperty("old_src")]
56-
public DomainInfo OldSrc { set; get; }
57-
}
58-
internal class DomainInfo
59-
{
60-
[JsonProperty("main")]
61-
public string[] Main { set; get; }
43+
[JsonProperty("s3")]
44+
public ServiceDomains S3 { get; set; }
6245

63-
[JsonProperty("backup", NullValueHandling = NullValueHandling.Ignore)]
64-
public string[] Backup { set; get; }
65-
66-
[JsonProperty("info", NullValueHandling = NullValueHandling.Ignore)]
67-
public string Info { set; get; }
68-
}
46+
[JsonProperty("uc")]
47+
public ServiceDomains Uc { get; set; }
6948

70-
internal class Api
71-
{
72-
[JsonProperty("acc")]
73-
public DomainInfo Acc { set; get; }
74-
}
75-
76-
internal class Rs
77-
{
78-
[JsonProperty("acc")]
79-
public DomainInfo Acc { set; get; }
80-
}
81-
82-
internal class Rsf
83-
{
84-
[JsonProperty("acc")]
85-
public DomainInfo Acc { set; get; }
49+
internal class ServiceDomains
50+
{
51+
[JsonProperty("domains")]
52+
public string[] Domains { get; set; }
53+
54+
[JsonProperty("old", NullValueHandling = NullValueHandling.Ignore)]
55+
public string[] Old { get; set; }
56+
57+
[JsonProperty("region_alias", NullValueHandling = NullValueHandling.Ignore)]
58+
public string RegionAlias { get; set; }
59+
}
8660
}
8761
}

0 commit comments

Comments
 (0)