Esempio n. 1
0
        /// <summary>
        /// Get the status of documents in the translation operation.
        /// </summary>
        /// <param name="options">Options to use when filtering result.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> used for the service call.</param>
        public virtual AsyncPageable <DocumentStatusResult> GetDocumentStatusesAsync(GetDocumentStatusesOptions options = default, CancellationToken cancellationToken = default)
        {
            async Task <Page <DocumentStatusResult> > FirstPageFunc(int?pageSizeHint)
            {
                using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(DocumentTranslationOperation)}.{nameof(GetDocumentStatuses)}");
                scope.Start();

                try
                {
                    var idList      = options?.Ids.Count > 0 ? options.Ids.Select(id => ClientCommon.ValidateModelId(id, "Id Filter")) : null;
                    var statusList  = options?.Statuses.Count > 0 ? options.Statuses.Select(status => status.ToString()) : null;
                    var orderByList = options?.OrderBy.Count > 0 ? options.OrderBy.Select(order => order.ToGenerated()) : null;

                    var response = await _serviceClient.GetDocumentsStatusAsync(
                        new Guid(Id),
                        ids : idList,
                        statuses : statusList,
                        createdDateTimeUtcStart : options?.CreatedAfter,
                        createdDateTimeUtcEnd : options?.CreatedBefore,
                        orderBy : orderByList,
                        cancellationToken : cancellationToken).ConfigureAwait(false);

                    return(Page.FromValues(response.Value.Value, response.Value.NextLink, response.GetRawResponse()));
                }
                catch (Exception e)
                {
                    scope.Failed(e);
                    throw;
                }
            }

            async Task <Page <DocumentStatusResult> > NextPageFunc(string nextLink, int?pageSizeHint)
            {
                using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(DocumentTranslationOperation)}.{nameof(GetDocumentStatuses)}");
                scope.Start();

                try
                {
                    var response = await _serviceClient.GetDocumentsStatusNextPageAsync(nextLink, new Guid(Id), cancellationToken : cancellationToken).ConfigureAwait(false);

                    return(Page.FromValues(response.Value.Value, response.Value.NextLink, response.GetRawResponse()));
                }
                catch (Exception e)
                {
                    scope.Failed(e);
                    throw;
                }
            }

            return(PageableHelpers.CreateAsyncEnumerable(FirstPageFunc, NextPageFunc));
        }
Esempio n. 2
0
        /// <summary>
        /// Periodically calls the server till the long-running operation completes.
        /// </summary>
        /// <param name="pollingInterval">
        /// The interval between status requests to the server.
        /// The interval can change based on information returned from the server.
        /// For example, the server might communicate to the client that there is not reason to poll for status change sooner than some time.
        /// </param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> used for the periodical service calls.</param>
        /// <returns>The last HTTP response received from the server.</returns>
        /// <remarks>
        /// This method will periodically call UpdateStatusAsync till HasCompleted is true.
        /// An API call is then made to retrieve the status of the documents.
        /// </remarks>
        public async override ValueTask <Response <AsyncPageable <DocumentStatusResult> > > WaitForCompletionAsync(TimeSpan pollingInterval, CancellationToken cancellationToken = default)
        {
            while (true)
            {
                await UpdateStatusAsync(cancellationToken).ConfigureAwait(false);

                if (!HasCompleted)
                {
                    pollingInterval = _retryAfterHeaderValue.HasValue ? TimeSpan.FromSeconds(_retryAfterHeaderValue.Value) : pollingInterval;
                    await Task.Delay(pollingInterval, cancellationToken).ConfigureAwait(false);
                }
                else
                {
                    var response = await _serviceClient.GetDocumentsStatusAsync(new Guid(Id), cancellationToken : cancellationToken).ConfigureAwait(false);

                    _firstPage = Page.FromValues(response.Value.Value, response.Value.NextLink, response.GetRawResponse());
                    _hasValue  = true;
                    async Task <Page <DocumentStatusResult> > NextPageFunc(string nextLink, int?pageSizeHint)
                    {
                        // TODO: diagnostics scope?
                        try
                        {
                            var response = await _serviceClient.GetDocumentsStatusNextPageAsync(nextLink, new Guid(Id), cancellationToken : cancellationToken).ConfigureAwait(false);

                            return(Page.FromValues(response.Value.Value, response.Value.NextLink, response.GetRawResponse()));
                        }
                        catch (Exception)
                        {
                            throw;
                        }
                    }

                    var result = PageableHelpers.CreateAsyncEnumerable(_ => Task.FromResult(_firstPage), NextPageFunc);
                    return(Response.FromValue(result, response));
                }
            }
        }
        /// <summary>
        /// Periodically calls the server till the long-running operation completes.
        /// </summary>
        /// <param name="pollingInterval">
        /// The interval between status requests to the server.
        /// The interval can change based on information returned from the server.
        /// For example, the server might communicate to the client that there is not reason to poll for status change sooner than some time.
        /// </param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> used for the periodical service calls.</param>
        /// <returns>The last HTTP response received from the server.</returns>
        /// <remarks>
        /// This method will periodically call UpdateStatusAsync till HasCompleted is true.
        /// An API call is then made to retrieve the status of the documents.
        /// </remarks>
        public async override ValueTask <Response <AsyncPageable <DocumentStatusResult> > > WaitForCompletionAsync(
            TimeSpan pollingInterval,
            CancellationToken cancellationToken = default)
        {
            await this.DefaultWaitForCompletionAsync(pollingInterval, cancellationToken).ConfigureAwait(false);

            var response = await _serviceClient.GetDocumentsStatusAsync(new Guid(Id), cancellationToken : cancellationToken).ConfigureAwait(false);

            _firstPage = Page.FromValues(response.Value.Value, response.Value.NextLink, response.GetRawResponse());
            _hasValue  = true;

            async Task <Page <DocumentStatusResult> > NextPageFunc(string nextLink, int?pageSizeHint)
            {
                // TODO: diagnostics scope?
                var response = await _serviceClient.GetDocumentsStatusNextPageAsync(nextLink, new Guid(Id), cancellationToken : cancellationToken)
                               .ConfigureAwait(false);

                return(Page.FromValues(response.Value.Value, response.Value.NextLink, response.GetRawResponse()));
            }

            var result = PageableHelpers.CreateAsyncEnumerable(_ => Task.FromResult(_firstPage), NextPageFunc);

            return(Response.FromValue(result, response));
        }