Exemple #1
0
        /// <summary>
        /// Invokes the web request asynchronously.
        /// </summary>
        /// <remarks>
        /// This is where the magic starts. This definitely works but needs more time to expand and harden for all potential use cases. Also want to clean up and make it
        /// easier to understand. Note: since we're using a HTTPClientFactory, we do not need to (and should not) dispose of the HttpClient, this is the factory's responsibility.
        /// </remarks>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="T1">The type of the 1.</typeparam>
        /// <param name="request">The request.</param>
        /// <returns></returns>
        protected async Task <T1> InvokeWebRequestAsync <T, T1>(T request)
            where T : Request
            where T1 : Response, new()
        {
            var httpRequest = _requestManager.ConstructRequest(request, ResourcePath);

            var token = await _authorizationManager.GetAuthenticationTokenAsync();

            var httpClient = _httpClientFactory.CreateClient("Tulkas");

            httpClient.SetBearerToken(token.AccessToken);

            HttpResponseMessage response;

            try
            {
                if (request.CancellationToken != null)
                {
                    response = await httpClient.SendAsync(httpRequest, request.CancellationToken);
                }
                else
                {
                    response = await httpClient.SendAsync(httpRequest);
                }
            }
            catch (TaskCanceledException taskCanceledException)
            {
                throw new TulkasException("User initiated task cancel", taskCanceledException);
            }
            catch (Exception ex)
            {
                //TODO SFold: Improve this exception handling
                throw new TulkasException(ex);
            }

            var returnProperty = typeof(T1).GetProperties().FirstOrDefault()?.PropertyType;

            return(await ProcessResponseAsync <T1>(response, returnProperty));
        }