示例#1
0
        protected Task WaitForJobCancellation(List <string> jobIDs, DocumentTranslationClient client)
        {
            if (Mode == RecordedTestMode.Playback)
            {
                return(Task.CompletedTask);
            }

            using (Recording.DisableRecording())
            {
                var options = new GetTranslationStatusesOptions();

                foreach (var result in jobIDs)
                {
                    options.Ids.Add(result);
                }

                bool cancellationHasPropagated = false; // flag for successful cancelling

                return(TestRetryHelper.RetryAsync(async() =>
                {
                    var statuses = await client.GetTranslationStatusesAsync(options: options).ToEnumerableAsync();
                    cancellationHasPropagated = statuses.TrueForAll(status => status.Status == DocumentTranslationStatus.Canceled);

                    if (!cancellationHasPropagated)
                    {
                        throw new InvalidOperationException("Cancellation not propagated to all documents");
                    }
                    else
                    {
                        return (Response)null;
                    }
                },
                                                  maxIterations: 100, delay: TimeSpan.FromSeconds(5)));
            }
        }
        public async Task GetTranslationStatusesTest(bool usetokenCredential)
        {
            Uri source = await CreateSourceContainerAsync(oneTestDocuments);

            Uri target = await CreateTargetContainerAsync();

            DocumentTranslationClient client = GetClient(useTokenCredential: usetokenCredential);

            var input = new DocumentTranslationInput(source, target, "fr");
            await client.StartTranslationAsync(input);

            List <TranslationStatusResult> translations = await client.GetTranslationStatusesAsync().ToEnumerableAsync();

            Assert.GreaterOrEqual(translations.Count, 1);
            TranslationStatusResult oneTranslation = translations[0];

            Assert.AreNotEqual(new DateTimeOffset(), oneTranslation.CreatedOn);
            Assert.AreNotEqual(new DateTimeOffset(), oneTranslation.LastModified);
            Assert.GreaterOrEqual(oneTranslation.DocumentsCanceled, 0);
            Assert.GreaterOrEqual(oneTranslation.DocumentsFailed, 0);
            Assert.GreaterOrEqual(oneTranslation.DocumentsInProgress, 0);
            Assert.GreaterOrEqual(oneTranslation.DocumentsNotStarted, 0);
            Assert.GreaterOrEqual(oneTranslation.DocumentsSucceeded, 0);
            Assert.GreaterOrEqual(oneTranslation.DocumentsTotal, 0);

            if (oneTranslation.Status == DocumentTranslationStatus.Succeeded)
            {
                Assert.Greater(oneTranslation.TotalCharactersCharged, 0);
            }
            else
            {
                Assert.AreEqual(0, oneTranslation.TotalCharactersCharged);
            }
        }
        public async Task OperationsHistoryAsync()
        {
#if SNIPPET
            string endpoint = "<Document Translator Resource Endpoint>";
            string apiKey   = "<Document Translator Resource API Key>";
#else
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;
#endif

            var client = new DocumentTranslationClient(new Uri(endpoint), new AzureKeyCredential(apiKey));

            #region Snippet:OperationsHistoryAsync

            int operationsCount = 0;
            int totalDocs       = 0;
            int docsCanceled    = 0;
            int docsSucceeded   = 0;
            int docsFailed      = 0;

            DateTimeOffset lastWeekTimestamp = DateTimeOffset.Now.AddDays(-7);

            var options = new GetTranslationStatusesOptions
            {
                CreatedAfter = lastWeekTimestamp
            };

            await foreach (TranslationStatusResult translationStatus in client.GetTranslationStatusesAsync(options))
            {
                if (translationStatus.Status == DocumentTranslationStatus.NotStarted ||
                    translationStatus.Status == DocumentTranslationStatus.Running)
                {
                    DocumentTranslationOperation operation = new DocumentTranslationOperation(translationStatus.Id, client);
                    await operation.WaitForCompletionAsync();
                }

                operationsCount++;
                totalDocs     += translationStatus.DocumentsTotal;
                docsCanceled  += translationStatus.DocumentsCanceled;
                docsSucceeded += translationStatus.DocumentsSucceeded;
                docsFailed    += translationStatus.DocumentsFailed;
            }

            Console.WriteLine($"# of operations: {operationsCount}");
            Console.WriteLine($"Total Documents: {totalDocs}");
            Console.WriteLine($"Succeeded Document: {docsSucceeded}");
            Console.WriteLine($"Failed Document: {docsFailed}");
            Console.WriteLine($"Canceled Documents: {docsCanceled}");

            #endregion
        }