Ejemplo n.º 1
0
        public void OperationsHistory()
        {
#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));

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

            TimeSpan pollingInterval = new(1000);

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

            var options = new GetTranslationStatusesOptions
            {
                CreatedAfter = lastWeekTimestamp
            };

            foreach (TranslationStatusResult translationStatus in client.GetTranslationStatuses(options))
            {
                if (translationStatus.Status == DocumentTranslationStatus.NotStarted ||
                    translationStatus.Status == DocumentTranslationStatus.Running)
                {
                    DocumentTranslationOperation operation = new DocumentTranslationOperation(translationStatus.Id, client);
                    operation.UpdateStatus();

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

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

                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($"Cancelled Documents: {docsCanceled}");
        }
Ejemplo n.º 2
0
        protected override async ValueTask <bool> IsEnvironmentReadyAsync()
        {
            string endpoint = GetOptionalVariable(EndpointEnvironmentVariableName);
            var    client   = new DocumentTranslationClient(new Uri(endpoint), Credential);

            try
            {
                await client.GetSupportedDocumentFormatsAsync();

                await client.GetSupportedGlossaryFormatsAsync();

                client.GetTranslationStatuses().Take(2);
            }
            catch (RequestFailedException e) when(e.Status == 401)
            {
                return(false);
            }
            return(true);
        }