Skip to content

Commit b6e2230

Browse files
Mohamed Shabankristapratico
andauthored
async client (Azure#17366)
* initial commit * update samples * updates from feedback * johans feedback * renaming to use job terminology * update samples - optional src language * samples hero scenarios (Azure#16936) * [samples] added 'batch_translation_async' sample * [samples] added 'batch_translation_with_storage_async' sample * [samples] added remianing async samples * [samples] update file names * [samples] added self to instance methods * [samples][async] fix import textanalytics :) * [samples] fix self. when calling instance methods * [samples] fixed async check status to use AsyncItemPaged used in Async Client * [samples] async -> some async operations instead of sync ones * [samples][async] use async blob operations * [samples][async] blob download async * [samples][async] check_documents async * [samples][async] added some missing await methods * [async samples] change await time to recommended period * [samples] updated async samples to comply with new changes * [client] modify the 'create_translation_job()' method * [models wrapping 'JobStatusDetail' * [models wrapping] update client * [models wrapping] formats mapping [documents, glossaries, storage] * [models wrapping] batch document input * [models wrapping] added document status * [models wrapping] added support for list document status * [models wrapping] remove unwanted code * [models wrapping] forgot to add job id * [model wrapping] list all jobs * [pr review] extract job id * [refactor] some refactoring * [pr reviews] refactor * [refactor] to_generated in glossary * [refactor] to generated in StorageTargets * [refactor] to_generated in BatchDocumentInput * [refactor] to_generated in Glossary more * [refactor] make code readable -> storage formats * [integration] added support for wait_until_done * [refactor] spelling error * [integrate sdk] updated "wait_until_done" to use azure core poller instead of busy wait * [sync client][wait till done] initial poller * [sync client][wait poller] poller algorithm completed * [PR comments] wrap model before returning * [pr reviews] adjust parameter type to list * [pr reviews] some linter checks * [pr reviews] remove static methods, and fix private naming convention in model functions * [pr reviews] fix return type value * [PR reviews] implement unimplemented inherited abstract method * [pr reviews] fix python private method naming in client * [pr review] handle case with no error happening * [pr reviews] renaming stuff :) * [pr reviews] renaming more stuff :) * [pr reviews] refactor private functions and linting options * [bug fix] poller -> handle non-standard status responses * [Azure#17289] remove supported storage sources method * [bug fix] private method name * [linting] _models.py * [linting] client * [linting] models * [bug fix] wait until done pipeline response * [linting] more client linting * [linting] more client * [linting] more in models * more linting * [linting] more linting things * [linting] client * [linting] more * [linting] more * [linting] for goodness sakes! * [bug fix] models -> StorageTarget -> handle when user doesn't pass glossaries * [linting] line-too-long * [async client] submit translation job * [async client] job status * [async client] more function support added * [async client] added document status * [async client] add remaining methods * [async client] use async poller * [linting] asycn client * [linting] more Co-authored-by: Krista Pratico <[email protected]>
1 parent 6a7cedd commit b6e2230

File tree

1 file changed

+98
-20
lines changed
  • sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/aio

1 file changed

+98
-20
lines changed

sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/aio/_client_async.py

Lines changed: 98 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,22 @@
77
from typing import Union, Any, List, TYPE_CHECKING
88
from azure.core.tracing.decorator_async import distributed_trace_async
99
from azure.core.tracing.decorator import distributed_trace
10+
from azure.core.polling import AsyncLROPoller
11+
from azure.core.polling.async_base_polling import AsyncLROBasePolling
1012
from azure.core.async_paging import AsyncItemPaged
1113
from .._generated.aio import BatchDocumentTranslationClient as _BatchDocumentTranslationClient
1214
from .._user_agent import USER_AGENT
13-
from .._models import JobStatusDetail, DocumentStatusDetail, BatchDocumentInput, FileFormat
15+
from .._generated.models import (
16+
BatchStatusDetail as _BatchStatusDetail,
17+
)
18+
from .._models import (
19+
JobStatusDetail,
20+
BatchDocumentInput,
21+
FileFormat,
22+
DocumentStatusDetail
23+
)
1424
from .._helpers import get_authentication_policy
25+
from .._polling import TranslationPolling
1526
if TYPE_CHECKING:
1627
from azure.core.credentials_async import AsyncTokenCredential
1728
from azure.core.credentials import AzureKeyCredential
@@ -58,12 +69,27 @@ async def create_translation_job(self, batch, **kwargs):
5869
:rtype: JobStatusDetail
5970
"""
6071

61-
return await self._client.document_translation.begin_submit_batch_request(
62-
inputs=batch,
72+
# submit translation job
73+
response_headers = await self._client.document_translation._submit_batch_request_initial( # pylint: disable=protected-access
74+
# pylint: disable=protected-access
75+
inputs=BatchDocumentInput._to_generated_list(batch),
76+
cls=lambda pipeline_response, _, response_headers: response_headers,
6377
polling=True,
6478
**kwargs
6579
)
6680

81+
def get_job_id(response_headers):
82+
# extract job id.
83+
operation_location_header = response_headers['Operation-Location']
84+
return operation_location_header.split('/')[-1]
85+
86+
# get job id from response header
87+
job_id = get_job_id(response_headers)
88+
89+
# get job status
90+
return await self.get_job_status(job_id)
91+
92+
6793
@distributed_trace_async
6894
async def get_job_status(self, job_id, **kwargs):
6995
# type: (str, **Any) -> JobStatusDetail
@@ -74,7 +100,9 @@ async def get_job_status(self, job_id, **kwargs):
74100
:rtype: ~azure.ai.documenttranslation.JobStatusDetail
75101
"""
76102

77-
return await self._client.document_translation.get_operation_status(job_id, **kwargs)
103+
job_status = await self._client.document_translation.get_operation_status(job_id, **kwargs)
104+
# pylint: disable=protected-access
105+
return JobStatusDetail._from_generated(job_status)
78106

79107
@distributed_trace_async
80108
async def cancel_job(self, job_id, **kwargs):
@@ -98,7 +126,26 @@ async def wait_until_done(self, job_id, **kwargs):
98126
:return: JobStatusDetail
99127
:rtype: JobStatusDetail
100128
"""
101-
pass # pylint: disable=unnecessary-pass
129+
pipeline_response = await self._client.document_translation.get_operation_status(
130+
job_id,
131+
cls=lambda pipeline_response, _, response_headers: pipeline_response
132+
)
133+
134+
def callback(raw_response):
135+
detail = self._client._deserialize(_BatchStatusDetail, raw_response) # pylint: disable=protected-access
136+
return JobStatusDetail._from_generated(detail) # pylint: disable=protected-access
137+
138+
poller = AsyncLROPoller(
139+
client=self._client._client, # pylint: disable=protected-access
140+
initial_response=pipeline_response,
141+
deserialization_callback=callback,
142+
polling_method=AsyncLROBasePolling(
143+
timeout=30,
144+
lro_algorithms=[TranslationPolling()],
145+
**kwargs
146+
),
147+
)
148+
return poller.result()
102149

103150
@distributed_trace
104151
def list_submitted_jobs(self, **kwargs):
@@ -109,7 +156,24 @@ def list_submitted_jobs(self, **kwargs):
109156
:keyword int skip:
110157
:rtype: ~azure.core.polling.AsyncItemPaged[JobStatusDetail]
111158
"""
112-
return self._client.document_translation.get_operations(**kwargs)
159+
skip = kwargs.pop('skip', None)
160+
results_per_page = kwargs.pop('results_per_page', None)
161+
162+
def _convert_from_generated_model(generated_model):
163+
# pylint: disable=protected-access
164+
return JobStatusDetail._from_generated(generated_model)
165+
166+
model_conversion_function = kwargs.pop(
167+
"cls",
168+
lambda job_statuses: [_convert_from_generated_model(job_status) for job_status in job_statuses]
169+
)
170+
171+
return self._client.document_translation.get_operations(
172+
top=results_per_page,
173+
skip=skip,
174+
cls=model_conversion_function,
175+
**kwargs
176+
)
113177

114178
@distributed_trace
115179
def list_documents_statuses(self, job_id, **kwargs):
@@ -122,8 +186,26 @@ def list_documents_statuses(self, job_id, **kwargs):
122186
:keyword int skip:
123187
:rtype: ~azure.core.paging.AsyncItemPaged[DocumentStatusDetail]
124188
"""
189+
skip = kwargs.pop('skip', None)
190+
results_per_page = kwargs.pop('results_per_page', None)
191+
192+
def _convert_from_generated_model(generated_model):
193+
# pylint: disable=protected-access
194+
return DocumentStatusDetail._from_generated(generated_model)
195+
196+
model_conversion_function = kwargs.pop(
197+
"cls",
198+
lambda doc_statuses: [_convert_from_generated_model(doc_status) for doc_status in doc_statuses]
199+
)
200+
201+
return self._client.document_translation.get_operation_documents_status(
202+
id=job_id,
203+
top=results_per_page,
204+
skip=skip,
205+
cls=model_conversion_function,
206+
**kwargs
207+
)
125208

126-
return self._client.document_translation.get_operation_documents_status(job_id, **kwargs)
127209

128210
@distributed_trace_async
129211
async def get_document_status(self, job_id, document_id, **kwargs):
@@ -136,16 +218,10 @@ async def get_document_status(self, job_id, document_id, **kwargs):
136218
:type document_id: str
137219
:rtype: ~azure.ai.documenttranslation.DocumentStatusDetail
138220
"""
139-
return await self._client.document_translation.get_document_status(job_id, document_id, **kwargs)
221+
document_status = await self._client.document_translation.get_document_status(job_id, document_id, **kwargs)
222+
# pylint: disable=protected-access
223+
return DocumentStatusDetail._from_generated(document_status)
140224

141-
@distributed_trace_async
142-
async def get_supported_storage_sources(self, **kwargs):
143-
# type: (**Any) -> List[str]
144-
"""
145-
146-
:rtype: list[str]
147-
"""
148-
return await self._client.document_translation.get_document_storage_source(**kwargs)
149225

150226
@distributed_trace_async
151227
async def get_supported_glossary_formats(self, **kwargs):
@@ -154,8 +230,9 @@ async def get_supported_glossary_formats(self, **kwargs):
154230
155231
:rtype: list[FileFormat]
156232
"""
157-
158-
return await self._client.document_translation.get_glossary_formats(**kwargs)
233+
glossary_formats = await self._client.document_translation.get_glossary_formats(**kwargs)
234+
# pylint: disable=protected-access
235+
return FileFormat._from_generated_list(glossary_formats.value)
159236

160237
@distributed_trace_async
161238
async def get_supported_document_formats(self, **kwargs):
@@ -164,5 +241,6 @@ async def get_supported_document_formats(self, **kwargs):
164241
165242
:rtype: list[FileFormat]
166243
"""
167-
168-
return await self._client.document_translation.get_document_formats(**kwargs)
244+
document_formats = await self._client.document_translation.get_document_formats(**kwargs)
245+
# pylint: disable=protected-access
246+
return FileFormat._from_generated_list(document_formats.value)

0 commit comments

Comments
 (0)