Ejemplo n.º 1
0
        private async Task <string> MakeRequest(IAbpMethodInvocation invocation)
        {
            using (var client = _httpClientFactory.Create())
            {
                var clientConfig = _clientOptions.HttpClientProxies.GetOrDefault(typeof(TService)) ?? throw new AbpException($"Could not get DynamicHttpClientProxyConfig for {typeof(TService).FullName}.");

                var baseUrl = GetBaseUrl(clientConfig);
                var action  = await _apiDescriptionFinder.FindActionAsync(baseUrl, typeof(TService), invocation.Method);

                var apiVersion = GetApiVersionInfo(action);
                var url        = baseUrl + UrlBuilder.GenerateUrlWithParameters(action, invocation.ArgumentsDictionary, apiVersion);

                var requestMessage = new HttpRequestMessage(action.GetHttpMethod(), url)
                {
                    Content = RequestPayloadBuilder.BuildContent(action, invocation.ArgumentsDictionary, _jsonSerializer, apiVersion)
                };

                AddHeaders(invocation, action, requestMessage, apiVersion);

                var accessToken = await _accessTokenProvider.GetOrNullAsync();

                if (accessToken != null)
                {
                    //TODO: "Bearer" should not be static.
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                }

                var response = await client.SendAsync(requestMessage);

                if (!response.IsSuccessStatusCode)
                {
                    await ThrowExceptionForResponseAsync(response);
                }

                return(await response.Content.ReadAsStringAsync());
            }
        }