|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using Qiniu.Util; |
| 6 | +using Qiniu.Fusion.Model; |
| 7 | +using System.Net; |
| 8 | +using Qiniu.Http; |
| 9 | +using Newtonsoft.Json; |
| 10 | + |
| 11 | +namespace Qiniu.Fusion |
| 12 | +{ |
| 13 | + public class FusionManager |
| 14 | + { |
| 15 | + private Mac mac; |
| 16 | + private HttpManager httpMgr; |
| 17 | + |
| 18 | + public FusionManager(Mac mac) |
| 19 | + { |
| 20 | + this.mac = mac; |
| 21 | + httpMgr = new HttpManager(); |
| 22 | + } |
| 23 | + |
| 24 | + private string refreshUrl() |
| 25 | + { |
| 26 | + return string.Format("{0}/v2/tune/refresh", Common.Config.FUSION_API_HOST); |
| 27 | + } |
| 28 | + |
| 29 | + private string prefetchUrl() |
| 30 | + { |
| 31 | + return string.Format("{0}/v2/tune/prefetch", Common.Config.FUSION_API_HOST); |
| 32 | + } |
| 33 | + |
| 34 | + private string bandwidthUrl() |
| 35 | + { |
| 36 | + return string.Format("{0}/v2/tune/bandwidth", Common.Config.FUSION_API_HOST); |
| 37 | + } |
| 38 | + |
| 39 | + private string fluxUrl() |
| 40 | + { |
| 41 | + return string.Format("{0}/v2/tune/flux", Common.Config.FUSION_API_HOST); |
| 42 | + } |
| 43 | + |
| 44 | + private string loglistUrl() |
| 45 | + { |
| 46 | + return string.Format("{0}/v2/tune/log/list", Common.Config.FUSION_API_HOST); |
| 47 | + } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// 缓存刷新 |
| 51 | + /// </summary> |
| 52 | + /// <param name="request"></param> |
| 53 | + /// <returns></returns> |
| 54 | + public RefreshResult Refresh(RefreshRequest request) |
| 55 | + { |
| 56 | + RefreshResult result = new RefreshResult(); |
| 57 | + |
| 58 | + string url = refreshUrl(); |
| 59 | + string body = request.ToJsonStr(); |
| 60 | + byte[] data = Encoding.UTF8.GetBytes(body); |
| 61 | + |
| 62 | + string token = Auth.createManageToken(url, null, mac); |
| 63 | + |
| 64 | + Dictionary<string, string> headers = new Dictionary<string, string>(); |
| 65 | + headers.Add("Authorization", token); |
| 66 | + |
| 67 | + httpMgr.postData(url, headers, data, HttpManager.FORM_MIME_JSON, |
| 68 | + new CompletionHandler(delegate(ResponseInfo respInfo,string respJson) |
| 69 | + { |
| 70 | + if(respInfo.StatusCode!=200) |
| 71 | + { |
| 72 | + Console.WriteLine(respInfo); |
| 73 | + } |
| 74 | + |
| 75 | + result = JsonConvert.DeserializeObject<RefreshResult>(respJson); |
| 76 | + })); |
| 77 | + |
| 78 | + |
| 79 | + return result; |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// 文件预取 |
| 84 | + /// </summary> |
| 85 | + /// <param name="request"></param> |
| 86 | + /// <returns></returns> |
| 87 | + public PrefetchResult Prefetch(PrefetchRequest request) |
| 88 | + { |
| 89 | + PrefetchResult result = new PrefetchResult(); |
| 90 | + |
| 91 | + string url = prefetchUrl(); |
| 92 | + string body = request.ToJsonStr(); |
| 93 | + byte[] data = Encoding.UTF8.GetBytes(body); |
| 94 | + |
| 95 | + string token = Auth.createManageToken(url, null, mac); |
| 96 | + |
| 97 | + Dictionary<string, string> headers = new Dictionary<string, string>(); |
| 98 | + headers.Add("Authorization", token); |
| 99 | + |
| 100 | + httpMgr.postData(url, headers, data, HttpManager.FORM_MIME_JSON, |
| 101 | + new CompletionHandler(delegate (ResponseInfo respInfo, string respJson) |
| 102 | + { |
| 103 | + if (respInfo.StatusCode != 200) |
| 104 | + { |
| 105 | + Console.WriteLine(respInfo); |
| 106 | + } |
| 107 | + |
| 108 | + result = JsonConvert.DeserializeObject<PrefetchResult>(respJson); |
| 109 | + })); |
| 110 | + |
| 111 | + return result; |
| 112 | + } |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// 带宽 |
| 116 | + /// </summary> |
| 117 | + /// <param name="request"></param> |
| 118 | + /// <returns></returns> |
| 119 | + public BandwidthResult Bandwidth(BandwidthRequest request) |
| 120 | + { |
| 121 | + BandwidthResult result = new BandwidthResult(); |
| 122 | + |
| 123 | + string url = bandwidthUrl(); |
| 124 | + string body = request.ToJsonStr(); |
| 125 | + byte[] data = Encoding.UTF8.GetBytes(body); |
| 126 | + |
| 127 | + string token = Auth.createManageToken(url, null, mac); |
| 128 | + |
| 129 | + Dictionary<string, string> headers = new Dictionary<string, string>(); |
| 130 | + headers.Add("Authorization", token); |
| 131 | + |
| 132 | + httpMgr.postData(url, headers, data, HttpManager.FORM_MIME_JSON, |
| 133 | + new CompletionHandler(delegate (ResponseInfo respInfo, string respJson) |
| 134 | + { |
| 135 | + if (respInfo.StatusCode != 200) |
| 136 | + { |
| 137 | + Console.WriteLine(respInfo); |
| 138 | + } |
| 139 | + |
| 140 | + result = JsonConvert.DeserializeObject<BandwidthResult>(respJson); |
| 141 | + })); |
| 142 | + |
| 143 | + return result; |
| 144 | + } |
| 145 | + |
| 146 | + /// <summary> |
| 147 | + /// 流量 |
| 148 | + /// </summary> |
| 149 | + /// <param name="request"></param> |
| 150 | + /// <returns></returns> |
| 151 | + public FluxResult Flux(FluxRequest request) |
| 152 | + { |
| 153 | + FluxResult result = new FluxResult(); |
| 154 | + |
| 155 | + string url = fluxUrl(); |
| 156 | + string body = request.ToJsonStr(); |
| 157 | + byte[] data = Encoding.UTF8.GetBytes(body); |
| 158 | + |
| 159 | + string token = Auth.createManageToken(url, null, mac); |
| 160 | + |
| 161 | + Dictionary<string, string> headers = new Dictionary<string, string>(); |
| 162 | + headers.Add("Authorization", token); |
| 163 | + |
| 164 | + httpMgr.postData(url, headers, data, HttpManager.FORM_MIME_JSON, |
| 165 | + new CompletionHandler(delegate (ResponseInfo respInfo, string respJson) |
| 166 | + { |
| 167 | + if (respInfo.StatusCode != 200) |
| 168 | + { |
| 169 | + Console.WriteLine(respInfo); |
| 170 | + } |
| 171 | + |
| 172 | + result = JsonConvert.DeserializeObject<FluxResult>(respJson); |
| 173 | + })); |
| 174 | + |
| 175 | + return result; |
| 176 | + } |
| 177 | + |
| 178 | + /// <summary> |
| 179 | + /// 日志列表 |
| 180 | + /// </summary> |
| 181 | + /// <param name="request"></param> |
| 182 | + /// <returns></returns> |
| 183 | + public LogListResult LogList(LogListRequest request) |
| 184 | + { |
| 185 | + LogListResult result = new LogListResult(); |
| 186 | + |
| 187 | + string url = loglistUrl(); |
| 188 | + string body = request.ToJsonStr(); |
| 189 | + byte[] data = Encoding.UTF8.GetBytes(body); |
| 190 | + |
| 191 | + string token = Auth.createManageToken(url, null, mac); |
| 192 | + |
| 193 | + Dictionary<string, string> headers = new Dictionary<string, string>(); |
| 194 | + headers.Add("Authorization", token); |
| 195 | + |
| 196 | + httpMgr.postData(url, headers, data, HttpManager.FORM_MIME_JSON, |
| 197 | + new CompletionHandler(delegate (ResponseInfo respInfo, string respJson) |
| 198 | + { |
| 199 | + result = JsonConvert.DeserializeObject<LogListResult>(respJson); |
| 200 | + result.Code = respInfo.StatusCode; |
| 201 | + if (respInfo.StatusCode != 200) |
| 202 | + { |
| 203 | + Console.WriteLine(respInfo); |
| 204 | + } |
| 205 | + })); |
| 206 | + |
| 207 | + return result; |
| 208 | + } |
| 209 | + } |
| 210 | +} |
0 commit comments