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..75645df41cf7 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( - inputs=batch, - polling=True, + # submit translation job + response_headers = self._client.document_translation._submit_batch_request_initial( + inputs = BatchDocumentInput._to_generated_list(batch), + cls = lambda x,y,z: z, **kwargs ) + # get job id from response header + job_id = response_headers['Operation-Location'] + + # get job status + return self.get_job_status(job_id) + + @distributed_trace def get_job_status(self, job_id, **kwargs): # type: (str, **Any) -> JobStatusDetail @@ -71,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): @@ -142,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): @@ -152,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): @@ -162,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 e5928093c103..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. @@ -143,6 +205,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 +289,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. @@ -233,3 +320,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 ] )