Ejemplo n.º 1
0
 private T Execute <T>(IJdRequest <T> request, string accessToken, DateTime timestamp) where T : JdResponse
 {
     if (disableTrace)
     {
         return(DoExecute <T>(request, accessToken, timestamp));
     }
     else
     {
         try
         {
             return(DoExecute <T>(request, accessToken, timestamp));
         }
         catch (Exception e)
         {
             topLogger.Error(this.serverUrl + "\r\n" + e.StackTrace);
             throw e;
         }
     }
 }
Ejemplo n.º 2
0
        public new T Execute <T> (IJdRequest <T> request, string session, DateTime timestamp) where T : JdResponse
        {
            T rsp = null;

            try {
                retryCounter++;
                rsp = base.Execute(request, session, timestamp);
                if (rsp.IsError)
                {
                    if (retryCounter < maxRetryCount)
                    {
                        if (rsp.SubErrCode != null && rsp.SubErrCode.StartsWith("isp."))
                        {
                            Thread.Sleep(retryWaitTime);
                            return(Execute(request, session, timestamp));
                        }
                    }
                    else
                    {
                        if (throwIfOverMaxRetry)
                        {
                            throw RETRY_FAIL;
                        }
                    }
                }
            } catch (Exception e) {
                if (e != RETRY_FAIL && retryCounter < maxRetryCount)
                {
                    Thread.Sleep(retryWaitTime);
                    return(Execute(request, session, timestamp));
                }
            } finally {
                retryWaitTime = -1;
            }
            return(rsp);
        }
Ejemplo n.º 3
0
 public T Execute <T> (IJdRequest <T> request, string session) where T : JdResponse
 {
     return(Execute <T> (request, session, DateTime.Now));
 }
Ejemplo n.º 4
0
 public T Execute <T> (IJdRequest <T> request) where T : JdResponse
 {
     return(Execute <T> (request, null));
 }
Ejemplo n.º 5
0
        private T DoExecute <T> (IJdRequest <T> request, string access_token, DateTime timestamp) where T : JdResponse
        {
            // 提前检查业务参数
            try {
                request.Validate();
            } catch (JdException e) {
                return(createErrorResponse <T> (e.ErrorCode, e.ErrorMsg));
            }

            // 添加协议级请求参数
            JdDictionary txtParams = new JdDictionary();

            txtParams.Add(METHOD, request.GetApiName());
            txtParams.Add(VERSION, "2.0");
            txtParams.Add(APP_KEY, appKey);
            txtParams.Add(TIMESTAMP, timestamp);
            if (access_token != null)
            {
                txtParams.Add(ACCESS_TOKEN, access_token);
            }
            txtParams.Add(PARAM_JSON, JsonConvert.ExportToString(request.GetParameters()));
            // 添加签名参数
            txtParams.Add(SIGN, JdUtils.SignJdRequest(txtParams, appSecret, true));
            string reqUrl = webUtils.BuildGetUrl(this.serverUrl, txtParams);

            try {
                string body;
                if (request is IJdUploadRequest <T> )                 // 是否需要上传文件
                {
                    IJdUploadRequest <T>           uRequest   = (IJdUploadRequest <T>)request;
                    IDictionary <string, FileItem> fileParams = JdUtils.CleanupDictionary(uRequest.GetFileParameters());
                    body = webUtils.DoPost(this.serverUrl, txtParams, fileParams);
                }
                else
                {
                    body = webUtils.DoPost(this.serverUrl, txtParams);
                }

                // 解释响应结果
                T rsp;
                if (disableParser)
                {
                    rsp      = Activator.CreateInstance <T> ();
                    rsp.Body = body;
                }
                else
                {
                    if (FORMAT_XML.Equals(format))
                    {
                        IJdParser tp = new JdXmlParser();
                        rsp = tp.Parse <T> (body);
                    }
                    else
                    {
                        IJdParser tp = new JdJsonParser();
                        rsp = tp.Parse <T> (body);
                    }
                }

                // 追踪错误的请求
                if (!disableTrace && rsp.IsError)
                {
                    StringBuilder sb = new StringBuilder(reqUrl).Append(" response error!\r\n").Append(rsp.Body);
                    JdLogger.Warn(sb.ToString());
                }
                return(rsp);
            } catch (Exception e) {
                if (!disableTrace)
                {
                    StringBuilder sb = new StringBuilder(reqUrl).Append(" request error!\r\n").Append(e.StackTrace);
                    JdLogger.Error(sb.ToString());
                }
                throw e;
            }
        }
Ejemplo n.º 6
0
 public T Execute <T> (IJdRequest <T> request, string session, DateTime timestamp) where T : JdResponse
 {
     return(DoExecute <T> (request, session, timestamp));
 }
Ejemplo n.º 7
0
 public T Execute <T>(IJdRequest <T> request, string accessToken) where T : JdResponse
 {
     return(Execute <T>(request, accessToken, DateTime.Now));
 }
Ejemplo n.º 8
0
 public T Execute <T>(IJdRequest <T> request) where T : JdResponse
 {
     return(Execute <T>(request, accessToken));
 }
Ejemplo n.º 9
0
        private T DoExecute <T>(IJdRequest <T> request, string accessToken, DateTime timestamp) where T : JdResponse
        {
            //if (String.IsNullOrEmpty(accessToken))
            //{
            //    throw new Exception("Access Token 必须提供!");
            //}

            // 提前检查业务参数
            try
            {
                request.Validate();
            }
            catch (JdException e)
            {
                return(createErrorResponse <T>(e.ErrorCode, e.ErrorMsg));
            }

            // 添加协议级请求参数
            JdDictionary txtParams = new JdDictionary();

            txtParams.Add(APP_PARAM, request.GetParamJson());
            txtParams.Add(METHOD, request.ApiName);
            txtParams.Add(VERSION, "2.0");
            txtParams.Add(APP_KEY, appKey);
            //txtParams.Add(FORMAT, format);
            //txtParams.Add(PARTNER_ID, "top-sdk-net-20111024");
            txtParams.Add(TIMESTAMP, timestamp);
            txtParams.Add(ACCESSTOKEN, accessToken);

            // 添加签名参数
            txtParams.Add(SIGN, JdUtils.SignJdRequest(txtParams, appSecret));

            // 是否需要上传文件
            string body;

            if (request is IJdUploadRequest <T> )
            {
                IJdUploadRequest <T>           uRequest   = (IJdUploadRequest <T>)request;
                IDictionary <string, FileItem> fileParams = JdUtils.CleanupDictionary(uRequest.GetFileParameters());
                body = webUtils.DoPost(this.serverUrl, txtParams, fileParams);
            }
            else
            {
                body = webUtils.DoPost(this.serverUrl, txtParams);
            }

            // 解释响应结果
            T rsp;

            if (disableParser)
            {
                rsp      = Activator.CreateInstance <T>();
                rsp.Body = body;
            }
            else
            {
                if (FORMAT_JSON.Equals(format))
                {
                    IJdParser <T> tp = new JdJsonParser <T>();
                    rsp = tp.Parse(body);
                }
                else
                {
                    IJdParser <T> tp = new JdXmlParser <T>();
                    rsp = tp.Parse(body);
                }
            }

            // 追踪错误的请求
            if (!disableTrace)
            {
                rsp.ReqUrl = webUtils.BuildGetUrl(this.serverUrl, txtParams);
                if (rsp.IsError)
                {
                    topLogger.Warn(rsp.ReqUrl + "\r\n" + rsp.Body);
                }
            }

            return(rsp);
        }