diff --git a/sdk/translation/azure-ai-translation-document/CHANGELOG.md b/sdk/translation/azure-ai-translation-document/CHANGELOG.md index afffa883ace8..7155c9e533cd 100644 --- a/sdk/translation/azure-ai-translation-document/CHANGELOG.md +++ b/sdk/translation/azure-ai-translation-document/CHANGELOG.md @@ -21,6 +21,9 @@ translation has completed. - Authentication using `azure-identity` credentials now supported. - see the [Azure Identity documentation](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/identity/azure-identity/README.md) for more information. - Added paging and filtering options to `list_all_document_statuses` and `list_submitted_jobs`. +- The input to `begin_translation` now accepts either the parameter `inputs` as a `List[DocumentTranslationInput]` to +perform multiple translations, or the parameters `source_url`, `target_url`, and `target_language_code` to perform a +single translation of your documents. **Dependency updates** diff --git a/sdk/translation/azure-ai-translation-document/README.md b/sdk/translation/azure-ai-translation-document/README.md index 635100330827..7b5a4b599d9d 100644 --- a/sdk/translation/azure-ai-translation-document/README.md +++ b/sdk/translation/azure-ai-translation-document/README.md @@ -141,26 +141,21 @@ To begin translating your documents, pass a list of `DocumentTranslationInput` i Constructing a `DocumentTranslationInput` requires that you pass the SAS URLs to your source and target containers (or files) and the target language(s) for translation. -A single source container with documents can be translated to many different languages: +A single source container with documents can be translated to a different language: ```python -from azure.ai.translation.document import DocumentTranslationInput, TranslationTarget +from azure.core.credentials import AzureKeyCredential +from azure.ai.translation.document import DocumentTranslationClient -my_input = [ - DocumentTranslationInput( - source_url="", - targets=[ - TranslationTarget(target_url="", language_code="fr"), - TranslationTarget(target_url="", language_code="de") - ] - ) -] +document_translation_client = DocumentTranslationClient("", AzureKeyCredential("")) +poller = document_translation_client.begin_translation("", "", "") ``` Or multiple different sources can be provided each with their own targets. ```python -from azure.ai.translation.document import DocumentTranslationInput, TranslationTarget +from azure.core.credentials import AzureKeyCredential +from azure.ai.translation.document import DocumentTranslationClient, DocumentTranslationInput, TranslationTarget my_input = [ DocumentTranslationInput( @@ -185,6 +180,9 @@ my_input = [ ] ) ] + +document_translation_client = DocumentTranslationClient("", AzureKeyCredential("")) +poller = document_translation_client.begin_translation(my_input) ``` > Note: the target_url for each target language must be unique. @@ -206,10 +204,49 @@ Sample code snippets are provided to illustrate using long-running operations [b The following section provides several code snippets covering some of the most common Document Translation tasks, including: * [Translate your documents](#translate-your-documents "Translate Your Documents") +* [Translate multiple inputs](#translate-multiple-inputs "Translate Multiple Inputs") * [List translation operations](#list-translation-operations "List Translation Operations") ### Translate your documents -Translate the documents in your source container to the target containers. +Translate the documents in your source container to the target container. + +```python +from azure.core.credentials import AzureKeyCredential +from azure.ai.translation.document import DocumentTranslationClient + +endpoint = "https://.cognitiveservices.azure.com/" +credential = AzureKeyCredential("") +source_container_sas_url_en = "" +target_container_sas_url_es = "" + +document_translation_client = DocumentTranslationClient(endpoint, credential) + +poller = document_translation_client.begin_translation(source_container_sas_url_en, target_container_sas_url_es, "es") + +result = poller.result() + +print("Status: {}".format(poller.status())) +print("Created on: {}".format(poller.details.created_on)) +print("Last updated on: {}".format(poller.details.last_updated_on)) +print("Total number of translations on documents: {}".format(poller.details.documents_total_count)) + +print("\nOf total documents...") +print("{} failed".format(poller.details.documents_failed_count)) +print("{} succeeded".format(poller.details.documents_succeeded_count)) + +for document in result: + print("Document ID: {}".format(document.id)) + print("Document status: {}".format(document.status)) + if document.status == "Succeeded": + print("Source document location: {}".format(document.source_document_url)) + print("Translated document location: {}".format(document.translated_document_url)) + print("Translated to language: {}\n".format(document.translate_to)) + else: + print("Error Code: {}, Message: {}\n".format(document.error.code, document.error.message)) +``` + +### Translate multiple inputs +Begin translating with documents in multiple source containers to multiple target containers in different languages. ```python from azure.core.credentials import AzureKeyCredential @@ -217,9 +254,11 @@ from azure.ai.translation.document import DocumentTranslationClient, DocumentTra endpoint = "https://.cognitiveservices.azure.com/" credential = AzureKeyCredential("") +source_container_sas_url_de = "" source_container_sas_url_en = "" target_container_sas_url_es = "" target_container_sas_url_fr = "" +target_container_sas_url_ar = "" document_translation_client = DocumentTranslationClient(endpoint, credential) @@ -231,21 +270,18 @@ poller = document_translation_client.begin_translation( TranslationTarget(target_url=target_container_sas_url_es, language_code="es"), TranslationTarget(target_url=target_container_sas_url_fr, language_code="fr"), ], + ), + DocumentTranslationInput( + source_url=source_container_sas_url_de, + targets=[ + TranslationTarget(target_url=target_container_sas_url_ar, language_code="ar"), + ], ) ] ) result = poller.result() -print("Status: {}".format(poller.status())) -print("Created on: {}".format(poller.details.created_on)) -print("Last updated on: {}".format(poller.details.last_updated_on)) -print("Total number of translations on documents: {}".format(poller.details.documents_total_count)) - -print("\nOf total documents...") -print("{} failed".format(poller.details.documents_failed_count)) -print("{} succeeded".format(poller.details.documents_succeeded_count)) - for document in result: print("Document ID: {}".format(document.id)) print("Document status: {}".format(document.status)) @@ -321,6 +357,7 @@ These code samples show common scenario operations with the Azure Document Trans * Client authentication: [sample_authentication.py][sample_authentication] * Begin translating documents: [sample_begin_translation.py][sample_begin_translation] +* Translate with multiple inputs: [sample_translate_multiple_inputs.py][sample_translate_multiple_inputs] * Check the status of documents: [sample_check_document_statuses.py][sample_check_document_statuses] * List all submitted translation jobs: [sample_list_all_submitted_jobs.py][sample_list_all_submitted_jobs] * Apply a custom glossary to translation: [sample_translation_with_glossaries.py][sample_translation_with_glossaries] @@ -334,6 +371,7 @@ are found under the `azure.ai.translation.document.aio` namespace. * Client authentication: [sample_authentication_async.py][sample_authentication_async] * Begin translating documents: [sample_begin_translation_async.py][sample_begin_translation_async] +* Translate with multiple inputs: [sample_translate_multiple_inputs_async.py][sample_translate_multiple_inputs_async] * Check the status of documents: [sample_check_document_statuses_async.py][sample_check_document_statuses_async] * List all submitted translation jobs: [sample_list_all_submitted_jobs_async.py][sample_list_all_submitted_jobs_async] * Apply a custom glossary to translation: [sample_translation_with_glossaries_async.py][sample_translation_with_glossaries_async] @@ -390,6 +428,8 @@ This project has adopted the [Microsoft Open Source Code of Conduct][code_of_con [sample_authentication_async]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_authentication_async.py [sample_begin_translation]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/translation/azure-ai-translation-document/samples/sample_begin_translation.py [sample_begin_translation_async]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_begin_translation_async.py +[sample_translate_multiple_inputs]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/translation/azure-ai-translation-document/samples/sample_translate_multiple_inputs.py +[sample_translate_multiple_inputs_async]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_translate_multiple_inputs_async.py [sample_check_document_statuses]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/translation/azure-ai-translation-document/samples/sample_check_document_statuses.py [sample_check_document_statuses_async]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_check_document_statuses_async.py [sample_list_all_submitted_jobs]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/translation/azure-ai-translation-document/samples/sample_list_all_submitted_jobs.py diff --git a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_client.py b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_client.py index 7eaf5eb53069..1beb77a1af7a 100644 --- a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_client.py +++ b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_client.py @@ -5,7 +5,7 @@ # ------------------------------------ import json -from typing import Any, TYPE_CHECKING, List, Union +from typing import Any, TYPE_CHECKING, List, Union, overload from azure.core.tracing.decorator import distributed_trace from ._generated import BatchDocumentTranslationClient as _BatchDocumentTranslationClient from ._models import ( @@ -16,7 +16,7 @@ ) from ._user_agent import USER_AGENT from ._polling import TranslationPolling, DocumentTranslationLROPollingMethod -from ._helpers import get_http_logging_policy, convert_datetime, get_authentication_policy +from ._helpers import get_http_logging_policy, convert_datetime, get_authentication_policy, get_translation_input if TYPE_CHECKING: from azure.core.paging import ItemPaged from azure.core.credentials import TokenCredential, AzureKeyCredential @@ -89,15 +89,32 @@ def close(self): """Close the :class:`~azure.ai.translation.document.DocumentTranslationClient` session.""" return self._client.close() - @distributed_trace + @overload + def begin_translation(self, source_url, target_url, target_language_code, **kwargs): + # type: (str, str, str, **Any) -> DocumentTranslationPoller[ItemPaged[DocumentStatusResult]] + pass + + @overload def begin_translation(self, inputs, **kwargs): # type: (List[DocumentTranslationInput], **Any) -> DocumentTranslationPoller[ItemPaged[DocumentStatusResult]] - """Begin translating the document(s) in your source container to your TranslationTarget(s) - in the given language. + pass + + def begin_translation(self, *args, **kwargs): # pylint: disable=client-method-missing-type-annotations + """Begin translating the document(s) in your source container to your target container + in the given language. To perform a single translation from source to target, pass the `source_url`, + `target_url`, and `target_language_code` parameters. To pass multiple inputs for translation, including + other translation options, pass the `inputs` parameter as a list of DocumentTranslationInput. For supported languages and document formats, see the service documentation: https://docs.microsoft.com/azure/cognitive-services/translator/document-translation/overview + :param str source_url: The source SAS URL to the Azure Blob container containing the documents + to be translated. Requires read and list permissions at the minimum. + :param str target_url: The target SAS URL to the Azure Blob container where the translated documents + should be written. Requires write and list permissions at the minimum. + :param str target_language_code: This is the language you want your documents to be translated to. + See supported language codes here: + https://docs.microsoft.com/azure/cognitive-services/translator/language-support#translate :param inputs: A list of translation inputs. Each individual input has a single source URL to documents and can contain multiple TranslationTargets (one for each language) for the destination to write translated documents. @@ -118,6 +135,10 @@ def begin_translation(self, inputs, **kwargs): :caption: Translate the documents in your storage container. """ + continuation_token = kwargs.pop("continuation_token", None) + + inputs = get_translation_input(args, kwargs, continuation_token) + def deserialization_callback( raw_response, _, headers ): # pylint: disable=unused-argument @@ -128,7 +149,6 @@ def deserialization_callback( "polling_interval", self._client._config.polling_interval # pylint: disable=protected-access ) - continuation_token = kwargs.pop("continuation_token", None) pipeline_response = None if continuation_token: pipeline_response = self._client.document_translation.get_translation_status( @@ -138,8 +158,7 @@ def deserialization_callback( callback = kwargs.pop("cls", deserialization_callback) return self._client.document_translation.begin_start_translation( - inputs=DocumentTranslationInput._to_generated_list(inputs) # pylint: disable=protected-access - if not continuation_token else None, + inputs=inputs if not continuation_token else None, polling=DocumentTranslationLROPollingMethod( timeout=polling_interval, lro_algorithms=[ diff --git a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_helpers.py b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_helpers.py index 586ba746585f..4f094c483832 100644 --- a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_helpers.py +++ b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_helpers.py @@ -10,9 +10,51 @@ from azure.core.credentials import AzureKeyCredential from azure.core.pipeline.policies import AzureKeyCredentialPolicy from azure.core.pipeline.policies import HttpLoggingPolicy +from ._generated.models import ( + BatchRequest as _BatchRequest, + SourceInput as _SourceInput, + TargetInput as _TargetInput, +) +from ._models import DocumentTranslationInput COGNITIVE_KEY_HEADER = "Ocp-Apim-Subscription-Key" +def get_translation_input(args, kwargs, continuation_token): + try: + inputs = kwargs.pop('inputs', None) + if not inputs: + inputs = args[0] + request = DocumentTranslationInput._to_generated_list(inputs) \ + if not continuation_token else None # pylint: disable=protected-access + except (AttributeError, TypeError, IndexError): + try: + source_url = kwargs.pop('source_url', None) + if not source_url: + source_url = args[0] + target_url = kwargs.pop("target_url", None) + if not target_url: + target_url = args[1] + target_language_code = kwargs.pop("target_language_code", None) + if not target_language_code: + target_language_code = args[2] + request = [ + _BatchRequest( + source=_SourceInput( + source_url=source_url + ), + targets=[_TargetInput( + target_url=target_url, + language=target_language_code + )] + ) + ] + except (AttributeError, TypeError, IndexError): + raise ValueError("Pass 'inputs' for multiple inputs or 'source_url', 'target_url', " + "and 'target_language_code' for a single input.") + + return request + + def get_authentication_policy(credential): authentication_policy = None if credential is None: diff --git a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/_client_async.py b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/_client_async.py index cd716d1b1568..153491d3bd6b 100644 --- a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/_client_async.py +++ b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/_client_async.py @@ -5,7 +5,7 @@ # ------------------------------------ import json -from typing import Any, List, Union, TYPE_CHECKING +from typing import Any, List, Union, TYPE_CHECKING, overload from azure.core.tracing.decorator_async import distributed_trace_async from azure.core.tracing.decorator import distributed_trace from azure.core.async_paging import AsyncItemPaged @@ -17,7 +17,7 @@ FileFormat, DocumentStatusResult ) -from .._helpers import get_http_logging_policy, convert_datetime, get_authentication_policy +from .._helpers import get_http_logging_policy, convert_datetime, get_authentication_policy, get_translation_input from ._async_polling import AsyncDocumentTranslationLROPollingMethod, AsyncDocumentTranslationPoller from .._polling import TranslationPolling if TYPE_CHECKING: @@ -88,16 +88,39 @@ async def close(self) -> None: """Close the :class:`~azure.ai.translation.document.aio.DocumentTranslationClient` session.""" await self._client.__aexit__() - @distributed_trace_async + @overload async def begin_translation( - self, inputs: List[DocumentTranslationInput], **kwargs: Any + self, source_url: str, + target_url: str, + target_language_code: str, + **kwargs: Any ) -> AsyncDocumentTranslationPoller[AsyncItemPaged[DocumentStatusResult]]: - """Begin translating the document(s) in your source container to your TranslationTarget(s) - in the given language. + ... + + @overload + async def begin_translation( + self, inputs: List[DocumentTranslationInput], + **kwargs: Any + ) -> AsyncDocumentTranslationPoller[AsyncItemPaged[DocumentStatusResult]]: + ... + + @distributed_trace_async + async def begin_translation(self, *args, **kwargs): # pylint: disable=client-method-missing-type-annotations + """Begin translating the document(s) in your source container to your target container + in the given language. To perform a single translation from source to target, pass the `source_url`, + `target_url`, and `target_language_code` parameters. To pass multiple inputs for translation, including + other translation options, pass the `inputs` parameter as a list of DocumentTranslationInput. For supported languages and document formats, see the service documentation: https://docs.microsoft.com/azure/cognitive-services/translator/document-translation/overview + :param str source_url: The source SAS URL to the Azure Blob container containing the documents + to be translated. Requires read and list permissions at the minimum. + :param str target_url: The target SAS URL to the Azure Blob container where the translated documents + should be written. Requires write and list permissions at the minimum. + :param str target_language_code: This is the language you want your documents to be translated to. + See supported language codes here: + https://docs.microsoft.com/azure/cognitive-services/translator/language-support#translate :param inputs: A list of translation inputs. Each individual input has a single source URL to documents and can contain multiple TranslationTargets (one for each language) for the destination to write translated documents. @@ -118,8 +141,12 @@ async def begin_translation( :caption: Translate the documents in your storage container. """ + continuation_token = kwargs.pop("continuation_token", None) + + inputs = get_translation_input(args, kwargs, continuation_token) + def deserialization_callback( - raw_response, _, headers + raw_response, _, headers ): # pylint: disable=unused-argument translation_status = json.loads(raw_response.http_response.text()) return self.list_all_document_statuses(translation_status["id"]) @@ -128,7 +155,6 @@ def deserialization_callback( "polling_interval", self._client._config.polling_interval # pylint: disable=protected-access ) - continuation_token = kwargs.pop("continuation_token", None) pipeline_response = None if continuation_token: pipeline_response = await self._client.document_translation.get_translation_status( @@ -138,8 +164,7 @@ def deserialization_callback( callback = kwargs.pop("cls", deserialization_callback) return await self._client.document_translation.begin_start_translation( - inputs=DocumentTranslationInput._to_generated_list(inputs) # pylint: disable=protected-access - if not continuation_token else None, + inputs=inputs if not continuation_token else None, polling=AsyncDocumentTranslationLROPollingMethod( timeout=polling_interval, lro_algorithms=[ diff --git a/sdk/translation/azure-ai-translation-document/samples/README.md b/sdk/translation/azure-ai-translation-document/samples/README.md index 3a6c5bcaed3f..45714f6d5849 100644 --- a/sdk/translation/azure-ai-translation-document/samples/README.md +++ b/sdk/translation/azure-ai-translation-document/samples/README.md @@ -22,6 +22,7 @@ These sample programs show common scenarios for the Document Translation client' |**File Name**|**Description**| |----------------|-------------| |[sample_begin_translation.py][begin_translation] and [sample_begin_translation_async.py][begin_translation_async]|Translate your documents| +|[sample_translate_multiple_inputs.py][sample_translate_multiple_inputs] and [sample_translate_multiple_inputs_async.py][sample_translate_multiple_inputs_async]|Translate multiple source containers with documents to multiple target containers in different languages| |[sample_translation_with_glossaries.py][create_translation_job_with_glossaries] and [sample_translation_with_glossaries_async.py][create_translation_job_with_glossaries_async]|Translate your documents using custom glossaries| |[sample_check_document_statuses.py][check_document_statuses] and [sample_check_document_statuses_async.py][check_document_statuses_async]|Check status of submitted documents| |[sample_list_all_submitted_jobs.py][list_all_submitted_jobs] and [sample_list_all_submitted_jobs_async.py][list_all_submitted_jobs_async]|Check status of all submitted translation jobs| @@ -66,6 +67,8 @@ what you can do with the Azure Document Translation client library. [sample_authentication_async]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_authentication_async.py [begin_translation]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/translation/azure-ai-translation-document/samples/sample_begin_translation.py [begin_translation_async]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_begin_translation_async.py +[sample_translate_multiple_inputs]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/translation/azure-ai-translation-document/samples/sample_translate_multiple_inputs.py +[sample_translate_multiple_inputs_async]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_translate_multiple_inputs_async.py [create_translation_job_with_azure_blob]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/translation/azure-ai-translation-document/samples/sample_translation_with_azure_blob.py [create_translation_job_with_azure_blob_async]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_translation_with_azure_blob_async.py [create_translation_job_with_glossaries]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/translation/azure-ai-translation-document/samples/sample_translation_with_glossaries.py diff --git a/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_begin_translation_async.py b/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_begin_translation_async.py index b3845df0258a..90e7a58d1a10 100644 --- a/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_begin_translation_async.py +++ b/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_begin_translation_async.py @@ -34,10 +34,6 @@ async def sample_translation_async(): import os from azure.core.credentials import AzureKeyCredential from azure.ai.translation.document.aio import DocumentTranslationClient - from azure.ai.translation.document import ( - DocumentTranslationInput, - TranslationTarget - ) endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] @@ -47,18 +43,7 @@ async def sample_translation_async(): client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) async with client: - poller = await client.begin_translation(inputs=[ - DocumentTranslationInput( - source_url=source_container_url, - targets=[ - TranslationTarget( - target_url=target_container_url, - language_code="es" - ) - ] - ) - ] - ) + poller = await client.begin_translation(source_container_url, target_container_url, "fr") result = await poller.result() print("Status: {}".format(poller.status())) diff --git a/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_check_document_statuses_async.py b/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_check_document_statuses_async.py index 108620c0ace5..414ce67d83ca 100644 --- a/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_check_document_statuses_async.py +++ b/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_check_document_statuses_async.py @@ -34,10 +34,6 @@ async def sample_document_status_checks_async(): import os from azure.core.credentials import AzureKeyCredential from azure.ai.translation.document.aio import DocumentTranslationClient - from azure.ai.translation.document import ( - DocumentTranslationInput, - TranslationTarget - ) endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] @@ -47,18 +43,7 @@ async def sample_document_status_checks_async(): client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) async with client: - poller = await client.begin_translation(inputs=[ - DocumentTranslationInput( - source_url=source_container_url, - targets=[ - TranslationTarget( - target_url=target_container_url, - language_code="es" - ) - ] - ) - ] - ) + poller = await client.begin_translation(source_container_url, target_container_url, "es") completed_docs = [] while not poller.done(): diff --git a/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_translate_multiple_inputs_async.py b/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_translate_multiple_inputs_async.py new file mode 100644 index 000000000000..804c7bb722d6 --- /dev/null +++ b/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_translate_multiple_inputs_async.py @@ -0,0 +1,110 @@ +# coding=utf-8 +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ + +""" +FILE: sample_translate_multiple_inputs_async.py + +DESCRIPTION: + This sample demonstrates how to begin translating with documents in multiple source containers to + multiple target containers in different languages. + + To set up your containers for translation and generate SAS tokens to your containers (or files) + with the appropriate permissions, see the README. + +USAGE: + python sample_translate_multiple_inputs_async.py + + Set the environment variables with your own values before running the sample: + 1) AZURE_DOCUMENT_TRANSLATION_ENDPOINT - the endpoint to your Document Translation resource. + 2) AZURE_DOCUMENT_TRANSLATION_KEY - your Document Translation API key. + 3) AZURE_SOURCE_CONTAINER_URL_1 - the first container SAS URL to your source container which has the documents + to be translated. + 4) AZURE_SOURCE_CONTAINER_URL_2 - the second container SAS URL to your source container which has the documents + to be translated. + 5) AZURE_TARGET_CONTAINER_URL_FR - the container SAS URL to your target container where the translated documents + will be written in French. + 6) AZURE_TARGET_CONTAINER_URL_AR - the container SAS URL to your target container where the translated documents + will be written in Arabic. + 7) AZURE_TARGET_CONTAINER_URL_ES - the container SAS URL to your target container where the translated documents + will be written in Spanish. +""" + +import asyncio + + +async def sample_multiple_translation_async(): + import os + from azure.core.credentials import AzureKeyCredential + from azure.ai.translation.document.aio import DocumentTranslationClient + from azure.ai.translation.document import ( + DocumentTranslationInput, + TranslationTarget + ) + + endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] + key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] + source_container_url_1 = os.environ["AZURE_SOURCE_CONTAINER_URL_1"] + source_container_url_2 = os.environ["AZURE_SOURCE_CONTAINER_URL_2"] + target_container_url_fr = os.environ["AZURE_TARGET_CONTAINER_URL_FR"] + target_container_url_ar = os.environ["AZURE_TARGET_CONTAINER_URL_AR"] + target_container_url_es = os.environ["AZURE_TARGET_CONTAINER_URL_ES"] + + client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) + + async with client: + poller = await client.begin_translation(inputs=[ + DocumentTranslationInput( + source_url=source_container_url_1, + targets=[ + TranslationTarget( + target_url=target_container_url_fr, + language_code="fr" + ), + TranslationTarget( + target_url=target_container_url_ar, + language_code="ar" + ) + ] + ), + DocumentTranslationInput( + source_url=source_container_url_2, + targets=[ + TranslationTarget( + target_url=target_container_url_es, + language_code="es" + ) + ] + ) + ] + ) + result = await poller.result() + + print("Status: {}".format(poller.status())) + print("Created on: {}".format(poller.details.created_on)) + print("Last updated on: {}".format(poller.details.last_updated_on)) + print("Total number of translations on documents: {}".format(poller.details.documents_total_count)) + + print("\nOf total documents...") + print("{} failed".format(poller.details.documents_failed_count)) + print("{} succeeded".format(poller.details.documents_succeeded_count)) + + async for document in result: + print("Document ID: {}".format(document.id)) + print("Document status: {}".format(document.status)) + if document.status == "Succeeded": + print("Source document location: {}".format(document.source_document_url)) + print("Translated document location: {}".format(document.translated_document_url)) + print("Translated to language: {}\n".format(document.translate_to)) + else: + print("Error Code: {}, Message: {}\n".format(document.error.code, document.error.message)) + + +async def main(): + await sample_multiple_translation_async() + +if __name__ == '__main__': + loop = asyncio.get_event_loop() + loop.run_until_complete(main()) diff --git a/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_translation_with_azure_blob_async.py b/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_translation_with_azure_blob_async.py index 919ddf8c6f4b..067aa1ffe32a 100644 --- a/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_translation_with_azure_blob_async.py +++ b/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_translation_with_azure_blob_async.py @@ -39,10 +39,6 @@ from azure.core.credentials import AzureKeyCredential from azure.core.exceptions import ResourceExistsError from azure.ai.translation.document.aio import DocumentTranslationClient -from azure.ai.translation.document import ( - DocumentTranslationInput, - TranslationTarget -) from azure.storage.blob.aio import BlobServiceClient, BlobClient from azure.storage.blob import generate_container_sas @@ -93,19 +89,7 @@ async def sample_translation_with_azure_blob(self): source_container_sas_url = self.generate_sas_url(source_container, permissions="rl") target_container_sas_url = self.generate_sas_url(target_container, permissions="wl") - translation_inputs = [ - DocumentTranslationInput( - source_url=source_container_sas_url, - targets=[ - TranslationTarget( - target_url=target_container_sas_url, - language_code="fr" - ) - ] - ) - ] - - poller = await translation_client.begin_translation(translation_inputs) + poller = await translation_client.begin_translation(source_container_sas_url, target_container_sas_url, "fr") print("Created translation job with ID: {}".format(poller.id)) print("Waiting until translation completes...") diff --git a/sdk/translation/azure-ai-translation-document/samples/sample_begin_translation.py b/sdk/translation/azure-ai-translation-document/samples/sample_begin_translation.py index b8c1a8c04eca..478333a54be1 100644 --- a/sdk/translation/azure-ai-translation-document/samples/sample_begin_translation.py +++ b/sdk/translation/azure-ai-translation-document/samples/sample_begin_translation.py @@ -30,11 +30,7 @@ def sample_translation(): # [START begin_translation] import os from azure.core.credentials import AzureKeyCredential - from azure.ai.translation.document import ( - DocumentTranslationClient, - DocumentTranslationInput, - TranslationTarget - ) + from azure.ai.translation.document import DocumentTranslationClient endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] @@ -43,18 +39,7 @@ def sample_translation(): client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) - poller = client.begin_translation(inputs=[ - DocumentTranslationInput( - source_url=source_container_url, - targets=[ - TranslationTarget( - target_url=target_container_url, - language_code="es" - ) - ] - ) - ] - ) + poller = client.begin_translation(source_container_url, target_container_url, "fr") result = poller.result() print("Status: {}".format(poller.status())) diff --git a/sdk/translation/azure-ai-translation-document/samples/sample_check_document_statuses.py b/sdk/translation/azure-ai-translation-document/samples/sample_check_document_statuses.py index 2aea426ba260..0f2db7c6941c 100644 --- a/sdk/translation/azure-ai-translation-document/samples/sample_check_document_statuses.py +++ b/sdk/translation/azure-ai-translation-document/samples/sample_check_document_statuses.py @@ -32,11 +32,7 @@ def sample_document_status_checks(): import os import time from azure.core.credentials import AzureKeyCredential - from azure.ai.translation.document import ( - DocumentTranslationClient, - DocumentTranslationInput, - TranslationTarget - ) + from azure.ai.translation.document import DocumentTranslationClient endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] @@ -45,18 +41,7 @@ def sample_document_status_checks(): client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) - poller = client.begin_translation(inputs=[ - DocumentTranslationInput( - source_url=source_container_url, - targets=[ - TranslationTarget( - target_url=target_container_url, - language_code="es" - ) - ] - ) - ] - ) + poller = client.begin_translation(source_container_url, target_container_url, "es") completed_docs = [] while not poller.done(): diff --git a/sdk/translation/azure-ai-translation-document/samples/sample_translate_multiple_inputs.py b/sdk/translation/azure-ai-translation-document/samples/sample_translate_multiple_inputs.py new file mode 100644 index 000000000000..17a915dd58c2 --- /dev/null +++ b/sdk/translation/azure-ai-translation-document/samples/sample_translate_multiple_inputs.py @@ -0,0 +1,103 @@ +# coding=utf-8 +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ + +""" +FILE: sample_translate_multiple_inputs.py + +DESCRIPTION: + This sample demonstrates how to begin translating with documents in multiple source containers to + multiple target containers in different languages. + + To set up your containers for translation and generate SAS tokens to your containers (or files) + with the appropriate permissions, see the README. + +USAGE: + python sample_translate_multiple_inputs.py + + Set the environment variables with your own values before running the sample: + 1) AZURE_DOCUMENT_TRANSLATION_ENDPOINT - the endpoint to your Document Translation resource. + 2) AZURE_DOCUMENT_TRANSLATION_KEY - your Document Translation API key. + 3) AZURE_SOURCE_CONTAINER_URL_1 - the first container SAS URL to your source container which has the documents + to be translated. + 4) AZURE_SOURCE_CONTAINER_URL_2 - the second container SAS URL to your source container which has the documents + to be translated. + 5) AZURE_TARGET_CONTAINER_URL_FR - the container SAS URL to your target container where the translated documents + will be written in French. + 6) AZURE_TARGET_CONTAINER_URL_AR - the container SAS URL to your target container where the translated documents + will be written in Arabic. + 7) AZURE_TARGET_CONTAINER_URL_ES - the container SAS URL to your target container where the translated documents + will be written in Spanish. +""" + + +def sample_multiple_translation(): + import os + from azure.core.credentials import AzureKeyCredential + from azure.ai.translation.document import ( + DocumentTranslationClient, + DocumentTranslationInput, + TranslationTarget + ) + + endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] + key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] + source_container_url_1 = os.environ["AZURE_SOURCE_CONTAINER_URL_1"] + source_container_url_2 = os.environ["AZURE_SOURCE_CONTAINER_URL_2"] + target_container_url_fr = os.environ["AZURE_TARGET_CONTAINER_URL_FR"] + target_container_url_ar = os.environ["AZURE_TARGET_CONTAINER_URL_AR"] + target_container_url_es = os.environ["AZURE_TARGET_CONTAINER_URL_ES"] + + client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) + + poller = client.begin_translation(inputs=[ + DocumentTranslationInput( + source_url=source_container_url_1, + targets=[ + TranslationTarget( + target_url=target_container_url_fr, + language_code="fr" + ), + TranslationTarget( + target_url=target_container_url_ar, + language_code="ar" + ) + ] + ), + DocumentTranslationInput( + source_url=source_container_url_2, + targets=[ + TranslationTarget( + target_url=target_container_url_es, + language_code="es" + ) + ] + ) + ] + ) + result = poller.result() + + print("Status: {}".format(poller.status())) + print("Created on: {}".format(poller.details.created_on)) + print("Last updated on: {}".format(poller.details.last_updated_on)) + print("Total number of translations on documents: {}".format(poller.details.documents_total_count)) + + print("\nOf total documents...") + print("{} failed".format(poller.details.documents_failed_count)) + print("{} succeeded".format(poller.details.documents_succeeded_count)) + + for document in result: + print("Document ID: {}".format(document.id)) + print("Document status: {}".format(document.status)) + if document.status == "Succeeded": + print("Source document location: {}".format(document.source_document_url)) + print("Translated document location: {}".format(document.translated_document_url)) + print("Translated to language: {}\n".format(document.translate_to)) + else: + print("Error Code: {}, Message: {}\n".format(document.error.code, document.error.message)) + + +if __name__ == '__main__': + sample_multiple_translation() diff --git a/sdk/translation/azure-ai-translation-document/samples/sample_translation_with_azure_blob.py b/sdk/translation/azure-ai-translation-document/samples/sample_translation_with_azure_blob.py index 2a068aa27255..21ef61440caf 100644 --- a/sdk/translation/azure-ai-translation-document/samples/sample_translation_with_azure_blob.py +++ b/sdk/translation/azure-ai-translation-document/samples/sample_translation_with_azure_blob.py @@ -37,11 +37,7 @@ import datetime from azure.core.credentials import AzureKeyCredential from azure.core.exceptions import ResourceExistsError -from azure.ai.translation.document import ( - DocumentTranslationClient, - DocumentTranslationInput, - TranslationTarget -) +from azure.ai.translation.document import DocumentTranslationClient from azure.storage.blob import BlobServiceClient, BlobClient, generate_container_sas @@ -91,19 +87,7 @@ def sample_translation_with_azure_blob(self): source_container_sas_url = self.generate_sas_url(source_container, permissions="rl") target_container_sas_url = self.generate_sas_url(target_container, permissions="wl") - translation_inputs = [ - DocumentTranslationInput( - source_url=source_container_sas_url, - targets=[ - TranslationTarget( - target_url=target_container_sas_url, - language_code="fr" - ) - ] - ) - ] - - poller = translation_client.begin_translation(translation_inputs) + poller = translation_client.begin_translation(source_container_sas_url, target_container_sas_url, "fr") print("Created translation job with ID: {}".format(poller.id)) print("Waiting until translation completes...") diff --git a/sdk/translation/azure-ai-translation-document/tests/recordings/test_translation.test_overloaded_inputs.yaml b/sdk/translation/azure-ai-translation-document/tests/recordings/test_translation.test_overloaded_inputs.yaml new file mode 100644 index 000000000000..14896d481cc3 --- /dev/null +++ b/sdk/translation/azure-ai-translation-document/tests/recordings/test_translation.test_overloaded_inputs.yaml @@ -0,0 +1,350 @@ +interactions: +- request: + body: null + headers: + Accept: + - application/xml + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '0' + User-Agent: + - azsdk-python-storage-blob/12.9.0b1 Python/3.9.0 (Windows-10-10.0.19041-SP0) + x-ms-date: + - Wed, 02 Jun 2021 22:46:22 GMT + x-ms-version: + - '2020-08-04' + method: PUT + uri: https://redacted.blob.core.windows.net/srcfd1e7047-d61f-4703-b708-bbbfdffd5c5f?restype=container + response: + body: + string: '' + headers: + content-length: + - '0' + date: + - Wed, 02 Jun 2021 22:46:22 GMT + etag: + - '"0x8D926183F2A6B34"' + last-modified: + - Wed, 02 Jun 2021 22:46:23 GMT + server: + - Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0 + x-ms-version: + - '2020-08-04' + status: + code: 201 + message: Created +- request: + body: hello world + headers: + Accept: + - application/xml + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '11' + Content-Type: + - application/octet-stream + If-None-Match: + - '*' + User-Agent: + - azsdk-python-storage-blob/12.9.0b1 Python/3.9.0 (Windows-10-10.0.19041-SP0) + x-ms-blob-type: + - BlockBlob + x-ms-date: + - Wed, 02 Jun 2021 22:46:23 GMT + x-ms-version: + - '2020-08-04' + method: PUT + uri: https://redacted.blob.core.windows.net/srcfd1e7047-d61f-4703-b708-bbbfdffd5c5f/47b0a9ca-8b7c-4dd5-ad81-38ed9852234c.txt + response: + body: + string: '' + headers: + content-length: + - '0' + content-md5: + - XrY7u+Ae7tCTyyK7j1rNww== + date: + - Wed, 02 Jun 2021 22:46:22 GMT + etag: + - '"0x8D926183F38F222"' + last-modified: + - Wed, 02 Jun 2021 22:46:23 GMT + server: + - Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0 + x-ms-content-crc64: + - vo7q9sPVKY0= + x-ms-request-server-encrypted: + - 'true' + x-ms-version: + - '2020-08-04' + status: + code: 201 + message: Created +- request: + body: null + headers: + Accept: + - application/xml + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '0' + User-Agent: + - azsdk-python-storage-blob/12.9.0b1 Python/3.9.0 (Windows-10-10.0.19041-SP0) + x-ms-date: + - Wed, 02 Jun 2021 22:46:23 GMT + x-ms-version: + - '2020-08-04' + method: PUT + uri: https://redacted.blob.core.windows.net/target8850a2c7-b7fa-44db-ae09-9060c70ab440?restype=container + response: + body: + string: '' + headers: + content-length: + - '0' + date: + - Wed, 02 Jun 2021 22:46:22 GMT + etag: + - '"0x8D926183F59C62F"' + last-modified: + - Wed, 02 Jun 2021 22:46:23 GMT + server: + - Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0 + x-ms-version: + - '2020-08-04' + status: + code: 201 + message: Created +- request: + body: null + headers: + Accept: + - application/xml + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '0' + User-Agent: + - azsdk-python-storage-blob/12.9.0b1 Python/3.9.0 (Windows-10-10.0.19041-SP0) + x-ms-date: + - Wed, 02 Jun 2021 22:46:23 GMT + x-ms-version: + - '2020-08-04' + method: PUT + uri: https://redacted.blob.core.windows.net/target2a92fe99-2a8c-4348-8c3f-05cfe229492f?restype=container + response: + body: + string: '' + headers: + content-length: + - '0' + date: + - Wed, 02 Jun 2021 22:46:23 GMT + etag: + - '"0x8D926183F8033FE"' + last-modified: + - Wed, 02 Jun 2021 22:46:23 GMT + server: + - Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0 + x-ms-version: + - '2020-08-04' + status: + code: 201 + message: Created +- request: + body: '{"inputs": [{"source": {"sourceUrl": "https://redacted.blob.core.windows.net/srcfd1e7047-d61f-4703-b708-bbbfdffd5c5f?se=end&sp=rl&sv=2020-08-04&sr=c&sig=fake_token_value", + "filter": {}}, "targets": [{"targetUrl": "https://redacted.blob.core.windows.net/target8850a2c7-b7fa-44db-ae09-9060c70ab440?se=end&sp=rw&sv=2020-08-04&sr=c&sig=fake_token_value", + "language": "es"}]}]}' + headers: + Accept: + - application/json + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '490' + Content-Type: + - application/json + User-Agent: + - azsdk-python-ai-translation-document/1.0.0b2 Python/3.9.0 (Windows-10-10.0.19041-SP0) + method: POST + uri: https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches + response: + body: + string: '' + headers: + apim-request-id: + - 098a39d5-3904-4b36-bae5-c8d289df1048 + content-length: + - '0' + date: + - Wed, 02 Jun 2021 22:46:34 GMT + operation-location: + - https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches/2cd1d2a6-ac6c-4f3c-92ef-829e11c53473 + set-cookie: + - ARRAffinity=0ff46fa6d58b00f2d44b7073721903c0eace480139d8ecfc38f61130af5e1587;Path=/;HttpOnly;Secure;Domain=mtbatch.nam.microsofttranslator.com + - ARRAffinitySameSite=0ff46fa6d58b00f2d44b7073721903c0eace480139d8ecfc38f61130af5e1587;Path=/;HttpOnly;SameSite=None;Secure;Domain=mtbatch.nam.microsofttranslator.com + strict-transport-security: + - max-age=31536000; includeSubDomains; preload + x-content-type-options: + - nosniff + x-powered-by: + - ASP.NET + x-requestid: + - 098a39d5-3904-4b36-bae5-c8d289df1048 + status: + code: 202 + message: Accepted +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + User-Agent: + - azsdk-python-ai-translation-document/1.0.0b2 Python/3.9.0 (Windows-10-10.0.19041-SP0) + method: GET + uri: https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches/2cd1d2a6-ac6c-4f3c-92ef-829e11c53473 + response: + body: + string: '{"id":"2cd1d2a6-ac6c-4f3c-92ef-829e11c53473","createdDateTimeUtc":"2021-06-02T22:46:35.8351418Z","lastActionDateTimeUtc":"2021-06-02T22:46:37.2424895Z","status":"Succeeded","summary":{"total":1,"failed":0,"success":1,"inProgress":0,"notYetStarted":0,"cancelled":0,"totalCharacterCharged":11}}' + headers: + apim-request-id: + - 45a84f3d-a37c-4982-aca2-0a39aff07883 + cache-control: + - public,max-age=1 + content-length: + - '292' + content-type: + - application/json; charset=utf-8 + date: + - Wed, 02 Jun 2021 22:47:05 GMT + etag: + - '"BE1ADCC7FEF63AD50697E3FFFD3C09ED017D8CDC1298459FF3280E388FB74DDE"' + set-cookie: + - ARRAffinity=20358cd7aa5d6b0695f01ef171fc9a95880154357830e1c6bb513b73834a2e5f;Path=/;HttpOnly;Secure;Domain=mtbatch.nam.microsofttranslator.com + - ARRAffinitySameSite=20358cd7aa5d6b0695f01ef171fc9a95880154357830e1c6bb513b73834a2e5f;Path=/;HttpOnly;SameSite=None;Secure;Domain=mtbatch.nam.microsofttranslator.com + strict-transport-security: + - max-age=31536000; includeSubDomains; preload + transfer-encoding: + - chunked + vary: + - Accept-Encoding + x-content-type-options: + - nosniff + x-powered-by: + - ASP.NET + x-requestid: + - 45a84f3d-a37c-4982-aca2-0a39aff07883 + status: + code: 200 + message: OK +- request: + body: '{"inputs": [{"source": {"sourceUrl": "https://redacted.blob.core.windows.net/srcfd1e7047-d61f-4703-b708-bbbfdffd5c5f?se=end&sp=rl&sv=2020-08-04&sr=c&sig=fake_token_value", + "filter": {}}, "targets": [{"targetUrl": "https://redacted.blob.core.windows.net/target2a92fe99-2a8c-4348-8c3f-05cfe229492f?se=end&sp=rw&sv=2020-08-04&sr=c&sig=fake_token_value", + "language": "es"}]}]}' + headers: + Accept: + - application/json + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '492' + Content-Type: + - application/json + User-Agent: + - azsdk-python-ai-translation-document/1.0.0b2 Python/3.9.0 (Windows-10-10.0.19041-SP0) + method: POST + uri: https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches + response: + body: + string: '' + headers: + apim-request-id: + - e3327c6d-a0f6-4477-9233-076af73ca374 + content-length: + - '0' + date: + - Wed, 02 Jun 2021 22:47:05 GMT + operation-location: + - https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches/3472ce8b-1f79-4636-9b43-6d812a49feea + set-cookie: + - ARRAffinity=0ff46fa6d58b00f2d44b7073721903c0eace480139d8ecfc38f61130af5e1587;Path=/;HttpOnly;Secure;Domain=mtbatch.nam.microsofttranslator.com + - ARRAffinitySameSite=0ff46fa6d58b00f2d44b7073721903c0eace480139d8ecfc38f61130af5e1587;Path=/;HttpOnly;SameSite=None;Secure;Domain=mtbatch.nam.microsofttranslator.com + strict-transport-security: + - max-age=31536000; includeSubDomains; preload + x-content-type-options: + - nosniff + x-powered-by: + - ASP.NET + x-requestid: + - e3327c6d-a0f6-4477-9233-076af73ca374 + status: + code: 202 + message: Accepted +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + User-Agent: + - azsdk-python-ai-translation-document/1.0.0b2 Python/3.9.0 (Windows-10-10.0.19041-SP0) + method: GET + uri: https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches/3472ce8b-1f79-4636-9b43-6d812a49feea + response: + body: + string: '{"id":"3472ce8b-1f79-4636-9b43-6d812a49feea","createdDateTimeUtc":"2021-06-02T22:47:06.3202913Z","lastActionDateTimeUtc":"2021-06-02T22:47:12.0995152Z","status":"Succeeded","summary":{"total":1,"failed":0,"success":1,"inProgress":0,"notYetStarted":0,"cancelled":0,"totalCharacterCharged":11}}' + headers: + apim-request-id: + - 4d1b0a39-22de-40c8-b9b5-4d0f30c89a40 + cache-control: + - public,max-age=1 + content-length: + - '292' + content-type: + - application/json; charset=utf-8 + date: + - Wed, 02 Jun 2021 22:47:36 GMT + etag: + - '"262E15D3BD2756D42A3CF5147009BB7FEE519224A0FC02489F6E3D96CA751AB6"' + set-cookie: + - ARRAffinity=20358cd7aa5d6b0695f01ef171fc9a95880154357830e1c6bb513b73834a2e5f;Path=/;HttpOnly;Secure;Domain=mtbatch.nam.microsofttranslator.com + - ARRAffinitySameSite=20358cd7aa5d6b0695f01ef171fc9a95880154357830e1c6bb513b73834a2e5f;Path=/;HttpOnly;SameSite=None;Secure;Domain=mtbatch.nam.microsofttranslator.com + strict-transport-security: + - max-age=31536000; includeSubDomains; preload + transfer-encoding: + - chunked + vary: + - Accept-Encoding + x-content-type-options: + - nosniff + x-powered-by: + - ASP.NET + x-requestid: + - 4d1b0a39-22de-40c8-b9b5-4d0f30c89a40 + status: + code: 200 + message: OK +version: 1 diff --git a/sdk/translation/azure-ai-translation-document/tests/recordings/test_translation.test_overloaded_single_input.yaml b/sdk/translation/azure-ai-translation-document/tests/recordings/test_translation.test_overloaded_single_input.yaml new file mode 100644 index 000000000000..265801e82c2f --- /dev/null +++ b/sdk/translation/azure-ai-translation-document/tests/recordings/test_translation.test_overloaded_single_input.yaml @@ -0,0 +1,350 @@ +interactions: +- request: + body: null + headers: + Accept: + - application/xml + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '0' + User-Agent: + - azsdk-python-storage-blob/12.9.0b1 Python/3.9.0 (Windows-10-10.0.19041-SP0) + x-ms-date: + - Wed, 02 Jun 2021 22:58:52 GMT + x-ms-version: + - '2020-08-04' + method: PUT + uri: https://redacted.blob.core.windows.net/srcabfbb430-c285-4982-a3f8-fdca62f38151?restype=container + response: + body: + string: '' + headers: + content-length: + - '0' + date: + - Wed, 02 Jun 2021 22:58:52 GMT + etag: + - '"0x8D92619FE2D3147"' + last-modified: + - Wed, 02 Jun 2021 22:58:53 GMT + server: + - Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0 + x-ms-version: + - '2020-08-04' + status: + code: 201 + message: Created +- request: + body: hello world + headers: + Accept: + - application/xml + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '11' + Content-Type: + - application/octet-stream + If-None-Match: + - '*' + User-Agent: + - azsdk-python-storage-blob/12.9.0b1 Python/3.9.0 (Windows-10-10.0.19041-SP0) + x-ms-blob-type: + - BlockBlob + x-ms-date: + - Wed, 02 Jun 2021 22:58:53 GMT + x-ms-version: + - '2020-08-04' + method: PUT + uri: https://redacted.blob.core.windows.net/srcabfbb430-c285-4982-a3f8-fdca62f38151/a27607c2-9ddc-4ca8-bfa7-5e59418d113a.txt + response: + body: + string: '' + headers: + content-length: + - '0' + content-md5: + - XrY7u+Ae7tCTyyK7j1rNww== + date: + - Wed, 02 Jun 2021 22:58:52 GMT + etag: + - '"0x8D92619FE3652F0"' + last-modified: + - Wed, 02 Jun 2021 22:58:53 GMT + server: + - Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0 + x-ms-content-crc64: + - vo7q9sPVKY0= + x-ms-request-server-encrypted: + - 'true' + x-ms-version: + - '2020-08-04' + status: + code: 201 + message: Created +- request: + body: null + headers: + Accept: + - application/xml + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '0' + User-Agent: + - azsdk-python-storage-blob/12.9.0b1 Python/3.9.0 (Windows-10-10.0.19041-SP0) + x-ms-date: + - Wed, 02 Jun 2021 22:58:53 GMT + x-ms-version: + - '2020-08-04' + method: PUT + uri: https://redacted.blob.core.windows.net/targetd0aa009e-cc16-4d9f-a031-28934319f893?restype=container + response: + body: + string: '' + headers: + content-length: + - '0' + date: + - Wed, 02 Jun 2021 22:58:52 GMT + etag: + - '"0x8D92619FE56A015"' + last-modified: + - Wed, 02 Jun 2021 22:58:53 GMT + server: + - Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0 + x-ms-version: + - '2020-08-04' + status: + code: 201 + message: Created +- request: + body: null + headers: + Accept: + - application/xml + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '0' + User-Agent: + - azsdk-python-storage-blob/12.9.0b1 Python/3.9.0 (Windows-10-10.0.19041-SP0) + x-ms-date: + - Wed, 02 Jun 2021 22:58:53 GMT + x-ms-version: + - '2020-08-04' + method: PUT + uri: https://redacted.blob.core.windows.net/target35716fda-61a9-4565-97e1-e3e207e18a76?restype=container + response: + body: + string: '' + headers: + content-length: + - '0' + date: + - Wed, 02 Jun 2021 22:58:53 GMT + etag: + - '"0x8D92619FE70BF18"' + last-modified: + - Wed, 02 Jun 2021 22:58:53 GMT + server: + - Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0 + x-ms-version: + - '2020-08-04' + status: + code: 201 + message: Created +- request: + body: '{"inputs": [{"source": {"sourceUrl": "https://redacted.blob.core.windows.net/srcabfbb430-c285-4982-a3f8-fdca62f38151?se=end&sp=rl&sv=2020-08-04&sr=c&sig=fake_token_value"}, + "targets": [{"targetUrl": "https://redacted.blob.core.windows.net/targetd0aa009e-cc16-4d9f-a031-28934319f893?se=end&sp=rw&sv=2020-08-04&sr=c&sig=fake_token_value", + "language": "es"}]}]}' + headers: + Accept: + - application/json + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '468' + Content-Type: + - application/json + User-Agent: + - azsdk-python-ai-translation-document/1.0.0b2 Python/3.9.0 (Windows-10-10.0.19041-SP0) + method: POST + uri: https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches + response: + body: + string: '' + headers: + apim-request-id: + - 1856bc71-076b-4d43-83a0-33c241433854 + content-length: + - '0' + date: + - Wed, 02 Jun 2021 22:58:53 GMT + operation-location: + - https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches/cf7dba1a-9ff0-45e0-9771-a925d302c5fc + set-cookie: + - ARRAffinity=0ff46fa6d58b00f2d44b7073721903c0eace480139d8ecfc38f61130af5e1587;Path=/;HttpOnly;Secure;Domain=mtbatch.nam.microsofttranslator.com + - ARRAffinitySameSite=0ff46fa6d58b00f2d44b7073721903c0eace480139d8ecfc38f61130af5e1587;Path=/;HttpOnly;SameSite=None;Secure;Domain=mtbatch.nam.microsofttranslator.com + strict-transport-security: + - max-age=31536000; includeSubDomains; preload + x-content-type-options: + - nosniff + x-powered-by: + - ASP.NET + x-requestid: + - 1856bc71-076b-4d43-83a0-33c241433854 + status: + code: 202 + message: Accepted +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + User-Agent: + - azsdk-python-ai-translation-document/1.0.0b2 Python/3.9.0 (Windows-10-10.0.19041-SP0) + method: GET + uri: https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches/cf7dba1a-9ff0-45e0-9771-a925d302c5fc + response: + body: + string: '{"id":"cf7dba1a-9ff0-45e0-9771-a925d302c5fc","createdDateTimeUtc":"2021-06-02T22:58:54.0566988Z","lastActionDateTimeUtc":"2021-06-02T22:59:03.0441781Z","status":"Succeeded","summary":{"total":1,"failed":0,"success":1,"inProgress":0,"notYetStarted":0,"cancelled":0,"totalCharacterCharged":11}}' + headers: + apim-request-id: + - b1f24499-6e24-44ae-b846-6cb70dea38fe + cache-control: + - public,max-age=1 + content-length: + - '292' + content-type: + - application/json; charset=utf-8 + date: + - Wed, 02 Jun 2021 22:59:24 GMT + etag: + - '"40BA1A2A41EEB38A301F70BC82E0C5DB46B5270E609061E52650200A6CA44097"' + set-cookie: + - ARRAffinity=20358cd7aa5d6b0695f01ef171fc9a95880154357830e1c6bb513b73834a2e5f;Path=/;HttpOnly;Secure;Domain=mtbatch.nam.microsofttranslator.com + - ARRAffinitySameSite=20358cd7aa5d6b0695f01ef171fc9a95880154357830e1c6bb513b73834a2e5f;Path=/;HttpOnly;SameSite=None;Secure;Domain=mtbatch.nam.microsofttranslator.com + strict-transport-security: + - max-age=31536000; includeSubDomains; preload + transfer-encoding: + - chunked + vary: + - Accept-Encoding + x-content-type-options: + - nosniff + x-powered-by: + - ASP.NET + x-requestid: + - b1f24499-6e24-44ae-b846-6cb70dea38fe + status: + code: 200 + message: OK +- request: + body: '{"inputs": [{"source": {"sourceUrl": "https://redacted.blob.core.windows.net/srcabfbb430-c285-4982-a3f8-fdca62f38151?se=end&sp=rl&sv=2020-08-04&sr=c&sig=fake_token_value"}, + "targets": [{"targetUrl": "https://redacted.blob.core.windows.net/target35716fda-61a9-4565-97e1-e3e207e18a76?se=end&sp=rw&sv=2020-08-04&sr=c&sig=fake_token_value", + "language": "es"}]}]}' + headers: + Accept: + - application/json + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '468' + Content-Type: + - application/json + User-Agent: + - azsdk-python-ai-translation-document/1.0.0b2 Python/3.9.0 (Windows-10-10.0.19041-SP0) + method: POST + uri: https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches + response: + body: + string: '' + headers: + apim-request-id: + - 273ecb97-1fbe-41d7-aefa-97256d4a33c9 + content-length: + - '0' + date: + - Wed, 02 Jun 2021 22:59:24 GMT + operation-location: + - https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches/b4d8e002-7ec0-47e8-8c5d-3cc242510c15 + set-cookie: + - ARRAffinity=0ff46fa6d58b00f2d44b7073721903c0eace480139d8ecfc38f61130af5e1587;Path=/;HttpOnly;Secure;Domain=mtbatch.nam.microsofttranslator.com + - ARRAffinitySameSite=0ff46fa6d58b00f2d44b7073721903c0eace480139d8ecfc38f61130af5e1587;Path=/;HttpOnly;SameSite=None;Secure;Domain=mtbatch.nam.microsofttranslator.com + strict-transport-security: + - max-age=31536000; includeSubDomains; preload + x-content-type-options: + - nosniff + x-powered-by: + - ASP.NET + x-requestid: + - 273ecb97-1fbe-41d7-aefa-97256d4a33c9 + status: + code: 202 + message: Accepted +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + User-Agent: + - azsdk-python-ai-translation-document/1.0.0b2 Python/3.9.0 (Windows-10-10.0.19041-SP0) + method: GET + uri: https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches/b4d8e002-7ec0-47e8-8c5d-3cc242510c15 + response: + body: + string: '{"id":"b4d8e002-7ec0-47e8-8c5d-3cc242510c15","createdDateTimeUtc":"2021-06-02T22:59:24.2566664Z","lastActionDateTimeUtc":"2021-06-02T22:59:27.0584419Z","status":"Succeeded","summary":{"total":1,"failed":0,"success":1,"inProgress":0,"notYetStarted":0,"cancelled":0,"totalCharacterCharged":11}}' + headers: + apim-request-id: + - f9485977-eb39-4416-aadd-3ac92c01628d + cache-control: + - public,max-age=1 + content-length: + - '292' + content-type: + - application/json; charset=utf-8 + date: + - Wed, 02 Jun 2021 22:59:53 GMT + etag: + - '"BE0AFF41831BE9D6CE246A4D66CE9C7B669E91652ACD0D24852308F577B80E05"' + set-cookie: + - ARRAffinity=20358cd7aa5d6b0695f01ef171fc9a95880154357830e1c6bb513b73834a2e5f;Path=/;HttpOnly;Secure;Domain=mtbatch.nam.microsofttranslator.com + - ARRAffinitySameSite=20358cd7aa5d6b0695f01ef171fc9a95880154357830e1c6bb513b73834a2e5f;Path=/;HttpOnly;SameSite=None;Secure;Domain=mtbatch.nam.microsofttranslator.com + strict-transport-security: + - max-age=31536000; includeSubDomains; preload + transfer-encoding: + - chunked + vary: + - Accept-Encoding + x-content-type-options: + - nosniff + x-powered-by: + - ASP.NET + x-requestid: + - f9485977-eb39-4416-aadd-3ac92c01628d + status: + code: 200 + message: OK +version: 1 diff --git a/sdk/translation/azure-ai-translation-document/tests/recordings/test_translation_async.test_overloaded_inputs.yaml b/sdk/translation/azure-ai-translation-document/tests/recordings/test_translation_async.test_overloaded_inputs.yaml new file mode 100644 index 000000000000..ebe01c6226e0 --- /dev/null +++ b/sdk/translation/azure-ai-translation-document/tests/recordings/test_translation_async.test_overloaded_inputs.yaml @@ -0,0 +1,284 @@ +interactions: +- request: + body: null + headers: + Accept: + - application/xml + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '0' + User-Agent: + - azsdk-python-storage-blob/12.9.0b1 Python/3.9.0 (Windows-10-10.0.19041-SP0) + x-ms-date: + - Wed, 02 Jun 2021 23:46:10 GMT + x-ms-version: + - '2020-08-04' + method: PUT + uri: https://redacted.blob.core.windows.net/srcb838ac9b-3dde-4cfc-b209-d85ac8fe84ca?restype=container + response: + body: + string: '' + headers: + content-length: + - '0' + date: + - Wed, 02 Jun 2021 23:46:09 GMT + etag: + - '"0x8D926209967C583"' + last-modified: + - Wed, 02 Jun 2021 23:46:10 GMT + server: + - Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0 + x-ms-version: + - '2020-08-04' + status: + code: 201 + message: Created +- request: + body: hello world + headers: + Accept: + - application/xml + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '11' + Content-Type: + - application/octet-stream + If-None-Match: + - '*' + User-Agent: + - azsdk-python-storage-blob/12.9.0b1 Python/3.9.0 (Windows-10-10.0.19041-SP0) + x-ms-blob-type: + - BlockBlob + x-ms-date: + - Wed, 02 Jun 2021 23:46:10 GMT + x-ms-version: + - '2020-08-04' + method: PUT + uri: https://redacted.blob.core.windows.net/srcb838ac9b-3dde-4cfc-b209-d85ac8fe84ca/68fcfd56-4124-485f-9051-1eb90b89cd47.txt + response: + body: + string: '' + headers: + content-length: + - '0' + content-md5: + - XrY7u+Ae7tCTyyK7j1rNww== + date: + - Wed, 02 Jun 2021 23:46:09 GMT + etag: + - '"0x8D926209972E400"' + last-modified: + - Wed, 02 Jun 2021 23:46:10 GMT + server: + - Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0 + x-ms-content-crc64: + - vo7q9sPVKY0= + x-ms-request-server-encrypted: + - 'true' + x-ms-version: + - '2020-08-04' + status: + code: 201 + message: Created +- request: + body: null + headers: + Accept: + - application/xml + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '0' + User-Agent: + - azsdk-python-storage-blob/12.9.0b1 Python/3.9.0 (Windows-10-10.0.19041-SP0) + x-ms-date: + - Wed, 02 Jun 2021 23:46:10 GMT + x-ms-version: + - '2020-08-04' + method: PUT + uri: https://redacted.blob.core.windows.net/targetd403708a-5d48-406e-bebb-fffecc9ea98e?restype=container + response: + body: + string: '' + headers: + content-length: + - '0' + date: + - Wed, 02 Jun 2021 23:46:09 GMT + etag: + - '"0x8D926209990CF39"' + last-modified: + - Wed, 02 Jun 2021 23:46:10 GMT + server: + - Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0 + x-ms-version: + - '2020-08-04' + status: + code: 201 + message: Created +- request: + body: null + headers: + Accept: + - application/xml + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '0' + User-Agent: + - azsdk-python-storage-blob/12.9.0b1 Python/3.9.0 (Windows-10-10.0.19041-SP0) + x-ms-date: + - Wed, 02 Jun 2021 23:46:10 GMT + x-ms-version: + - '2020-08-04' + method: PUT + uri: https://redacted.blob.core.windows.net/target28a96862-d588-47db-a4f0-1a6491078379?restype=container + response: + body: + string: '' + headers: + content-length: + - '0' + date: + - Wed, 02 Jun 2021 23:46:10 GMT + etag: + - '"0x8D9262099ABDA6F"' + last-modified: + - Wed, 02 Jun 2021 23:46:10 GMT + server: + - Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0 + x-ms-version: + - '2020-08-04' + status: + code: 201 + message: Created +- request: + body: '{"inputs": [{"source": {"sourceUrl": "https://redacted.blob.core.windows.net/srcb838ac9b-3dde-4cfc-b209-d85ac8fe84ca?se=end&sp=rl&sv=2020-08-04&sr=c&sig=fake_token_value", + "filter": {}}, "targets": [{"targetUrl": "https://redacted.blob.core.windows.net/targetd403708a-5d48-406e-bebb-fffecc9ea98e?se=end&sp=rw&sv=2020-08-04&sr=c&sig=fake_token_value", + "language": "es"}]}]}' + headers: + Accept: + - application/json + Content-Length: + - '486' + Content-Type: + - application/json + User-Agent: + - azsdk-python-ai-translation-document/1.0.0b2 Python/3.9.0 (Windows-10-10.0.19041-SP0) + method: POST + uri: https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches + response: + body: + string: '' + headers: + apim-request-id: ff8db814-1ddc-40a8-a3fa-8f3a1334b3f7 + content-length: '0' + date: Wed, 02 Jun 2021 23:46:15 GMT + operation-location: https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches/644a1ccc-6288-44ad-9846-1c35cb2a4d80 + set-cookie: ARRAffinitySameSite=20358cd7aa5d6b0695f01ef171fc9a95880154357830e1c6bb513b73834a2e5f;Path=/;HttpOnly;SameSite=None;Secure;Domain=mtbatch.nam.microsofttranslator.com + strict-transport-security: max-age=31536000; includeSubDomains; preload + x-content-type-options: nosniff + x-powered-by: ASP.NET + x-requestid: ff8db814-1ddc-40a8-a3fa-8f3a1334b3f7 + status: + code: 202 + message: Accepted + url: https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches +- request: + body: null + headers: + User-Agent: + - azsdk-python-ai-translation-document/1.0.0b2 Python/3.9.0 (Windows-10-10.0.19041-SP0) + method: GET + uri: https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches/644a1ccc-6288-44ad-9846-1c35cb2a4d80 + response: + body: + string: '{"id":"644a1ccc-6288-44ad-9846-1c35cb2a4d80","createdDateTimeUtc":"2021-06-02T23:46:14.1494102Z","lastActionDateTimeUtc":"2021-06-02T23:46:30.3991413Z","status":"Succeeded","summary":{"total":1,"failed":0,"success":1,"inProgress":0,"notYetStarted":0,"cancelled":0,"totalCharacterCharged":11}}' + headers: + apim-request-id: 6050c6d1-f17d-4f2b-b3a7-46b7cffe89af + cache-control: public,max-age=1 + content-type: application/json; charset=utf-8 + date: Wed, 02 Jun 2021 23:46:46 GMT + etag: '"FFDEA3CC84AAFD6284CB20EF0BB49E2C7CC4F2ADD308EC0126951F679F92B809"' + set-cookie: ARRAffinitySameSite=20358cd7aa5d6b0695f01ef171fc9a95880154357830e1c6bb513b73834a2e5f;Path=/;HttpOnly;SameSite=None;Secure;Domain=mtbatch.nam.microsofttranslator.com + strict-transport-security: max-age=31536000; includeSubDomains; preload + transfer-encoding: chunked + vary: Accept-Encoding + x-content-type-options: nosniff + x-powered-by: ASP.NET + x-requestid: 6050c6d1-f17d-4f2b-b3a7-46b7cffe89af + status: + code: 200 + message: OK + url: https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches/644a1ccc-6288-44ad-9846-1c35cb2a4d80 +- request: + body: '{"inputs": [{"source": {"sourceUrl": "https://redacted.blob.core.windows.net/srcb838ac9b-3dde-4cfc-b209-d85ac8fe84ca?se=end&sp=rl&sv=2020-08-04&sr=c&sig=fake_token_value", + "filter": {}}, "targets": [{"targetUrl": "https://redacted.blob.core.windows.net/target28a96862-d588-47db-a4f0-1a6491078379?se=end&sp=rw&sv=2020-08-04&sr=c&sig=fake_token_value", + "language": "es"}]}]}' + headers: + Accept: + - application/json + Content-Length: + - '484' + Content-Type: + - application/json + User-Agent: + - azsdk-python-ai-translation-document/1.0.0b2 Python/3.9.0 (Windows-10-10.0.19041-SP0) + method: POST + uri: https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches + response: + body: + string: '' + headers: + apim-request-id: 3d1e0612-88ac-4eea-a403-09e765d5e7f5 + content-length: '0' + date: Wed, 02 Jun 2021 23:46:50 GMT + operation-location: https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches/71e2e4c7-7a17-4744-9791-f73e1b06e6c1 + set-cookie: ARRAffinitySameSite=0ff46fa6d58b00f2d44b7073721903c0eace480139d8ecfc38f61130af5e1587;Path=/;HttpOnly;SameSite=None;Secure;Domain=mtbatch.nam.microsofttranslator.com + strict-transport-security: max-age=31536000; includeSubDomains; preload + x-content-type-options: nosniff + x-powered-by: ASP.NET + x-requestid: 3d1e0612-88ac-4eea-a403-09e765d5e7f5 + status: + code: 202 + message: Accepted + url: https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches +- request: + body: null + headers: + User-Agent: + - azsdk-python-ai-translation-document/1.0.0b2 Python/3.9.0 (Windows-10-10.0.19041-SP0) + method: GET + uri: https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches/71e2e4c7-7a17-4744-9791-f73e1b06e6c1 + response: + body: + string: '{"id":"71e2e4c7-7a17-4744-9791-f73e1b06e6c1","createdDateTimeUtc":"2021-06-02T23:46:49.3321147Z","lastActionDateTimeUtc":"2021-06-02T23:46:55.3029194Z","status":"Succeeded","summary":{"total":1,"failed":0,"success":1,"inProgress":0,"notYetStarted":0,"cancelled":0,"totalCharacterCharged":11}}' + headers: + apim-request-id: ee6a5e42-196d-426b-b14c-3cfa0810bf8e + cache-control: public,max-age=1 + content-type: application/json; charset=utf-8 + date: Wed, 02 Jun 2021 23:47:20 GMT + etag: '"11ED02A36FA9D05491F69F8E2691820F3E0CFEE4B217A17EEEDD42506A045431"' + set-cookie: ARRAffinitySameSite=20358cd7aa5d6b0695f01ef171fc9a95880154357830e1c6bb513b73834a2e5f;Path=/;HttpOnly;SameSite=None;Secure;Domain=mtbatch.nam.microsofttranslator.com + strict-transport-security: max-age=31536000; includeSubDomains; preload + transfer-encoding: chunked + vary: Accept-Encoding + x-content-type-options: nosniff + x-powered-by: ASP.NET + x-requestid: ee6a5e42-196d-426b-b14c-3cfa0810bf8e + status: + code: 200 + message: OK + url: https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches/71e2e4c7-7a17-4744-9791-f73e1b06e6c1 +version: 1 diff --git a/sdk/translation/azure-ai-translation-document/tests/recordings/test_translation_async.test_overloaded_single_input.yaml b/sdk/translation/azure-ai-translation-document/tests/recordings/test_translation_async.test_overloaded_single_input.yaml new file mode 100644 index 000000000000..c29a2a557164 --- /dev/null +++ b/sdk/translation/azure-ai-translation-document/tests/recordings/test_translation_async.test_overloaded_single_input.yaml @@ -0,0 +1,284 @@ +interactions: +- request: + body: null + headers: + Accept: + - application/xml + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '0' + User-Agent: + - azsdk-python-storage-blob/12.9.0b1 Python/3.9.0 (Windows-10-10.0.19041-SP0) + x-ms-date: + - Wed, 02 Jun 2021 23:47:30 GMT + x-ms-version: + - '2020-08-04' + method: PUT + uri: https://redacted.blob.core.windows.net/srcfda51b71-07e2-4b49-9a43-7e9c04337bf1?restype=container + response: + body: + string: '' + headers: + content-length: + - '0' + date: + - Wed, 02 Jun 2021 23:47:31 GMT + etag: + - '"0x8D92620C98B75C0"' + last-modified: + - Wed, 02 Jun 2021 23:47:31 GMT + server: + - Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0 + x-ms-version: + - '2020-08-04' + status: + code: 201 + message: Created +- request: + body: hello world + headers: + Accept: + - application/xml + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '11' + Content-Type: + - application/octet-stream + If-None-Match: + - '*' + User-Agent: + - azsdk-python-storage-blob/12.9.0b1 Python/3.9.0 (Windows-10-10.0.19041-SP0) + x-ms-blob-type: + - BlockBlob + x-ms-date: + - Wed, 02 Jun 2021 23:47:31 GMT + x-ms-version: + - '2020-08-04' + method: PUT + uri: https://redacted.blob.core.windows.net/srcfda51b71-07e2-4b49-9a43-7e9c04337bf1/947afc20-9f24-4597-8608-e283c6862dc9.txt + response: + body: + string: '' + headers: + content-length: + - '0' + content-md5: + - XrY7u+Ae7tCTyyK7j1rNww== + date: + - Wed, 02 Jun 2021 23:47:31 GMT + etag: + - '"0x8D92620C99838F7"' + last-modified: + - Wed, 02 Jun 2021 23:47:31 GMT + server: + - Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0 + x-ms-content-crc64: + - vo7q9sPVKY0= + x-ms-request-server-encrypted: + - 'true' + x-ms-version: + - '2020-08-04' + status: + code: 201 + message: Created +- request: + body: null + headers: + Accept: + - application/xml + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '0' + User-Agent: + - azsdk-python-storage-blob/12.9.0b1 Python/3.9.0 (Windows-10-10.0.19041-SP0) + x-ms-date: + - Wed, 02 Jun 2021 23:47:31 GMT + x-ms-version: + - '2020-08-04' + method: PUT + uri: https://redacted.blob.core.windows.net/target0f3fbfa5-df59-480f-b28c-95d4ce876518?restype=container + response: + body: + string: '' + headers: + content-length: + - '0' + date: + - Wed, 02 Jun 2021 23:47:30 GMT + etag: + - '"0x8D92620C9B665C8"' + last-modified: + - Wed, 02 Jun 2021 23:47:31 GMT + server: + - Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0 + x-ms-version: + - '2020-08-04' + status: + code: 201 + message: Created +- request: + body: null + headers: + Accept: + - application/xml + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '0' + User-Agent: + - azsdk-python-storage-blob/12.9.0b1 Python/3.9.0 (Windows-10-10.0.19041-SP0) + x-ms-date: + - Wed, 02 Jun 2021 23:47:31 GMT + x-ms-version: + - '2020-08-04' + method: PUT + uri: https://redacted.blob.core.windows.net/target04111e6a-2127-4d83-9a3a-eb0b2f9bafda?restype=container + response: + body: + string: '' + headers: + content-length: + - '0' + date: + - Wed, 02 Jun 2021 23:47:30 GMT + etag: + - '"0x8D92620C9D1973B"' + last-modified: + - Wed, 02 Jun 2021 23:47:31 GMT + server: + - Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0 + x-ms-version: + - '2020-08-04' + status: + code: 201 + message: Created +- request: + body: '{"inputs": [{"source": {"sourceUrl": "https://redacted.blob.core.windows.net/srcfda51b71-07e2-4b49-9a43-7e9c04337bf1?se=end&sp=rl&sv=2020-08-04&sr=c&sig=fake_token_value"}, + "targets": [{"targetUrl": "https://redacted.blob.core.windows.net/target0f3fbfa5-df59-480f-b28c-95d4ce876518?se=end&sp=rw&sv=2020-08-04&sr=c&sig=fake_token_value", + "language": "es"}]}]}' + headers: + Accept: + - application/json + Content-Length: + - '470' + Content-Type: + - application/json + User-Agent: + - azsdk-python-ai-translation-document/1.0.0b2 Python/3.9.0 (Windows-10-10.0.19041-SP0) + method: POST + uri: https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches + response: + body: + string: '' + headers: + apim-request-id: 1e9d4698-32e1-42f8-8286-e785311d0fd0 + content-length: '0' + date: Wed, 02 Jun 2021 23:47:31 GMT + operation-location: https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches/43c8ca8f-c00a-4eca-83a5-d9fbec238c80 + set-cookie: ARRAffinitySameSite=20358cd7aa5d6b0695f01ef171fc9a95880154357830e1c6bb513b73834a2e5f;Path=/;HttpOnly;SameSite=None;Secure;Domain=mtbatch.nam.microsofttranslator.com + strict-transport-security: max-age=31536000; includeSubDomains; preload + x-content-type-options: nosniff + x-powered-by: ASP.NET + x-requestid: 1e9d4698-32e1-42f8-8286-e785311d0fd0 + status: + code: 202 + message: Accepted + url: https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches +- request: + body: null + headers: + User-Agent: + - azsdk-python-ai-translation-document/1.0.0b2 Python/3.9.0 (Windows-10-10.0.19041-SP0) + method: GET + uri: https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches/43c8ca8f-c00a-4eca-83a5-d9fbec238c80 + response: + body: + string: '{"id":"43c8ca8f-c00a-4eca-83a5-d9fbec238c80","createdDateTimeUtc":"2021-06-02T23:47:31.8876228Z","lastActionDateTimeUtc":"2021-06-02T23:47:39.2609731Z","status":"Succeeded","summary":{"total":1,"failed":0,"success":1,"inProgress":0,"notYetStarted":0,"cancelled":0,"totalCharacterCharged":11}}' + headers: + apim-request-id: 15a10118-fc08-47f7-86cc-0a8952057509 + cache-control: public,max-age=1 + content-type: application/json; charset=utf-8 + date: Wed, 02 Jun 2021 23:48:02 GMT + etag: '"AC3B93C5DF30D8BAC4744197501D0150B45AD3043C0C512225FD1FD467CCADCA"' + set-cookie: ARRAffinitySameSite=0ff46fa6d58b00f2d44b7073721903c0eace480139d8ecfc38f61130af5e1587;Path=/;HttpOnly;SameSite=None;Secure;Domain=mtbatch.nam.microsofttranslator.com + strict-transport-security: max-age=31536000; includeSubDomains; preload + transfer-encoding: chunked + vary: Accept-Encoding + x-content-type-options: nosniff + x-powered-by: ASP.NET + x-requestid: 15a10118-fc08-47f7-86cc-0a8952057509 + status: + code: 200 + message: OK + url: https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches/43c8ca8f-c00a-4eca-83a5-d9fbec238c80 +- request: + body: '{"inputs": [{"source": {"sourceUrl": "https://redacted.blob.core.windows.net/srcfda51b71-07e2-4b49-9a43-7e9c04337bf1?se=end&sp=rl&sv=2020-08-04&sr=c&sig=fake_token_value"}, + "targets": [{"targetUrl": "https://redacted.blob.core.windows.net/target04111e6a-2127-4d83-9a3a-eb0b2f9bafda?se=end&sp=rw&sv=2020-08-04&sr=c&sig=fake_token_value", + "language": "es"}]}]}' + headers: + Accept: + - application/json + Content-Length: + - '474' + Content-Type: + - application/json + User-Agent: + - azsdk-python-ai-translation-document/1.0.0b2 Python/3.9.0 (Windows-10-10.0.19041-SP0) + method: POST + uri: https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches + response: + body: + string: '' + headers: + apim-request-id: c6287479-a86d-41b0-bdaf-a3757648e23b + content-length: '0' + date: Wed, 02 Jun 2021 23:48:02 GMT + operation-location: https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches/9c948200-c8f9-4d92-91c8-84ad2ed5445d + set-cookie: ARRAffinitySameSite=20358cd7aa5d6b0695f01ef171fc9a95880154357830e1c6bb513b73834a2e5f;Path=/;HttpOnly;SameSite=None;Secure;Domain=mtbatch.nam.microsofttranslator.com + strict-transport-security: max-age=31536000; includeSubDomains; preload + x-content-type-options: nosniff + x-powered-by: ASP.NET + x-requestid: c6287479-a86d-41b0-bdaf-a3757648e23b + status: + code: 202 + message: Accepted + url: https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches +- request: + body: null + headers: + User-Agent: + - azsdk-python-ai-translation-document/1.0.0b2 Python/3.9.0 (Windows-10-10.0.19041-SP0) + method: GET + uri: https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches/9c948200-c8f9-4d92-91c8-84ad2ed5445d + response: + body: + string: '{"id":"9c948200-c8f9-4d92-91c8-84ad2ed5445d","createdDateTimeUtc":"2021-06-02T23:48:02.7925364Z","lastActionDateTimeUtc":"2021-06-02T23:48:04.4151852Z","status":"Succeeded","summary":{"total":1,"failed":0,"success":1,"inProgress":0,"notYetStarted":0,"cancelled":0,"totalCharacterCharged":11}}' + headers: + apim-request-id: b2922600-7d31-419b-8f36-ae276252eb47 + cache-control: public,max-age=1 + content-type: application/json; charset=utf-8 + date: Wed, 02 Jun 2021 23:48:32 GMT + etag: '"A0F4CD3EEB32A146AB8FC8A0B18EF8E17C1CF44BA7E7089E7617844A4BF73C6E"' + set-cookie: ARRAffinitySameSite=0ff46fa6d58b00f2d44b7073721903c0eace480139d8ecfc38f61130af5e1587;Path=/;HttpOnly;SameSite=None;Secure;Domain=mtbatch.nam.microsofttranslator.com + strict-transport-security: max-age=31536000; includeSubDomains; preload + transfer-encoding: chunked + vary: Accept-Encoding + x-content-type-options: nosniff + x-powered-by: ASP.NET + x-requestid: b2922600-7d31-419b-8f36-ae276252eb47 + status: + code: 200 + message: OK + url: https://redacted.cognitiveservices.azure.com/translator/text/batch/v1.0/batches/9c948200-c8f9-4d92-91c8-84ad2ed5445d +version: 1 diff --git a/sdk/translation/azure-ai-translation-document/tests/test_translation.py b/sdk/translation/azure-ai-translation-document/tests/test_translation.py index 3bef3f346fb1..22789262d54c 100644 --- a/sdk/translation/azure-ai-translation-document/tests/test_translation.py +++ b/sdk/translation/azure-ai-translation-document/tests/test_translation.py @@ -346,3 +346,81 @@ def test_empty_document(self, client): for doc in result: assert doc.status == "Failed" assert doc.error.code == "WrongDocumentEncoding" + + @DocumentTranslationPreparer() + @DocumentTranslationClientPreparer() + def test_overloaded_inputs(self, client): + # prepare containers and test data + source_container_sas_url = self.create_source_container(data=Document(data=b'hello world')) + target_container_sas_url = self.create_target_container() + target_container_sas_url_2 = self.create_target_container() + + # prepare translation inputs + translation_inputs = [ + DocumentTranslationInput( + source_url=source_container_sas_url, + targets=[ + TranslationTarget( + target_url=target_container_sas_url, + language_code="es" + ) + ] + ) + ] + + + # positional + poller = client.begin_translation(translation_inputs) + result = poller.result() + self._validate_translation_metadata(poller, status="Succeeded", total=1, succeeded=1) + + # keyword + translation_inputs[0].targets[0].target_url = target_container_sas_url_2 + poller = client.begin_translation(inputs=translation_inputs) + result = poller.result() + self._validate_translation_metadata(poller, status="Succeeded", total=1, succeeded=1) + + @DocumentTranslationPreparer() + @DocumentTranslationClientPreparer() + def test_overloaded_single_input(self, client): + # prepare containers and test data + source_container_sas_url = self.create_source_container(data=Document(data=b'hello world')) + target_container_sas_url = self.create_target_container() + target_container_sas_url_2 = self.create_target_container() + + # positional + poller = client.begin_translation(source_container_sas_url, target_container_sas_url, "es") + result = poller.result() + self._validate_translation_metadata(poller, status="Succeeded", total=1, succeeded=1) + + # keyword + poller = client.begin_translation(source_url=source_container_sas_url, target_url=target_container_sas_url_2, target_language_code="es") + result = poller.result() + self._validate_translation_metadata(poller, status="Succeeded", total=1, succeeded=1) + + @DocumentTranslationPreparer() + @DocumentTranslationClientPreparer() + def test_overloaded_bad_input(self, client): + translation_inputs = [ + DocumentTranslationInput( + source_url="container", + targets=[ + TranslationTarget( + target_url="container", + language_code="es" + ) + ] + ) + ] + + with pytest.raises(ValueError): + client.begin_translation("container") + + with pytest.raises(ValueError): + client.begin_translation("container", "container") + + with pytest.raises(ValueError): + client.begin_translation(source_url=translation_inputs) + + with pytest.raises(ValueError): + client.begin_translation(inputs="container") diff --git a/sdk/translation/azure-ai-translation-document/tests/test_translation_async.py b/sdk/translation/azure-ai-translation-document/tests/test_translation_async.py index 4dbbcb41536a..53f7f7a14fe5 100644 --- a/sdk/translation/azure-ai-translation-document/tests/test_translation_async.py +++ b/sdk/translation/azure-ai-translation-document/tests/test_translation_async.py @@ -348,3 +348,81 @@ async def test_empty_document(self, client): async for doc in result: assert doc.status == "Failed" assert doc.error.code == "WrongDocumentEncoding" + + @DocumentTranslationPreparer() + @DocumentTranslationClientPreparer() + async def test_overloaded_inputs(self, client): + # prepare containers and test data + source_container_sas_url = self.create_source_container(data=Document(data=b'hello world')) + target_container_sas_url = self.create_target_container() + target_container_sas_url_2 = self.create_target_container() + + # prepare translation inputs + translation_inputs = [ + DocumentTranslationInput( + source_url=source_container_sas_url, + targets=[ + TranslationTarget( + target_url=target_container_sas_url, + language_code="es" + ) + ] + ) + ] + + + # positional + poller = await client.begin_translation(translation_inputs) + result = await poller.result() + self._validate_translation_metadata(poller, status="Succeeded", total=1, succeeded=1) + + # keyword + translation_inputs[0].targets[0].target_url = target_container_sas_url_2 + poller = await client.begin_translation(inputs=translation_inputs) + result = await poller.result() + self._validate_translation_metadata(poller, status="Succeeded", total=1, succeeded=1) + + @DocumentTranslationPreparer() + @DocumentTranslationClientPreparer() + async def test_overloaded_single_input(self, client): + # prepare containers and test data + source_container_sas_url = self.create_source_container(data=Document(data=b'hello world')) + target_container_sas_url = self.create_target_container() + target_container_sas_url_2 = self.create_target_container() + + # positional + poller = await client.begin_translation(source_container_sas_url, target_container_sas_url, "es") + result = await poller.result() + self._validate_translation_metadata(poller, status="Succeeded", total=1, succeeded=1) + + # keyword + poller = await client.begin_translation(source_url=source_container_sas_url, target_url=target_container_sas_url_2, target_language_code="es") + result = await poller.result() + self._validate_translation_metadata(poller, status="Succeeded", total=1, succeeded=1) + + @DocumentTranslationPreparer() + @DocumentTranslationClientPreparer() + async def test_overloaded_bad_input(self, client): + translation_inputs = [ + DocumentTranslationInput( + source_url="container", + targets=[ + TranslationTarget( + target_url="container", + language_code="es" + ) + ] + ) + ] + + with pytest.raises(ValueError): + await client.begin_translation("container") + + with pytest.raises(ValueError): + await client.begin_translation("container", "container") + + with pytest.raises(ValueError): + await client.begin_translation(source_url=translation_inputs) + + with pytest.raises(ValueError): + await client.begin_translation(inputs="container")