Example #1
0
        /// <summary>
        /// 使用AccessToken进行操作时,如果遇到AccessToken错误的情况,重新获取AccessToken一次,并重试。
        /// 使用此方法之前必须使用AccessTokenContainer.Register(_clientId, _clientSecret);
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fun"></param>
        /// <param name="clientId"></param>
        /// <param name="accessToken">AccessToken 如果为null,则自动取已经注册的第一个clientId/appSecret来信息获取AccessToken。</param>
        /// <param name="retryIfFaild">请保留默认值true,不用输入。</param>
        /// <returns></returns>
        public static T TryCommonApi <T>(Func <string, T> fun, string clientId = null, string accessToken = null, SessionType sessionType = SessionType.Prod, bool retryIfFaild = true) where T : WishJsonResult
        {
            if (clientId == null)
            {
                clientId = AccessTokenContainer.GetFirstOrDefaultClientId();
                if (clientId == null)
                {
                    throw new UnRegisterClientIdException(null,
                                                          "尚无已经注册的ClientId,请先使用AccessTokenContainer.Register完成注册(全局执行一次即可)!");
                }
            }
            else
            {
                if (!AccessTokenContainer.CheckRegistered(clientId))
                {
                    throw new UnRegisterClientIdException(clientId, string.Format("此clientId({0})尚未注册,请先使用AccessTokenContainer.Register完成注册(全局执行一次即可)!", clientId));
                }
            }


            T result = null;

            try
            {
                if (accessToken == null)
                {
                    var accessTokenResult = AccessTokenContainer.GetAccessTokenResult(clientId, false, sessionType);
                    accessToken = accessTokenResult.data.access_token;
                }
                result = fun(accessToken);
            }
            catch (ErrorJsonResultException ex)
            {
                if (retryIfFaild &&
                    clientId != null &&
                    ex.JsonResult.code > 0)
                {
                    //尝试重新验证
                    //var accessTokenResult = AccessTokenContainer.GetAccessTokenResult(clientId, true);
                    var accessTokenResult = AccessTokenContainer.RefreshAccessTokenResult(clientId, sessionType);
                    //强制获取并刷新最新的AccessToken
                    accessToken = accessTokenResult.data.access_token;
                    result      = TryCommonApi(fun, clientId, accessToken, sessionType, false);
                }
                else
                {
                    ex.ClientId = clientId;
                    throw;
                }
            }
            catch (WishException ex)
            {
                ex.ClientId = clientId;
                throw;
            }

            return(result);
        }
Example #2
0
        /// <summary>
        /// 使用AccessToken进行操作时,如果遇到AccessToken错误的情况,重新获取AccessToken一次,并重试。
        /// 使用此方法之前必须使用AccessTokenContainer.Register(_clientId, _clientSecret);
        ///
        /// AccessToken 如果为null,则自动取已经注册的第一个clientId/appSecret来信息获取AccessToken。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fun"></param>
        /// <param name="requestPolicy"></param>
        /// <param name="retryIfFaild">请保留默认值</param>
        /// <returns></returns>
        public static T TryCommonApi <T>(
            Func <Com.Alibaba.Entities.Request.RequestPolicy, T> fun,
            Com.Alibaba.Entities.Request.RequestPolicy requestPolicy,
            bool retryIfFaild = true) where T : AliJsonResult
        {
            if (string.IsNullOrEmpty(requestPolicy.ClientId))
            {
                requestPolicy.ClientId = AccessTokenContainer.GetFirstOrDefaultClientId();
                if (string.IsNullOrEmpty(requestPolicy.ClientId))
                {
                    throw new UnRegisterClientIdException(null, "尚无已经注册的ClientId,请先使用AccessTokenContainer.Register完成注册(全局执行一次即可)!");
                }
            }
            else
            {
                if (!AccessTokenContainer.CheckRegistered(requestPolicy.ClientId))
                {
                    throw new UnRegisterClientIdException(requestPolicy.ClientId, string.Format("此clientId({0})尚未注册,请先使用AccessTokenContainer.Register完成注册(全局执行一次即可)!", requestPolicy.ClientId));
                }
            }


            T result = null;

            try
            {
                if (string.IsNullOrEmpty(requestPolicy.AccessToken))
                {
                    var accessTokenResult = AccessTokenContainer.GetAccessTokenResult(requestPolicy.ClientId, false, requestPolicy.SessionType, requestPolicy.RequestSchema);
                    requestPolicy.AccessToken = accessTokenResult.access_token;
                }
                result = fun(requestPolicy);
            }
            catch (ErrorJsonResultException ex)
            {
                //Console.WriteLine($"clientId:{requestPolicy.ClientId},code:{ex.JsonResult.code},errorCode:{ex.JsonResult.errorCode}");
                if (retryIfFaild &&
                    !string.IsNullOrEmpty(requestPolicy.ClientId) &&
                    (ex.JsonResult.code > 0 || !string.IsNullOrEmpty(ex.JsonResult.errorCode)))
                {
                    //尝试重新验证
                    //var accessTokenResult = AccessTokenContainer.GetAccessTokenResult(clientId, true);
                    var accessTokenResult = AccessTokenContainer.RefreshAccessTokenResult(
                        requestPolicy.ClientId,
                        requestPolicy.SessionType,
                        requestPolicy.RequestSchema);

                    //强制获取并刷新最新的AccessToken
                    requestPolicy.AccessToken = accessTokenResult.access_token;
                    //Console.WriteLine($"refresh access token {requestPolicy.AccessToken}");
                    result = TryCommonApi(fun, requestPolicy, false);
                }
                else
                {
                    ex.ClientId = requestPolicy.ClientId;
                    throw;
                }
            }
            catch (AliException ex)
            {
                ex.ClientId = requestPolicy.ClientId;
                throw;
            }

            return(result);
        }