Пример #1
0
        /// <summary>
        /// Send a simple request authenticated with a bearer token
        /// </summary>
        /// <param name="method">The http method</param>
        /// <param name="requestUri">The request uri</param>
        /// <param name="requestId">The request id</param>
        /// <param name="correlationId">The correlation id</param>
        /// <param name="bearerToken">The bearer token</param>
        /// <param name="customRequestBuilder">A custom request builder</param>
        /// <param name="content">The request content</param>
        /// <param name="cancellationToken">The cancellation token</param>
        /// <param name="caller">The caller</param>
        /// <returns></returns>
        protected async Task <HttpResponseMessage> SendRequest(
            HttpMethod method,
            Uri requestUri,
            Guid requestId,
            Guid correlationId,
            string bearerToken,
            Action <HttpRequestMessage> customRequestBuilder = null,
            string content = "",
            CancellationToken cancellationToken = default,
            [CallerMemberName] string caller    = "")
        {
            _logger.LogInformation(LoggingUtils.ComposeHttpRequestLogMessage(
                                       requestId,
                                       correlationId,
                                       caller,
                                       method,
                                       requestUri,
                                       content));

            var request = BuildRequest(
                method,
                requestUri,
                requestId,
                correlationId,
                bearerToken,
                content);

            // Give option to modify the request for non-default settings
            customRequestBuilder?.Invoke(request);

            return(await _httpClient.SendAsync(request, cancellationToken));
        }
Пример #2
0
        /// <summary>
        /// Send a request and return the http response message
        /// </summary>
        /// <param name="method">The http method</param>
        /// <param name="requestUri">The request uri</param>
        /// <param name="requestId">The request id</param>
        /// <param name="correlationId">The correlation id</param>
        /// <param name="bearerToken">The bearer token</param>
        /// <param name="customRequestBuilder">The custom request builder</param>
        /// <param name="content">The content</param>
        /// <param name="cancellationToken">The cancellation token</param>
        /// <param name="caller">The caller</param>
        /// <returns></returns>
        protected async Task <HttpResponseMessage> SendRequestAndReturnResult(
            HttpMethod method,
            Uri requestUri,
            Guid requestId,
            Guid correlationId,
            string bearerToken,
            Action <HttpRequestMessage> customRequestBuilder = null,
            string content = "",
            CancellationToken cancellationToken = default,
            [CallerMemberName] string caller    = "")
        {
            _logger.LogInformation(LoggingUtils.ComposeHttpRequestLogMessage(
                                       requestId,
                                       correlationId,
                                       caller,
                                       method,
                                       requestUri,
                                       content));

            var request = BuildRequest(
                method,
                requestUri,
                requestId,
                correlationId,
                bearerToken,
                content);

            // Give option to modify the request for non-default settings
            customRequestBuilder?.Invoke(request);

            var response = await _httpClient.SendAsync(request, cancellationToken);

            var result = await response.Content.ReadAsStringAsync();

            var responseLogMessage = LoggingUtils.ComposeHttpResponseLogMessage(
                requestId,
                correlationId,
                caller,
                response.StatusCode,
                result
                );

            if (response.IsSuccessStatusCode)
            {
                _logger.LogInformation(responseLogMessage);
                return(response);
            }
            else
            {
                throw new LunaServerException(
                          responseLogMessage,
                          ExceptionUtils.IsHttpErrorCodeRetryable(response.StatusCode));
            }
        }