/// <summary> /// Execute method /// </summary> /// <returns>himself</returns> public ApiManager Execute() { //try //{ //BackgroundWorker bw = new BackgroundWorker(); //bw.DoWork += (o, e) => // { this.apiResponseXml = null; string req = this.builder.BuildQuery(); this.OnLog("Request string: " + req); ApiRequest.Timeout = this.Timeout; this.apiResponseString = ApiRequest.Send(req); this.debugMsg(this.apiResponseString); if (!this.apiResponseString.Equals("") || this.apiResponseString.Length > 0) { this.apiResponseXml = new XmlDocument(); this.apiResponseXml.LoadXml(this.apiResponseString); XmlNode isError = this.apiResponseXml.SelectSingleNode("/error"); if (isError == null) { this.MethodSuccessed = true; } else { int code = Convert.ToInt32(isError.SelectSingleNode("error_code").InnerText); string msg = isError.SelectSingleNode("error_msg").InnerText; Hashtable ht = new Hashtable(); XmlNodeList pparams = isError.SelectNodes("request_params/param"); foreach (XmlNode n in pparams) { ht[n.SelectSingleNode("key").InnerText.ToString()] = n.SelectSingleNode("value").InnerText.ToString(); } throw new ApiRequestErrorException("Server error occurred", code, msg, ht); } //return this; } else { throw new ApiRequestEmptyAnswerException("API Server returns an empty answer or request timeout"); } //}; //bw.RunWorkerCompleted += (o, e) => // { // } //bw.WorkerSupportsCancellation = true; //bw.RunWorkerAsync(); return(this); //} //catch(Exception e) //{ // throw new ApiRequestNullResult("source message: "+e.Message); //} }
private ApiManager getResponse() { if (this.isCancelled) return this; try { this.apiResponseXml = null; string req = this.builder.BuildQuery(); if (this.cacheString == null) { this.Log("Request string: " + req); ApiRequest request = new ApiRequest(); request.Timeout = this.Timeout; this.apiResponseString = request.Send(req); if (this.isCacheEnabled) { this.AppendCache(req, this.apiResponseString); } this.DebugMessage(this.apiResponseString); } else { this.apiResponseString = cacheString; this.DebugMessage("FROM CACHE: "+this.apiResponseString); } if (!this.apiResponseString.Equals("") || this.apiResponseString.Length > 0) { this.ResponseData(this.apiResponseString, (cacheString != null)); this.apiResponseXml = new XmlDocument(); this.apiResponseXml.LoadXml(this.apiResponseString); XmlNode isError = this.apiResponseXml.SelectSingleNode("/error"); if (isError == null) { this.MethodSuccessed = true; this.RequestCompleted(RequestResult.Success); } else { int code = Convert.ToInt32(isError.SelectSingleNode("error_code").InnerText); string msg = isError.SelectSingleNode("error_msg").InnerText; Hashtable ht = new Hashtable(); XmlNodeList pparams = isError.SelectNodes("request_params/param"); foreach (XmlNode n in pparams) { ht[n.SelectSingleNode("key").InnerText.ToString()] = n.SelectSingleNode("value").InnerText.ToString(); } this.RequestCompleted(RequestResult.Error); throw new ApiRequestErrorException(msg, code, ht); } //return this; } else { this.RequestCompleted(RequestResult.Error); throw new ApiRequestEmptyAnswerException("API Server returns an empty answer or request timeout"); } } catch (ApiRequestErrorException e) { if (e.Code == (int)ErrorCodes.CapthaRequired) { if (!this.isCancelled) { XmlUtils.UseNode(this.apiResponseXml.SelectSingleNode("/error")); this.CapthaRequired(XmlUtils.String("captcha_img"), XmlUtils.String("captcha_sid")); } //return this; } else throw new ApiRequestErrorException(e.Message, e.Code, e.ParamsPassed); } return this; }