public async Task MultipleInputsAsync()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

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

            Uri source1SasUriUri     = new Uri("<source1 SAS URI>");
            Uri source2SasUri        = new Uri("<source2 SAS URI>");
            Uri frenchTargetSasUri   = new Uri("<french target SAS URI>");
            Uri arabicTargetSasUri   = new Uri("<arabic target SAS URI>");
            Uri spanishTargetSasUri  = new Uri("<spanish target SAS URI>");
            Uri frenchGlossarySasUri = new Uri("<french glossary SAS URI>");

            #region Snippet:MultipleInputsAsync

            //@@ Uri source1SasUriUri = <source1 SAS URI>;
            //@@ Uri source2SasUri = <source2 SAS URI>;
            //@@ Uri frenchTargetSasUri = <french target SAS URI>;
            //@@ Uri arabicTargetSasUri = <arabic target SAS URI>;
            //@@ Uri spanishTargetSasUri = <spanish target SAS URI>;
            //@@ Uri frenchGlossarySasUri = <french glossary SAS URI>;
            var glossaryFormat = "TSV";

            var input1 = new DocumentTranslationInput(source1SasUriUri, frenchTargetSasUri, "fr", new TranslationGlossary(frenchGlossarySasUri, glossaryFormat));
            input1.AddTarget(spanishTargetSasUri, "es");

            var input2 = new DocumentTranslationInput(source2SasUri, arabicTargetSasUri, "ar");
            input2.AddTarget(frenchTargetSasUri, "fr", new TranslationGlossary(frenchGlossarySasUri, glossaryFormat));

            var inputs = new List <DocumentTranslationInput>()
            {
                input1,
                input2
            };

            DocumentTranslationOperation operation = await client.StartTranslationAsync(inputs);

            await operation.WaitForCompletionAsync();

            await foreach (DocumentStatusResult document in operation.GetValuesAsync())
            {
                Console.WriteLine($"Document with Id: {document.DocumentId}");
                Console.WriteLine($"  Status:{document.Status}");
                if (document.Status == TranslationStatus.Succeeded)
                {
                    Console.WriteLine($"  Translated Document Uri: {document.TranslatedDocumentUri}");
                    Console.WriteLine($"  Translated to language: {document.TranslateTo}.");
                    Console.WriteLine($"  Document source Uri: {document.SourceDocumentUri}");
                }
                else
                {
                    Console.WriteLine($"  Document source Uri: {document.SourceDocumentUri}");
                    Console.WriteLine($"  Error Code: {document.Error.ErrorCode}");
                    Console.WriteLine($"  Message: {document.Error.Message}");
                }
            }

            #endregion
        }
Пример #2
0
        public async Task GetAllTranslationStatusesTest(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 <TranslationStatus> translations = await client.GetAllTranslationStatusesAsync().ToEnumerableAsync();

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

            Assert.AreNotEqual(new DateTimeOffset(), oneTranslation.CreatedOn);
            Assert.AreNotEqual(new DateTimeOffset(), oneTranslation.LastModified);
            Assert.GreaterOrEqual(oneTranslation.DocumentsCancelled, 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);
            }
        }
Пример #3
0
        public async Task SingleSourceSingleTargetWithSuffixTest()
        {
            Uri sourceUri = await CreateSourceContainerAsync(twoTestDocuments);

            Uri targetUri = await CreateTargetContainerAsync();

            DocumentTranslationClient client = GetClient();

            var source = new TranslationSource(sourceUri)
            {
                Suffix = "1.txt"
            };

            var targets = new List <TranslationTarget> {
                new TranslationTarget(targetUri, "fr")
            };
            var input = new DocumentTranslationInput(source, targets);
            DocumentTranslationOperation operation = await client.StartTranslationAsync(input);

            await operation.WaitForCompletionAsync();

            if (operation.DocumentsSucceeded < 1)
            {
                await PrintNotSucceededDocumentsAsync(operation);
            }

            Assert.IsTrue(operation.HasCompleted);
            Assert.IsTrue(operation.HasValue);
            Assert.AreEqual(1, operation.DocumentsTotal);
            Assert.AreEqual(1, operation.DocumentsSucceeded);
            Assert.AreEqual(0, operation.DocumentsFailed);
            Assert.AreEqual(0, operation.DocumentsCancelled);
            Assert.AreEqual(0, operation.DocumentsInProgress);
            Assert.AreEqual(0, operation.DocumentsNotStarted);
        }
        public async Task DocumentStatusAsync()
        {
            string endpoint  = TestEnvironment.Endpoint;
            string apiKey    = TestEnvironment.ApiKey;
            Uri    sourceUri = new Uri("");
            Uri    targetUri = new Uri("");

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

            var input = new DocumentTranslationInput(sourceUri, targetUri, "es");
            DocumentTranslationOperation operation = await client.StartTranslationAsync(input);

            var documentscompleted = new HashSet <string>();

            while (!operation.HasCompleted)
            {
                await operation.UpdateStatusAsync();

                AsyncPageable <DocumentStatusResult> documentsStatus = operation.GetAllDocumentStatusesAsync();
                await foreach (DocumentStatusResult docStatus in documentsStatus)
                {
                    if (documentscompleted.Contains(docStatus.DocumentId))
                    {
                        continue;
                    }
                    if (docStatus.Status == TranslationStatus.Succeeded || docStatus.Status == TranslationStatus.Failed)
                    {
                        documentscompleted.Add(docStatus.DocumentId);
                        Console.WriteLine($"Document {docStatus.TranslatedDocumentUri} completed with status ${docStatus.Status}");
                    }
                }
            }
        }
Пример #5
0
        public async Task GetDocumentStatusTest()
        {
            Uri sourceUri = await CreateSourceContainerAsync(oneTestDocuments);

            Uri targetUri = await CreateTargetContainerAsync();

            string translateTo = "fr";

            DocumentTranslationClient client = GetClient();

            var input = new DocumentTranslationInput(sourceUri, targetUri, translateTo);
            DocumentTranslationOperation operation = await client.StartTranslationAsync(input);

            await operation.WaitForCompletionAsync();

            AsyncPageable <DocumentStatus> documents = operation.GetAllDocumentStatusesAsync();

            List <DocumentStatus> documentsList = await documents.ToEnumerableAsync();

            Assert.AreEqual(1, documentsList.Count);

            DocumentStatus document = await operation.GetDocumentStatusAsync(documentsList[0].Id);

            CheckDocumentStatus(document, translateTo);
        }
        public void DocumentTranslationInput()
        {
            #region Snippet:DocumentTranslationSingleInput
            Uri sourceSasUri       = new Uri("<source SAS URI>");
            Uri frenchTargetSasUri = new Uri("<french target SAS URI>");

            var input = new DocumentTranslationInput(sourceSasUri, frenchTargetSasUri, "fr");
            #endregion

            #region Snippet:DocumentTranslationMultipleInputs
            Uri arabicTargetSasUri  = new Uri("<arabic target SAS URI>");
            Uri spanishTargetSasUri = new Uri("<spanish target SAS URI>");
            Uri source1SasUri       = new Uri("<source1 SAS URI>");
            Uri source2SasUri       = new Uri("<source2 SAS URI>");

            var inputs = new List <DocumentTranslationInput>
            {
                new DocumentTranslationInput(source1SasUri, spanishTargetSasUri, "es"),
                new DocumentTranslationInput(
                    source: new TranslationSource(source2SasUri),
                    targets: new List <TranslationTarget>
                {
                    new TranslationTarget(frenchTargetSasUri, "fr"),
                    new TranslationTarget(arabicTargetSasUri, "ar")
                }),
            };
            #endregion
        }
Пример #7
0
        public async Task WrongDocumentEncoding()
        {
            var document = new List <TestDocument>
            {
                new TestDocument("Document1.txt", string.Empty),
            };

            Uri source = await CreateSourceContainerAsync(document);

            Uri target = await CreateTargetContainerAsync();

            DocumentTranslationClient client = GetClient();

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

            AsyncPageable <DocumentStatus> documents = await operation.WaitForCompletionAsync();

            Assert.IsTrue(operation.HasCompleted);
            Assert.IsTrue(operation.HasValue);
            Assert.AreEqual(DocumentTranslationStatus.Failed, operation.Status);

            Assert.AreEqual(1, operation.DocumentsTotal);
            Assert.AreEqual(0, operation.DocumentsSucceeded);
            Assert.AreEqual(1, operation.DocumentsFailed);
            Assert.AreEqual(0, operation.DocumentsCancelled);
            Assert.AreEqual(0, operation.DocumentsInProgress);
            Assert.AreEqual(0, operation.DocumentsNotStarted);

            List <DocumentStatus> documentsList = await documents.ToEnumerableAsync();

            Assert.AreEqual(1, documentsList.Count);
            Assert.AreEqual(DocumentTranslationStatus.Failed, documentsList[0].Status);
            Assert.AreEqual(new DocumentTranslationErrorCode("WrongDocumentEncoding"), documentsList[0].Error.ErrorCode);
        }
Пример #8
0
        public async Task ExistingFileInTargetContainer()
        {
            Uri source = await CreateSourceContainerAsync(oneTestDocuments);

            Uri target = await CreateTargetContainerAsync(oneTestDocuments);

            DocumentTranslationClient client = GetClient();

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

            AsyncPageable <DocumentStatus> documents = await operation.WaitForCompletionAsync();

            Assert.IsTrue(operation.HasCompleted);
            Assert.IsTrue(operation.HasValue);
            Assert.AreEqual(DocumentTranslationStatus.Failed, operation.Status);

            Assert.AreEqual(1, operation.DocumentsTotal);
            Assert.AreEqual(0, operation.DocumentsSucceeded);
            Assert.AreEqual(1, operation.DocumentsFailed);
            Assert.AreEqual(0, operation.DocumentsCancelled);
            Assert.AreEqual(0, operation.DocumentsInProgress);
            Assert.AreEqual(0, operation.DocumentsNotStarted);

            List <DocumentStatus> documentsList = await documents.ToEnumerableAsync();

            Assert.AreEqual(1, documentsList.Count);
            Assert.AreEqual(DocumentTranslationStatus.Failed, documentsList[0].Status);
            Assert.AreEqual(new DocumentTranslationErrorCode("TargetFileAlreadyExists"), documentsList[0].Error.ErrorCode);
        }
Пример #9
0
        public async Task SingleSourceSingleTargetListDocumentsTest()
        {
            Uri sourceUri = await CreateSourceContainerAsync(oneTestDocuments);

            Uri targetUri = await CreateTargetContainerAsync();

            string translateTo = "fr";

            DocumentTranslationClient client = GetClient();

            var input = new DocumentTranslationInput(sourceUri, targetUri, translateTo);
            DocumentTranslationOperation operation = await client.StartTranslationAsync(input);

            AsyncPageable <DocumentStatus> documentsFromOperation = await operation.WaitForCompletionAsync();

            List <DocumentStatus> documentsFromOperationList = await documentsFromOperation.ToEnumerableAsync();

            Assert.AreEqual(1, documentsFromOperationList.Count);
            CheckDocumentStatus(documentsFromOperationList[0], translateTo);

            AsyncPageable <DocumentStatus> documentsFromGetAll     = operation.GetAllDocumentStatusesAsync();
            List <DocumentStatus>          documentsFromGetAllList = await documentsFromGetAll.ToEnumerableAsync();

            Assert.AreEqual(documentsFromOperationList[0].Status, documentsFromGetAllList[0].Status);
            Assert.AreEqual(documentsFromOperationList[0].Id, documentsFromGetAllList[0].Id);
            Assert.AreEqual(documentsFromOperationList[0].SourceDocumentUri, documentsFromGetAllList[0].SourceDocumentUri);
            Assert.AreEqual(documentsFromOperationList[0].TranslatedDocumentUri, documentsFromGetAllList[0].TranslatedDocumentUri);
            Assert.AreEqual(documentsFromOperationList[0].TranslationProgressPercentage, documentsFromGetAllList[0].TranslationProgressPercentage);
            Assert.AreEqual(documentsFromOperationList[0].TranslatedTo, documentsFromGetAllList[0].TranslatedTo);
            Assert.AreEqual(documentsFromOperationList[0].CreatedOn, documentsFromGetAllList[0].CreatedOn);
            Assert.AreEqual(documentsFromOperationList[0].LastModified, documentsFromGetAllList[0].LastModified);
        }
        public async Task GetTranslationsTest()
        {
            Uri source = await CreateSourceContainerAsync(oneTestDocuments);

            Uri target = await CreateTargetContainerAsync();

            var client = GetClient();

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

            List <TranslationStatusResult> translations = await client.GetTranslationsAsync().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.DocumentsCancelled, 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.Error == null && oneTranslation.HasCompleted)
            {
                Assert.Greater(oneTranslation.TotalCharactersCharged, 0);
            }
            else
            {
                Assert.AreEqual(0, oneTranslation.TotalCharactersCharged);
            }
        }
        public void BadRequestSnippet()
        {
#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 credentials = new AzureKeyCredential(apiKey);
            var client      = new DocumentTranslationClient(new Uri(endpoint), credentials);

            #region Snippet:BadRequest
            var invalidInput = new DocumentTranslationInput(new TranslationSource(new Uri(endpoint)), new List <TranslationTarget>());

            try
            {
                DocumentTranslationOperation operation = client.StartTranslation(invalidInput);
            }
            catch (RequestFailedException e)
            {
                Console.WriteLine(e.ToString());
            }
            #endregion
        }
Пример #12
0
        public async Task <List <string> > CreateTranslationJobsAsync(DocumentTranslationClient client, int jobsCount = 1, int docsPerJob = 1, DocumentTranslationStatus jobTerminalStatus = default)
        {
            // create source container
            if (jobTerminalStatus == DocumentTranslationStatus.Canceled)
            {
                docsPerJob = 20; // in order to avoid job completing before canceling
            }
            var testDocs        = CreateDummyTestDocuments(count: docsPerJob);
            var sourceContainer = await CreateSourceContainerAsync(testDocs);

            // create a translation job
            var resultIds = new List <string>();

            for (int i = 1; i <= jobsCount; i++)
            {
                var targetContainer = await CreateTargetContainerAsync();

                var input         = new DocumentTranslationInput(sourceContainer, targetContainer, "fr");
                var translationOp = await client.StartTranslationAsync(input);

                resultIds.Add(translationOp.Id);
                if (jobTerminalStatus == DocumentTranslationStatus.Succeeded)
                {
                    await translationOp.WaitForCompletionAsync();
                }
                else if (jobTerminalStatus == DocumentTranslationStatus.Canceled)
                {
                    await translationOp.CancelAsync(default);
        public async Task SingleSourceSingleTargetListDocumentsTest()
        {
            Uri sourceUri = await CreateSourceContainerAsync(oneTestDocuments);

            Uri targetUri = await CreateTargetContainerAsync();

            string translateTo = "fr";

            DocumentTranslationClient client = GetClient();

            var input = new DocumentTranslationInput(sourceUri, targetUri, translateTo);
            DocumentTranslationOperation operation = await client.StartTranslationAsync(input);

            AsyncPageable <DocumentStatusResult> documentsFromOperation = await operation.WaitForCompletionAsync(PollingInterval);

            List <DocumentStatusResult> documentsFromOperationList = await documentsFromOperation.ToEnumerableAsync();

            Assert.AreEqual(1, documentsFromOperationList.Count);
            CheckDocumentStatusResult(documentsFromOperationList[0], translateTo);

            AsyncPageable <DocumentStatusResult> documentsFromGetAll     = operation.GetAllDocumentStatusesAsync();
            List <DocumentStatusResult>          documentsFromGetAllList = await documentsFromGetAll.ToEnumerableAsync();

            Assert.AreEqual(documentsFromOperationList[0].Status, documentsFromGetAllList[0].Status);
            Assert.AreEqual(documentsFromOperationList[0].HasCompleted, documentsFromGetAllList[0].HasCompleted);
            Assert.AreEqual(documentsFromOperationList[0].DocumentId, documentsFromGetAllList[0].DocumentId);
            Assert.AreEqual(documentsFromOperationList[0].SourceDocumentUri, documentsFromGetAllList[0].SourceDocumentUri);
            Assert.AreEqual(documentsFromOperationList[0].TranslatedDocumentUri, documentsFromGetAllList[0].TranslatedDocumentUri);
            Assert.AreEqual(documentsFromOperationList[0].TranslationProgressPercentage, documentsFromGetAllList[0].TranslationProgressPercentage);
            Assert.AreEqual(documentsFromOperationList[0].TranslateTo, documentsFromGetAllList[0].TranslateTo);
            Assert.AreEqual(documentsFromOperationList[0].CreatedOn, documentsFromGetAllList[0].CreatedOn);
            // Ignore because of flaky behavior. Service issue has been created.
            // https://github.com/Azure/azure-sdk-for-net/issues/20116
            // Assert.AreEqual(documentsFromOperationList[0].LastModified, documentsFromGetAllList[0].LastModified);
        }
Пример #14
0
        public async Task SingleSourceMultipleTargetsTest()
        {
            Uri source = await CreateSourceContainerAsync(oneDocumentList);

            Uri targetFrench = await CreateTargetContainerAsync();

            Uri targetSpanish = await CreateTargetContainerAsync();

            Uri targetArabic = await CreateTargetContainerAsync();

            var client = GetClient();

            var input = new DocumentTranslationInput(source, targetFrench, "fr");

            input.AddTarget(targetSpanish, "es");
            input.AddTarget(targetArabic, "ar");
            var operation = await client.StartTranslationAsync(input);

            await operation.WaitForCompletionAsync(PollingInterval);

            if (operation.DocumentsSucceeded < 3)
            {
                await PrintNotSucceededDocumentsAsync(operation);
            }

            Assert.IsTrue(operation.HasCompleted);
            Assert.IsTrue(operation.HasValue);
            Assert.AreEqual(3, operation.DocumentsTotal);
            Assert.AreEqual(3, operation.DocumentsSucceeded);
            Assert.AreEqual(0, operation.DocumentsFailed);
            Assert.AreEqual(0, operation.DocumentsCancelled);
            Assert.AreEqual(0, operation.DocumentsInProgress);
            Assert.AreEqual(0, operation.DocumentsNotStarted);
        }
Пример #15
0
        public async Task ContainerWithSupportedAndUnsupportedFiles()
        {
            var documentsList = new List <TestDocument>
            {
                new TestDocument("Document1.txt", "First english test document"),
                new TestDocument("File2.jpg", "jpg"),
            };

            Uri source = await CreateSourceContainerAsync(documentsList);

            Uri target = await CreateTargetContainerAsync();

            DocumentTranslationClient client = GetClient();

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

            await operation.WaitForCompletionAsync();

            if (operation.DocumentsSucceeded < 1)
            {
                await PrintNotSucceededDocumentsAsync(operation);
            }

            Assert.IsTrue(operation.HasCompleted);
            Assert.IsTrue(operation.HasValue);
            Assert.AreEqual(1, operation.DocumentsTotal);
            Assert.AreEqual(1, operation.DocumentsSucceeded);
            Assert.AreEqual(0, operation.DocumentsFailed);
            Assert.AreEqual(0, operation.DocumentsCancelled);
            Assert.AreEqual(0, operation.DocumentsInProgress);
            Assert.AreEqual(0, operation.DocumentsNotStarted);
        }
Пример #16
0
        public async Task SingleSourceSingleTargetTest(bool usetokenCredential)
        {
            Uri source = await CreateSourceContainerAsync(oneTestDocuments);

            Uri target = await CreateTargetContainerAsync();

            DocumentTranslationClient client = GetClient(useTokenCredential: usetokenCredential);

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

            await operation.WaitForCompletionAsync();

            if (operation.DocumentsSucceeded < 1)
            {
                await PrintNotSucceededDocumentsAsync(operation);
            }

            Assert.IsTrue(operation.HasCompleted);
            Assert.IsTrue(operation.HasValue);
            Assert.AreEqual(1, operation.DocumentsTotal);
            Assert.AreEqual(1, operation.DocumentsSucceeded);
            Assert.AreEqual(0, operation.DocumentsFailed);
            Assert.AreEqual(0, operation.DocumentsCancelled);
            Assert.AreEqual(0, operation.DocumentsInProgress);
            Assert.AreEqual(0, operation.DocumentsNotStarted);
        }
        public void DocumentStatus()
        {
#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));

#if SNIPPET
            Uri sourceUri = new Uri("<source SAS URI>");
            Uri targetUri = new Uri("<target SAS URI>")
#else
            Uri sourceUri = CreateSourceContainer(oneTestDocuments);
            Uri targetUri = CreateTargetContainer();
#endif

                            var input = new DocumentTranslationInput(sourceUri, targetUri, "es");
            DocumentTranslationOperation operation = client.StartTranslation(input);

            TimeSpan pollingInterval = new(1000);

            var documentscompleted = new HashSet <string>();

            while (true)
            {
                operation.UpdateStatus();

                Pageable <DocumentStatusResult> documentsStatus = operation.GetDocumentStatuses();
                foreach (DocumentStatusResult docStatus in documentsStatus)
                {
                    if (documentscompleted.Contains(docStatus.Id))
                    {
                        continue;
                    }
                    if (docStatus.Status == DocumentTranslationStatus.Succeeded || docStatus.Status == DocumentTranslationStatus.Failed)
                    {
                        documentscompleted.Add(docStatus.Id);
                        Console.WriteLine($"Document {docStatus.TranslatedDocumentUri} completed with status {docStatus.Status}");
                    }
                }

                if (operation.HasCompleted)
                {
                    break;
                }
                else
                {
                    if (operation.GetRawResponse().Headers.TryGetValue("Retry-After", out string value))
                    {
                        pollingInterval = TimeSpan.FromSeconds(Convert.ToInt32(value));
                    }
                    Thread.Sleep(pollingInterval);
                }
            }
        }
Пример #18
0
        public async Task PollIndividualDocumentsAsync()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

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

            Uri sourceUri = new Uri("<source SAS URI>");
            Uri targetUri = new Uri("<target SAS URI>");

            #region Snippet:PollIndividualDocumentsAsync

            //@@ Uri sourceUri = <source SAS URI>;
            //@@ Uri targetUri = <target SAS URI>;

            var input = new DocumentTranslationInput(sourceUri, targetUri, "es");

            DocumentTranslationOperation operation = await client.StartTranslationAsync(input);

            TimeSpan pollingInterval = new(1000);

            AsyncPageable <DocumentStatusResult> documents = operation.GetAllDocumentStatusesAsync();
            await foreach (DocumentStatusResult document in documents)
            {
                Console.WriteLine($"Polling Status for document{document.TranslatedDocumentUri}");

                Response <DocumentStatusResult> status = await operation.GetDocumentStatusAsync(document.DocumentId);

                while (!status.Value.HasCompleted)
                {
                    if (operation.GetRawResponse().Headers.TryGetValue("Retry-After", out string value))
                    {
                        pollingInterval = TimeSpan.FromSeconds(Convert.ToInt32(value));
                    }

                    await Task.Delay(pollingInterval);

                    status = await operation.GetDocumentStatusAsync(document.DocumentId);
                }

                if (status.Value.Status == TranslationStatus.Succeeded)
                {
                    Console.WriteLine($"  Translated Document Uri: {document.TranslatedDocumentUri}");
                    Console.WriteLine($"  Translated to language: {document.TranslateTo}.");
                    Console.WriteLine($"  Document source Uri: {document.SourceDocumentUri}");
                }
                else
                {
                    Console.WriteLine($"  Document source Uri: {document.SourceDocumentUri}");
                    Console.WriteLine($"  Error Code: {document.Error.ErrorCode}");
                    Console.WriteLine($"  Message: {document.Error.Message}");
                }
            }

            #endregion
        }
        public void StartTranslation()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

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

            Uri sourceUri = new Uri("<source SAS URI>");
            Uri targetUri = new Uri("<target SAS URI>");

            #region Snippet:StartTranslation

            //@@ Uri sourceUri = <source SAS URI>;
            //@@ Uri targetUri = <target SAS URI>;

            var input = new DocumentTranslationInput(sourceUri, targetUri, "es");

            DocumentTranslationOperation operation = client.StartTranslation(input);

            TimeSpan pollingInterval = new TimeSpan(1000);

            while (!operation.HasCompleted)
            {
                Thread.Sleep(pollingInterval);
                operation.UpdateStatus();

                Console.WriteLine($"  Status: {operation.Status}");
                Console.WriteLine($"  Created on: {operation.CreatedOn}");
                Console.WriteLine($"  Last modified: {operation.LastModified}");
                Console.WriteLine($"  Total documents: {operation.DocumentsTotal}");
                Console.WriteLine($"    Succeeded: {operation.DocumentsSucceeded}");
                Console.WriteLine($"    Failed: {operation.DocumentsFailed}");
                Console.WriteLine($"    In Progress: {operation.DocumentsInProgress}");
                Console.WriteLine($"    Not started: {operation.DocumentsNotStarted}");
            }

            foreach (DocumentStatusResult document in operation.GetValues())
            {
                Console.WriteLine($"Document with Id: {document.DocumentId}");
                Console.WriteLine($"  Status:{document.Status}");
                if (document.Status == TranslationStatus.Succeeded)
                {
                    Console.WriteLine($"  Translated Document Uri: {document.TranslatedDocumentUri}");
                    Console.WriteLine($"  Translated to language: {document.TranslateTo}.");
                    Console.WriteLine($"  Document source Uri: {document.SourceDocumentUri}");
                }
                else
                {
                    Console.WriteLine($"  Document source Uri: {document.SourceDocumentUri}");
                    Console.WriteLine($"  Error Code: {document.Error.ErrorCode}");
                    Console.WriteLine($"  Message: {document.Error.Message}");
                }
            }

            #endregion
        }
Пример #20
0
        public async Task StartTranslationAsync()
        {
#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:StartTranslationAsync
#if SNIPPET
            Uri sourceUri = new Uri("<source SAS URI>");
            Uri targetUri = new Uri("<target SAS URI>");
#else
            Uri sourceUri = await CreateSourceContainerAsync(oneTestDocuments);

            Uri targetUri = await CreateTargetContainerAsync();
#endif
            var input = new DocumentTranslationInput(sourceUri, targetUri, "es");

            DocumentTranslationOperation operation = await client.StartTranslationAsync(input);

            await operation.WaitForCompletionAsync();

            Console.WriteLine($"  Status: {operation.Status}");
            Console.WriteLine($"  Created on: {operation.CreatedOn}");
            Console.WriteLine($"  Last modified: {operation.LastModified}");
            Console.WriteLine($"  Total documents: {operation.DocumentsTotal}");
            Console.WriteLine($"    Succeeded: {operation.DocumentsSucceeded}");
            Console.WriteLine($"    Failed: {operation.DocumentsFailed}");
            Console.WriteLine($"    In Progress: {operation.DocumentsInProgress}");
            Console.WriteLine($"    Not started: {operation.DocumentsNotStarted}");

            await foreach (DocumentStatusResult document in operation.Value)
            {
                Console.WriteLine($"Document with Id: {document.Id}");
                Console.WriteLine($"  Status:{document.Status}");
                if (document.Status == DocumentTranslationStatus.Succeeded)
                {
                    Console.WriteLine($"  Translated Document Uri: {document.TranslatedDocumentUri}");
                    Console.WriteLine($"  Translated to language code: {document.TranslatedToLanguageCode}.");
                    Console.WriteLine($"  Document source Uri: {document.SourceDocumentUri}");
                }
                else
                {
                    Console.WriteLine($"  Error Code: {document.Error.ErrorCode}");
                    Console.WriteLine($"  Message: {document.Error.Message}");
                }
            }

            #endregion
        }
        public void PollIndividualDocuments()
        {
#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));
#if SNIPPET
            Uri sourceUri = new Uri("<source SAS URI>");
            Uri targetUri = new Uri("<target SAS URI>");
#else
            Uri sourceUri = CreateSourceContainer(oneTestDocuments);
            Uri targetUri = CreateTargetContainer();
#endif
            var input = new DocumentTranslationInput(sourceUri, targetUri, "es");
            DocumentTranslationOperation operation = client.StartTranslation(input);

            TimeSpan pollingInterval = new(1000);

            foreach (DocumentStatusResult document in operation.GetDocumentStatuses())
            {
                Console.WriteLine($"Polling Status for document{document.SourceDocumentUri}");

                Response <DocumentStatusResult> responseDocumentStatus = operation.GetDocumentStatus(document.Id);

                while (responseDocumentStatus.Value.Status != DocumentTranslationStatus.Failed &&
                       responseDocumentStatus.Value.Status != DocumentTranslationStatus.Succeeded)
                {
                    if (responseDocumentStatus.GetRawResponse().Headers.TryGetValue("Retry-After", out string value))
                    {
                        pollingInterval = TimeSpan.FromSeconds(Convert.ToInt32(value));
                    }

                    Thread.Sleep(pollingInterval);
                    responseDocumentStatus = operation.GetDocumentStatus(document.Id);
                }

                if (responseDocumentStatus.Value.Status == DocumentTranslationStatus.Succeeded)
                {
                    Console.WriteLine($"  Translated Document Uri: {document.TranslatedDocumentUri}");
                    Console.WriteLine($"  Translated to language code: {document.TranslatedToLanguageCode}.");
                    Console.WriteLine($"  Document source Uri: {document.SourceDocumentUri}");
                }
                else
                {
                    Console.WriteLine($"  Document source Uri: {document.SourceDocumentUri}");
                    Console.WriteLine($"  Error Code: {document.Error.ErrorCode}");
                    Console.WriteLine($"  Message: {document.Error.Message}");
                }
            }
        }
Пример #22
0
        private async Task <DocumentTranslationOperation> CreateSingleTranslationJobAsync(DocumentTranslationClient client, int docsCount = 1)
        {
            var testDocs        = CreateDummyTestDocuments(count: docsCount);
            var sourceContainer = await CreateSourceContainerAsync(testDocs);

            var targetContainer = await CreateTargetContainerAsync();

            var input         = new DocumentTranslationInput(sourceContainer, targetContainer, "fr");
            var translationOp = await client.StartTranslationAsync(input);

            return(translationOp);
        }
        public async Task StartTranslationAsync()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

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

            Uri sourceUri = new Uri("<source SAS URI>");
            Uri targetUri = new Uri("<target SAS URI>");

            #region Snippet:StartTranslationAsync

            //@@ Uri sourceUri = <source SAS URI>;
            //@@ Uri targetUri = <target SAS URI>;

            var input = new DocumentTranslationInput(sourceUri, targetUri, "es");

            DocumentTranslationOperation operation = await client.StartTranslationAsync(input);

            Response <AsyncPageable <DocumentStatusResult> > operationResult = await operation.WaitForCompletionAsync();

            Console.WriteLine($"  Status: {operation.Status}");
            Console.WriteLine($"  Created on: {operation.CreatedOn}");
            Console.WriteLine($"  Last modified: {operation.LastModified}");
            Console.WriteLine($"  Total documents: {operation.DocumentsTotal}");
            Console.WriteLine($"    Succeeded: {operation.DocumentsSucceeded}");
            Console.WriteLine($"    Failed: {operation.DocumentsFailed}");
            Console.WriteLine($"    In Progress: {operation.DocumentsInProgress}");
            Console.WriteLine($"    Not started: {operation.DocumentsNotStarted}");

            await foreach (DocumentStatusResult document in operationResult.Value)
            {
                Console.WriteLine($"Document with Id: {document.DocumentId}");
                Console.WriteLine($"  Status:{document.Status}");
                if (document.Status == TranslationStatus.Succeeded)
                {
                    Console.WriteLine($"  Translated Document Uri: {document.TranslatedDocumentUri}");
                    Console.WriteLine($"  Translated to language: {document.TranslateTo}.");
                    Console.WriteLine($"  Document source Uri: {document.SourceDocumentUri}");
                }
                else
                {
                    Console.WriteLine($"  Error Code: {document.Error.ErrorCode}");
                    Console.WriteLine($"  Message: {document.Error.Message}");
                }
            }

            #endregion
        }
        public void PollIndividualDocuments()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

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

            Uri sourceUri = new Uri("<source SAS URI>");
            Uri targetUri = new Uri("<target SAS URI>");

            #region Snippet:PollIndividualDocuments

            //@@ Uri sourceUri = <source SAS URI>;
            //@@ Uri targetUri = <target SAS URI>;

            var input = new DocumentTranslationInput(sourceUri, targetUri, "es");
            DocumentTranslationOperation operation = client.StartTranslation(input);

            TimeSpan pollingInterval = new TimeSpan(1000);

            Pageable <DocumentStatusResult> documents = operation.GetAllDocumentStatuses();
            foreach (DocumentStatusResult document in documents)
            {
                Console.WriteLine($"Polling Status for document{document.TranslatedDocumentUri}");

                Response <DocumentStatusResult> status = operation.GetDocumentStatus(document.DocumentId);

                while (!status.Value.HasCompleted)
                {
                    Thread.Sleep(pollingInterval);
                    status = operation.GetDocumentStatus(document.DocumentId);
                }

                if (status.Value.Status == TranslationStatus.Succeeded)
                {
                    Console.WriteLine($"  Translated Document Uri: {document.TranslatedDocumentUri}");
                    Console.WriteLine($"  Translated to language: {document.TranslateTo}.");
                    Console.WriteLine($"  Document source Uri: {document.SourceDocumentUri}");
                }
                else
                {
                    Console.WriteLine($"  Document source Uri: {document.SourceDocumentUri}");
                    Console.WriteLine($"  Error Code: {document.Error.ErrorCode}");
                    Console.WriteLine($"  Message: {document.Error.Message}");
                }
            }

            #endregion
        }
Пример #25
0
        public async Task DocumentStatusAsync()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

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

            Uri sourceUri = new Uri("<source SAS URI>");
            Uri targetUri = new Uri("<target SAS URI>");

            var input = new DocumentTranslationInput(sourceUri, targetUri, "es");
            DocumentTranslationOperation operation = await client.StartTranslationAsync(input);

            TimeSpan pollingInterval = new(1000);

            var documentscompleted = new HashSet <string>();

            while (true)
            {
                await operation.UpdateStatusAsync();

                AsyncPageable <DocumentStatus> documentsStatus = operation.GetAllDocumentStatusesAsync();
                await foreach (DocumentStatus docStatus in documentsStatus)
                {
                    if (documentscompleted.Contains(docStatus.Id))
                    {
                        continue;
                    }
                    if (docStatus.Status == DocumentTranslationStatus.Succeeded || docStatus.Status == DocumentTranslationStatus.Failed)
                    {
                        documentscompleted.Add(docStatus.Id);
                        Console.WriteLine($"Document {docStatus.TranslatedDocumentUri} completed with status {docStatus.Status}");
                    }
                }

                if (operation.HasCompleted)
                {
                    break;
                }
                else
                {
                    if (operation.GetRawResponse().Headers.TryGetValue("Retry-After", out string value))
                    {
                        pollingInterval = TimeSpan.FromSeconds(Convert.ToInt32(value));
                    }
                    await Task.Delay(pollingInterval);
                }
            }
        }
        public void PollIndividualDocuments()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

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

            #region Snippet:PollIndividualDocuments
            Uri sourceUri = new Uri("<source SAS URI>");
            Uri targetUri = new Uri("<target SAS URI>");

            var input = new DocumentTranslationInput(sourceUri, targetUri, "es");
            DocumentTranslationOperation operation = client.StartTranslation(input);

            TimeSpan pollingInterval = new(1000);

            foreach (DocumentStatusResult document in operation.GetAllDocumentStatuses())
            {
                Console.WriteLine($"Polling Status for document{document.SourceDocumentUri}");

                Response <DocumentStatusResult> responseDocumentStatus = operation.GetDocumentStatus(document.DocumentId);

                while (!responseDocumentStatus.Value.HasCompleted)
                {
                    if (responseDocumentStatus.GetRawResponse().Headers.TryGetValue("Retry-After", out string value))
                    {
                        pollingInterval = TimeSpan.FromSeconds(Convert.ToInt32(value));
                    }

                    Thread.Sleep(pollingInterval);
                    responseDocumentStatus = operation.GetDocumentStatus(document.DocumentId);
                }

                if (responseDocumentStatus.Value.Status == TranslationStatus.Succeeded)
                {
                    Console.WriteLine($"  Translated Document Uri: {document.TranslatedDocumentUri}");
                    Console.WriteLine($"  Translated to language: {document.TranslatedTo}.");
                    Console.WriteLine($"  Document source Uri: {document.SourceDocumentUri}");
                }
                else
                {
                    Console.WriteLine($"  Document source Uri: {document.SourceDocumentUri}");
                    Console.WriteLine($"  Error Code: {document.Error.ErrorCode}");
                    Console.WriteLine($"  Message: {document.Error.Message}");
                }
            }

            #endregion
        }
Пример #27
0
        public async Task GetDocumentStatusWithInvalidGuidTest(string invalidGuid, Type expectedException)
        {
            var sourceUri = await CreateSourceContainerAsync(oneTestDocuments);

            var targetUri = await CreateTargetContainerAsync();

            string translateTo = "fr";

            var client = GetClient();

            var input     = new DocumentTranslationInput(sourceUri, targetUri, translateTo);
            var operation = await client.StartTranslationAsync(input);

            await operation.WaitForCompletionAsync();

            Assert.Throws(expectedException, () => operation.GetDocumentStatus(invalidGuid));
        }
        public async Task WrongSourceRightTarget()
        {
            Uri source = new("https://idont.ex.ist");
            Uri target = await CreateTargetContainerAsync();

            DocumentTranslationClient client = GetClient();

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

            RequestFailedException ex = Assert.ThrowsAsync <RequestFailedException>(async() => await operation.UpdateStatusAsync());

            Assert.AreEqual("InvalidDocumentAccessLevel", ex.ErrorCode);

            Assert.IsTrue(operation.HasCompleted);
            Assert.IsFalse(operation.HasValue);
            Assert.AreEqual(TranslationStatus.ValidationFailed, operation.Status);
        }
        public void DocumentStatus()
        {
            string endpoint  = TestEnvironment.Endpoint;
            string apiKey    = TestEnvironment.ApiKey;
            Uri    sourceUri = new Uri("");
            Uri    targetUri = new Uri("");

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

            var input = new DocumentTranslationInput(sourceUri, targetUri, "es");
            DocumentTranslationOperation operation = client.StartTranslation(input);

            TimeSpan pollingInterval = new(1000);

            var documentscompleted = new HashSet <string>();

            while (!operation.HasCompleted)
            {
                if (operation.GetRawResponse().Headers.TryGetValue("Retry-After", out string value))
                {
                    pollingInterval = TimeSpan.FromSeconds(Convert.ToInt32(value));
                }

                Thread.Sleep(pollingInterval);
                operation.UpdateStatus();

                Pageable <DocumentStatusResult> documentsStatus = operation.GetAllDocumentStatuses();
                foreach (DocumentStatusResult docStatus in documentsStatus)
                {
                    if (documentscompleted.Contains(docStatus.DocumentId))
                    {
                        continue;
                    }
                    if (docStatus.Status == TranslationStatus.Succeeded || docStatus.Status == TranslationStatus.Failed)
                    {
                        documentscompleted.Add(docStatus.DocumentId);
                        Console.WriteLine($"Document {docStatus.TranslatedDocumentUri} completed with status ${docStatus.Status}");
                    }
                }
            }
        }
        public void DocumentTranslationInput()
        {
            #region Snippet:DocumentTranslationSingleInput
#if SNIPPET
            Uri sourceSasUri       = new Uri("<source SAS URI>");
            Uri frenchTargetSasUri = new Uri("<french target SAS URI>");
#else
            Uri sourceSasUri       = new Uri("https://soure.storage.blob.core.windows.net/source1");
            Uri frenchTargetSasUri = new Uri("https://target.storage.blob.core.windows.net/frenchcontainer");
#endif
            var input = new DocumentTranslationInput(sourceSasUri, frenchTargetSasUri, "fr");
            #endregion

            #region Snippet:DocumentTranslationMultipleInputs

#if SNIPPET
            Uri arabicTargetSasUri  = new Uri("<arabic target SAS URI>");
            Uri spanishTargetSasUri = new Uri("<spanish target SAS URI>");
            Uri source1SasUri       = new Uri("<source1 SAS URI>");
            Uri source2SasUri       = new Uri("<source2 SAS URI>");
#else
            Uri source1SasUri       = new Uri("https://soure.storage.blob.core.windows.net/source1");
            Uri source2SasUri       = new Uri("https://soure.storage.blob.core.windows.net/source2");
            Uri arabicTargetSasUri  = new Uri("https://target.storage.blob.core.windows.net/arabiccontainer");
            Uri spanishTargetSasUri = new Uri("https://target.storage.blob.core.windows.net/spanishcontainer");
#endif

            var inputs = new List <DocumentTranslationInput>
            {
                new DocumentTranslationInput(source1SasUri, spanishTargetSasUri, "es"),
                new DocumentTranslationInput(
                    source: new TranslationSource(source2SasUri),
                    targets: new List <TranslationTarget>
                {
                    new TranslationTarget(frenchTargetSasUri, "fr"),
                    new TranslationTarget(arabicTargetSasUri, "ar")
                }),
            };
            #endregion
        }