Exemple #1
0
        /// <summary>
        /// Gets a token for the specified subscription.
        /// </summary>
        /// <returns>The encoded JWT token prefixed with the string "Bearer ".</returns>
        /// <remarks>
        /// This method uses a cache to limit the number of request to the token service.
        /// A fresh token can be re-used during its lifetime of 10 minutes. After a successful
        /// request to the token service, this method caches the access token. Subsequent
        /// invocations of the method return the cached token for the next 5 minutes. After
        /// 5 minutes, a new token is fetched from the token service and the cache is updated.
        /// </remarks>
        public async Task <string> GetAccessTokenAsync()
        {
            if (string.IsNullOrEmpty(SubscriptionKey))
            {
                throw new ArgumentNullException(nameof(SubscriptionKey), "A subscription key is required");
            }

            // Re-use the cached token if there is one.
            if ((DateTime.Now - _storedTokenTime) < TokenCacheDuration && !string.IsNullOrWhiteSpace(_storedTokenValue))
            {
                return(_storedTokenValue);
            }

            using (var request = new HttpHelperRequest(ServiceUrl, HttpMethod.Post))
            {
                request.Headers.Add(OcpApimSubscriptionKeyHeader, SubscriptionKey);

                var response = await HttpHelper.Instance.SendRequestAsync(request).ConfigureAwait(false);

                var token = await response.GetTextResultAsync().ConfigureAwait(false);

                _storedTokenTime  = DateTime.Now;
                _storedTokenValue = $"Bearer {token}";

                return(_storedTokenValue);
            }
        }
Exemple #2
0
        /// <summary>
        /// Process Http Request using instance of HttpClient.
        /// </summary>
        /// <param name="request">instance of <see cref="HttpHelperRequest"/></param>
        /// <param name="cancellationToken">instance of <see cref="CancellationToken"/></param>
        /// <returns>Instane of <see cref="HttpHelperResponse"/></returns>
        public async Task <HttpHelperResponse> SendRequestAsync(HttpHelperRequest request, CancellationToken cancellationToken = default(CancellationToken))
        {
            await _semaphore.WaitAsync().ConfigureAwait(false);

            HttpClient client = null;

            try
            {
                var httpRequestMessage = request.ToHttpRequestMessage();

                client = GetHttpClientInstance();

                var response = await client.SendRequestAsync(httpRequestMessage).AsTask(cancellationToken).ConfigureAwait(false);

                FixInvalidCharset(response);

                return(new HttpHelperResponse(response));
            }
            finally
            {
                // Add the HttpClient instance back to the queue.
                if (client != null)
                {
                    _httpClientQueue.Enqueue(client);
                }

                _semaphore.Release();
            }
        }
Exemple #3
0
        private HttpHelperRequest CreateHttpRequest(string uriString)
        {
            var request = new HttpHelperRequest(new Uri(uriString));

            request.Headers.Add(AuthorizationUri, _authorizationHeaderValue);

            return(request);
        }