示例#1
0
        protected override async Task SetValueAsync(bool async, Response finalResponse, CancellationToken cancellationToken = default)
        {
            _value = async
                ? await _source.CreateResultAsync(finalResponse, cancellationToken).ConfigureAwait(false)
                : _source.CreateResult(finalResponse, cancellationToken);

            _hasValue = true;
        }
        private async ValueTask <Response> UpdateStatusAsync(bool async, CancellationToken cancellationToken)
        {
            if (_hasCompleted)
            {
                return(GetRawResponse());
            }

            if (_shouldPoll)
            {
                UpdatePollUri();
                _rawResponse = async
                    ? await GetResponseAsync(_pollUri, cancellationToken).ConfigureAwait(false)
                    : GetResponse(_pollUri, cancellationToken);
            }

            _shouldPoll   = true;
            _hasCompleted = IsTerminalState(out string state);
            if (_hasCompleted)
            {
                Response finalResponse = GetRawResponse();
                if (s_failureStates.Contains(state))
                {
                    throw _clientDiagnostics.CreateRequestFailedException(finalResponse);
                }

                string?finalUri = GetFinalUri();
                if (finalUri != null)
                {
                    finalResponse = async
                        ? await GetResponseAsync(finalUri, cancellationToken).ConfigureAwait(false)
                        : GetResponse(finalUri, cancellationToken);
                }

                switch (finalResponse.Status)
                {
                case 200:
                case 201 when _requestMethod == RequestMethod.Put:
                case 204 when !(_requestMethod == RequestMethod.Put || _requestMethod == RequestMethod.Patch):
                {
                    _value = async
                            ? await _source.CreateResultAsync(finalResponse, cancellationToken).ConfigureAwait(false)
                            : _source.CreateResult(finalResponse, cancellationToken);

                    _rawResponse = finalResponse;
                    _hasValue    = true;
                    break;
                }

                default:
                    throw _clientDiagnostics.CreateRequestFailedException(finalResponse);
                }
            }

            return(GetRawResponse());
        }