Пример #1
0
        /// <summary>
        /// Sends an HTTP get request to cluster http gateway.
        /// </summary>
        /// <param name="requestFunc">Func to create HttpRequest to send.</param>
        /// <param name="relativeUri">The relative URI.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The payload of the GET response.</returns>
        private async Task <HttpResponseMessage> SendAsyncHandleUnsuccessfulResponse(
            Func <HttpRequestMessage> requestFunc,
            string relativeUri,
            CancellationToken cancellationToken)
        {
            HttpRequestMessage FinalRequestFunc()
            {
                var request = requestFunc.Invoke();

                request.RequestUri = new Uri($"http://{DaprEndpoint}:{this.daprPort}/{relativeUri}");
                return(request);
            }

            var response = await this.SendAsyncHandleSecurityExceptions(FinalRequestFunc, cancellationToken);

            if (response.IsSuccessStatusCode)
            {
                return(response);
            }

            // TODO Log Unsuccessful Response.

            // Try to get Error Information if present in response body.
            if (response.Content != null)
            {
                DaprError error = null;

                try
                {
                    var contentStream = await response.Content.ReadAsStreamAsync();

                    if (contentStream.Length != 0)
                    {
                        error = await JsonSerializer.DeserializeAsync <DaprError>(contentStream, jsonSerializerOptions);
                    }
                }
                catch (Exception ex)
                {
                    throw new DaprApiException(string.Format("ServerErrorNoMeaningFulResponse", response.StatusCode), ex);
                }

                if (error != null)
                {
                    throw new DaprApiException(error.Message, error.ErrorCode, false);
                }
                else
                {
                    // Handle NotFound 404, without any ErrorCode.
                    if (response.StatusCode.Equals(HttpStatusCode.NotFound))
                    {
                        throw new DaprApiException("ErrorMessageHTTP404", Constants.ErrorDoesNotExist, false);
                    }
                }
            }

            // Couldn't determine Error information from response., throw exception with status code.
            throw new DaprApiException(string.Format("ServerErrorNoMeaningFulResponse", response.StatusCode));
        }
Пример #2
0
        /// <summary>
        /// Sends an HTTP get request to cluster http gateway.
        /// </summary>
        /// <param name="requestFunc">Func to create HttpRequest to send.</param>
        /// <param name="relativeUri">The relative URI.</param>
        /// <param name="requestId">Request Id for corelation.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The payload of the GET response.</returns>
        private async Task <HttpResponseMessage> SendAsyncHandleUnsuccessfulResponse(
            Func <HttpRequestMessage> requestFunc,
            string relativeUri,
            string requestId,
            CancellationToken cancellationToken)
        {
            HttpRequestMessage FinalRequestFunc()
            {
                var request = requestFunc.Invoke();

                request.RequestUri = new Uri($"http://{DaprEndpoint}:{this.daprPort}/{relativeUri}");

                // Add correlation IDs.
                request.Headers.Add(Constants.RequestIdHeaderName, this.GetClientRequestIdWithCorrelation(requestId));
                return(request);
            }

            var response = await this.SendAsyncHandleSecurityExceptions(FinalRequestFunc, cancellationToken);

            if (response.IsSuccessStatusCode)
            {
                return(response);
            }

            // TODO Log Unsuccessful Response.

            // Try to get Error Information if present in response body.
            if (response.Content != null)
            {
                DaprError error = null;

                try
                {
                    var contentStream = await response.Content.ReadAsStreamAsync();

                    if (contentStream.Length != 0)
                    {
                        using var streamReader = new StreamReader(contentStream);
                        var json = await streamReader.ReadToEndAsync();

                        error = JsonConvert.DeserializeObject <DaprError>(json);
                    }
                }
                catch (Exception ex)
                {
                    throw new DaprException(string.Format("ServerErrorNoMeaningFulResponse", response.StatusCode), ex);
                }

                if (error != null)
                {
                    throw new DaprException(error.Message, error.ErrorCode ?? DaprErrorCodes.UNKNOWN, false);
                }
                else
                {
                    // Handle NotFound 404, without any ErrorCode.
                    if (response.StatusCode.Equals(HttpStatusCode.NotFound))
                    {
                        throw new DaprException("ErrorMessageHTTP404", DaprErrorCodes.ERR_DOES_NOT_EXIST, false);
                    }
                }
            }

            // Couldn't determine Error information from response., throw exception with status code.
            throw new DaprException(string.Format("ServerErrorNoMeaningFulResponse", response.StatusCode));
        }