Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

models wrapping + sdk integration with generated client #17071

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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']
mshaban-msft marked this conversation as resolved.
Show resolved Hide resolved

# get job status
return self.get_job_status(job_id)


@distributed_trace
def get_job_status(self, job_id, **kwargs):
# type: (str, **Any) -> JobStatusDetail
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it work to fold the helper methods into one like this?

return [FileFormat._from_generated(glossary_format) for glossary_format in glossary_formats.value]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i used to do it inline, but i thought it's much cleaner if i created "_from_generated_list()" to process lists
instead of having to same code for the same object over and over


@distributed_trace
def get_supported_document_formats(self, **kwargs):
Expand All @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -32,6 +40,27 @@ def __init__(
self.format_version = kwargs.get("format_version", None)
self.storage_source = kwargs.get("storage_source", None)

@classmethod
mshaban-msft marked this conversation as resolved.
Show resolved Hide resolved
def _to_generated_list(cls, glossaries):
result = list(_Glossary)
mshaban-msft marked this conversation as resolved.
Show resolved Hide resolved
for glossary in glossaries:
if isinstance(TranslationGlossary):
result.append(
_Glossary(
glossary_url = glossary.glossary_url,
format = glossary.format,
mshaban-msft marked this conversation as resolved.
Show resolved Hide resolved
version = glossary.version,
storage_source = glossary.storage_source
)
)
elif isinstance(str):
mshaban-msft marked this conversation as resolved.
Show resolved Hide resolved
result.append(
_Glossary(
glossary_url = glossary,
)
)
return result


class StorageTarget(object):
"""Destination for the finished translated documents.
Expand Down Expand Up @@ -60,6 +89,19 @@ def __init__(
self.glossaries = kwargs.get("glossaries", None)
self.storage_source = kwargs.get("storage_source", None)

@classmethod
mshaban-msft marked this conversation as resolved.
Show resolved Hide resolved
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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
file_extentions=file_format.file_extentions,
file_extensions=file_format.file_extensions,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spelling

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry :)

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 ] )