/// <summary>
        /// Gets the authorization token. If token is no longer valid, performs the authorization call asynchronously.
        /// </summary>
        /// <param name="forceRefresh">if set to <c>true</c>, then discard current token and authorize again.</param>
        /// <returns>The token holder.</returns>
        public async Task <TokenHolder> GetAuthorizationTokenAsync(bool forceRefresh = false)
        {
            if (forceRefresh || this.tokenHolder == null || this.tokenHolder.Token == null || this.tokenHolder.ExpirationDate <= DateTime.Now)
            {
                string userId        = this.config.UserId;
                string group         = this.config.Group;
                string secret        = this.config.ClientSecret;
                string domain        = this.config.Domain;
                string formatVersion = FormatVersion;
                string clientId      = this.CreateCredentialsString(userId, group, secret, domain, formatVersion);
                var    response      = await this.AuthorizeAsync(clientId);

                if (response.IsSuccess)
                {
                    var value = response.Value;
                    this.tokenHolder = TokenHolder.Valid(value.AccessToken, value.ExpiresIn);
                }
                else
                {
                    Logger.DebugFormat("Authorization failed.\nStatus code: {0}\nMessage:\n{1}", response.StatusCode, response.Message);
                    this.tokenHolder = TokenHolder.Invalid(response.StatusCode, response.Message);
                }
            }

            return(this.tokenHolder);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Performs a HTTP call with authentication with specified number of retry attempts.
        /// </summary>
        /// <typeparam name="TResponse">The type of the response.</typeparam>
        /// <param name="method">The callback used to perform request.</param>
        /// <param name="retryCount">The retry count.</param>
        /// <param name="forceRefresh">If set to <c>true</c>, then acquire a new authentication token.</param>
        /// <returns>The HTTP response.</returns>
        private async Task <HttpResponse <TResponse> > CallAuthRetry <TResponse>(
            Func <HttpClient, Task <HttpResponseMessage> > method,
            int retryCount    = 1,
            bool forceRefresh = false)
        {
            TokenHolder tokenHolder = await this.restAuthorizationManager.GetAuthorizationTokenAsync(forceRefresh);

            if (tokenHolder.IsValid)
            {
                var response = await this.Call <TResponse>(method, tokenHolder.Token);

                if (response.StatusCode == HttpStatusCode.Unauthorized && retryCount > 0)
                {
                    return(await this.CallAuthRetry <TResponse>(method, retryCount - 1, true));
                }

                return(response);
            }
            else
            {
                return(HttpResponse <TResponse> .Fail(tokenHolder.ErrorStatusCode, tokenHolder.ErrorMessage));
            }
        }