From 3d9b5c6ef61544a0c59e899266db17494ef2768d Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Wed, 3 Mar 2021 23:06:10 +0200 Subject: [PATCH 1/5] [client] modify the 'create_translation_job()' method --- .../azure/ai/documenttranslation/_client.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index e48ec7f1148b..6f29e33732df 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -55,12 +55,20 @@ def create_translation_job(self, batch, **kwargs): :rtype: JobStatusDetail """ - return self._client.document_translation.begin_submit_batch_request( + # submit translation job + response_headers = self._client.document_translation._submit_batch_request_initial( inputs=batch, - polling=True, + cls=lambda x,y,z: z, **kwargs ) + # get job id from response header + job_id = response_headers['Operation-Location'] + + # call + return self.get_job_status(job_id) + + @distributed_trace def get_job_status(self, job_id, **kwargs): # type: (str, **Any) -> JobStatusDetail From 27f62c116f943f6a60c78f989808924d76bd9489 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Thu, 4 Mar 2021 18:21:30 +0200 Subject: [PATCH 2/5] [models wrapping 'JobStatusDetail' --- .../azure/ai/documenttranslation/_models.py | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py index e5928093c103..c55e2d4a9817 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py @@ -143,6 +143,23 @@ def __init__( self.documents_cancelled_count = kwargs.get('documents_cancelled_count', None) self.total_characters_charged = kwargs.get('total_characters_charged', None) + @classmethod + def _from_generated(cls, batch_status_details): + return cls( + id=batch_status_details.id, + created_on=batch_status_details.created_date_time_utc, + last_updated_on=batch_status_details.last_action_date_time_utc, + status=batch_status_details.status, + error=DocumentTranslationError._from_generated(batch_status_details.error), + documents_total_count=batch_status_details.summary.total, + documents_failed_count=batch_status_details.summary.failed, + documents_succeeded_count=batch_status_details.summary.success, + documents_in_progress_count=batch_status_details.summary.in_progress, + documents_not_yet_started_count=batch_status_details.summary.not_yet_started, + documents_cancelled_count=batch_status_details.summary.cancelled, + total_characters_charged=batch_status_details.summary.total_character_charged + ) + class DocumentStatusDetail(object): """DocumentStatusDetail. @@ -210,6 +227,14 @@ def __init__( self.message = None self.target = None + @classmethod + def _from_generated(cls, error): + return cls( + code=error.code, + message=error.message, + target=error.target + ) + class FileFormat(object): """FileFormat. From b1c82d8aeb419fd2508e028b366af9a79066df63 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Thu, 4 Mar 2021 18:25:12 +0200 Subject: [PATCH 3/5] [models wrapping] update client --- .../azure/ai/documenttranslation/_client.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index 6f29e33732df..6475ea4ee460 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -65,7 +65,7 @@ def create_translation_job(self, batch, **kwargs): # get job id from response header job_id = response_headers['Operation-Location'] - # call + # get job status return self.get_job_status(job_id) @@ -79,7 +79,8 @@ def get_job_status(self, job_id, **kwargs): :rtype: ~azure.ai.documenttranslation.JobStatusDetail """ - return self._client.document_translation.get_operation_status(job_id, **kwargs) + job_status = self._client.document_translation.get_operation_status(job_id, **kwargs) + return JobStatusDetail._from_generated(job_status) @distributed_trace def cancel_job(self, job_id, **kwargs): From 48bddc8c09cdfbd06c44a7e16b050c5980588b2f Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Thu, 4 Mar 2021 19:36:27 +0200 Subject: [PATCH 4/5] [models wrapping] formats mapping [documents, glossaries, storage] --- .../azure/ai/documenttranslation/_client.py | 9 ++++++--- .../azure/ai/documenttranslation/_models.py | 13 +++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index 6475ea4ee460..b51095feb725 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -151,7 +151,8 @@ def get_supported_storage_sources(self, **kwargs): :rtype: List[str] """ - return self._client.document_translation.get_document_storage_source(**kwargs) + result = self._client.document_translation.get_document_storage_source(**kwargs) + return result.value @distributed_trace def get_supported_glossary_formats(self, **kwargs): @@ -161,7 +162,8 @@ def get_supported_glossary_formats(self, **kwargs): :rtype: List[FileFormat] """ - return self._client.document_translation.get_glossary_formats(**kwargs) + glossary_formats = self._client.document_translation.get_glossary_formats(**kwargs) + return FileFormat._from_generated_list(glossary_formats) @distributed_trace def get_supported_document_formats(self, **kwargs): @@ -171,4 +173,5 @@ def get_supported_document_formats(self, **kwargs): :rtype: List[FileFormat] """ - return self._client.document_translation.get_document_formats(**kwargs) + document_formats = self._client.document_translation.get_document_formats(**kwargs) + return FileFormat._from_generated_list(document_formats) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py index c55e2d4a9817..99773889a648 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py @@ -258,3 +258,16 @@ def __init__( self.file_extensions = kwargs.get('file_extensions', None) self.content_types = kwargs.get('content_types', None) self.versions = kwargs.get('versions', None) + + @classmethod + def _from_generated(cls, file_format): + return cls( + format=file_format.format, + file_extentions=file_format.file_extentions, + content_types=file_format.content_types, + versions=file_format.versions + ) + + @classmethod + def _from_generated_list(cls, file_formats): + return list( [ FileFormat._from_generated(file_formats) for file_formats in file_formats ] ) From 6153f284cebf74ab6f819d47290c95c17a11ce00 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Thu, 4 Mar 2021 20:51:33 +0200 Subject: [PATCH 5/5] [models wrapping] batch document input --- .../azure/ai/documenttranslation/_client.py | 4 +- .../azure/ai/documenttranslation/_models.py | 86 ++++++++++++++++--- 2 files changed, 76 insertions(+), 14 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index b51095feb725..75645df41cf7 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -57,8 +57,8 @@ def create_translation_job(self, batch, **kwargs): # submit translation job response_headers = self._client.document_translation._submit_batch_request_initial( - inputs=batch, - cls=lambda x,y,z: z, + inputs = BatchDocumentInput._to_generated_list(batch), + cls = lambda x,y,z: z, **kwargs ) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py index 99773889a648..6159a0badd71 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py @@ -6,6 +6,14 @@ from typing import Any, List +from ._generated.models import ( + BatchRequest as _BatchRequest, + SourceInput as _SourceInput, + DocumentFilter as _DocumentFilter, + TargetInput as _TargetInput, + Glossary as _Glossary +) + class TranslationGlossary(object): """Glossary / translation memory for the request. @@ -32,6 +40,27 @@ def __init__( self.format_version = kwargs.get("format_version", None) self.storage_source = kwargs.get("storage_source", None) + @classmethod + def _to_generated_list(cls, glossaries): + result = list(_Glossary) + for glossary in glossaries: + if isinstance(TranslationGlossary): + result.append( + _Glossary( + glossary_url = glossary.glossary_url, + format = glossary.format, + version = glossary.version, + storage_source = glossary.storage_source + ) + ) + elif isinstance(str): + result.append( + _Glossary( + glossary_url = glossary, + ) + ) + return result + class StorageTarget(object): """Destination for the finished translated documents. @@ -60,6 +89,19 @@ def __init__( self.glossaries = kwargs.get("glossaries", None) self.storage_source = kwargs.get("storage_source", None) + @classmethod + def _to_generated_list(cls, targets): + return [ + _TargetInput( + target_url = target.target_url, + category = target.category_id, + language = target.language, + storage_source = target.storage_source, + glossaries = TranslationGlossary._to_generated_list(target.glossaries) + ) + for target in targets + ] + class BatchDocumentInput(object): """Definition for the input batch translation request. @@ -97,6 +139,26 @@ def __init__( self.prefix = kwargs.get("prefix", None) self.suffix = kwargs.get("suffix", None) + @classmethod + def _to_generated_list(cls, batch_document_inputs): + return [ + _BatchRequest( + source = _SourceInput( + source_url = batch_document_input.source_url, + filter = _DocumentFilter( + prefix = batch_document_input.prefix, + suffix = batch_document_input.suffix + ), + language = batch_document_input.source_language, + storage_source = batch_document_input.storage_source + ), + targets = StorageTarget._to_generated_list(batch_document_input.targets), + storage_type = batch_document_input.storage_type + ) + for batch_document_input in batch_document_inputs + ] + + class JobStatusDetail(object): """Job status response. @@ -146,18 +208,18 @@ def __init__( @classmethod def _from_generated(cls, batch_status_details): return cls( - id=batch_status_details.id, - created_on=batch_status_details.created_date_time_utc, - last_updated_on=batch_status_details.last_action_date_time_utc, - status=batch_status_details.status, - error=DocumentTranslationError._from_generated(batch_status_details.error), - documents_total_count=batch_status_details.summary.total, - documents_failed_count=batch_status_details.summary.failed, - documents_succeeded_count=batch_status_details.summary.success, - documents_in_progress_count=batch_status_details.summary.in_progress, - documents_not_yet_started_count=batch_status_details.summary.not_yet_started, - documents_cancelled_count=batch_status_details.summary.cancelled, - total_characters_charged=batch_status_details.summary.total_character_charged + id = batch_status_details.id, + created_on = batch_status_details.created_date_time_utc, + last_updated_on = batch_status_details.last_action_date_time_utc, + status = batch_status_details.status, + error = DocumentTranslationError._from_generated(batch_status_details.error), + documents_total_count = batch_status_details.summary.total, + documents_failed_count = batch_status_details.summary.failed, + documents_succeeded_count = batch_status_details.summary.success, + documents_in_progress_count = batch_status_details.summary.in_progress, + documents_not_yet_started_count = batch_status_details.summary.not_yet_started, + documents_cancelled_count = batch_status_details.summary.cancelled, + total_characters_charged = batch_status_details.summary.total_character_charged )