/// <summary> /// 同步执行api /// </summary> /// <typeparam name="TR"></typeparam> /// <param name="method"></param> /// <param name="resource"></param> /// <param name="headers"></param> /// <param name="input"></param> /// <returns></returns> internal TR execute <TR>(Method method, string resource, Dictionary <string, string> headers, MQSRequest input = null) where TR : MQSResponse, new() { //var restClient = new RestClient(this.url); var request = new RestRequest(resource, this.map(method)); request.RequestFormat = DataFormat.Xml; this.requestInit(method, request, headers, input); var response = restClient.Execute <TR>(request); if (response.Data != null) { return(response.Data); } else if (response.StatusCode == System.Net.HttpStatusCode.OK || response.StatusCode == System.Net.HttpStatusCode.NoContent) { return(null); } else { throw new MQSRequestException(string.Format("{0}:{1}", response.StatusCode, response.StatusDescription)); } }
/// <summary> /// 异步执行api,主用用在popmessage上,当queue的长轮询等待时间大于0时可以非阻塞执行代码 /// </summary> /// <typeparam name="TR"></typeparam> /// <param name="method"></param> /// <param name="resource"></param> /// <param name="headers"></param> /// <param name="callBack"></param> /// <param name="input"></param> internal void executeAsync <TR>(Method method, string resource, Dictionary <string, string> headers, Action <TR> callBack, MQSRequest input = null) where TR : MQSResponse, new() { //var restClient = new RestClient(this.url); var request = new RestRequest(resource, this.map(method)); request.RequestFormat = DataFormat.Xml; this.requestInit(method, request, headers, input); restClient.ExecuteAsync <TR>(request, IRR => { callBack(IRR.Data); }); }
/// <summary> /// 初始化参数与头部信息 /// </summary> /// <param name="request"></param> /// <param name="headers"></param> /// <param name="input"></param> private void requestInit(Method method, RestRequest request, Dictionary <string, string> headers, MQSRequest input) { if (!headers.ContainsKey(HeaderConst.HOST)) { headers[HeaderConst.HOST] = this.host; } if (!headers.ContainsKey(HeaderConst.DATE)) { headers[HeaderConst.DATE] = DateTime.Now.ToUniversalTime().ToString("r"); } if (!headers.ContainsKey(HeaderConst.MQVERSION)) { headers[HeaderConst.MQVERSION] = this.version; } //如果request不为空,需指定content-type,否则签名无法通过 if (input != null) { headers[HeaderConst.CONTENTTYPE] = "text/xml"; } headers[HeaderConst.AUTHORIZATION] = this.authorization(method, headers, string.Format("/{0}", request.Resource)); foreach (var kv in headers) { request.AddHeader(kv.Key, kv.Value); } if (input != null) { request.AddBody(input); } }