diff --git a/sdk/translation/azure-ai-translation-document/_meta.json b/sdk/translation/azure-ai-translation-document/_meta.json new file mode 100644 index 000000000000..eeee1848a192 --- /dev/null +++ b/sdk/translation/azure-ai-translation-document/_meta.json @@ -0,0 +1,6 @@ +{ + "commit": "c29452397f8d29456182b951987a765d1ae01bb1", + "repository_url": "https://github.com/test-repo-billy/azure-rest-api-specs", + "typespec_src": "specification/translation/Azure.AI.DocumentTranslation", + "@azure-tools/typespec-python": "0.31.1" +} \ No newline at end of file diff --git a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/__init__.py b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/__init__.py index d4b4d4d35fa9..0896c5bf86a5 100644 --- a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/__init__.py +++ b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/__init__.py @@ -6,39 +6,23 @@ # Changes may cause incorrect behavior and will be lost if the code is regenerated. # -------------------------------------------------------------------------- -from ._patch import DocumentTranslationClient +from ._client import DocumentTranslationClient from ._client import SingleDocumentTranslationClient from ._version import VERSION __version__ = VERSION - -from ._patch import DocumentTranslationApiVersion -from ._patch import DocumentTranslationLROPoller -from ._patch import TranslationGlossary -from ._patch import TranslationTarget -from ._patch import DocumentTranslationInput -from ._patch import TranslationStatus -from ._patch import DocumentStatus -from ._patch import DocumentTranslationError -from ._patch import DocumentTranslationFileFormat -from ._patch import StorageInputType +try: + from ._patch import __all__ as _patch_all + from ._patch import * # pylint: disable=unused-wildcard-import +except ImportError: + _patch_all = [] from ._patch import patch_sdk as _patch_sdk __all__ = [ - "DocumentTranslationApiVersion", - "DocumentTranslationLROPoller", - "TranslationGlossary", - "TranslationTarget", - "DocumentTranslationInput", - "TranslationStatus", - "DocumentStatus", - "DocumentTranslationError", - "DocumentTranslationFileFormat", - "StorageInputType", "DocumentTranslationClient", "SingleDocumentTranslationClient", ] - +__all__.extend([p for p in _patch_all if p not in __all__]) _patch_sdk() 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 89f2320bf460..5c958182adba 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 @@ -8,6 +8,7 @@ from copy import deepcopy from typing import Any, TYPE_CHECKING, Union +from typing_extensions import Self from azure.core import PipelineClient from azure.core.credentials import AzureKeyCredential @@ -97,7 +98,7 @@ def send_request(self, request: HttpRequest, *, stream: bool = False, **kwargs: def close(self) -> None: self._client.close() - def __enter__(self) -> "DocumentTranslationClient": + def __enter__(self) -> Self: self._client.__enter__() return self @@ -177,7 +178,7 @@ def send_request(self, request: HttpRequest, *, stream: bool = False, **kwargs: def close(self) -> None: self._client.close() - def __enter__(self) -> "SingleDocumentTranslationClient": + def __enter__(self) -> Self: self._client.__enter__() return self diff --git a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_model_base.py b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_model_base.py index 5cf70733404d..12ad7f29c71e 100644 --- a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_model_base.py +++ b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_model_base.py @@ -4,7 +4,7 @@ # Licensed under the MIT License. See License.txt in the project root for # license information. # -------------------------------------------------------------------------- -# pylint: disable=protected-access, arguments-differ, signature-differs, broad-except +# pylint: disable=protected-access, arguments-differ, signature-differs, broad-except, too-many-lines import copy import calendar @@ -19,6 +19,7 @@ import email.utils from datetime import datetime, date, time, timedelta, timezone from json import JSONEncoder +import xml.etree.ElementTree as ET from typing_extensions import Self import isodate from azure.core.exceptions import DeserializationError @@ -123,7 +124,7 @@ def _serialize_datetime(o, format: typing.Optional[str] = None): def _is_readonly(p): try: - return p._visibility == ["read"] # pylint: disable=protected-access + return p._visibility == ["read"] except AttributeError: return False @@ -286,6 +287,12 @@ def _deserialize_decimal(attr): return decimal.Decimal(str(attr)) +def _deserialize_int_as_str(attr): + if isinstance(attr, int): + return attr + return int(attr) + + _DESERIALIZE_MAPPING = { datetime: _deserialize_datetime, date: _deserialize_date, @@ -307,9 +314,11 @@ def _deserialize_decimal(attr): def get_deserializer(annotation: typing.Any, rf: typing.Optional["_RestField"] = None): + if annotation is int and rf and rf._format == "str": + return _deserialize_int_as_str if rf and rf._format: return _DESERIALIZE_MAPPING_WITHFORMAT.get(rf._format) - return _DESERIALIZE_MAPPING.get(annotation) + return _DESERIALIZE_MAPPING.get(annotation) # pyright: ignore def _get_type_alias_type(module_name: str, alias_name: str): @@ -441,6 +450,10 @@ def _serialize(o, format: typing.Optional[str] = None): # pylint: disable=too-m return float(o) if isinstance(o, enum.Enum): return o.value + if isinstance(o, int): + if format == "str": + return str(o) + return o try: # First try datetime.datetime return _serialize_datetime(o, format) @@ -471,11 +484,16 @@ def _create_value(rf: typing.Optional["_RestField"], value: typing.Any) -> typin return value if rf._is_model: return _deserialize(rf._type, value) + if isinstance(value, ET.Element): + value = _deserialize(rf._type, value) return _serialize(value, rf._format) class Model(_MyMutableMapping): _is_model = True + # label whether current class's _attr_to_rest_field has been calculated + # could not see _attr_to_rest_field directly because subclass inherits it from parent class + _calculated: typing.Set[str] = set() def __init__(self, *args: typing.Any, **kwargs: typing.Any) -> None: class_name = self.__class__.__name__ @@ -486,10 +504,58 @@ def __init__(self, *args: typing.Any, **kwargs: typing.Any) -> None: for rest_field in self._attr_to_rest_field.values() if rest_field._default is not _UNSET } - if args: - dict_to_pass.update( - {k: _create_value(_get_rest_field(self._attr_to_rest_field, k), v) for k, v in args[0].items()} - ) + if args: # pylint: disable=too-many-nested-blocks + if isinstance(args[0], ET.Element): + existed_attr_keys = [] + model_meta = getattr(self, "_xml", {}) + + for rf in self._attr_to_rest_field.values(): + prop_meta = getattr(rf, "_xml", {}) + xml_name = prop_meta.get("name", rf._rest_name) + xml_ns = prop_meta.get("ns", model_meta.get("ns", None)) + if xml_ns: + xml_name = "{" + xml_ns + "}" + xml_name + + # attribute + if prop_meta.get("attribute", False) and args[0].get(xml_name) is not None: + existed_attr_keys.append(xml_name) + dict_to_pass[rf._rest_name] = _deserialize(rf._type, args[0].get(xml_name)) + continue + + # unwrapped element is array + if prop_meta.get("unwrapped", False): + # unwrapped array could either use prop items meta/prop meta + if prop_meta.get("itemsName"): + xml_name = prop_meta.get("itemsName") + xml_ns = prop_meta.get("itemNs") + if xml_ns: + xml_name = "{" + xml_ns + "}" + xml_name + items = args[0].findall(xml_name) # pyright: ignore + if len(items) > 0: + existed_attr_keys.append(xml_name) + dict_to_pass[rf._rest_name] = _deserialize(rf._type, items) + continue + + # text element is primitive type + if prop_meta.get("text", False): + if args[0].text is not None: + dict_to_pass[rf._rest_name] = _deserialize(rf._type, args[0].text) + continue + + # wrapped element could be normal property or array, it should only have one element + item = args[0].find(xml_name) + if item is not None: + existed_attr_keys.append(xml_name) + dict_to_pass[rf._rest_name] = _deserialize(rf._type, item) + + # rest thing is additional properties + for e in args[0]: + if e.tag not in existed_attr_keys: + dict_to_pass[e.tag] = _convert_element(e) + else: + dict_to_pass.update( + {k: _create_value(_get_rest_field(self._attr_to_rest_field, k), v) for k, v in args[0].items()} + ) else: non_attr_kwargs = [k for k in kwargs if k not in self._attr_to_rest_field] if non_attr_kwargs: @@ -508,24 +574,27 @@ def copy(self) -> "Model": return Model(self.__dict__) def __new__(cls, *args: typing.Any, **kwargs: typing.Any) -> Self: # pylint: disable=unused-argument - # we know the last three classes in mro are going to be 'Model', 'dict', and 'object' - mros = cls.__mro__[:-3][::-1] # ignore model, dict, and object parents, and reverse the mro order - attr_to_rest_field: typing.Dict[str, _RestField] = { # map attribute name to rest_field property - k: v for mro_class in mros for k, v in mro_class.__dict__.items() if k[0] != "_" and hasattr(v, "_type") - } - annotations = { - k: v - for mro_class in mros - if hasattr(mro_class, "__annotations__") # pylint: disable=no-member - for k, v in mro_class.__annotations__.items() # pylint: disable=no-member - } - for attr, rf in attr_to_rest_field.items(): - rf._module = cls.__module__ - if not rf._type: - rf._type = rf._get_deserialize_callable_from_annotation(annotations.get(attr, None)) - if not rf._rest_name_input: - rf._rest_name_input = attr - cls._attr_to_rest_field: typing.Dict[str, _RestField] = dict(attr_to_rest_field.items()) + if f"{cls.__module__}.{cls.__qualname__}" not in cls._calculated: + # we know the last nine classes in mro are going to be 'Model', '_MyMutableMapping', 'MutableMapping', + # 'Mapping', 'Collection', 'Sized', 'Iterable', 'Container' and 'object' + mros = cls.__mro__[:-9][::-1] # ignore parents, and reverse the mro order + attr_to_rest_field: typing.Dict[str, _RestField] = { # map attribute name to rest_field property + k: v for mro_class in mros for k, v in mro_class.__dict__.items() if k[0] != "_" and hasattr(v, "_type") + } + annotations = { + k: v + for mro_class in mros + if hasattr(mro_class, "__annotations__") # pylint: disable=no-member + for k, v in mro_class.__annotations__.items() # pylint: disable=no-member + } + for attr, rf in attr_to_rest_field.items(): + rf._module = cls.__module__ + if not rf._type: + rf._type = rf._get_deserialize_callable_from_annotation(annotations.get(attr, None)) + if not rf._rest_name_input: + rf._rest_name_input = attr + cls._attr_to_rest_field: typing.Dict[str, _RestField] = dict(attr_to_rest_field.items()) + cls._calculated.add(f"{cls.__module__}.{cls.__qualname__}") return super().__new__(cls) # pylint: disable=no-value-for-parameter @@ -535,12 +604,10 @@ def __init_subclass__(cls, discriminator: typing.Optional[str] = None) -> None: base.__mapping__[discriminator or cls.__name__] = cls # type: ignore # pylint: disable=no-member @classmethod - def _get_discriminator(cls, exist_discriminators) -> typing.Optional[str]: + def _get_discriminator(cls, exist_discriminators) -> typing.Optional["_RestField"]: for v in cls.__dict__.values(): - if ( - isinstance(v, _RestField) and v._is_discriminator and v._rest_name not in exist_discriminators - ): # pylint: disable=protected-access - return v._rest_name # pylint: disable=protected-access + if isinstance(v, _RestField) and v._is_discriminator and v._rest_name not in exist_discriminators: + return v return None @classmethod @@ -548,11 +615,25 @@ def _deserialize(cls, data, exist_discriminators): if not hasattr(cls, "__mapping__"): # pylint: disable=no-member return cls(data) discriminator = cls._get_discriminator(exist_discriminators) - exist_discriminators.append(discriminator) - mapped_cls = cls.__mapping__.get(data.get(discriminator), cls) # pyright: ignore # pylint: disable=no-member - if mapped_cls == cls: + if discriminator is None: return cls(data) - return mapped_cls._deserialize(data, exist_discriminators) # pylint: disable=protected-access + exist_discriminators.append(discriminator._rest_name) + if isinstance(data, ET.Element): + model_meta = getattr(cls, "_xml", {}) + prop_meta = getattr(discriminator, "_xml", {}) + xml_name = prop_meta.get("name", discriminator._rest_name) + xml_ns = prop_meta.get("ns", model_meta.get("ns", None)) + if xml_ns: + xml_name = "{" + xml_ns + "}" + xml_name + + if data.get(xml_name) is not None: + discriminator_value = data.get(xml_name) + else: + discriminator_value = data.find(xml_name).text # pyright: ignore + else: + discriminator_value = data.get(discriminator._rest_name) + mapped_cls = cls.__mapping__.get(discriminator_value, cls) # pyright: ignore # pylint: disable=no-member + return mapped_cls._deserialize(data, exist_discriminators) def as_dict(self, *, exclude_readonly: bool = False) -> typing.Dict[str, typing.Any]: """Return a dict that can be JSONify using json.dump. @@ -563,6 +644,7 @@ def as_dict(self, *, exclude_readonly: bool = False) -> typing.Dict[str, typing. """ result = {} + readonly_props = [] if exclude_readonly: readonly_props = [p._rest_name for p in self._attr_to_rest_field.values() if _is_readonly(p)] for k, v in self.items(): @@ -617,6 +699,8 @@ def _deserialize_dict( ): if obj is None: return obj + if isinstance(obj, ET.Element): + obj = {child.tag: child for child in obj} return {k: _deserialize(value_deserializer, v, module) for k, v in obj.items()} @@ -637,6 +721,8 @@ def _deserialize_sequence( ): if obj is None: return obj + if isinstance(obj, ET.Element): + obj = list(obj) return type(obj)(_deserialize(deserializer, entry, module) for entry in obj) @@ -652,7 +738,7 @@ def _get_deserialize_callable_from_annotation( # pylint: disable=R0911, R0915, module: typing.Optional[str], rf: typing.Optional["_RestField"] = None, ) -> typing.Optional[typing.Callable[[typing.Any], typing.Any]]: - if not annotation or annotation in [int, float]: + if not annotation: return None # is it a type alias? @@ -727,7 +813,6 @@ def _get_deserialize_callable_from_annotation( # pylint: disable=R0911, R0915, try: if annotation._name in ["List", "Set", "Tuple", "Sequence"]: # pyright: ignore if len(annotation.__args__) > 1: # pyright: ignore - entry_deserializers = [ _get_deserialize_callable_from_annotation(dt, module, rf) for dt in annotation.__args__ # pyright: ignore @@ -762,12 +847,23 @@ def _deserialize_default( def _deserialize_with_callable( deserializer: typing.Optional[typing.Callable[[typing.Any], typing.Any]], value: typing.Any, -): +): # pylint: disable=too-many-return-statements try: if value is None or isinstance(value, _Null): return None + if isinstance(value, ET.Element): + if deserializer is str: + return value.text or "" + if deserializer is int: + return int(value.text) if value.text else None + if deserializer is float: + return float(value.text) if value.text else None + if deserializer is bool: + return value.text == "true" if value.text else None if deserializer is None: return value + if deserializer in [int, float, bool]: + return deserializer(value) if isinstance(deserializer, CaseInsensitiveEnumMeta): try: return deserializer(value) @@ -808,6 +904,7 @@ def __init__( default: typing.Any = _UNSET, format: typing.Optional[str] = None, is_multipart_file_input: bool = False, + xml: typing.Optional[typing.Dict[str, typing.Any]] = None, ): self._type = type self._rest_name_input = name @@ -818,6 +915,7 @@ def __init__( self._default = default self._format = format self._is_multipart_file_input = is_multipart_file_input + self._xml = xml if xml is not None else {} @property def _class_type(self) -> typing.Any: @@ -868,6 +966,7 @@ def rest_field( default: typing.Any = _UNSET, format: typing.Optional[str] = None, is_multipart_file_input: bool = False, + xml: typing.Optional[typing.Dict[str, typing.Any]] = None, ) -> typing.Any: return _RestField( name=name, @@ -876,6 +975,7 @@ def rest_field( default=default, format=format, is_multipart_file_input=is_multipart_file_input, + xml=xml, ) @@ -883,5 +983,176 @@ def rest_discriminator( *, name: typing.Optional[str] = None, type: typing.Optional[typing.Callable] = None, # pylint: disable=redefined-builtin + visibility: typing.Optional[typing.List[str]] = None, + xml: typing.Optional[typing.Dict[str, typing.Any]] = None, +) -> typing.Any: + return _RestField(name=name, type=type, is_discriminator=True, visibility=visibility, xml=xml) + + +def serialize_xml(model: Model, exclude_readonly: bool = False) -> str: + """Serialize a model to XML. + + :param Model model: The model to serialize. + :param bool exclude_readonly: Whether to exclude readonly properties. + :returns: The XML representation of the model. + :rtype: str + """ + return ET.tostring(_get_element(model, exclude_readonly), encoding="unicode") # type: ignore + + +def _get_element( + o: typing.Any, + exclude_readonly: bool = False, + parent_meta: typing.Optional[typing.Dict[str, typing.Any]] = None, + wrapped_element: typing.Optional[ET.Element] = None, +) -> typing.Union[ET.Element, typing.List[ET.Element]]: + if _is_model(o): + model_meta = getattr(o, "_xml", {}) + + # if prop is a model, then use the prop element directly, else generate a wrapper of model + if wrapped_element is None: + wrapped_element = _create_xml_element( + model_meta.get("name", o.__class__.__name__), + model_meta.get("prefix"), + model_meta.get("ns"), + ) + + readonly_props = [] + if exclude_readonly: + readonly_props = [p._rest_name for p in o._attr_to_rest_field.values() if _is_readonly(p)] + + for k, v in o.items(): + # do not serialize readonly properties + if exclude_readonly and k in readonly_props: + continue + + prop_rest_field = _get_rest_field(o._attr_to_rest_field, k) + if prop_rest_field: + prop_meta = getattr(prop_rest_field, "_xml").copy() + # use the wire name as xml name if no specific name is set + if prop_meta.get("name") is None: + prop_meta["name"] = k + else: + # additional properties will not have rest field, use the wire name as xml name + prop_meta = {"name": k} + + # if no ns for prop, use model's + if prop_meta.get("ns") is None and model_meta.get("ns"): + prop_meta["ns"] = model_meta.get("ns") + prop_meta["prefix"] = model_meta.get("prefix") + + if prop_meta.get("unwrapped", False): + # unwrapped could only set on array + wrapped_element.extend(_get_element(v, exclude_readonly, prop_meta)) + elif prop_meta.get("text", False): + # text could only set on primitive type + wrapped_element.text = _get_primitive_type_value(v) + elif prop_meta.get("attribute", False): + xml_name = prop_meta.get("name", k) + if prop_meta.get("ns"): + ET.register_namespace(prop_meta.get("prefix"), prop_meta.get("ns")) # pyright: ignore + xml_name = "{" + prop_meta.get("ns") + "}" + xml_name # pyright: ignore + # attribute should be primitive type + wrapped_element.set(xml_name, _get_primitive_type_value(v)) + else: + # other wrapped prop element + wrapped_element.append(_get_wrapped_element(v, exclude_readonly, prop_meta)) + return wrapped_element + if isinstance(o, list): + return [_get_element(x, exclude_readonly, parent_meta) for x in o] # type: ignore + if isinstance(o, dict): + result = [] + for k, v in o.items(): + result.append( + _get_wrapped_element( + v, + exclude_readonly, + { + "name": k, + "ns": parent_meta.get("ns") if parent_meta else None, + "prefix": parent_meta.get("prefix") if parent_meta else None, + }, + ) + ) + return result + + # primitive case need to create element based on parent_meta + if parent_meta: + return _get_wrapped_element( + o, + exclude_readonly, + { + "name": parent_meta.get("itemsName", parent_meta.get("name")), + "prefix": parent_meta.get("itemsPrefix", parent_meta.get("prefix")), + "ns": parent_meta.get("itemsNs", parent_meta.get("ns")), + }, + ) + + raise ValueError("Could not serialize value into xml: " + o) + + +def _get_wrapped_element( + v: typing.Any, + exclude_readonly: bool, + meta: typing.Optional[typing.Dict[str, typing.Any]], +) -> ET.Element: + wrapped_element = _create_xml_element( + meta.get("name") if meta else None, meta.get("prefix") if meta else None, meta.get("ns") if meta else None + ) + if isinstance(v, (dict, list)): + wrapped_element.extend(_get_element(v, exclude_readonly, meta)) + elif _is_model(v): + _get_element(v, exclude_readonly, meta, wrapped_element) + else: + wrapped_element.text = _get_primitive_type_value(v) + return wrapped_element + + +def _get_primitive_type_value(v) -> str: + if v is True: + return "true" + if v is False: + return "false" + if isinstance(v, _Null): + return "" + return str(v) + + +def _create_xml_element(tag, prefix=None, ns=None): + if prefix and ns: + ET.register_namespace(prefix, ns) + if ns: + return ET.Element("{" + ns + "}" + tag) + return ET.Element(tag) + + +def _deserialize_xml( + deserializer: typing.Any, + value: str, ) -> typing.Any: - return _RestField(name=name, type=type, is_discriminator=True) + element = ET.fromstring(value) # nosec + return _deserialize(deserializer, element) + + +def _convert_element(e: ET.Element): + # dict case + if len(e.attrib) > 0 or len({child.tag for child in e}) > 1: + dict_result: typing.Dict[str, typing.Any] = {} + for child in e: + if dict_result.get(child.tag) is not None: + if isinstance(dict_result[child.tag], list): + dict_result[child.tag].append(_convert_element(child)) + else: + dict_result[child.tag] = [dict_result[child.tag], _convert_element(child)] + else: + dict_result[child.tag] = _convert_element(child) + dict_result.update(e.attrib) + return dict_result + # array case + if len(e) > 0: + array_result: typing.List[typing.Any] = [] + for child in e: + array_result.append(_convert_element(child)) + return array_result + # primitive case + return e.text diff --git a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_operations/__init__.py b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_operations/__init__.py index 9e327bba3bf1..5f45db60645f 100644 --- a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_operations/__init__.py +++ b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_operations/__init__.py @@ -6,15 +6,16 @@ # Changes may cause incorrect behavior and will be lost if the code is regenerated. # -------------------------------------------------------------------------- -from ._patch import DocumentTranslationClientOperationsMixin -from ._patch import SingleDocumentTranslationClientOperationsMixin - +from ._operations import DocumentTranslationClientOperationsMixin +from ._operations import SingleDocumentTranslationClientOperationsMixin +from ._patch import __all__ as _patch_all +from ._patch import * # pylint: disable=unused-wildcard-import from ._patch import patch_sdk as _patch_sdk __all__ = [ "DocumentTranslationClientOperationsMixin", "SingleDocumentTranslationClientOperationsMixin", ] - +__all__.extend([p for p in _patch_all if p not in __all__]) _patch_sdk() diff --git a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_operations/_operations.py b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_operations/_operations.py index d6a4fe454945..b9dc2ae9938a 100644 --- a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_operations/_operations.py +++ b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_operations/_operations.py @@ -19,6 +19,8 @@ ResourceExistsError, ResourceNotFoundError, ResourceNotModifiedError, + StreamClosedError, + StreamConsumedError, map_error, ) from azure.core.paging import ItemPaged @@ -29,9 +31,7 @@ from azure.core.tracing.decorator import distributed_trace from azure.core.utils import case_insensitive_dict -from ..models import _models -from ..models import _enums -from .. import _model_base +from .. import _model_base, models as _models from .._model_base import SdkJSONEncoder, _deserialize from .._serialization import Serializer from .._vendor import ( @@ -52,7 +52,9 @@ _SERIALIZER.client_side_validation = False -def build_document_translation_start_translation_request(**kwargs: Any) -> HttpRequest: # pylint: disable=name-too-long +def build_document_translation__begin_translation_request( # pylint: disable=name-too-long + **kwargs: Any, +) -> HttpRequest: _headers = case_insensitive_dict(kwargs.pop("headers", {}) or {}) _params = case_insensitive_dict(kwargs.pop("params", {}) or {}) @@ -74,7 +76,7 @@ def build_document_translation_start_translation_request(**kwargs: Any) -> HttpR return HttpRequest(method="POST", url=_url, params=_params, headers=_headers, **kwargs) -def build_document_translation_get_translations_status_request( # pylint: disable=name-too-long +def build_document_translation_list_translation_statuses_request( # pylint: disable=name-too-long *, top: Optional[int] = None, skip: Optional[int] = None, @@ -84,7 +86,7 @@ def build_document_translation_get_translations_status_request( # pylint: disab created_date_time_utc_start: Optional[datetime.datetime] = None, created_date_time_utc_end: Optional[datetime.datetime] = None, orderby: Optional[List[str]] = None, - **kwargs: Any + **kwargs: Any, ) -> HttpRequest: _headers = case_insensitive_dict(kwargs.pop("headers", {}) or {}) _params = case_insensitive_dict(kwargs.pop("params", {}) or {}) @@ -125,7 +127,7 @@ def build_document_translation_get_translations_status_request( # pylint: disab def build_document_translation_get_document_status_request( # pylint: disable=name-too-long - id: str, document_id: str, **kwargs: Any + translation_id: str, document_id: str, **kwargs: Any ) -> HttpRequest: _headers = case_insensitive_dict(kwargs.pop("headers", {}) or {}) _params = case_insensitive_dict(kwargs.pop("params", {}) or {}) @@ -136,7 +138,7 @@ def build_document_translation_get_document_status_request( # pylint: disable=n # Construct URL _url = "/document/batches/{id}/documents/{documentId}" path_format_arguments = { - "id": _SERIALIZER.url("id", id, "str"), + "id": _SERIALIZER.url("translation_id", translation_id, "str"), "documentId": _SERIALIZER.url("document_id", document_id, "str"), } @@ -152,7 +154,7 @@ def build_document_translation_get_document_status_request( # pylint: disable=n def build_document_translation_get_translation_status_request( # pylint: disable=name-too-long - id: str, **kwargs: Any + translation_id: str, **kwargs: Any ) -> HttpRequest: _headers = case_insensitive_dict(kwargs.pop("headers", {}) or {}) _params = case_insensitive_dict(kwargs.pop("params", {}) or {}) @@ -163,7 +165,7 @@ def build_document_translation_get_translation_status_request( # pylint: disabl # Construct URL _url = "/document/batches/{id}" path_format_arguments = { - "id": _SERIALIZER.url("id", id, "str"), + "id": _SERIALIZER.url("translation_id", translation_id, "str"), } _url: str = _url.format(**path_format_arguments) # type: ignore @@ -178,7 +180,7 @@ def build_document_translation_get_translation_status_request( # pylint: disabl def build_document_translation_cancel_translation_request( # pylint: disable=name-too-long - id: str, **kwargs: Any + translation_id: str, **kwargs: Any ) -> HttpRequest: _headers = case_insensitive_dict(kwargs.pop("headers", {}) or {}) _params = case_insensitive_dict(kwargs.pop("params", {}) or {}) @@ -189,7 +191,7 @@ def build_document_translation_cancel_translation_request( # pylint: disable=na # Construct URL _url = "/document/batches/{id}" path_format_arguments = { - "id": _SERIALIZER.url("id", id, "str"), + "id": _SERIALIZER.url("translation_id", translation_id, "str"), } _url: str = _url.format(**path_format_arguments) # type: ignore @@ -203,8 +205,8 @@ def build_document_translation_cancel_translation_request( # pylint: disable=na return HttpRequest(method="DELETE", url=_url, params=_params, headers=_headers, **kwargs) -def build_document_translation_get_documents_status_request( # pylint: disable=name-too-long - id: str, +def build_document_translation_list_document_statuses_request( # pylint: disable=name-too-long + translation_id: str, *, top: Optional[int] = None, skip: Optional[int] = None, @@ -214,7 +216,7 @@ def build_document_translation_get_documents_status_request( # pylint: disable= created_date_time_utc_start: Optional[datetime.datetime] = None, created_date_time_utc_end: Optional[datetime.datetime] = None, orderby: Optional[List[str]] = None, - **kwargs: Any + **kwargs: Any, ) -> HttpRequest: _headers = case_insensitive_dict(kwargs.pop("headers", {}) or {}) _params = case_insensitive_dict(kwargs.pop("params", {}) or {}) @@ -225,7 +227,7 @@ def build_document_translation_get_documents_status_request( # pylint: disable= # Construct URL _url = "/document/batches/{id}/documents" path_format_arguments = { - "id": _SERIALIZER.url("id", id, "str"), + "id": _SERIALIZER.url("translation_id", translation_id, "str"), } _url: str = _url.format(**path_format_arguments) # type: ignore @@ -260,7 +262,7 @@ def build_document_translation_get_documents_status_request( # pylint: disable= def build_document_translation_get_supported_formats_request( # pylint: disable=name-too-long - *, type: Optional[Union[str, _enums.FileFormatType]] = None, **kwargs: Any + *, type: Optional[Union[str, _models.FileFormatType]] = None, **kwargs: Any ) -> HttpRequest: _headers = case_insensitive_dict(kwargs.pop("headers", {}) or {}) _params = case_insensitive_dict(kwargs.pop("params", {}) or {}) @@ -288,7 +290,7 @@ def build_single_document_translation_document_translate_request( # pylint: dis source_language: Optional[str] = None, category: Optional[str] = None, allow_fallback: Optional[bool] = None, - **kwargs: Any + **kwargs: Any, ) -> HttpRequest: _headers = case_insensitive_dict(kwargs.pop("headers", {}) or {}) _params = case_insensitive_dict(kwargs.pop("params", {}) or {}) @@ -317,10 +319,10 @@ def build_single_document_translation_document_translate_request( # pylint: dis class DocumentTranslationClientOperationsMixin(DocumentTranslationClientMixinABC): - def _start_translation_initial( # pylint: disable=inconsistent-return-statements + def __begin_translation_initial( self, body: Union[_models.StartTranslationDetails, JSON, IO[bytes]], **kwargs: Any - ) -> None: - error_map: MutableMapping[int, Type[HttpResponseError]] = { + ) -> Iterator[bytes]: + error_map: MutableMapping[int, Type[HttpResponseError]] = { # pylint: disable=unsubscriptable-object 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError, @@ -332,7 +334,7 @@ def _start_translation_initial( # pylint: disable=inconsistent-return-statement _params = kwargs.pop("params", {}) or {} content_type: Optional[str] = kwargs.pop("content_type", _headers.pop("Content-Type", None)) - cls: ClsType[None] = kwargs.pop("cls", None) + cls: ClsType[Iterator[bytes]] = kwargs.pop("cls", None) content_type = content_type or "application/json" _content = None @@ -341,7 +343,7 @@ def _start_translation_initial( # pylint: disable=inconsistent-return-statement else: _content = json.dumps(body, cls=SdkJSONEncoder, exclude_readonly=True) # type: ignore - _request = build_document_translation_start_translation_request( + _request = build_document_translation__begin_translation_request( content_type=content_type, api_version=self._config.api_version, content=_content, @@ -353,7 +355,7 @@ def _start_translation_initial( # pylint: disable=inconsistent-return-statement } _request.url = self._client.format_url(_request.url, **path_format_arguments) - _stream = False + _stream = True pipeline_response: PipelineResponse = self._client._pipeline.run( # pylint: disable=protected-access _request, stream=_stream, **kwargs ) @@ -361,22 +363,27 @@ def _start_translation_initial( # pylint: disable=inconsistent-return-statement response = pipeline_response.http_response if response.status_code not in [202]: - if _stream: + try: response.read() # Load the body in memory and close the socket + except (StreamConsumedError, StreamClosedError): + pass map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response) response_headers = {} response_headers["Operation-Location"] = self._deserialize("str", response.headers.get("Operation-Location")) + deserialized = response.iter_bytes() + if cls: - return cls(pipeline_response, None, response_headers) # type: ignore + return cls(pipeline_response, deserialized, response_headers) # type: ignore + + return deserialized # type: ignore @overload - def begin_start_translation( # pylint: disable=protected-access + def _begin_translation( self, body: _models.StartTranslationDetails, *, content_type: str = "application/json", **kwargs: Any ) -> LROPoller[None]: - # pylint: disable=line-too-long """Submit a document translation request to the Document Translation service. Use this API to submit a bulk (batch) translation request to the Document @@ -399,76 +406,18 @@ def begin_start_translation( # pylint: disable=protected-access destination, it will be overwritten. The targetUrl for each target language must be unique. - :param body: Required. - :type body: ~azure.ai.translation.document.models._models.StartTranslationDetails + :param body: Translation job submission batch request. Required. + :type body: ~azure.ai.translation.document.models.StartTranslationDetails :keyword content_type: Body Parameter content-type. Content type parameter for JSON body. Default value is "application/json". :paramtype content_type: str :return: An instance of LROPoller that returns None :rtype: ~azure.core.polling.LROPoller[None] :raises ~azure.core.exceptions.HttpResponseError: - - Example: - .. code-block:: python - - # JSON input template you can fill out and use as your body input. - body = { - "inputs": [ - { - "source": { - "sourceUrl": "str", # Location of the folder / - container or single file with your documents. Required. - "filter": { - "prefix": "str", # Optional. A - case-sensitive prefix string to filter documents in the source - path for translation. For example, when using a Azure storage - blob Uri, use the prefix to restrict sub folders for translation. - "suffix": "str" # Optional. A case-sensitive - suffix string to filter documents in the source path for - translation. This is most often use for file extensions. - }, - "language": "str", # Optional. Language code If none - is specified, we will perform auto detect on the document. - "storageSource": "str" # Optional. Storage Source. - "AzureBlob" - }, - "targets": [ - { - "language": "str", # Target Language. - Required. - "targetUrl": "str", # Location of the folder - / container with your documents. Required. - "category": "str", # Optional. Category / - custom system for translation request. - "glossaries": [ - { - "format": "str", # Format. - Required. - "glossaryUrl": "str", # - Location of the glossary. We will use the file extension - to extract the formatting if the format parameter is not - supplied. If the translation language pair is not - present in the glossary, it will not be applied. - Required. - "storageSource": "str", # - Optional. Storage Source. "AzureBlob" - "version": "str" # Optional. - Optional Version. If not specified, default is used. - } - ], - "storageSource": "str" # Optional. Storage - Source. "AzureBlob" - } - ], - "storageType": "str" # Optional. Storage type of the input - documents source string. Known values are: "Folder" and "File". - } - ] - } """ @overload - def begin_start_translation( + def _begin_translation( self, body: JSON, *, content_type: str = "application/json", **kwargs: Any ) -> LROPoller[None]: """Submit a document translation request to the Document Translation service. @@ -493,7 +442,7 @@ def begin_start_translation( destination, it will be overwritten. The targetUrl for each target language must be unique. - :param body: Required. + :param body: Translation job submission batch request. Required. :type body: JSON :keyword content_type: Body Parameter content-type. Content type parameter for JSON body. Default value is "application/json". @@ -504,7 +453,7 @@ def begin_start_translation( """ @overload - def begin_start_translation( + def _begin_translation( self, body: IO[bytes], *, content_type: str = "application/json", **kwargs: Any ) -> LROPoller[None]: """Submit a document translation request to the Document Translation service. @@ -529,7 +478,7 @@ def begin_start_translation( destination, it will be overwritten. The targetUrl for each target language must be unique. - :param body: Required. + :param body: Translation job submission batch request. Required. :type body: IO[bytes] :keyword content_type: Body Parameter content-type. Content type parameter for binary body. Default value is "application/json". @@ -540,10 +489,9 @@ def begin_start_translation( """ @distributed_trace - def begin_start_translation( + def _begin_translation( self, body: Union[_models.StartTranslationDetails, JSON, IO[bytes]], **kwargs: Any ) -> LROPoller[None]: - # pylint: disable=line-too-long """Submit a document translation request to the Document Translation service. Use this API to submit a bulk (batch) translation request to the Document @@ -566,70 +514,12 @@ def begin_start_translation( destination, it will be overwritten. The targetUrl for each target language must be unique. - :param body: Is one of the following types: StartTranslationDetails, JSON, IO[bytes] Required. - :type body: ~azure.ai.translation.document.models._models.StartTranslationDetails or JSON or - IO[bytes] + :param body: Translation job submission batch request. Is one of the following types: + StartTranslationDetails, JSON, IO[bytes] Required. + :type body: ~azure.ai.translation.document.models.StartTranslationDetails or JSON or IO[bytes] :return: An instance of LROPoller that returns None :rtype: ~azure.core.polling.LROPoller[None] :raises ~azure.core.exceptions.HttpResponseError: - - Example: - .. code-block:: python - - # JSON input template you can fill out and use as your body input. - body = { - "inputs": [ - { - "source": { - "sourceUrl": "str", # Location of the folder / - container or single file with your documents. Required. - "filter": { - "prefix": "str", # Optional. A - case-sensitive prefix string to filter documents in the source - path for translation. For example, when using a Azure storage - blob Uri, use the prefix to restrict sub folders for translation. - "suffix": "str" # Optional. A case-sensitive - suffix string to filter documents in the source path for - translation. This is most often use for file extensions. - }, - "language": "str", # Optional. Language code If none - is specified, we will perform auto detect on the document. - "storageSource": "str" # Optional. Storage Source. - "AzureBlob" - }, - "targets": [ - { - "language": "str", # Target Language. - Required. - "targetUrl": "str", # Location of the folder - / container with your documents. Required. - "category": "str", # Optional. Category / - custom system for translation request. - "glossaries": [ - { - "format": "str", # Format. - Required. - "glossaryUrl": "str", # - Location of the glossary. We will use the file extension - to extract the formatting if the format parameter is not - supplied. If the translation language pair is not - present in the glossary, it will not be applied. - Required. - "storageSource": "str", # - Optional. Storage Source. "AzureBlob" - "version": "str" # Optional. - Optional Version. If not specified, default is used. - } - ], - "storageSource": "str" # Optional. Storage - Source. "AzureBlob" - } - ], - "storageType": "str" # Optional. Storage type of the input - documents source string. Known values are: "Folder" and "File". - } - ] - } """ _headers = case_insensitive_dict(kwargs.pop("headers", {}) or {}) _params = kwargs.pop("params", {}) or {} @@ -640,9 +530,10 @@ def begin_start_translation( lro_delay = kwargs.pop("polling_interval", self._config.polling_interval) cont_token: Optional[str] = kwargs.pop("continuation_token", None) if cont_token is None: - raw_result = self._start_translation_initial( # type: ignore + raw_result = self.__begin_translation_initial( body=body, content_type=content_type, cls=lambda x, y, z: x, headers=_headers, params=_params, **kwargs ) + raw_result.http_response.read() # type: ignore kwargs.pop("error_map", None) def get_long_running_output(pipeline_response): # pylint: disable=inconsistent-return-statements @@ -671,7 +562,7 @@ def get_long_running_output(pipeline_response): # pylint: disable=inconsistent- return LROPoller[None](self._client, raw_result, get_long_running_output, polling_method) # type: ignore @distributed_trace - def get_translations_status( + def list_translation_statuses( self, *, top: Optional[int] = None, @@ -681,9 +572,8 @@ def get_translations_status( created_date_time_utc_start: Optional[datetime.datetime] = None, created_date_time_utc_end: Optional[datetime.datetime] = None, orderby: Optional[List[str]] = None, - **kwargs: Any + **kwargs: Any, ) -> Iterable["_models.TranslationStatus"]: - # pylint: disable=line-too-long """Returns a list of batch requests submitted and the status for each request. Returns a list of batch requests submitted and the status for each @@ -778,61 +668,16 @@ def get_translations_status( asc','CreatedDateTimeUtc desc'). Default value is None. :paramtype orderby: list[str] :return: An iterator like instance of TranslationStatus - :rtype: - ~azure.core.paging.ItemPaged[~azure.ai.translation.document.models._models.TranslationStatus] + :rtype: ~azure.core.paging.ItemPaged[~azure.ai.translation.document.models.TranslationStatus] :raises ~azure.core.exceptions.HttpResponseError: - - Example: - .. code-block:: python - - # response body for status code(s): 200 - response == { - "createdDateTimeUtc": "2020-02-20 00:00:00", # Operation created date time. - Required. - "id": "str", # Id of the operation. Required. - "lastActionDateTimeUtc": "2020-02-20 00:00:00", # Date time in which the - operation's status has been updated. Required. - "status": "str", # List of possible statuses for job or document. Required. - Known values are: "NotStarted", "Running", "Succeeded", "Failed", "Cancelled", - "Cancelling", and "ValidationFailed". - "summary": { - "cancelled": 0, # Number of cancelled. Required. - "failed": 0, # Failed count. Required. - "inProgress": 0, # Number of in progress. Required. - "notYetStarted": 0, # Count of not yet started. Required. - "success": 0, # Number of Success. Required. - "total": 0, # Total count. Required. - "totalCharacterCharged": 0 # Total characters charged by the API. - Required. - }, - "error": { - "code": "str", # Enums containing high level error codes. Required. - Known values are: "InvalidRequest", "InvalidArgument", "InternalServerError", - "ServiceUnavailable", "ResourceNotFound", "Unauthorized", and - "RequestRateTooHigh". - "message": "str", # Gets high level error message. Required. - "innerError": { - "code": "str", # Gets code error string. Required. - "message": "str", # Gets high level error message. Required. - "innerError": ..., - "target": "str" # Optional. Gets the source of the error. - For example it would be "documents" or "document id" in case of invalid - document. - }, - "target": "str" # Optional. Gets the source of the error. For - example it would be "documents" or "document id" in case of invalid document. - } - } """ _headers = kwargs.pop("headers", {}) or {} _params = kwargs.pop("params", {}) or {} maxpagesize = kwargs.pop("maxpagesize", None) - cls: ClsType[List[_models.TranslationStatus]] = kwargs.pop( # pylint: disable=protected-access - "cls", None - ) + cls: ClsType[List[_models.TranslationStatus]] = kwargs.pop("cls", None) - error_map: MutableMapping[int, Type[HttpResponseError]] = { + error_map: MutableMapping[int, Type[HttpResponseError]] = { # pylint: disable=unsubscriptable-object 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError, @@ -843,7 +688,7 @@ def get_translations_status( def prepare_request(next_link=None): if not next_link: - _request = build_document_translation_get_translations_status_request( + _request = build_document_translation_list_translation_statuses_request( top=top, skip=skip, maxpagesize=maxpagesize, @@ -896,14 +741,12 @@ def get_next(next_link=None): _request = prepare_request(next_link) _stream = False - pipeline_response: PipelineResponse = self._client._pipeline.run( # type: ignore[annotation-unchecked] # pylint: disable=protected-access + pipeline_response: PipelineResponse = self._client._pipeline.run( # pylint: disable=protected-access _request, stream=_stream, **kwargs ) response = pipeline_response.http_response if response.status_code not in [200]: - if _stream: - response.read() # Load the body in memory and close the socket map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response) @@ -912,61 +755,21 @@ def get_next(next_link=None): return ItemPaged(get_next, extract_data) @distributed_trace - def get_document_status( # pylint: disable=protected-access - self, id: str, document_id: str, **kwargs: Any - ) -> _models.DocumentStatus: - # pylint: disable=line-too-long + def get_document_status(self, translation_id: str, document_id: str, **kwargs: Any) -> _models.DocumentStatus: """Returns the status for a specific document. Returns the translation status for a specific document based on the request Id and document Id. - :param id: Format - uuid. The batch id. Required. - :type id: str + :param translation_id: Format - uuid. The batch id. Required. + :type translation_id: str :param document_id: Format - uuid. The document id. Required. :type document_id: str :return: DocumentStatus. The DocumentStatus is compatible with MutableMapping - :rtype: ~azure.ai.translation.document.models._models.DocumentStatus + :rtype: ~azure.ai.translation.document.models.DocumentStatus :raises ~azure.core.exceptions.HttpResponseError: - - Example: - .. code-block:: python - - # response body for status code(s): 200 - response == { - "createdDateTimeUtc": "2020-02-20 00:00:00", # Operation created date time. - Required. - "id": "str", # Document Id. Required. - "lastActionDateTimeUtc": "2020-02-20 00:00:00", # Date time in which the - operation's status has been updated. Required. - "progress": 0.0, # Progress of the translation if available. Required. - "sourcePath": "str", # Location of the source document. Required. - "status": "str", # List of possible statuses for job or document. Required. - Known values are: "NotStarted", "Running", "Succeeded", "Failed", "Cancelled", - "Cancelling", and "ValidationFailed". - "to": "str", # To language. Required. - "characterCharged": 0, # Optional. Character charged by the API. - "error": { - "code": "str", # Enums containing high level error codes. Required. - Known values are: "InvalidRequest", "InvalidArgument", "InternalServerError", - "ServiceUnavailable", "ResourceNotFound", "Unauthorized", and - "RequestRateTooHigh". - "message": "str", # Gets high level error message. Required. - "innerError": { - "code": "str", # Gets code error string. Required. - "message": "str", # Gets high level error message. Required. - "innerError": ..., - "target": "str" # Optional. Gets the source of the error. - For example it would be "documents" or "document id" in case of invalid - document. - }, - "target": "str" # Optional. Gets the source of the error. For - example it would be "documents" or "document id" in case of invalid document. - }, - "path": "str" # Optional. Location of the document or folder. - } """ - error_map: MutableMapping[int, Type[HttpResponseError]] = { + error_map: MutableMapping[int, Type[HttpResponseError]] = { # pylint: disable=unsubscriptable-object 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError, @@ -977,10 +780,10 @@ def get_document_status( # pylint: disable=protected-access _headers = kwargs.pop("headers", {}) or {} _params = kwargs.pop("params", {}) or {} - cls: ClsType[_models.DocumentStatus] = kwargs.pop("cls", None) # pylint: disable=protected-access + cls: ClsType[_models.DocumentStatus] = kwargs.pop("cls", None) _request = build_document_translation_get_document_status_request( - id=id, + translation_id=translation_id, document_id=document_id, api_version=self._config.api_version, headers=_headers, @@ -1000,16 +803,17 @@ def get_document_status( # pylint: disable=protected-access if response.status_code not in [200]: if _stream: - response.read() # Load the body in memory and close the socket + try: + response.read() # Load the body in memory and close the socket + except (StreamConsumedError, StreamClosedError): + pass map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response) if _stream: deserialized = response.iter_bytes() else: - deserialized = _deserialize( - _models.DocumentStatus, response.json() # pylint: disable=protected-access - ) + deserialized = _deserialize(_models.DocumentStatus, response.json()) if cls: return cls(pipeline_response, deserialized, {}) # type: ignore @@ -1017,10 +821,7 @@ def get_document_status( # pylint: disable=protected-access return deserialized # type: ignore @distributed_trace - def get_translation_status( # pylint: disable=protected-access - self, id: str, **kwargs: Any - ) -> _models.TranslationStatus: - # pylint: disable=line-too-long + def get_translation_status(self, translation_id: str, **kwargs: Any) -> _models.TranslationStatus: """Returns the status for a document translation request. Returns the status for a document translation request. @@ -1028,55 +829,13 @@ def get_translation_status( # pylint: disable=protected-access overall request status, as well as the status for documents that are being translated as part of that request. - :param id: Format - uuid. The operation id. Required. - :type id: str + :param translation_id: Format - uuid. The operation id. Required. + :type translation_id: str :return: TranslationStatus. The TranslationStatus is compatible with MutableMapping - :rtype: ~azure.ai.translation.document.models._models.TranslationStatus + :rtype: ~azure.ai.translation.document.models.TranslationStatus :raises ~azure.core.exceptions.HttpResponseError: - - Example: - .. code-block:: python - - # response body for status code(s): 200 - response == { - "createdDateTimeUtc": "2020-02-20 00:00:00", # Operation created date time. - Required. - "id": "str", # Id of the operation. Required. - "lastActionDateTimeUtc": "2020-02-20 00:00:00", # Date time in which the - operation's status has been updated. Required. - "status": "str", # List of possible statuses for job or document. Required. - Known values are: "NotStarted", "Running", "Succeeded", "Failed", "Cancelled", - "Cancelling", and "ValidationFailed". - "summary": { - "cancelled": 0, # Number of cancelled. Required. - "failed": 0, # Failed count. Required. - "inProgress": 0, # Number of in progress. Required. - "notYetStarted": 0, # Count of not yet started. Required. - "success": 0, # Number of Success. Required. - "total": 0, # Total count. Required. - "totalCharacterCharged": 0 # Total characters charged by the API. - Required. - }, - "error": { - "code": "str", # Enums containing high level error codes. Required. - Known values are: "InvalidRequest", "InvalidArgument", "InternalServerError", - "ServiceUnavailable", "ResourceNotFound", "Unauthorized", and - "RequestRateTooHigh". - "message": "str", # Gets high level error message. Required. - "innerError": { - "code": "str", # Gets code error string. Required. - "message": "str", # Gets high level error message. Required. - "innerError": ..., - "target": "str" # Optional. Gets the source of the error. - For example it would be "documents" or "document id" in case of invalid - document. - }, - "target": "str" # Optional. Gets the source of the error. For - example it would be "documents" or "document id" in case of invalid document. - } - } """ - error_map: MutableMapping[int, Type[HttpResponseError]] = { + error_map: MutableMapping[int, Type[HttpResponseError]] = { # pylint: disable=unsubscriptable-object 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError, @@ -1087,10 +846,10 @@ def get_translation_status( # pylint: disable=protected-access _headers = kwargs.pop("headers", {}) or {} _params = kwargs.pop("params", {}) or {} - cls: ClsType[_models.TranslationStatus] = kwargs.pop("cls", None) # pylint: disable=protected-access + cls: ClsType[_models.TranslationStatus] = kwargs.pop("cls", None) _request = build_document_translation_get_translation_status_request( - id=id, + translation_id=translation_id, api_version=self._config.api_version, headers=_headers, params=_params, @@ -1109,16 +868,17 @@ def get_translation_status( # pylint: disable=protected-access if response.status_code not in [200]: if _stream: - response.read() # Load the body in memory and close the socket + try: + response.read() # Load the body in memory and close the socket + except (StreamConsumedError, StreamClosedError): + pass map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response) if _stream: deserialized = response.iter_bytes() else: - deserialized = _deserialize( - _models.TranslationStatus, response.json() # pylint: disable=protected-access - ) + deserialized = _deserialize(_models.TranslationStatus, response.json()) if cls: return cls(pipeline_response, deserialized, {}) # type: ignore @@ -1126,10 +886,7 @@ def get_translation_status( # pylint: disable=protected-access return deserialized # type: ignore @distributed_trace - def cancel_translation( # pylint: disable=protected-access - self, id: str, **kwargs: Any - ) -> _models.TranslationStatus: - # pylint: disable=line-too-long + def cancel_translation(self, translation_id: str, **kwargs: Any) -> _models.TranslationStatus: """Cancel a currently processing or queued translation. Cancel a currently processing or queued translation. @@ -1141,55 +898,13 @@ def cancel_translation( # pylint: disable=protected-access All pending documents will be cancelled if possible. - :param id: Format - uuid. The operation-id. Required. - :type id: str + :param translation_id: Format - uuid. The operation-id. Required. + :type translation_id: str :return: TranslationStatus. The TranslationStatus is compatible with MutableMapping - :rtype: ~azure.ai.translation.document.models._models.TranslationStatus + :rtype: ~azure.ai.translation.document.models.TranslationStatus :raises ~azure.core.exceptions.HttpResponseError: - - Example: - .. code-block:: python - - # response body for status code(s): 200 - response == { - "createdDateTimeUtc": "2020-02-20 00:00:00", # Operation created date time. - Required. - "id": "str", # Id of the operation. Required. - "lastActionDateTimeUtc": "2020-02-20 00:00:00", # Date time in which the - operation's status has been updated. Required. - "status": "str", # List of possible statuses for job or document. Required. - Known values are: "NotStarted", "Running", "Succeeded", "Failed", "Cancelled", - "Cancelling", and "ValidationFailed". - "summary": { - "cancelled": 0, # Number of cancelled. Required. - "failed": 0, # Failed count. Required. - "inProgress": 0, # Number of in progress. Required. - "notYetStarted": 0, # Count of not yet started. Required. - "success": 0, # Number of Success. Required. - "total": 0, # Total count. Required. - "totalCharacterCharged": 0 # Total characters charged by the API. - Required. - }, - "error": { - "code": "str", # Enums containing high level error codes. Required. - Known values are: "InvalidRequest", "InvalidArgument", "InternalServerError", - "ServiceUnavailable", "ResourceNotFound", "Unauthorized", and - "RequestRateTooHigh". - "message": "str", # Gets high level error message. Required. - "innerError": { - "code": "str", # Gets code error string. Required. - "message": "str", # Gets high level error message. Required. - "innerError": ..., - "target": "str" # Optional. Gets the source of the error. - For example it would be "documents" or "document id" in case of invalid - document. - }, - "target": "str" # Optional. Gets the source of the error. For - example it would be "documents" or "document id" in case of invalid document. - } - } """ - error_map: MutableMapping[int, Type[HttpResponseError]] = { + error_map: MutableMapping[int, Type[HttpResponseError]] = { # pylint: disable=unsubscriptable-object 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError, @@ -1200,10 +915,10 @@ def cancel_translation( # pylint: disable=protected-access _headers = kwargs.pop("headers", {}) or {} _params = kwargs.pop("params", {}) or {} - cls: ClsType[_models.TranslationStatus] = kwargs.pop("cls", None) # pylint: disable=protected-access + cls: ClsType[_models.TranslationStatus] = kwargs.pop("cls", None) _request = build_document_translation_cancel_translation_request( - id=id, + translation_id=translation_id, api_version=self._config.api_version, headers=_headers, params=_params, @@ -1222,16 +937,17 @@ def cancel_translation( # pylint: disable=protected-access if response.status_code not in [200]: if _stream: - response.read() # Load the body in memory and close the socket + try: + response.read() # Load the body in memory and close the socket + except (StreamConsumedError, StreamClosedError): + pass map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response) if _stream: deserialized = response.iter_bytes() else: - deserialized = _deserialize( - _models.TranslationStatus, response.json() # pylint: disable=protected-access - ) + deserialized = _deserialize(_models.TranslationStatus, response.json()) if cls: return cls(pipeline_response, deserialized, {}) # type: ignore @@ -1239,9 +955,9 @@ def cancel_translation( # pylint: disable=protected-access return deserialized # type: ignore @distributed_trace - def get_documents_status( + def list_document_statuses( self, - id: str, + translation_id: str, *, top: Optional[int] = None, skip: Optional[int] = None, @@ -1250,9 +966,8 @@ def get_documents_status( created_date_time_utc_start: Optional[datetime.datetime] = None, created_date_time_utc_end: Optional[datetime.datetime] = None, orderby: Optional[List[str]] = None, - **kwargs: Any + **kwargs: Any, ) -> Iterable["_models.DocumentStatus"]: - # pylint: disable=line-too-long """Returns the status for all documents in a batch document translation request. Returns the status for all documents in a batch document translation request. @@ -1300,8 +1015,8 @@ def get_documents_status( This reduces the risk of the client making assumptions about the data returned. - :param id: Format - uuid. The operation id. Required. - :type id: str + :param translation_id: Format - uuid. The operation id. Required. + :type translation_id: str :keyword top: top indicates the total number of records the user wants to be returned across all pages. @@ -1343,54 +1058,16 @@ def get_documents_status( asc','CreatedDateTimeUtc desc'). Default value is None. :paramtype orderby: list[str] :return: An iterator like instance of DocumentStatus - :rtype: - ~azure.core.paging.ItemPaged[~azure.ai.translation.document.models._models.DocumentStatus] + :rtype: ~azure.core.paging.ItemPaged[~azure.ai.translation.document.models.DocumentStatus] :raises ~azure.core.exceptions.HttpResponseError: - - Example: - .. code-block:: python - - # response body for status code(s): 200 - response == { - "createdDateTimeUtc": "2020-02-20 00:00:00", # Operation created date time. - Required. - "id": "str", # Document Id. Required. - "lastActionDateTimeUtc": "2020-02-20 00:00:00", # Date time in which the - operation's status has been updated. Required. - "progress": 0.0, # Progress of the translation if available. Required. - "sourcePath": "str", # Location of the source document. Required. - "status": "str", # List of possible statuses for job or document. Required. - Known values are: "NotStarted", "Running", "Succeeded", "Failed", "Cancelled", - "Cancelling", and "ValidationFailed". - "to": "str", # To language. Required. - "characterCharged": 0, # Optional. Character charged by the API. - "error": { - "code": "str", # Enums containing high level error codes. Required. - Known values are: "InvalidRequest", "InvalidArgument", "InternalServerError", - "ServiceUnavailable", "ResourceNotFound", "Unauthorized", and - "RequestRateTooHigh". - "message": "str", # Gets high level error message. Required. - "innerError": { - "code": "str", # Gets code error string. Required. - "message": "str", # Gets high level error message. Required. - "innerError": ..., - "target": "str" # Optional. Gets the source of the error. - For example it would be "documents" or "document id" in case of invalid - document. - }, - "target": "str" # Optional. Gets the source of the error. For - example it would be "documents" or "document id" in case of invalid document. - }, - "path": "str" # Optional. Location of the document or folder. - } """ _headers = kwargs.pop("headers", {}) or {} _params = kwargs.pop("params", {}) or {} maxpagesize = kwargs.pop("maxpagesize", None) - cls: ClsType[List[_models.DocumentStatus]] = kwargs.pop("cls", None) # pylint: disable=protected-access + cls: ClsType[List[_models.DocumentStatus]] = kwargs.pop("cls", None) - error_map: MutableMapping[int, Type[HttpResponseError]] = { + error_map: MutableMapping[int, Type[HttpResponseError]] = { # pylint: disable=unsubscriptable-object 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError, @@ -1401,8 +1078,8 @@ def get_documents_status( def prepare_request(next_link=None): if not next_link: - _request = build_document_translation_get_documents_status_request( - id=id, + _request = build_document_translation_list_document_statuses_request( + translation_id=translation_id, top=top, skip=skip, maxpagesize=maxpagesize, @@ -1455,14 +1132,12 @@ def get_next(next_link=None): _request = prepare_request(next_link) _stream = False - pipeline_response: PipelineResponse = self._client._pipeline.run( # type: ignore[annotation-unchecked] # pylint: disable=protected-access + pipeline_response: PipelineResponse = self._client._pipeline.run( # pylint: disable=protected-access _request, stream=_stream, **kwargs ) response = pipeline_response.http_response if response.status_code not in [200]: - if _stream: - response.read() # Load the body in memory and close the socket map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response) @@ -1471,9 +1146,9 @@ def get_next(next_link=None): return ItemPaged(get_next, extract_data) @distributed_trace - def get_supported_formats( # pylint: disable=protected-access - self, *, type: Optional[Union[str, _enums.FileFormatType]] = None, **kwargs: Any - ) -> _models.SupportedFileFormats: + def _get_supported_formats( # pylint: disable=protected-access + self, *, type: Optional[Union[str, _models.FileFormatType]] = None, **kwargs: Any + ) -> _models._models.SupportedFileFormats: """Returns a list of supported document formats. The list of supported formats supported by the Document Translation @@ -1487,34 +1162,8 @@ def get_supported_formats( # pylint: disable=protected-access :return: SupportedFileFormats. The SupportedFileFormats is compatible with MutableMapping :rtype: ~azure.ai.translation.document.models._models.SupportedFileFormats :raises ~azure.core.exceptions.HttpResponseError: - - Example: - .. code-block:: python - - # response body for status code(s): 200 - response == { - "value": [ - { - "contentTypes": [ - "str" # Supported Content-Types for this format. - Required. - ], - "fileExtensions": [ - "str" # Supported file extension for this format. - Required. - ], - "format": "str", # Name of the format. Required. - "defaultVersion": "str", # Optional. Default version if none - is specified. - "type": "str", # Optional. Supported Type for this format. - "versions": [ - "str" # Optional. Supported Version. - ] - } - ] - } """ - error_map: MutableMapping[int, Type[HttpResponseError]] = { + error_map: MutableMapping[int, Type[HttpResponseError]] = { # pylint: disable=unsubscriptable-object 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError, @@ -1525,7 +1174,7 @@ def get_supported_formats( # pylint: disable=protected-access _headers = kwargs.pop("headers", {}) or {} _params = kwargs.pop("params", {}) or {} - cls: ClsType[_models.SupportedFileFormats] = kwargs.pop("cls", None) # pylint: disable=protected-access + cls: ClsType[_models._models.SupportedFileFormats] = kwargs.pop("cls", None) # pylint: disable=protected-access _request = build_document_translation_get_supported_formats_request( type=type, @@ -1547,7 +1196,10 @@ def get_supported_formats( # pylint: disable=protected-access if response.status_code not in [200]: if _stream: - response.read() # Load the body in memory and close the socket + try: + response.read() # Load the body in memory and close the socket + except (StreamConsumedError, StreamClosedError): + pass map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response) @@ -1555,7 +1207,7 @@ def get_supported_formats( # pylint: disable=protected-access deserialized = response.iter_bytes() else: deserialized = _deserialize( - _models.SupportedFileFormats, response.json() # pylint: disable=protected-access + _models._models.SupportedFileFormats, response.json() # pylint: disable=protected-access ) if cls: @@ -1577,13 +1229,13 @@ def document_translate( source_language: Optional[str] = None, category: Optional[str] = None, allow_fallback: Optional[bool] = None, - **kwargs: Any + **kwargs: Any, ) -> Iterator[bytes]: """Submit a single document translation request to the Document Translation service. Use this API to submit a single translation request to the Document Translation Service. - :param body: Required. + :param body: Document Translate Request Content. Required. :type body: ~azure.ai.translation.document.models.DocumentTranslateContent :keyword target_language: Specifies the language of the output document. The target language must be one of the supported languages included in the translation scope. @@ -1610,15 +1262,6 @@ def document_translate( :return: Iterator[bytes] :rtype: Iterator[bytes] :raises ~azure.core.exceptions.HttpResponseError: - - Example: - .. code-block:: python - - # JSON input template you can fill out and use as your body input. - body = { - "document": filetype, - "glossary": [filetype] - } """ @overload @@ -1630,13 +1273,13 @@ def document_translate( source_language: Optional[str] = None, category: Optional[str] = None, allow_fallback: Optional[bool] = None, - **kwargs: Any + **kwargs: Any, ) -> Iterator[bytes]: """Submit a single document translation request to the Document Translation service. Use this API to submit a single translation request to the Document Translation Service. - :param body: Required. + :param body: Document Translate Request Content. Required. :type body: JSON :keyword target_language: Specifies the language of the output document. The target language must be one of the supported languages included in the translation scope. @@ -1674,13 +1317,14 @@ def document_translate( source_language: Optional[str] = None, category: Optional[str] = None, allow_fallback: Optional[bool] = None, - **kwargs: Any + **kwargs: Any, ) -> Iterator[bytes]: """Submit a single document translation request to the Document Translation service. Use this API to submit a single translation request to the Document Translation Service. - :param body: Is either a DocumentTranslateContent type or a JSON type. Required. + :param body: Document Translate Request Content. Is either a DocumentTranslateContent type or a + JSON type. Required. :type body: ~azure.ai.translation.document.models.DocumentTranslateContent or JSON :keyword target_language: Specifies the language of the output document. The target language must be one of the supported languages included in the translation scope. @@ -1707,17 +1351,8 @@ def document_translate( :return: Iterator[bytes] :rtype: Iterator[bytes] :raises ~azure.core.exceptions.HttpResponseError: - - Example: - .. code-block:: python - - # JSON input template you can fill out and use as your body input. - body = { - "document": filetype, - "glossary": [filetype] - } """ - error_map: MutableMapping[int, Type[HttpResponseError]] = { + error_map: MutableMapping[int, Type[HttpResponseError]] = { # pylint: disable=unsubscriptable-object 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError, @@ -1760,7 +1395,10 @@ def document_translate( if response.status_code not in [200]: if _stream: - response.read() # Load the body in memory and close the socket + try: + response.read() # Load the body in memory and close the socket + except (StreamConsumedError, StreamClosedError): + pass map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response) @@ -1768,6 +1406,7 @@ def document_translate( response_headers["x-ms-client-request-id"] = self._deserialize( "str", response.headers.get("x-ms-client-request-id") ) + response_headers["content-type"] = self._deserialize("str", response.headers.get("content-type")) deserialized = response.iter_bytes() diff --git a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_operations/_patch.py b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_operations/_patch.py index 6bbb44de7c92..f7dd32510333 100644 --- a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_operations/_patch.py +++ b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_operations/_patch.py @@ -2,321 +2,13 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT License. # ------------------------------------ -# pylint: disable=too-many-lines """Customize generated code here. Follow our quickstart for examples: https://aka.ms/azsdk/python/dpcodegen/python/customize """ +from typing import List -import sys -from typing import Any, IO, Callable, Dict, Iterator, List, Optional, Type, TypeVar, Union, cast, overload -from azure.core.polling import NoPolling, PollingMethod -from azure.core.polling.base_polling import LROBasePolling -from azure.core.tracing.decorator import distributed_trace -from azure.core.utils import case_insensitive_dict -from azure.core.rest import HttpRequest, HttpResponse -from azure.core.pipeline import PipelineResponse -from azure.core.exceptions import ( - ClientAuthenticationError, - HttpResponseError, - ResourceExistsError, - ResourceNotFoundError, - ResourceNotModifiedError, - map_error, -) -from ..models import _models -from .. import _model_base -from .._model_base import _deserialize -from ._operations import ( - DocumentTranslationClientOperationsMixin as GeneratedDocumentTranslationClientOperationsMixin, - SingleDocumentTranslationClientOperationsMixin as GeneratedSingleDocumentTranslationClientOperationsMixin, - JSON, - ClsType, - build_single_document_translation_document_translate_request, -) -from .._patch import DocumentTranslationLROPoller -from .._vendor import prepare_multipart_form_data - -if sys.version_info >= (3, 9): - from collections.abc import MutableMapping -else: - from typing import MutableMapping # type: ignore # pylint: disable=ungrouped-imports -JSON = MutableMapping[str, Any] # type: ignore[misc] # pylint: disable=unsubscriptable-object -T = TypeVar("T") -ClsType = Optional[Callable[[PipelineResponse[HttpRequest, HttpResponse], T, Dict[str, Any]], Any]] # type: ignore - - -class DocumentTranslationClientOperationsMixin(GeneratedDocumentTranslationClientOperationsMixin): - - @distributed_trace - def begin_start_translation( # type: ignore[override] - self, body: Union[_models.StartTranslationDetails, JSON, IO[bytes]], **kwargs: Any - ) -> DocumentTranslationLROPoller[_models.TranslationStatus]: - _headers = case_insensitive_dict(kwargs.pop("headers", {}) or {}) - _params = kwargs.pop("params", {}) or {} - - content_type: Optional[str] = kwargs.pop("content_type", _headers.pop("Content-Type", None)) - cls: ClsType[_models.TranslationStatus] = kwargs.pop("cls", None) - polling: Union[bool, PollingMethod] = kwargs.pop("polling", True) - lro_delay = kwargs.pop("polling_interval", self._config.polling_interval) - cont_token: Optional[str] = kwargs.pop("continuation_token", None) - if cont_token is None: - raw_result = self._start_translation_initial( # type: ignore[func-returns-value] - body=body, content_type=content_type, cls=lambda x, y, z: x, headers=_headers, params=_params, **kwargs - ) - kwargs.pop("error_map", None) - - def get_long_running_output(pipeline_response): - response_headers = {} - response = pipeline_response.http_response - response_headers["Operation-Location"] = self._deserialize( - "str", response.headers.get("Operation-Location") - ) - - deserialized = _deserialize(_models.TranslationStatus, response.json()) - if cls: - return cls(pipeline_response, deserialized, response_headers) - return deserialized - - path_format_arguments = { - "endpoint": self._serialize.url("self._config.endpoint", self._config.endpoint, "str", skip_quote=True), - } - - if polling is True: - polling_method: PollingMethod = cast( - PollingMethod, LROBasePolling(lro_delay, path_format_arguments=path_format_arguments, **kwargs) - ) - elif polling is False: - polling_method = cast(PollingMethod, NoPolling()) - else: - polling_method = polling - if cont_token: - return DocumentTranslationLROPoller[_models.TranslationStatus].from_continuation_token( - polling_method=polling_method, - continuation_token=cont_token, - client=self._client, - deserialization_callback=get_long_running_output, - ) - return DocumentTranslationLROPoller[_models.TranslationStatus]( - self._client, raw_result, get_long_running_output, polling_method - ) - - -class SingleDocumentTranslationClientOperationsMixin( - GeneratedSingleDocumentTranslationClientOperationsMixin -): # pylint: disable=name-too-long - - @overload - def document_translate( - self, - body: _models.DocumentTranslateContent, - *, - target_language: str, - source_language: Optional[str] = None, - category: Optional[str] = None, - allow_fallback: Optional[bool] = None, - **kwargs: Any - ) -> Iterator[bytes]: - """Submit a single document translation request to the Document Translation service. - - Use this API to submit a single translation request to the Document Translation Service. - - :param body: Required. - :type body: ~azure.ai.translation.document.models.DocumentTranslateContent - :keyword target_language: Specifies the language of the output document. - The target language must be one of the supported languages included in the translation scope. - For example if you want to translate the document in German language, then use - targetLanguage=de. Required. - :paramtype target_language: str - :keyword source_language: Specifies source language of the input document. - If this parameter isn't specified, automatic language detection is applied to determine the - source language. - For example if the source document is written in English, then use sourceLanguage=en. Default - value is None. - :paramtype source_language: str - :keyword category: A string specifying the category (domain) of the translation. This parameter - is used to get translations - from a customized system built with Custom Translator. Add the Category ID from your Custom - Translator - project details to this parameter to use your deployed customized system. Default value is: - general. Default value is None. - :paramtype category: str - :keyword allow_fallback: Specifies that the service is allowed to fall back to a general system - when a custom system doesn't exist. - Possible values are: true (default) or false. Default value is None. - :paramtype allow_fallback: bool - :return: Iterator[bytes] - :rtype: Iterator[bytes] - :raises ~azure.core.exceptions.HttpResponseError: - - Example: - .. code-block:: python - - # JSON input template you can fill out and use as your body input. - body = { - "document": filetype, - "glossary": [filetype] - } - """ - - @overload - def document_translate( - self, - body: JSON, - *, - target_language: str, - source_language: Optional[str] = None, - category: Optional[str] = None, - allow_fallback: Optional[bool] = None, - **kwargs: Any - ) -> Iterator[bytes]: - """Submit a single document translation request to the Document Translation service. - - Use this API to submit a single translation request to the Document Translation Service. - - :param body: Required. - :type body: JSON - :keyword target_language: Specifies the language of the output document. - The target language must be one of the supported languages included in the translation scope. - For example if you want to translate the document in German language, then use - targetLanguage=de. Required. - :paramtype target_language: str - :keyword source_language: Specifies source language of the input document. - If this parameter isn't specified, automatic language detection is applied to determine the - source language. - For example if the source document is written in English, then use sourceLanguage=en. Default - value is None. - :paramtype source_language: str - :keyword category: A string specifying the category (domain) of the translation. This parameter - is used to get translations - from a customized system built with Custom Translator. Add the Category ID from your Custom - Translator - project details to this parameter to use your deployed customized system. Default value is: - general. Default value is None. - :paramtype category: str - :keyword allow_fallback: Specifies that the service is allowed to fall back to a general system - when a custom system doesn't exist. - Possible values are: true (default) or false. Default value is None. - :paramtype allow_fallback: bool - :return: Iterator[bytes] - :rtype: Iterator[bytes] - :raises ~azure.core.exceptions.HttpResponseError: - """ - - @distributed_trace - def document_translate( - self, - body: Union[_models.DocumentTranslateContent, JSON], - *, - target_language: str, - source_language: Optional[str] = None, - category: Optional[str] = None, - allow_fallback: Optional[bool] = None, - **kwargs: Any - ) -> Iterator[bytes]: - """Submit a single document translation request to the Document Translation service. - - Use this API to submit a single translation request to the Document Translation Service. - - :param body: Is either a DocumentTranslateContent type or a JSON type. Required. - :type body: ~azure.ai.translation.document.models.DocumentTranslateContent or JSON - :keyword target_language: Specifies the language of the output document. - The target language must be one of the supported languages included in the translation scope. - For example if you want to translate the document in German language, then use - targetLanguage=de. Required. - :paramtype target_language: str - :keyword source_language: Specifies source language of the input document. - If this parameter isn't specified, automatic language detection is applied to determine the - source language. - For example if the source document is written in English, then use sourceLanguage=en. Default - value is None. - :paramtype source_language: str - :keyword category: A string specifying the category (domain) of the translation. This parameter - is used to get translations - from a customized system built with Custom Translator. Add the Category ID from your Custom - Translator - project details to this parameter to use your deployed customized system. Default value is: - general. Default value is None. - :paramtype category: str - :keyword allow_fallback: Specifies that the service is allowed to fall back to a general system - when a custom system doesn't exist. - Possible values are: true (default) or false. Default value is None. - :paramtype allow_fallback: bool - :return: Iterator[bytes] - :rtype: Iterator[bytes] - :raises ~azure.core.exceptions.HttpResponseError: - - Example: - .. code-block:: python - - # JSON input template you can fill out and use as your body input. - body = { - "document": filetype, - "glossary": [filetype] - } - """ - error_map: MutableMapping[int, Type[HttpResponseError]] = { - 401: ClientAuthenticationError, - 404: ResourceNotFoundError, - 409: ResourceExistsError, - 304: ResourceNotModifiedError, - } - error_map.update(kwargs.pop("error_map", {}) or {}) - - _headers = kwargs.pop("headers", {}) or {} - _params = kwargs.pop("params", {}) or {} - - cls: ClsType[Iterator[bytes]] = kwargs.pop("cls", None) - - _body = body.as_dict() if isinstance(body, _model_base.Model) else body - _file_fields: List[str] = ["document", "glossary"] - _data_fields: List[str] = [] - _files, _data = prepare_multipart_form_data(_body, _file_fields, _data_fields) - - _request = build_single_document_translation_document_translate_request( - target_language=target_language, - source_language=source_language, - category=category, - allow_fallback=allow_fallback, - api_version=self._config.api_version, - files=_files, - data=_data, - headers=_headers, - params=_params, - ) - path_format_arguments = { - "endpoint": self._serialize.url("self._config.endpoint", self._config.endpoint, "str", skip_quote=True), - } - _request.url = self._client.format_url(_request.url, **path_format_arguments) - - _stream = kwargs.pop("stream", True) - pipeline_response: PipelineResponse = self._client._pipeline.run( # pylint: disable=protected-access - _request, stream=_stream, **kwargs - ) - - response = pipeline_response.http_response - - if response.status_code not in [200]: - if _stream: - response.read() # Load the body in memory and close the socket - map_error(status_code=response.status_code, response=response, error_map=error_map) - raise HttpResponseError(response=response) - - response_headers = {} - response_headers["x-ms-request-id"] = self._deserialize("str", response.headers.get("x-ms-request-id")) - # deserialized = response.iter_bytes() - - if cls: - # return cls(pipeline_response, deserialized, response_headers) - return cls(pipeline_response, response.read(), response_headers) # type: ignore - - return response.read() - - -__all__: List[str] = [ - "DocumentTranslationClientOperationsMixin", - "SingleDocumentTranslationClientOperationsMixin", -] # Add all objects you want publicly available to users at this package level +__all__: List[str] = [] # Add all objects you want publicly available to users at this package level def patch_sdk(): diff --git a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_patch.py b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_patch.py index 00c6bcfef821..f7dd32510333 100644 --- a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_patch.py +++ b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_patch.py @@ -2,1378 +2,13 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT License. # ------------------------------------ -# mypy: disable-error-code="attr-defined" -# pylint: disable=too-many-lines +"""Customize generated code here. -from typing import Any, List, Union, overload, Optional, cast, Tuple, TypeVar, Dict -from enum import Enum -import json -import datetime +Follow our quickstart for examples: https://aka.ms/azsdk/python/dpcodegen/python/customize +""" +from typing import List -from azure.core import CaseInsensitiveEnumMeta -from azure.core.tracing.decorator import distributed_trace -from azure.core.paging import ItemPaged -from azure.core.credentials import AzureKeyCredential, TokenCredential -from azure.core.pipeline.policies import HttpLoggingPolicy -from azure.core.exceptions import HttpResponseError, ODataV4Format -from azure.core.pipeline import PipelineResponse -from azure.core.rest import ( - HttpResponse, - AsyncHttpResponse, - HttpRequest, -) -from azure.core.polling import LROPoller -from azure.core.polling.base_polling import ( - LROBasePolling, - OperationResourcePolling, - _is_empty, - _as_json, - BadResponse, - OperationFailed, - _raise_if_bad_http_status_and_method, -) - -from .models._models import ( - BatchRequest as _BatchRequest, - SourceInput as _SourceInput, - TargetInput as _TargetInput, - DocumentFilter as _DocumentFilter, - StartTranslationDetails as _StartTranslationDetails, - Glossary as _Glossary, - TranslationStatus as _TranslationStatus, - DocumentStatus as _DocumentStatus, -) -from .models._enums import StorageInputType - -COGNITIVE_KEY_HEADER = "Ocp-Apim-Subscription-Key" -POLLING_INTERVAL = 1 - -ResponseType = Union[HttpResponse, AsyncHttpResponse] -PipelineResponseType = PipelineResponse[HttpRequest, ResponseType] -PollingReturnType_co = TypeVar("PollingReturnType_co", covariant=True) - -_FINISHED = frozenset(["succeeded", "cancelled", "cancelling", "failed"]) -_FAILED = frozenset(["validationfailed"]) - - -def convert_status(status, ll=False): - if ll is False: - if status == "Cancelled": - return "Canceled" - if status == "Cancelling": - return "Canceling" - elif ll is True: - if status == "Canceled": - return "Cancelled" - if status == "Canceling": - return "Cancelling" - return status - - -class TranslationGlossary: - """Glossary / translation memory to apply to the translation. - - :param str glossary_url: Required. Location of the glossary file. This should be a URL to - the glossary file in the storage blob container. The URL can be a SAS URL (see the - service documentation for the supported SAS permissions for accessing storage - containers/blobs: https://aka.ms/azsdk/documenttranslation/sas-permissions) or a managed identity - can be created and used to access documents in your storage account - (see https://aka.ms/azsdk/documenttranslation/managed-identity). Note that if the translation - language pair is not present in the glossary, it will not be applied. - :param str file_format: Required. Format of the glossary file. To see supported formats, - call the :func:`~DocumentTranslationClient.get_supported_glossary_formats()` client method. - :keyword Optional[str] format_version: File format version. If not specified, the service will - use the default_version for the file format returned from the - :func:`~DocumentTranslationClient.get_supported_glossary_formats()` client method. - :keyword Optional[str] storage_source: Storage Source. Default value: "AzureBlob". - Currently only "AzureBlob" is supported. - """ - - glossary_url: str - """Location of the glossary file. This should be a URL to - the glossary file in the storage blob container. The URL can be a SAS URL (see the - service documentation for the supported SAS permissions for accessing storage - containers/blobs: https://aka.ms/azsdk/documenttranslation/sas-permissions) or a managed identity - can be created and used to access documents in your storage account - (see https://aka.ms/azsdk/documenttranslation/managed-identity). Note that if the translation - language pair is not present in the glossary, it will not be applied.""" - file_format: str - """Format of the glossary file. To see supported formats, - call the :func:`~DocumentTranslationClient.get_supported_glossary_formats()` client method.""" - format_version: Optional[str] = None - """File format version. If not specified, the service will - use the default_version for the file format returned from the - :func:`~DocumentTranslationClient.get_supported_glossary_formats()` client method.""" - storage_source: Optional[str] = None - """Storage Source. Default value: "AzureBlob". - Currently only "AzureBlob" is supported.""" - - def __init__( - self, - glossary_url: str, - file_format: str, - *, - format_version: Optional[str] = None, - storage_source: Optional[str] = None - ) -> None: - self.glossary_url = glossary_url - self.file_format = file_format - self.format_version = format_version - self.storage_source = storage_source - - def _to_generated(self): - return _Glossary( - glossary_url=self.glossary_url, - format=self.file_format, - version=self.format_version, - storage_source=self.storage_source, - ) - - @staticmethod - def _to_generated_list(glossaries): - return [glossary._to_generated() for glossary in glossaries] # pylint: disable=protected-access - - def __repr__(self) -> str: - return ( - "TranslationGlossary(glossary_url={}, " - "file_format={}, format_version={}, storage_source={})".format( - self.glossary_url, - self.file_format, - self.format_version, - self.storage_source, - )[:1024] - ) - - -class TranslationTarget: - """Destination for the finished translated documents. - - :param str target_url: Required. The target location for your translated documents. - This can be a SAS URL (see the service documentation for the supported SAS permissions - for accessing target storage containers/blobs: https://aka.ms/azsdk/documenttranslation/sas-permissions) - or a managed identity can be created and used to access documents in your storage account - (see https://aka.ms/azsdk/documenttranslation/managed-identity). - :param str language: Required. Target Language Code. This is the language - you want your documents to be translated to. See supported languages here: - https://docs.microsoft.com/azure/cognitive-services/translator/language-support#translate - :keyword Optional[str] category_id: Category / custom model ID for using custom translation. - :keyword glossaries: Glossaries to apply to translation. - :paramtype glossaries: Optional[list[~azure.ai.translation.document.TranslationGlossary]] - :keyword Optional[str] storage_source: Storage Source. Default value: "AzureBlob". - Currently only "AzureBlob" is supported. - """ - - target_url: str - """The target location for your translated documents. - This can be a SAS URL (see the service documentation for the supported SAS permissions - for accessing target storage containers/blobs: https://aka.ms/azsdk/documenttranslation/sas-permissions) - or a managed identity can be created and used to access documents in your storage account - (see https://aka.ms/azsdk/documenttranslation/managed-identity).""" - language: str - """Target Language Code. This is the language - you want your documents to be translated to. See supported languages here: - https://docs.microsoft.com/azure/cognitive-services/translator/language-support#translate""" - category_id: Optional[str] = None - """Category / custom model ID for using custom translation.""" - glossaries: Optional[List[TranslationGlossary]] = None - """Glossaries to apply to translation.""" - storage_source: Optional[str] = None - """Storage Source. Default value: "AzureBlob". - Currently only "AzureBlob" is supported.""" - - def __init__( - self, - target_url: str, - language: str, - *, - category_id: Optional[str] = None, - glossaries: Optional[List[TranslationGlossary]] = None, - storage_source: Optional[str] = None - ) -> None: - self.target_url = target_url - self.language = language - self.category_id = category_id - self.glossaries = glossaries - self.storage_source = storage_source - - def _to_generated(self): - return _TargetInput( - target_url=self.target_url, - category=self.category_id, - language=self.language, - storage_source=self.storage_source, - glossaries=( - TranslationGlossary._to_generated_list(self.glossaries) # pylint: disable=protected-access - if self.glossaries - else None - ), - ) - - @staticmethod - def _to_generated_list(targets): - return [target._to_generated() for target in targets] # pylint: disable=protected-access - - def __repr__(self) -> str: - return ( - "TranslationTarget(target_url={}, language={}, " - "category_id={}, glossaries={}, storage_source={})".format( - self.target_url, - self.language, - self.category_id, - repr(self.glossaries), - self.storage_source, - )[:1024] - ) - - -class DocumentTranslationInput: - """Input for translation. This requires that you have your source document or - documents in an Azure Blob Storage container. Provide a URL to the source file or - source container containing the documents for translation. The source document(s) are - translated and written to the location provided by the TranslationTargets. - - :param str source_url: Required. Location of the folder / container or single file with your - documents. This can be a SAS URL (see the service documentation for the supported SAS permissions - for accessing source storage containers/blobs: https://aka.ms/azsdk/documenttranslation/sas-permissions) - or a managed identity can be created and used to access documents in your storage account - (see https://aka.ms/azsdk/documenttranslation/managed-identity). - :param targets: Required. Location of the destination for the output. This is a list of - TranslationTargets. Note that a TranslationTarget is required for each language code specified. - :type targets: list[~azure.ai.translation.document.TranslationTarget] - :keyword Optional[str] source_language: Language code for the source documents. - If none is specified, the source language will be auto-detected for each document. - :keyword Optional[str] prefix: A case-sensitive prefix string to filter documents in the source path for - translation. For example, when using a Azure storage blob Uri, use the prefix to restrict - sub folders for translation. - :keyword Optional[str] suffix: A case-sensitive suffix string to filter documents in the source path for - translation. This is most often use for file extensions. - :keyword storage_type: Storage type of the input documents source string. Possible values - include: "Folder", "File". - :paramtype storage_type: Optional[str or ~azure.ai.translation.document.StorageInputType] - :keyword Optional[str] storage_source: Storage Source. Default value: "AzureBlob". - Currently only "AzureBlob" is supported. - """ - - source_url: str - """Location of the folder / container or single file with your - documents. This can be a SAS URL (see the service documentation for the supported SAS permissions - for accessing source storage containers/blobs: https://aka.ms/azsdk/documenttranslation/sas-permissions) - or a managed identity can be created and used to access documents in your storage account - (see https://aka.ms/azsdk/documenttranslation/managed-identity).""" - targets: List[TranslationTarget] - """Location of the destination for the output. This is a list of - TranslationTargets. Note that a TranslationTarget is required for each language code specified.""" - source_language: Optional[str] = None - """Language code for the source documents. - If none is specified, the source language will be auto-detected for each document.""" - storage_type: Optional[Union[str, StorageInputType]] = None - """Storage type of the input documents source string. Possible values - include: "Folder", "File".""" - storage_source: Optional[str] = None - """Storage Source. Default value: "AzureBlob". - Currently only "AzureBlob" is supported.""" - prefix: Optional[str] = None - """A case-sensitive prefix string to filter documents in the source path for - translation. For example, when using a Azure storage blob Uri, use the prefix to restrict - sub folders for translation.""" - suffix: Optional[str] = None - """A case-sensitive suffix string to filter documents in the source path for - translation. This is most often use for file extensions.""" - - def __init__( - self, - source_url: str, - targets: List[TranslationTarget], - *, - source_language: Optional[str] = None, - storage_type: Optional[Union[str, StorageInputType]] = None, - storage_source: Optional[str] = None, - prefix: Optional[str] = None, - suffix: Optional[str] = None - ) -> None: - self.source_url = source_url - self.targets = targets - self.source_language = source_language - self.storage_type = storage_type - self.storage_source = storage_source - self.prefix = prefix - self.suffix = suffix - - def _to_generated(self): - return _BatchRequest( - source=_SourceInput( - source_url=self.source_url, - filter=_DocumentFilter(prefix=self.prefix, suffix=self.suffix), - language=self.source_language, - storage_source=self.storage_source, - ), - targets=TranslationTarget._to_generated_list(self.targets), # pylint: disable=protected-access - storage_type=self.storage_type, - ) - - @staticmethod - def _to_generated_list(batch_document_inputs): - return [ - batch_document_input._to_generated() # pylint: disable=protected-access - for batch_document_input in batch_document_inputs - ] - - def __repr__(self) -> str: - return ( - "DocumentTranslationInput(source_url={}, targets={}, " - "source_language={}, storage_type={}, " - "storage_source={}, prefix={}, suffix={})".format( - self.source_url, - repr(self.targets), - self.source_language, - repr(self.storage_type), - self.storage_source, - self.prefix, - self.suffix, - )[:1024] - ) - - -class TranslationStatus: # pylint: disable=too-many-instance-attributes - """Status information about the translation operation.""" - - id: str # pylint: disable=redefined-builtin - """Id of the translation operation.""" - created_on: datetime.datetime - """The date time when the translation operation was created.""" - last_updated_on: datetime.datetime - """The date time when the translation operation's status was last updated.""" - status: str - """Status for a translation operation. - - * `NotStarted` - the operation has not begun yet. - * `Running` - translation is in progress. - * `Succeeded` - at least one document translated successfully within the operation. - * `Canceled` - the operation was canceled. - * `Canceling` - the operation is being canceled. - * `ValidationFailed` - the input failed validation. E.g. there was insufficient permissions on blob containers. - * `Failed` - all the documents within the operation failed. - """ - documents_total_count: int - """Number of translations to be made on documents in the operation.""" - documents_failed_count: int - """Number of documents that failed translation.""" - documents_succeeded_count: int - """Number of successful translations on documents.""" - documents_in_progress_count: int - """Number of translations on documents in progress.""" - documents_not_started_count: int - """Number of documents that have not started being translated.""" - documents_canceled_count: int - """Number of documents that were canceled for translation.""" - total_characters_charged: int - """Total characters charged across all documents within the translation operation.""" - error: Optional["DocumentTranslationError"] = None - """Returned if there is an error with the translation operation. - Includes error code, message, target.""" - - def __init__(self, **kwargs: Any) -> None: - self.id = kwargs.get("id", None) - self.created_on = kwargs.get("created_on", None) - self.last_updated_on = kwargs.get("last_updated_on", None) - self.status = kwargs.get("status", None) - self.error = kwargs.get("error", None) - self.documents_total_count = kwargs.get("documents_total_count", None) - self.documents_failed_count = kwargs.get("documents_failed_count", None) - self.documents_succeeded_count = kwargs.get("documents_succeeded_count", None) - self.documents_in_progress_count = kwargs.get("documents_in_progress_count", None) - self.documents_not_started_count = kwargs.get("documents_not_started_count", None) - self.documents_canceled_count = kwargs.get("documents_canceled_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=convert_status(batch_status_details.status), - error=( - DocumentTranslationError._from_generated(batch_status_details.error) # pylint: disable=protected-access - if batch_status_details.error - else None - ), - 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_started_count=batch_status_details.summary.not_yet_started, - documents_canceled_count=batch_status_details.summary.cancelled, - total_characters_charged=batch_status_details.summary.total_character_charged, - ) - - def __repr__(self) -> str: - return ( - "TranslationStatus(id={}, created_on={}, " - "last_updated_on={}, status={}, error={}, documents_total_count={}, " - "documents_failed_count={}, documents_succeeded_count={}, " - "documents_in_progress_count={}, documents_not_started_count={}, " - "documents_canceled_count={}, total_characters_charged={})".format( - self.id, - self.created_on, - self.last_updated_on, - self.status, - repr(self.error), - self.documents_total_count, - self.documents_failed_count, - self.documents_succeeded_count, - self.documents_in_progress_count, - self.documents_not_started_count, - self.documents_canceled_count, - self.total_characters_charged, - )[:1024] - ) - - -class DocumentStatus: - """Status information about a particular document within a translation operation.""" - - id: str # pylint: disable=redefined-builtin - """Document Id.""" - source_document_url: str - """Location of the source document in the source - container. Note that any SAS tokens are removed from this path.""" - created_on: datetime.datetime - """The date time when the document was created.""" - last_updated_on: datetime.datetime - """The date time when the document's status was last updated.""" - status: str - """Status for a document. - - * `NotStarted` - the document has not been translated yet. - * `Running` - translation is in progress for document - * `Succeeded` - translation succeeded for the document - * `Failed` - the document failed to translate. Check the error property. - * `Canceled` - the operation was canceled, the document was not translated. - * `Canceling` - the operation is canceling, the document will not be translated.""" - translated_to: str - """The language code of the language the document was translated to, - if successful.""" - translation_progress: float - """Progress of the translation if available. - Value is between [0.0, 1.0].""" - translated_document_url: Optional[str] = None - """Location of the translated document in the target - container. Note that any SAS tokens are removed from this path.""" - characters_charged: Optional[int] = None - """Characters charged for the document.""" - error: Optional["DocumentTranslationError"] = None - """Returned if there is an error with the particular document. - Includes error code, message, target.""" - - def __init__(self, **kwargs: Any) -> None: - self.source_document_url = kwargs.get("source_document_url", None) - self.translated_document_url = kwargs.get("translated_document_url", None) - self.created_on = kwargs["created_on"] - self.last_updated_on = kwargs["last_updated_on"] - self.status = kwargs["status"] - self.translated_to = kwargs["translated_to"] - self.error = kwargs.get("error", None) - self.translation_progress = kwargs.get("translation_progress", None) - self.id = kwargs.get("id", None) - self.characters_charged = kwargs.get("characters_charged", None) - - @classmethod - def _from_generated(cls, doc_status): - return cls( - source_document_url=doc_status.source_path, - translated_document_url=doc_status.path, - created_on=doc_status.created_date_time_utc, - last_updated_on=doc_status.last_action_date_time_utc, - status=convert_status(doc_status.status), - translated_to=doc_status.to, - error=( - DocumentTranslationError._from_generated(doc_status.error) # pylint: disable=protected-access - if doc_status.error - else None - ), - translation_progress=doc_status.progress, - id=doc_status.id, - characters_charged=doc_status.character_charged, - ) - - def __repr__(self) -> str: - return ( - "DocumentStatus(id={}, source_document_url={}, " - "translated_document_url={}, created_on={}, last_updated_on={}, " - "status={}, translated_to={}, error={}, translation_progress={}, " - "characters_charged={})".format( - self.id, - self.source_document_url, - self.translated_document_url, - self.created_on, - self.last_updated_on, - self.status, - self.translated_to, - repr(self.error), - self.translation_progress, - self.characters_charged, - )[:1024] - ) - - -class DocumentTranslationError: - """This contains the error code, message, and target with descriptive details on why - a translation operation or particular document failed. - """ - - code: str - """The error code. Possible high level values include: - "InvalidRequest", "InvalidArgument", "InternalServerError", "ServiceUnavailable", - "ResourceNotFound", "Unauthorized", "RequestRateTooHigh".""" - message: str - """The error message associated with the failure.""" - target: Optional[str] = None - """The source of the error. - For example it would be "documents" or "document id" in case of invalid document.""" - - def __init__(self, **kwargs: Any) -> None: - self.code = kwargs.get("code", None) - self.message = kwargs.get("message", None) - self.target = kwargs.get("target", None) - - @classmethod - def _from_generated(cls, error): - if error.inner_error: - inner_error = error.inner_error - return cls( - code=inner_error.code, - message=inner_error.message, - target=inner_error.target if inner_error.target is not None else error.target, - ) - return cls(code=error.code, message=error.message, target=error.target) - - def __repr__(self) -> str: - return "DocumentTranslationError(code={}, message={}, target={}".format(self.code, self.message, self.target)[ - :1024 - ] - - -class DocumentTranslationFileFormat: - """Possible file formats supported by the Document Translation service.""" - - file_format: str - """Name of the format.""" - file_extensions: List[str] - """Supported file extension for this format.""" - content_types: List[str] - """Supported Content-Types for this format.""" - format_versions: List[str] - """Supported Version.""" - default_format_version: Optional[str] = None - """Default format version if none is specified.""" - - def __init__(self, **kwargs: Any) -> None: - self.file_format = kwargs.get("file_format", None) - self.file_extensions = kwargs.get("file_extensions", None) - self.content_types = kwargs.get("content_types", None) - self.format_versions = kwargs.get("format_versions", None) - self.default_format_version = kwargs.get("default_format_version", None) - - @classmethod - def _from_generated(cls, file_format): - return cls( - file_format=file_format.format, - file_extensions=file_format.file_extensions, - content_types=file_format.content_types, - format_versions=file_format.versions, - default_format_version=file_format.default_version, - ) - - @staticmethod - def _from_generated_list(file_formats): - return [DocumentTranslationFileFormat._from_generated(file_formats) for file_formats in file_formats] - - def __repr__(self) -> str: - return ( - "DocumentTranslationFileFormat(file_format={}, file_extensions={}, " - "content_types={}, format_versions={}, default_format_version={}".format( - self.file_format, - self.file_extensions, - self.content_types, - self.format_versions, - self.default_format_version, - )[:1024] - ) - - -class DocumentTranslationLROPoller(LROPoller[PollingReturnType_co]): - """A custom poller implementation for Document Translation. Call `result()` on the poller to return - a pageable of :class:`~azure.ai.translation.document.DocumentStatus`.""" - - @property - def id(self) -> str: - """The ID for the translation operation - - :return: The str ID for the translation operation. - :rtype: str - """ - if self._polling_method._current_body: # pylint: disable=protected-access - return self._polling_method._current_body.id # pylint: disable=protected-access - return self._polling_method._get_id_from_headers() # pylint: disable=protected-access - - @property - def details(self) -> TranslationStatus: - """The details for the translation operation - - :return: The details for the translation operation. - :rtype: ~azure.ai.translation.document.TranslationStatus - """ - if self._polling_method._current_body: # pylint: disable=protected-access - return TranslationStatus._from_generated( # pylint: disable=protected-access - self._polling_method._current_body # pylint: disable=protected-access - ) - return TranslationStatus(id=self._polling_method._get_id_from_headers()) # pylint: disable=protected-access - - @classmethod - def from_continuation_token( # pylint: disable=docstring-missing-return,docstring-missing-param,docstring-missing-rtype - cls, polling_method, continuation_token, **kwargs: Any - ): - """ - :meta private: - """ - ( - client, - initial_response, - deserialization_callback, - ) = polling_method.from_continuation_token(continuation_token, **kwargs) - - return cls(client, initial_response, deserialization_callback, polling_method) - - -class DocumentTranslationLROPollingMethod(LROBasePolling): - """A custom polling method implementation for Document Translation.""" - - def __init__(self, *args, **kwargs): - self._cont_token_response = kwargs.pop("cont_token_response") - super().__init__(*args, **kwargs) - - @property - def _current_body(self) -> _TranslationStatus: - try: - return _TranslationStatus(self._pipeline_response.http_response.json()) - except json.decoder.JSONDecodeError: - return _TranslationStatus() # type: ignore[call-overload] - - def _get_id_from_headers(self) -> str: - return ( - self._initial_response.http_response.headers["Operation-Location"] - .split("/batches/")[1] - .split("?api-version")[0] - ) - - def finished(self) -> bool: - """Is this polling finished? - - :return: True/False for whether polling is complete. - :rtype: bool - """ - return self._finished(self.status()) - - @staticmethod - def _finished(status) -> bool: - if hasattr(status, "value"): - status = status.value - return str(status).lower() in _FINISHED - - @staticmethod - def _failed(status) -> bool: - if hasattr(status, "value"): - status = status.value - return str(status).lower() in _FAILED - - def get_continuation_token(self) -> str: - if self._current_body: - return self._current_body.id - return self._get_id_from_headers() - - # pylint: disable=arguments-differ - def from_continuation_token(self, continuation_token: str, **kwargs: Any) -> Tuple: # type: ignore[override] - try: - client = kwargs["client"] - except KeyError as exc: - raise ValueError("Need kwarg 'client' to be recreated from continuation_token") from exc - - try: - deserialization_callback = kwargs["deserialization_callback"] - except KeyError as exc: - raise ValueError("Need kwarg 'deserialization_callback' to be recreated from continuation_token") from exc - - return client, self._cont_token_response, deserialization_callback - - def _poll(self) -> None: - """Poll status of operation so long as operation is incomplete and - we have an endpoint to query. - - :raises: OperationFailed if operation status 'Failed' or 'Canceled'. - :raises: BadStatus if response status invalid. - :raises: BadResponse if response invalid. - """ - - while not self.finished(): - self.update_status() - while not self.finished(): - self._delay() - self.update_status() - - if self._failed(self.status()): - raise OperationFailed("Operation failed or canceled") - - final_get_url = self._operation.get_final_get_url(self._pipeline_response) - if final_get_url: - self._pipeline_response = self.request_status(final_get_url) - _raise_if_bad_http_status_and_method(self._pipeline_response.http_response) - - -class TranslationPolling(OperationResourcePolling): - """Implements a Location polling.""" - - def can_poll(self, pipeline_response: PipelineResponseType) -> bool: - """Answer if this polling method could be used. - - :param pipeline_response: The PipelineResponse type - :type pipeline_response: PipelineResponseType - :return: Whether polling should be performed. - :rtype: bool - """ - response = pipeline_response.http_response - can_poll = self._operation_location_header in response.headers - if can_poll: - return True - - if not _is_empty(response): - body = _as_json(response) - status = body.get("status") - if status: - return True - return False - - def _set_async_url_if_present(self, response: ResponseType) -> None: - location_header = response.headers.get(self._operation_location_header) - if location_header: - self._async_url = location_header - else: - self._async_url = response.request.url - - def get_status(self, pipeline_response: PipelineResponseType) -> str: - """Process the latest status update retrieved from a 'location' header. - - :param azure.core.pipeline.PipelineResponse pipeline_response: latest REST call response. - :return: The current operation status - :rtype: str - :raises: BadResponse if response has no body and not status 202. - """ - response = pipeline_response.http_response - if not _is_empty(response): - body = _as_json(response) - status = body.get("status") - if status: - return self._map_nonstandard_statuses(status, body) - raise BadResponse("No status found in body") - raise BadResponse("The response from long running operation does not contain a body.") - - def _map_nonstandard_statuses(self, status: str, body: Dict[str, Any]) -> str: - """Map non-standard statuses. - - :param str status: lro process status. - :param str body: pipeline response body. - :return: The current operation status. - :rtype: str - """ - if status == "ValidationFailed": - self.raise_error(body) - return status - - def raise_error(self, body: Dict[str, Any]) -> None: - error = body["error"] - if body["error"].get("innerError", None): - error = body["error"]["innerError"] - http_response_error = HttpResponseError(message="({}): {}".format(error["code"], error["message"])) - http_response_error.error = ODataV4Format(error) # set error.code - raise http_response_error - - -class DocumentTranslationApiVersion(str, Enum, metaclass=CaseInsensitiveEnumMeta): - """Document Translation API versions supported by this package""" - - #: This is the default version - V2024_05_01 = "2024-05-01" - - -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) # pylint: disable=protected-access - if not continuation_token - else None - ) - 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 = kwargs.pop("target_language", None) - if not target_language: - target_language = args[2] - - # Additional kwargs - source_language = kwargs.pop("source_language", None) - prefix = kwargs.pop("prefix", None) - suffix = kwargs.pop("suffix", None) - storage_type = kwargs.pop("storage_type", None) - category_id = kwargs.pop("category_id", None) - glossaries = kwargs.pop("glossaries", None) - - request = [ - _BatchRequest( - source=_SourceInput( - source_url=source_url, - filter=_DocumentFilter(prefix=prefix, suffix=suffix), - language=source_language, - ), - targets=[ - _TargetInput( - target_url=target_url, - language=target_language, - glossaries=( - [g._to_generated() for g in glossaries] # pylint: disable=protected-access - if glossaries - else None - ), - category=category_id, - ) - ], - storage_type=storage_type, - ) - ] - except (AttributeError, TypeError, IndexError) as exc: - raise ValueError( - "Pass 'inputs' for multiple inputs or 'source_url', 'target_url', " - "and 'target_language' for a single input." - ) from exc - - return request - - -def get_http_logging_policy(**kwargs): - http_logging_policy = HttpLoggingPolicy(**kwargs) - http_logging_policy.allowed_header_names.update( - { - "Operation-Location", - "Content-Encoding", - "Vary", - "apim-request-id", - "X-RequestId", - "Set-Cookie", - "X-Powered-By", - "Strict-Transport-Security", - "x-content-type-options", - } - ) - http_logging_policy.allowed_query_params.update( - { - "top", - "skip", - "maxpagesize", - "ids", - "statuses", - "createdDateTimeUtcStart", - "createdDateTimeUtcEnd", - "orderby", - } - ) - return http_logging_policy - - -def convert_datetime(date_time: Union[str, datetime.datetime]) -> datetime.datetime: - if isinstance(date_time, datetime.datetime): - return date_time - if isinstance(date_time, str): - try: - return datetime.datetime.strptime(date_time, "%Y-%m-%d") - except ValueError: - try: - return datetime.datetime.strptime(date_time, "%Y-%m-%dT%H:%M:%SZ") - except ValueError: - return datetime.datetime.strptime(date_time, "%Y-%m-%d %H:%M:%S") - raise TypeError("Bad datetime type") - - -def convert_order_by(orderby: Optional[List[str]]) -> Optional[List[str]]: - if orderby: - orderby = [order.replace("created_on", "createdDateTimeUtc") for order in orderby] - return orderby - - -class DocumentTranslationClient: - def __init__(self, endpoint: str, credential: Union[AzureKeyCredential, TokenCredential], **kwargs: Any) -> None: - """DocumentTranslationClient is your interface to the Document Translation service. - Use the client to translate whole documents while preserving source document - structure and text formatting. - - :param str endpoint: Supported Document Translation endpoint (protocol and hostname, for example: - https://.cognitiveservices.azure.com/). - :param credential: Credentials needed for the client to connect to Azure. - This is an instance of AzureKeyCredential if using an API key or a token - credential from :mod:`azure.identity`. - :type credential: :class:`~azure.core.credentials.AzureKeyCredential` or - :class:`~azure.core.credentials.TokenCredential` - :keyword api_version: - The API version of the service to use for requests. It defaults to the latest service version. - Setting to an older version may result in reduced feature compatibility. - :paramtype api_version: str or ~azure.ai.translation.document.DocumentTranslationApiVersion - - .. admonition:: Example: - - .. literalinclude:: ../samples/sample_authentication.py - :start-after: [START create_dt_client_with_key] - :end-before: [END create_dt_client_with_key] - :language: python - :dedent: 4 - :caption: Creating the DocumentTranslationClient with an endpoint and API key. - - .. literalinclude:: ../samples/sample_authentication.py - :start-after: [START create_dt_client_with_aad] - :end-before: [END create_dt_client_with_aad] - :language: python - :dedent: 4 - :caption: Creating the DocumentTranslationClient with a token credential. - """ - try: - self._endpoint = endpoint.rstrip("/") - except AttributeError as exc: - raise ValueError("Parameter 'endpoint' must be a string.") from exc - self._credential = credential - polling_interval = kwargs.pop("polling_interval", POLLING_INTERVAL) - - from ._client import DocumentTranslationClient as _BatchDocumentTranslationClient - - self._client = _BatchDocumentTranslationClient( - endpoint=self._endpoint, - credential=credential, - http_logging_policy=kwargs.pop("http_logging_policy", get_http_logging_policy()), - polling_interval=polling_interval, - **kwargs - ) - - def __enter__(self) -> "DocumentTranslationClient": - self._client.__enter__() # pylint:disable=no-member - return self - - def __exit__(self, *args) -> None: - self._client.__exit__(*args) # pylint:disable=no-member - - def close(self) -> None: - """Close the :class:`~azure.ai.translation.document.DocumentTranslationClient` session.""" - return self._client.close() - - @overload - def begin_translation( - self, - source_url: str, - target_url: str, - target_language: str, - *, - source_language: Optional[str] = None, - prefix: Optional[str] = None, - suffix: Optional[str] = None, - storage_type: Optional[Union[str, StorageInputType]] = None, - category_id: Optional[str] = None, - glossaries: Optional[List[TranslationGlossary]] = None, - **kwargs: Any - ) -> DocumentTranslationLROPoller[ItemPaged[DocumentStatus]]: - """Begin translating the document(s) in your source container to your target container - in the given language. There are two ways to call this method: - - 1) To perform translation on documents from a single source container to a single target container, pass the - `source_url`, `target_url`, and `target_language` parameters including any optional keyword arguments. - - 2) To pass multiple inputs for translation (multiple sources or targets), pass the `inputs` parameter - as a list of :class:`~azure.ai.translation.document.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. See the service documentation for the supported SAS permissions for accessing - source storage containers/blobs: https://aka.ms/azsdk/documenttranslation/sas-permissions - :param str target_url: The target SAS URL to the Azure Blob container where the translated documents - should be written. See the service documentation for the supported SAS permissions for accessing - target storage containers/blobs: https://aka.ms/azsdk/documenttranslation/sas-permissions - :param str target_language: This is the language code you want your documents to be translated to. - See supported language codes here: - https://docs.microsoft.com/azure/cognitive-services/translator/language-support#translate - :keyword str source_language: Language code for the source documents. - If none is specified, the source language will be auto-detected for each document. - :keyword str prefix: A case-sensitive prefix string to filter documents in the source path for - translation. For example, when using a Azure storage blob Uri, use the prefix to restrict - sub folders for translation. - :keyword str suffix: A case-sensitive suffix string to filter documents in the source path for - translation. This is most often use for file extensions. - :keyword storage_type: Storage type of the input documents source string. Possible values - include: "Folder", "File". - :paramtype storage_type: str or ~azure.ai.translation.document.StorageInputType - :keyword str category_id: Category / custom model ID for using custom translation. - :keyword glossaries: Glossaries to apply to translation. - :paramtype glossaries: list[~azure.ai.translation.document.TranslationGlossary] - :return: An instance of a DocumentTranslationLROPoller. Call `result()` on the poller - object to return a pageable of DocumentStatus. A DocumentStatus will be - returned for each translation on a document. - :rtype: DocumentTranslationLROPoller[~azure.core.paging.ItemPaged[DocumentStatus]] - :raises ~azure.core.exceptions.HttpResponseError: - """ - - @overload - def begin_translation( - self, inputs: List[DocumentTranslationInput], **kwargs: Any - ) -> DocumentTranslationLROPoller[ItemPaged[DocumentStatus]]: - """Begin translating the document(s) in your source container to your target container - in the given language. There are two ways to call this method: - - 1) To perform translation on documents from a single source container to a single target container, pass the - `source_url`, `target_url`, and `target_language` parameters including any optional keyword arguments. - - 2) To pass multiple inputs for translation (multiple sources or targets), pass the `inputs` parameter - as a list of :class:`~azure.ai.translation.document.DocumentTranslationInput`. - - For supported languages and document formats, see the service documentation: - https://docs.microsoft.com/azure/cognitive-services/translator/document-translation/overview - - :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. - :type inputs: List[~azure.ai.translation.document.DocumentTranslationInput] - :return: An instance of a DocumentTranslationLROPoller. Call `result()` on the poller - object to return a pageable of DocumentStatus. A DocumentStatus will be - returned for each translation on a document. - :rtype: DocumentTranslationLROPoller[~azure.core.paging.ItemPaged[DocumentStatus]] - :raises ~azure.core.exceptions.HttpResponseError: - """ - - @distributed_trace - def begin_translation( # pylint: disable=docstring-missing-param - self, *args: Union[str, List[DocumentTranslationInput]], **kwargs: Any - ) -> DocumentTranslationLROPoller[ItemPaged[DocumentStatus]]: - """Begin translating the document(s) in your source container to your target container - in the given language. There are two ways to call this method: - - 1) To perform translation on documents from a single source container to a single target container, pass the - `source_url`, `target_url`, and `target_language` parameters including any optional keyword arguments. - - 2) To pass multiple inputs for translation (multiple sources or targets), pass the `inputs` parameter - as a list of :class:`~azure.ai.translation.document.DocumentTranslationInput`. - - For supported languages and document formats, see the service documentation: - https://docs.microsoft.com/azure/cognitive-services/translator/document-translation/overview - - :return: An instance of a DocumentTranslationLROPoller. Call `result()` on the poller - object to return a pageable of DocumentStatus. A DocumentStatus will be - returned for each translation on a document. - :rtype: DocumentTranslationLROPoller[~azure.core.paging.ItemPaged[DocumentStatus]] - :raises ~azure.core.exceptions.HttpResponseError: - - .. admonition:: Example: - - .. literalinclude:: ../samples/sample_begin_translation.py - :start-after: [START begin_translation] - :end-before: [END begin_translation] - :language: python - :dedent: 4 - :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 - translation_status = json.loads(raw_response.http_response.text()) - return self.list_document_statuses(translation_status["id"]) - - polling_interval = kwargs.pop( - "polling_interval", - self._client._config.polling_interval, # pylint: disable=protected-access - ) - - pipeline_response = None - if continuation_token: - pipeline_response = self._client.get_translation_status( - continuation_token, - cls=lambda pipeline_response, _, response_headers: pipeline_response, - ) - - callback = kwargs.pop("cls", deserialization_callback) - return cast( - DocumentTranslationLROPoller[ItemPaged[DocumentStatus]], - self._client.begin_start_translation( - body=_StartTranslationDetails(inputs=inputs), - polling=DocumentTranslationLROPollingMethod( - timeout=polling_interval, - lro_algorithms=[TranslationPolling()], - cont_token_response=pipeline_response, - **kwargs - ), - cls=callback, - continuation_token=continuation_token, - **kwargs - ), - ) - - @distributed_trace - def get_translation_status(self, translation_id: str, **kwargs: Any) -> TranslationStatus: - """Gets the status of the translation operation. - - Includes the overall status, as well as a summary of - the documents that are being translated as part of that translation operation. - - :param str translation_id: The translation operation ID. - :return: A TranslationStatus with information on the status of the translation operation. - :rtype: ~azure.ai.translation.document.TranslationStatus - :raises ~azure.core.exceptions.HttpResponseError or ~azure.core.exceptions.ResourceNotFoundError: - """ - - translation_status = self._client.get_translation_status(translation_id, **kwargs) - return TranslationStatus._from_generated( # pylint: disable=protected-access - _TranslationStatus(translation_status) # type: ignore[call-overload] - ) - - @distributed_trace - def cancel_translation(self, translation_id: str, **kwargs: Any) -> None: - """Cancel a currently processing or queued translation operation. - - A translation will not be canceled if it is already completed, failed, or canceling. - All documents that have completed translation will not be canceled and will be charged. - If possible, all pending documents will be canceled. - - :param str translation_id: The translation operation ID. - :return: None - :rtype: None - :raises ~azure.core.exceptions.HttpResponseError or ~azure.core.exceptions.ResourceNotFoundError: - """ - - self._client.cancel_translation(translation_id, **kwargs) - - @distributed_trace - def list_translation_statuses( - self, - *, - top: Optional[int] = None, - skip: Optional[int] = None, - translation_ids: Optional[List[str]] = None, - statuses: Optional[List[str]] = None, - created_after: Optional[Union[str, datetime.datetime]] = None, - created_before: Optional[Union[str, datetime.datetime]] = None, - order_by: Optional[List[str]] = None, - **kwargs: Any - ) -> ItemPaged[TranslationStatus]: - """List all the submitted translation operations under the Document Translation resource. - - :keyword int top: The total number of operations to return (across all pages) from all submitted translations. - :keyword int skip: The number of operations to skip (from beginning of all submitted operations). - By default, we sort by all submitted operations in descending order by start time. - :keyword list[str] translation_ids: Translation operations ids to filter by. - :keyword list[str] statuses: Translation operation statuses to filter by. Options include - 'NotStarted', 'Running', 'Succeeded', 'Failed', 'Canceled', 'Canceling', - and 'ValidationFailed'. - :keyword created_after: Get operations created after a certain datetime. - :paramtype created_after: str or ~datetime.datetime - :keyword created_before: Get operations created before a certain datetime. - :paramtype created_before: str or ~datetime.datetime - :keyword list[str] order_by: The sorting query for the operations returned. Currently only - 'created_on' supported. - format: ["param1 asc/desc", "param2 asc/desc", ...] - (ex: 'created_on asc', 'created_on desc'). - :return: A pageable of TranslationStatus. - :rtype: ~azure.core.paging.ItemPaged[TranslationStatus] - :raises ~azure.core.exceptions.HttpResponseError: - - .. admonition:: Example: - - .. literalinclude:: ../samples/sample_list_translations.py - :start-after: [START list_translations] - :end-before: [END list_translations] - :language: python - :dedent: 4 - :caption: List all submitted translations under the resource. - """ - - if statuses: - statuses = [convert_status(status, ll=True) for status in statuses] - order_by = convert_order_by(order_by) - created_after = convert_datetime(created_after) if created_after else None - created_before = convert_datetime(created_before) if created_before else None - - def _convert_from_generated_model( - generated_model, - ): # pylint: disable=protected-access - return TranslationStatus._from_generated( - _TranslationStatus(generated_model) - ) # pylint: disable=protected-access - - model_conversion_function = kwargs.pop( - "cls", - lambda translation_statuses: [_convert_from_generated_model(status) for status in translation_statuses], - ) - - return cast( - ItemPaged[TranslationStatus], - self._client.get_translations_status( - cls=model_conversion_function, - created_date_time_utc_start=created_after, - created_date_time_utc_end=created_before, - ids=translation_ids, - orderby=order_by, - statuses=statuses, - top=top, - skip=skip, - **kwargs - ), - ) - - @distributed_trace - def list_document_statuses( - self, - translation_id: str, - *, - top: Optional[int] = None, - skip: Optional[int] = None, - document_ids: Optional[List[str]] = None, - statuses: Optional[List[str]] = None, - created_after: Optional[Union[str, datetime.datetime]] = None, - created_before: Optional[Union[str, datetime.datetime]] = None, - order_by: Optional[List[str]] = None, - **kwargs: Any - ) -> ItemPaged[DocumentStatus]: - """List all the document statuses for a given translation operation. - - :param str translation_id: ID of translation operation to list documents for. - :keyword int top: The total number of documents to return (across all pages). - :keyword int skip: The number of documents to skip (from beginning). - By default, we sort by all documents in descending order by start time. - :keyword list[str] document_ids: Document IDs to filter by. - :keyword list[str] statuses: Document statuses to filter by. Options include - 'NotStarted', 'Running', 'Succeeded', 'Failed', 'Canceled', 'Canceling', - and 'ValidationFailed'. - :keyword created_after: Get documents created after a certain datetime. - :paramtype created_after: str or ~datetime.datetime - :keyword created_before: Get documents created before a certain datetime. - :paramtype created_before: str or ~datetime.datetime - :keyword list[str] order_by: The sorting query for the documents. Currently only - 'created_on' is supported. - format: ["param1 asc/desc", "param2 asc/desc", ...] - (ex: 'created_on asc', 'created_on desc'). - :return: A pageable of DocumentStatus. - :rtype: ~azure.core.paging.ItemPaged[DocumentStatus] - :raises ~azure.core.exceptions.HttpResponseError: - - .. admonition:: Example: - - .. literalinclude:: ../samples/sample_check_document_statuses.py - :start-after: [START list_document_statuses] - :end-before: [END list_document_statuses] - :language: python - :dedent: 4 - :caption: List all the document statuses as they are being translated. - """ - - if statuses: - statuses = [convert_status(status, ll=True) for status in statuses] - order_by = convert_order_by(order_by) - created_after = convert_datetime(created_after) if created_after else None - created_before = convert_datetime(created_before) if created_before else None - - def _convert_from_generated_model(generated_model): - return DocumentStatus._from_generated(_DocumentStatus(generated_model)) # pylint: disable=protected-access - - model_conversion_function = kwargs.pop( - "cls", - lambda doc_statuses: [_convert_from_generated_model(doc_status) for doc_status in doc_statuses], - ) - - return cast( - ItemPaged[DocumentStatus], - self._client.get_documents_status( - id=translation_id, - cls=model_conversion_function, - created_date_time_utc_start=created_after, - created_date_time_utc_end=created_before, - ids=document_ids, - orderby=order_by, - statuses=statuses, - top=top, - skip=skip, - **kwargs - ), - ) - - @distributed_trace - def get_document_status(self, translation_id: str, document_id: str, **kwargs: Any) -> DocumentStatus: - """Get the status of an individual document within a translation operation. - - :param str translation_id: The translation operation ID. - :param str document_id: The ID for the document. - :return: A DocumentStatus with information on the status of the document. - :rtype: ~azure.ai.translation.document.DocumentStatus - :raises ~azure.core.exceptions.HttpResponseError or ~azure.core.exceptions.ResourceNotFoundError: - """ - - document_status = self._client.get_document_status(translation_id, document_id, **kwargs) - return DocumentStatus._from_generated(_DocumentStatus(document_status)) # type: ignore[call-overload] # pylint: disable=protected-access - - @distributed_trace - def get_supported_glossary_formats(self, **kwargs: Any) -> List[DocumentTranslationFileFormat]: - """Get the list of the glossary formats supported by the Document Translation service. - - :return: A list of supported glossary formats. - :rtype: List[DocumentTranslationFileFormat] - :raises ~azure.core.exceptions.HttpResponseError: - """ - - glossary_formats = self._client.get_supported_formats(type="glossary", **kwargs) - return DocumentTranslationFileFormat._from_generated_list( # pylint: disable=protected-access - glossary_formats.value - ) - - @distributed_trace - def get_supported_document_formats(self, **kwargs: Any) -> List[DocumentTranslationFileFormat]: - """Get the list of the document formats supported by the Document Translation service. - - :return: A list of supported document formats for translation. - :rtype: List[DocumentTranslationFileFormat] - :raises ~azure.core.exceptions.HttpResponseError: - """ - - document_formats = self._client.get_supported_formats(type="document", **kwargs) - return DocumentTranslationFileFormat._from_generated_list( # pylint: disable=protected-access - document_formats.value - ) - - -__all__: List[str] = [ - "DocumentTranslationClient", - "DocumentTranslationApiVersion", - "DocumentTranslationLROPoller", - # re-export models at this level for backwards compatibility - "TranslationGlossary", - "TranslationTarget", - "DocumentTranslationInput", - "TranslationStatus", - "DocumentStatus", - "DocumentTranslationError", - "DocumentTranslationFileFormat", - "StorageInputType", -] # Add all objects you want publicly available to users at this package level +__all__: List[str] = [] # Add all objects you want publicly available to users at this package level def patch_sdk(): diff --git a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_serialization.py b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_serialization.py index 2f781d740827..01a226bd7f14 100644 --- a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_serialization.py +++ b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_serialization.py @@ -1,3 +1,4 @@ +# pylint: disable=too-many-lines # -------------------------------------------------------------------------- # # Copyright (c) Microsoft Corporation. All rights reserved. @@ -24,7 +25,6 @@ # # -------------------------------------------------------------------------- -# pylint: skip-file # pyright: reportUnnecessaryTypeIgnoreComment=false from base64 import b64decode, b64encode @@ -52,7 +52,6 @@ MutableMapping, Type, List, - Mapping, ) try: @@ -91,6 +90,8 @@ def deserialize_from_text(cls, data: Optional[Union[AnyStr, IO]], content_type: :param data: Input, could be bytes or stream (will be decoded with UTF8) or text :type data: str or bytes or IO :param str content_type: The content type. + :return: The deserialized data. + :rtype: object """ if hasattr(data, "read"): # Assume a stream @@ -112,7 +113,7 @@ def deserialize_from_text(cls, data: Optional[Union[AnyStr, IO]], content_type: try: return json.loads(data_as_str) except ValueError as err: - raise DeserializationError("JSON is invalid: {}".format(err), err) + raise DeserializationError("JSON is invalid: {}".format(err), err) from err elif "xml" in (content_type or []): try: @@ -144,6 +145,8 @@ def _json_attemp(data): # context otherwise. _LOGGER.critical("Wasn't XML not JSON, failing") raise DeserializationError("XML is invalid") from err + elif content_type.startswith("text/"): + return data_as_str raise DeserializationError("Cannot deserialize content-type: {}".format(content_type)) @classmethod @@ -153,6 +156,11 @@ def deserialize_from_http_generics(cls, body_bytes: Optional[Union[AnyStr, IO]], Use bytes and headers to NOT use any requests/aiohttp or whatever specific implementation. Headers will tested for "content-type" + + :param bytes body_bytes: The body of the response. + :param dict headers: The headers of the response. + :returns: The deserialized data. + :rtype: object """ # Try to use content-type from headers if available content_type = None @@ -182,15 +190,30 @@ class UTC(datetime.tzinfo): """Time Zone info for handling UTC""" def utcoffset(self, dt): - """UTF offset for UTC is 0.""" + """UTF offset for UTC is 0. + + :param datetime.datetime dt: The datetime + :returns: The offset + :rtype: datetime.timedelta + """ return datetime.timedelta(0) def tzname(self, dt): - """Timestamp representation.""" + """Timestamp representation. + + :param datetime.datetime dt: The datetime + :returns: The timestamp representation + :rtype: str + """ return "Z" def dst(self, dt): - """No daylight saving for UTC.""" + """No daylight saving for UTC. + + :param datetime.datetime dt: The datetime + :returns: The daylight saving time + :rtype: datetime.timedelta + """ return datetime.timedelta(hours=1) @@ -233,24 +256,26 @@ def __getinitargs__(self): _FLATTEN = re.compile(r"(? None: self.additional_properties: Optional[Dict[str, Any]] = {} - for k in kwargs: + for k in kwargs: # pylint: disable=consider-using-dict-items if k not in self._attribute_map: _LOGGER.warning("%s is not a known attribute of class %s and will be ignored", k, self.__class__) elif k in self._validation and self._validation[k].get("readonly", False): @@ -298,13 +330,23 @@ def __init__(self, **kwargs: Any) -> None: setattr(self, k, kwargs[k]) def __eq__(self, other: Any) -> bool: - """Compare objects by comparing all attributes.""" + """Compare objects by comparing all attributes. + + :param object other: The object to compare + :returns: True if objects are equal + :rtype: bool + """ if isinstance(other, self.__class__): return self.__dict__ == other.__dict__ return False def __ne__(self, other: Any) -> bool: - """Compare objects by comparing all attributes.""" + """Compare objects by comparing all attributes. + + :param object other: The object to compare + :returns: True if objects are not equal + :rtype: bool + """ return not self.__eq__(other) def __str__(self) -> str: @@ -324,7 +366,11 @@ def is_xml_model(cls) -> bool: @classmethod def _create_xml_node(cls): - """Create XML node.""" + """Create XML node. + + :returns: The XML node + :rtype: xml.etree.ElementTree.Element + """ try: xml_map = cls._xml_map # type: ignore except AttributeError: @@ -344,7 +390,9 @@ def serialize(self, keep_readonly: bool = False, **kwargs: Any) -> JSON: :rtype: dict """ serializer = Serializer(self._infer_class_models()) - return serializer._serialize(self, keep_readonly=keep_readonly, **kwargs) # type: ignore + return serializer._serialize( # type: ignore # pylint: disable=protected-access + self, keep_readonly=keep_readonly, **kwargs + ) def as_dict( self, @@ -378,12 +426,15 @@ def my_key_transformer(key, attr_desc, value): If you want XML serialization, you can pass the kwargs is_xml=True. + :param bool keep_readonly: If you want to serialize the readonly attributes :param function key_transformer: A key transformer function. :returns: A dict JSON compatible object :rtype: dict """ serializer = Serializer(self._infer_class_models()) - return serializer._serialize(self, key_transformer=key_transformer, keep_readonly=keep_readonly, **kwargs) # type: ignore + return serializer._serialize( # type: ignore # pylint: disable=protected-access + self, key_transformer=key_transformer, keep_readonly=keep_readonly, **kwargs + ) @classmethod def _infer_class_models(cls): @@ -393,7 +444,7 @@ def _infer_class_models(cls): client_models = {k: v for k, v in models.__dict__.items() if isinstance(v, type)} if cls.__name__ not in client_models: raise ValueError("Not Autorest generated code") - except Exception: + except Exception: # pylint: disable=broad-exception-caught # Assume it's not Autorest generated (tests?). Add ourselves as dependencies. client_models = {cls.__name__: cls} return client_models @@ -406,6 +457,7 @@ def deserialize(cls: Type[ModelType], data: Any, content_type: Optional[str] = N :param str content_type: JSON by default, set application/xml if XML. :returns: An instance of this model :raises: DeserializationError if something went wrong + :rtype: ModelType """ deserializer = Deserializer(cls._infer_class_models()) return deserializer(cls.__name__, data, content_type=content_type) # type: ignore @@ -424,9 +476,11 @@ def from_dict( and last_rest_key_case_insensitive_extractor) :param dict data: A dict using RestAPI structure + :param function key_extractors: A key extractor function. :param str content_type: JSON by default, set application/xml if XML. :returns: An instance of this model :raises: DeserializationError if something went wrong + :rtype: ModelType """ deserializer = Deserializer(cls._infer_class_models()) deserializer.key_extractors = ( # type: ignore @@ -446,7 +500,7 @@ def _flatten_subtype(cls, key, objects): return {} result = dict(cls._subtype_map[key]) for valuetype in cls._subtype_map[key].values(): - result.update(objects[valuetype]._flatten_subtype(key, objects)) + result.update(objects[valuetype]._flatten_subtype(key, objects)) # pylint: disable=protected-access return result @classmethod @@ -454,6 +508,11 @@ def _classify(cls, response, objects): """Check the class _subtype_map for any child classes. We want to ignore any inherited _subtype_maps. Remove the polymorphic key from the initial data. + + :param dict response: The initial data + :param dict objects: The class objects + :returns: The class to be used + :rtype: class """ for subtype_key in cls.__dict__.get("_subtype_map", {}).keys(): subtype_value = None @@ -499,11 +558,13 @@ def _decode_attribute_map_key(key): inside the received data. :param str key: A key string from the generated code + :returns: The decoded key + :rtype: str """ return key.replace("\\.", ".") -class Serializer(object): +class Serializer(object): # pylint: disable=too-many-public-methods """Request object model serializer.""" basic_types = {str: "str", int: "int", bool: "bool", float: "float"} @@ -558,13 +619,16 @@ def __init__(self, classes: Optional[Mapping[str, type]] = None): self.key_transformer = full_restapi_key_transformer self.client_side_validation = True - def _serialize(self, target_obj, data_type=None, **kwargs): + def _serialize( # pylint: disable=too-many-nested-blocks, too-many-branches, too-many-statements, too-many-locals + self, target_obj, data_type=None, **kwargs + ): """Serialize data into a string according to type. - :param target_obj: The data to be serialized. + :param object target_obj: The data to be serialized. :param str data_type: The type to be serialized from. :rtype: str, dict :raises: SerializationError if serialization fails. + :returns: The serialized data. """ key_transformer = kwargs.get("key_transformer", self.key_transformer) keep_readonly = kwargs.get("keep_readonly", False) @@ -590,12 +654,14 @@ def _serialize(self, target_obj, data_type=None, **kwargs): serialized = {} if is_xml_model_serialization: - serialized = target_obj._create_xml_node() + serialized = target_obj._create_xml_node() # pylint: disable=protected-access try: - attributes = target_obj._attribute_map + attributes = target_obj._attribute_map # pylint: disable=protected-access for attr, attr_desc in attributes.items(): attr_name = attr - if not keep_readonly and target_obj._validation.get(attr_name, {}).get("readonly", False): + if not keep_readonly and target_obj._validation.get( # pylint: disable=protected-access + attr_name, {} + ).get("readonly", False): continue if attr_name == "additional_properties" and attr_desc["key"] == "": @@ -631,7 +697,8 @@ def _serialize(self, target_obj, data_type=None, **kwargs): if isinstance(new_attr, list): serialized.extend(new_attr) # type: ignore elif isinstance(new_attr, ET.Element): - # If the down XML has no XML/Name, we MUST replace the tag with the local tag. But keeping the namespaces. + # If the down XML has no XML/Name, + # we MUST replace the tag with the local tag. But keeping the namespaces. if "name" not in getattr(orig_attr, "_xml_map", {}): splitted_tag = new_attr.tag.split("}") if len(splitted_tag) == 2: # Namespace @@ -662,17 +729,17 @@ def _serialize(self, target_obj, data_type=None, **kwargs): except (AttributeError, KeyError, TypeError) as err: msg = "Attribute {} in object {} cannot be serialized.\n{}".format(attr_name, class_name, str(target_obj)) raise SerializationError(msg) from err - else: - return serialized + return serialized def body(self, data, data_type, **kwargs): """Serialize data intended for a request body. - :param data: The data to be serialized. + :param object data: The data to be serialized. :param str data_type: The type to be serialized from. :rtype: dict :raises: SerializationError if serialization fails. :raises: ValueError if data is None + :returns: The serialized request body """ # Just in case this is a dict @@ -701,7 +768,7 @@ def body(self, data, data_type, **kwargs): attribute_key_case_insensitive_extractor, last_rest_key_case_insensitive_extractor, ] - data = deserializer._deserialize(data_type, data) + data = deserializer._deserialize(data_type, data) # pylint: disable=protected-access except DeserializationError as err: raise SerializationError("Unable to build a model: " + str(err)) from err @@ -710,9 +777,11 @@ def body(self, data, data_type, **kwargs): def url(self, name, data, data_type, **kwargs): """Serialize data intended for a URL path. - :param data: The data to be serialized. + :param str name: The name of the URL path parameter. + :param object data: The data to be serialized. :param str data_type: The type to be serialized from. :rtype: str + :returns: The serialized URL path :raises: TypeError if serialization fails. :raises: ValueError if data is None """ @@ -726,21 +795,20 @@ def url(self, name, data, data_type, **kwargs): output = output.replace("{", quote("{")).replace("}", quote("}")) else: output = quote(str(output), safe="") - except SerializationError: - raise TypeError("{} must be type {}.".format(name, data_type)) - else: - return output + except SerializationError as exc: + raise TypeError("{} must be type {}.".format(name, data_type)) from exc + return output def query(self, name, data, data_type, **kwargs): """Serialize data intended for a URL query. - :param data: The data to be serialized. + :param str name: The name of the query parameter. + :param object data: The data to be serialized. :param str data_type: The type to be serialized from. - :keyword bool skip_quote: Whether to skip quote the serialized result. - Defaults to False. :rtype: str, list :raises: TypeError if serialization fails. :raises: ValueError if data is None + :returns: The serialized query parameter """ try: # Treat the list aside, since we don't want to encode the div separator @@ -757,19 +825,20 @@ def query(self, name, data, data_type, **kwargs): output = str(output) else: output = quote(str(output), safe="") - except SerializationError: - raise TypeError("{} must be type {}.".format(name, data_type)) - else: - return str(output) + except SerializationError as exc: + raise TypeError("{} must be type {}.".format(name, data_type)) from exc + return str(output) def header(self, name, data, data_type, **kwargs): """Serialize data intended for a request header. - :param data: The data to be serialized. + :param str name: The name of the header. + :param object data: The data to be serialized. :param str data_type: The type to be serialized from. :rtype: str :raises: TypeError if serialization fails. :raises: ValueError if data is None + :returns: The serialized header """ try: if data_type in ["[str]"]: @@ -778,21 +847,20 @@ def header(self, name, data, data_type, **kwargs): output = self.serialize_data(data, data_type, **kwargs) if data_type == "bool": output = json.dumps(output) - except SerializationError: - raise TypeError("{} must be type {}.".format(name, data_type)) - else: - return str(output) + except SerializationError as exc: + raise TypeError("{} must be type {}.".format(name, data_type)) from exc + return str(output) def serialize_data(self, data, data_type, **kwargs): """Serialize generic data according to supplied data type. - :param data: The data to be serialized. + :param object data: The data to be serialized. :param str data_type: The type to be serialized from. - :param bool required: Whether it's essential that the data not be - empty or None :raises: AttributeError if required data is None. :raises: ValueError if data is None :raises: SerializationError if serialization fails. + :returns: The serialized data. + :rtype: str, int, float, bool, dict, list """ if data is None: raise ValueError("No value for given attribute") @@ -803,7 +871,7 @@ def serialize_data(self, data, data_type, **kwargs): if data_type in self.basic_types.values(): return self.serialize_basic(data, data_type, **kwargs) - elif data_type in self.serialize_type: + if data_type in self.serialize_type: return self.serialize_type[data_type](data, **kwargs) # If dependencies is empty, try with current data class @@ -819,11 +887,10 @@ def serialize_data(self, data, data_type, **kwargs): except (ValueError, TypeError) as err: msg = "Unable to serialize value: {!r} as type: {!r}." raise SerializationError(msg.format(data, data_type)) from err - else: - return self._serialize(data, **kwargs) + return self._serialize(data, **kwargs) @classmethod - def _get_custom_serializers(cls, data_type, **kwargs): + def _get_custom_serializers(cls, data_type, **kwargs): # pylint: disable=inconsistent-return-statements custom_serializer = kwargs.get("basic_types_serializers", {}).get(data_type) if custom_serializer: return custom_serializer @@ -839,23 +906,26 @@ def serialize_basic(cls, data, data_type, **kwargs): - basic_types_serializers dict[str, callable] : If set, use the callable as serializer - is_xml bool : If set, use xml_basic_types_serializers - :param data: Object to be serialized. + :param obj data: Object to be serialized. :param str data_type: Type of object in the iterable. + :rtype: str, int, float, bool + :return: serialized object """ custom_serializer = cls._get_custom_serializers(data_type, **kwargs) if custom_serializer: return custom_serializer(data) if data_type == "str": return cls.serialize_unicode(data) - return eval(data_type)(data) # nosec + return eval(data_type)(data) # nosec # pylint: disable=eval-used @classmethod def serialize_unicode(cls, data): """Special handling for serializing unicode strings in Py2. Encode to UTF-8 if unicode, otherwise handle as a str. - :param data: Object to be serialized. + :param str data: Object to be serialized. :rtype: str + :return: serialized object """ try: # If I received an enum, return its value return data.value @@ -869,8 +939,7 @@ def serialize_unicode(cls, data): return data except NameError: return str(data) - else: - return str(data) + return str(data) def serialize_iter(self, data, iter_type, div=None, **kwargs): """Serialize iterable. @@ -880,15 +949,13 @@ def serialize_iter(self, data, iter_type, div=None, **kwargs): serialization_ctxt['type'] should be same as data_type. - is_xml bool : If set, serialize as XML - :param list attr: Object to be serialized. + :param list data: Object to be serialized. :param str iter_type: Type of object in the iterable. - :param bool required: Whether the objects in the iterable must - not be None or empty. :param str div: If set, this str will be used to combine the elements in the iterable into a combined string. Default is 'None'. - :keyword bool do_quote: Whether to quote the serialized result of each iterable element. Defaults to False. :rtype: list, str + :return: serialized iterable """ if isinstance(data, str): raise SerializationError("Refuse str type as a valid iter type.") @@ -943,9 +1010,8 @@ def serialize_dict(self, attr, dict_type, **kwargs): :param dict attr: Object to be serialized. :param str dict_type: Type of object in the dictionary. - :param bool required: Whether the objects in the dictionary must - not be None or empty. :rtype: dict + :return: serialized dictionary """ serialization_ctxt = kwargs.get("serialization_ctxt", {}) serialized = {} @@ -969,7 +1035,7 @@ def serialize_dict(self, attr, dict_type, **kwargs): return serialized - def serialize_object(self, attr, **kwargs): + def serialize_object(self, attr, **kwargs): # pylint: disable=too-many-return-statements """Serialize a generic object. This will be handled as a dictionary. If object passed in is not a basic type (str, int, float, dict, list) it will simply be @@ -977,6 +1043,7 @@ def serialize_object(self, attr, **kwargs): :param dict attr: Object to be serialized. :rtype: dict or str + :return: serialized object """ if attr is None: return None @@ -1001,7 +1068,7 @@ def serialize_object(self, attr, **kwargs): return self.serialize_decimal(attr) # If it's a model or I know this dependency, serialize as a Model - elif obj_type in self.dependencies.values() or isinstance(attr, Model): + if obj_type in self.dependencies.values() or isinstance(attr, Model): return self._serialize(attr) if obj_type == dict: @@ -1032,56 +1099,61 @@ def serialize_enum(attr, enum_obj=None): try: enum_obj(result) # type: ignore return result - except ValueError: + except ValueError as exc: for enum_value in enum_obj: # type: ignore if enum_value.value.lower() == str(attr).lower(): return enum_value.value error = "{!r} is not valid value for enum {!r}" - raise SerializationError(error.format(attr, enum_obj)) + raise SerializationError(error.format(attr, enum_obj)) from exc @staticmethod - def serialize_bytearray(attr, **kwargs): + def serialize_bytearray(attr, **kwargs): # pylint: disable=unused-argument """Serialize bytearray into base-64 string. - :param attr: Object to be serialized. + :param str attr: Object to be serialized. :rtype: str + :return: serialized base64 """ return b64encode(attr).decode() @staticmethod - def serialize_base64(attr, **kwargs): + def serialize_base64(attr, **kwargs): # pylint: disable=unused-argument """Serialize str into base-64 string. - :param attr: Object to be serialized. + :param str attr: Object to be serialized. :rtype: str + :return: serialized base64 """ encoded = b64encode(attr).decode("ascii") return encoded.strip("=").replace("+", "-").replace("/", "_") @staticmethod - def serialize_decimal(attr, **kwargs): + def serialize_decimal(attr, **kwargs): # pylint: disable=unused-argument """Serialize Decimal object to float. - :param attr: Object to be serialized. + :param decimal attr: Object to be serialized. :rtype: float + :return: serialized decimal """ return float(attr) @staticmethod - def serialize_long(attr, **kwargs): + def serialize_long(attr, **kwargs): # pylint: disable=unused-argument """Serialize long (Py2) or int (Py3). - :param attr: Object to be serialized. + :param int attr: Object to be serialized. :rtype: int/long + :return: serialized long """ return _long_type(attr) @staticmethod - def serialize_date(attr, **kwargs): + def serialize_date(attr, **kwargs): # pylint: disable=unused-argument """Serialize Date object into ISO-8601 formatted string. :param Date attr: Object to be serialized. :rtype: str + :return: serialized date """ if isinstance(attr, str): attr = isodate.parse_date(attr) @@ -1089,11 +1161,12 @@ def serialize_date(attr, **kwargs): return t @staticmethod - def serialize_time(attr, **kwargs): + def serialize_time(attr, **kwargs): # pylint: disable=unused-argument """Serialize Time object into ISO-8601 formatted string. :param datetime.time attr: Object to be serialized. :rtype: str + :return: serialized time """ if isinstance(attr, str): attr = isodate.parse_time(attr) @@ -1103,30 +1176,32 @@ def serialize_time(attr, **kwargs): return t @staticmethod - def serialize_duration(attr, **kwargs): + def serialize_duration(attr, **kwargs): # pylint: disable=unused-argument """Serialize TimeDelta object into ISO-8601 formatted string. :param TimeDelta attr: Object to be serialized. :rtype: str + :return: serialized duration """ if isinstance(attr, str): attr = isodate.parse_duration(attr) return isodate.duration_isoformat(attr) @staticmethod - def serialize_rfc(attr, **kwargs): + def serialize_rfc(attr, **kwargs): # pylint: disable=unused-argument """Serialize Datetime object into RFC-1123 formatted string. :param Datetime attr: Object to be serialized. :rtype: str :raises: TypeError if format invalid. + :return: serialized rfc """ try: if not attr.tzinfo: _LOGGER.warning("Datetime with no tzinfo will be considered UTC.") utc = attr.utctimetuple() - except AttributeError: - raise TypeError("RFC1123 object must be valid Datetime object.") + except AttributeError as exc: + raise TypeError("RFC1123 object must be valid Datetime object.") from exc return "{}, {:02} {} {:04} {:02}:{:02}:{:02} GMT".format( Serializer.days[utc.tm_wday], @@ -1139,12 +1214,13 @@ def serialize_rfc(attr, **kwargs): ) @staticmethod - def serialize_iso(attr, **kwargs): + def serialize_iso(attr, **kwargs): # pylint: disable=unused-argument """Serialize Datetime object into ISO-8601 formatted string. :param Datetime attr: Object to be serialized. :rtype: str :raises: SerializationError if format invalid. + :return: serialized iso """ if isinstance(attr, str): attr = isodate.parse_datetime(attr) @@ -1170,13 +1246,14 @@ def serialize_iso(attr, **kwargs): raise TypeError(msg) from err @staticmethod - def serialize_unix(attr, **kwargs): + def serialize_unix(attr, **kwargs): # pylint: disable=unused-argument """Serialize Datetime object into IntTime format. This is represented as seconds. :param Datetime attr: Object to be serialized. :rtype: int :raises: SerializationError if format invalid + :return: serialied unix """ if isinstance(attr, int): return attr @@ -1184,11 +1261,11 @@ def serialize_unix(attr, **kwargs): if not attr.tzinfo: _LOGGER.warning("Datetime with no tzinfo will be considered UTC.") return int(calendar.timegm(attr.utctimetuple())) - except AttributeError: - raise TypeError("Unix time object must be valid Datetime object.") + except AttributeError as exc: + raise TypeError("Unix time object must be valid Datetime object.") from exc -def rest_key_extractor(attr, attr_desc, data): +def rest_key_extractor(attr, attr_desc, data): # pylint: disable=unused-argument key = attr_desc["key"] working_data = data @@ -1209,7 +1286,9 @@ def rest_key_extractor(attr, attr_desc, data): return working_data.get(key) -def rest_key_case_insensitive_extractor(attr, attr_desc, data): +def rest_key_case_insensitive_extractor( # pylint: disable=unused-argument, inconsistent-return-statements + attr, attr_desc, data +): key = attr_desc["key"] working_data = data @@ -1230,17 +1309,29 @@ def rest_key_case_insensitive_extractor(attr, attr_desc, data): return attribute_key_case_insensitive_extractor(key, None, working_data) -def last_rest_key_extractor(attr, attr_desc, data): - """Extract the attribute in "data" based on the last part of the JSON path key.""" +def last_rest_key_extractor(attr, attr_desc, data): # pylint: disable=unused-argument + """Extract the attribute in "data" based on the last part of the JSON path key. + + :param str attr: The attribute to extract + :param dict attr_desc: The attribute description + :param dict data: The data to extract from + :rtype: object + :returns: The extracted attribute + """ key = attr_desc["key"] dict_keys = _FLATTEN.split(key) return attribute_key_extractor(dict_keys[-1], None, data) -def last_rest_key_case_insensitive_extractor(attr, attr_desc, data): +def last_rest_key_case_insensitive_extractor(attr, attr_desc, data): # pylint: disable=unused-argument """Extract the attribute in "data" based on the last part of the JSON path key. This is the case insensitive version of "last_rest_key_extractor" + :param str attr: The attribute to extract + :param dict attr_desc: The attribute description + :param dict data: The data to extract from + :rtype: object + :returns: The extracted attribute """ key = attr_desc["key"] dict_keys = _FLATTEN.split(key) @@ -1277,7 +1368,7 @@ def _extract_name_from_internal_type(internal_type): return xml_name -def xml_key_extractor(attr, attr_desc, data): +def xml_key_extractor(attr, attr_desc, data): # pylint: disable=unused-argument,too-many-return-statements if isinstance(data, dict): return None @@ -1329,22 +1420,21 @@ def xml_key_extractor(attr, attr_desc, data): if is_iter_type: if is_wrapped: return None # is_wrapped no node, we want None - else: - return [] # not wrapped, assume empty list + return [] # not wrapped, assume empty list return None # Assume it's not there, maybe an optional node. # If is_iter_type and not wrapped, return all found children if is_iter_type: if not is_wrapped: return children - else: # Iter and wrapped, should have found one node only (the wrap one) - if len(children) != 1: - raise DeserializationError( - "Tried to deserialize an array not wrapped, and found several nodes '{}'. Maybe you should declare this array as wrapped?".format( - xml_name - ) + # Iter and wrapped, should have found one node only (the wrap one) + if len(children) != 1: + raise DeserializationError( + "Tried to deserialize an array not wrapped, and found several nodes '{}'. Maybe you should declare this array as wrapped?".format( # pylint: disable=line-too-long + xml_name ) - return list(children[0]) # Might be empty list and that's ok. + ) + return list(children[0]) # Might be empty list and that's ok. # Here it's not a itertype, we should have found one element only or empty if len(children) > 1: @@ -1361,7 +1451,7 @@ class Deserializer(object): basic_types = {str: "str", int: "int", bool: "bool", float: "float"} - valid_date = re.compile(r"\d{4}[-]\d{2}[-]\d{2}T\d{2}:\d{2}:\d{2}" r"\.?\d*Z?[-+]?[\d{2}]?:?[\d{2}]?") + valid_date = re.compile(r"\d{4}[-]\d{2}[-]\d{2}T\d{2}:\d{2}:\d{2}\.?\d*Z?[-+]?[\d{2}]?:?[\d{2}]?") def __init__(self, classes: Optional[Mapping[str, type]] = None): self.deserialize_type = { @@ -1401,11 +1491,12 @@ def __call__(self, target_obj, response_data, content_type=None): :param str content_type: Swagger "produces" if available. :raises: DeserializationError if deserialization fails. :return: Deserialized object. + :rtype: object """ data = self._unpack_content(response_data, content_type) return self._deserialize(target_obj, data) - def _deserialize(self, target_obj, data): + def _deserialize(self, target_obj, data): # pylint: disable=inconsistent-return-statements """Call the deserializer on a model. Data needs to be already deserialized as JSON or XML ElementTree @@ -1414,12 +1505,13 @@ def _deserialize(self, target_obj, data): :param object data: Object to deserialize. :raises: DeserializationError if deserialization fails. :return: Deserialized object. + :rtype: object """ # This is already a model, go recursive just in case if hasattr(data, "_attribute_map"): constants = [name for name, config in getattr(data, "_validation", {}).items() if config.get("constant")] try: - for attr, mapconfig in data._attribute_map.items(): + for attr, mapconfig in data._attribute_map.items(): # pylint: disable=protected-access if attr in constants: continue value = getattr(data, attr) @@ -1438,13 +1530,13 @@ def _deserialize(self, target_obj, data): if isinstance(response, str): return self.deserialize_data(data, response) - elif isinstance(response, type) and issubclass(response, Enum): + if isinstance(response, type) and issubclass(response, Enum): return self.deserialize_enum(data, response) - if data is None: + if data is None or data is CoreNull: return data try: - attributes = response._attribute_map # type: ignore + attributes = response._attribute_map # type: ignore # pylint: disable=protected-access d_attrs = {} for attr, attr_desc in attributes.items(): # Check empty string. If it's not empty, someone has a real "additionalProperties"... @@ -1474,9 +1566,8 @@ def _deserialize(self, target_obj, data): except (AttributeError, TypeError, KeyError) as err: msg = "Unable to deserialize to object: " + class_name # type: ignore raise DeserializationError(msg) from err - else: - additional_properties = self._build_additional_properties(attributes, data) - return self._instantiate_model(response, d_attrs, additional_properties) + additional_properties = self._build_additional_properties(attributes, data) + return self._instantiate_model(response, d_attrs, additional_properties) def _build_additional_properties(self, attribute_map, data): if not self.additional_properties_detection: @@ -1503,6 +1594,8 @@ def _classify_target(self, target, data): :param str target: The target object type to deserialize to. :param str/dict data: The response data to deserialize. + :return: The classified target object and its class name. + :rtype: tuple """ if target is None: return None, None @@ -1514,7 +1607,7 @@ def _classify_target(self, target, data): return target, target try: - target = target._classify(data, self.dependencies) # type: ignore + target = target._classify(data, self.dependencies) # type: ignore # pylint: disable=protected-access except AttributeError: pass # Target is not a Model, no classify return target, target.__class__.__name__ # type: ignore @@ -1529,10 +1622,12 @@ def failsafe_deserialize(self, target_obj, data, content_type=None): :param str target_obj: The target object type to deserialize to. :param str/dict data: The response data to deserialize. :param str content_type: Swagger "produces" if available. + :return: Deserialized object. + :rtype: object """ try: return self(target_obj, data, content_type=content_type) - except: + except: # pylint: disable=bare-except _LOGGER.debug( "Ran into a deserialization error. Ignoring since this is failsafe deserialization", exc_info=True ) @@ -1550,10 +1645,12 @@ def _unpack_content(raw_data, content_type=None): If raw_data is something else, bypass all logic and return it directly. - :param raw_data: Data to be processed. - :param content_type: How to parse if raw_data is a string/bytes. + :param obj raw_data: Data to be processed. + :param str content_type: How to parse if raw_data is a string/bytes. :raises JSONDecodeError: If JSON is requested and parsing is impossible. :raises UnicodeDecodeError: If bytes is not UTF8 + :rtype: object + :return: Unpacked content. """ # Assume this is enough to detect a Pipeline Response without importing it context = getattr(raw_data, "context", {}) @@ -1577,14 +1674,21 @@ def _unpack_content(raw_data, content_type=None): def _instantiate_model(self, response, attrs, additional_properties=None): """Instantiate a response model passing in deserialized args. - :param response: The response model class. - :param d_attrs: The deserialized response attributes. + :param Response response: The response model class. + :param dict attrs: The deserialized response attributes. + :param dict additional_properties: Additional properties to be set. + :rtype: Response + :return: The instantiated response model. """ if callable(response): subtype = getattr(response, "_subtype_map", {}) try: - readonly = [k for k, v in response._validation.items() if v.get("readonly")] - const = [k for k, v in response._validation.items() if v.get("constant")] + readonly = [ + k for k, v in response._validation.items() if v.get("readonly") # pylint: disable=protected-access + ] + const = [ + k for k, v in response._validation.items() if v.get("constant") # pylint: disable=protected-access + ] kwargs = {k: v for k, v in attrs.items() if k not in subtype and k not in readonly + const} response_obj = response(**kwargs) for attr in readonly: @@ -1594,7 +1698,7 @@ def _instantiate_model(self, response, attrs, additional_properties=None): return response_obj except TypeError as err: msg = "Unable to deserialize {} into model {}. ".format(kwargs, response) # type: ignore - raise DeserializationError(msg + str(err)) + raise DeserializationError(msg + str(err)) from err else: try: for attr, value in attrs.items(): @@ -1603,15 +1707,16 @@ def _instantiate_model(self, response, attrs, additional_properties=None): except Exception as exp: msg = "Unable to populate response model. " msg += "Type: {}, Error: {}".format(type(response), exp) - raise DeserializationError(msg) + raise DeserializationError(msg) from exp - def deserialize_data(self, data, data_type): + def deserialize_data(self, data, data_type): # pylint: disable=too-many-return-statements """Process data for deserialization according to data type. :param str data: The response string to be deserialized. :param str data_type: The type to deserialize to. :raises: DeserializationError if deserialization fails. :return: Deserialized object. + :rtype: object """ if data is None: return data @@ -1625,7 +1730,11 @@ def deserialize_data(self, data, data_type): if isinstance(data, self.deserialize_expected_types.get(data_type, tuple())): return data - is_a_text_parsing_type = lambda x: x not in ["object", "[]", r"{}"] + is_a_text_parsing_type = lambda x: x not in [ # pylint: disable=unnecessary-lambda-assignment + "object", + "[]", + r"{}", + ] if isinstance(data, ET.Element) and is_a_text_parsing_type(data_type) and not data.text: return None data_val = self.deserialize_type[data_type](data) @@ -1645,14 +1754,14 @@ def deserialize_data(self, data, data_type): msg = "Unable to deserialize response data." msg += " Data: {}, {}".format(data, data_type) raise DeserializationError(msg) from err - else: - return self._deserialize(obj_type, data) + return self._deserialize(obj_type, data) def deserialize_iter(self, attr, iter_type): """Deserialize an iterable. :param list attr: Iterable to be deserialized. :param str iter_type: The type of object in the iterable. + :return: Deserialized iterable. :rtype: list """ if attr is None: @@ -1669,6 +1778,7 @@ def deserialize_dict(self, attr, dict_type): :param dict/list attr: Dictionary to be deserialized. Also accepts a list of key, value pairs. :param str dict_type: The object type of the items in the dictionary. + :return: Deserialized dictionary. :rtype: dict """ if isinstance(attr, list): @@ -1679,11 +1789,12 @@ def deserialize_dict(self, attr, dict_type): attr = {el.tag: el.text for el in attr} return {k: self.deserialize_data(v, dict_type) for k, v in attr.items()} - def deserialize_object(self, attr, **kwargs): + def deserialize_object(self, attr, **kwargs): # pylint: disable=too-many-return-statements """Deserialize a generic object. This will be handled as a dictionary. :param dict attr: Dictionary to be deserialized. + :return: Deserialized object. :rtype: dict :raises: TypeError if non-builtin datatype encountered. """ @@ -1718,11 +1829,10 @@ def deserialize_object(self, attr, **kwargs): pass return deserialized - else: - error = "Cannot deserialize generic object with type: " - raise TypeError(error + str(obj_type)) + error = "Cannot deserialize generic object with type: " + raise TypeError(error + str(obj_type)) - def deserialize_basic(self, attr, data_type): + def deserialize_basic(self, attr, data_type): # pylint: disable=too-many-return-statements """Deserialize basic builtin data type from string. Will attempt to convert to str, int, float and bool. This function will also accept '1', '0', 'true' and 'false' as @@ -1730,6 +1840,7 @@ def deserialize_basic(self, attr, data_type): :param str attr: response string to be deserialized. :param str data_type: deserialization data type. + :return: Deserialized basic type. :rtype: str, int, float or bool :raises: TypeError if string format is not valid. """ @@ -1741,24 +1852,23 @@ def deserialize_basic(self, attr, data_type): if data_type == "str": # None or '', node is empty string. return "" - else: - # None or '', node with a strong type is None. - # Don't try to model "empty bool" or "empty int" - return None + # None or '', node with a strong type is None. + # Don't try to model "empty bool" or "empty int" + return None if data_type == "bool": if attr in [True, False, 1, 0]: return bool(attr) - elif isinstance(attr, str): + if isinstance(attr, str): if attr.lower() in ["true", "1"]: return True - elif attr.lower() in ["false", "0"]: + if attr.lower() in ["false", "0"]: return False raise TypeError("Invalid boolean value: {}".format(attr)) if data_type == "str": return self.deserialize_unicode(attr) - return eval(data_type)(attr) # nosec + return eval(data_type)(attr) # nosec # pylint: disable=eval-used @staticmethod def deserialize_unicode(data): @@ -1766,6 +1876,7 @@ def deserialize_unicode(data): as a string. :param str data: response string to be deserialized. + :return: Deserialized string. :rtype: str or unicode """ # We might be here because we have an enum modeled as string, @@ -1779,8 +1890,7 @@ def deserialize_unicode(data): return data except NameError: return str(data) - else: - return str(data) + return str(data) @staticmethod def deserialize_enum(data, enum_obj): @@ -1792,6 +1902,7 @@ def deserialize_enum(data, enum_obj): :param str data: Response string to be deserialized. If this value is None or invalid it will be returned as-is. :param Enum enum_obj: Enum object to deserialize to. + :return: Deserialized enum object. :rtype: Enum """ if isinstance(data, enum_obj) or data is None: @@ -1802,9 +1913,9 @@ def deserialize_enum(data, enum_obj): # Workaround. We might consider remove it in the future. try: return list(enum_obj.__members__.values())[data] - except IndexError: + except IndexError as exc: error = "{!r} is not a valid index for enum {!r}" - raise DeserializationError(error.format(data, enum_obj)) + raise DeserializationError(error.format(data, enum_obj)) from exc try: return enum_obj(str(data)) except ValueError: @@ -1820,6 +1931,7 @@ def deserialize_bytearray(attr): """Deserialize string into bytearray. :param str attr: response string to be deserialized. + :return: Deserialized bytearray :rtype: bytearray :raises: TypeError if string format invalid. """ @@ -1832,6 +1944,7 @@ def deserialize_base64(attr): """Deserialize base64 encoded string into string. :param str attr: response string to be deserialized. + :return: Deserialized base64 string :rtype: bytearray :raises: TypeError if string format invalid. """ @@ -1847,8 +1960,9 @@ def deserialize_decimal(attr): """Deserialize string into Decimal object. :param str attr: response string to be deserialized. - :rtype: Decimal + :return: Deserialized decimal :raises: DeserializationError if string format invalid. + :rtype: decimal """ if isinstance(attr, ET.Element): attr = attr.text @@ -1863,6 +1977,7 @@ def deserialize_long(attr): """Deserialize string into long (Py2) or int (Py3). :param str attr: response string to be deserialized. + :return: Deserialized int :rtype: long or int :raises: ValueError if string format invalid. """ @@ -1875,6 +1990,7 @@ def deserialize_duration(attr): """Deserialize ISO-8601 formatted string into TimeDelta object. :param str attr: response string to be deserialized. + :return: Deserialized duration :rtype: TimeDelta :raises: DeserializationError if string format invalid. """ @@ -1885,14 +2001,14 @@ def deserialize_duration(attr): except (ValueError, OverflowError, AttributeError) as err: msg = "Cannot deserialize duration object." raise DeserializationError(msg) from err - else: - return duration + return duration @staticmethod def deserialize_date(attr): """Deserialize ISO-8601 formatted string into Date object. :param str attr: response string to be deserialized. + :return: Deserialized date :rtype: Date :raises: DeserializationError if string format invalid. """ @@ -1908,6 +2024,7 @@ def deserialize_time(attr): """Deserialize ISO-8601 formatted string into time object. :param str attr: response string to be deserialized. + :return: Deserialized time :rtype: datetime.time :raises: DeserializationError if string format invalid. """ @@ -1922,6 +2039,7 @@ def deserialize_rfc(attr): """Deserialize RFC-1123 formatted string into Datetime object. :param str attr: response string to be deserialized. + :return: Deserialized RFC datetime :rtype: Datetime :raises: DeserializationError if string format invalid. """ @@ -1937,14 +2055,14 @@ def deserialize_rfc(attr): except ValueError as err: msg = "Cannot deserialize to rfc datetime object." raise DeserializationError(msg) from err - else: - return date_obj + return date_obj @staticmethod def deserialize_iso(attr): """Deserialize ISO-8601 formatted string into Datetime object. :param str attr: response string to be deserialized. + :return: Deserialized ISO datetime :rtype: Datetime :raises: DeserializationError if string format invalid. """ @@ -1974,8 +2092,7 @@ def deserialize_iso(attr): except (ValueError, OverflowError, AttributeError) as err: msg = "Cannot deserialize datetime object." raise DeserializationError(msg) from err - else: - return date_obj + return date_obj @staticmethod def deserialize_unix(attr): @@ -1983,6 +2100,7 @@ def deserialize_unix(attr): This is represented as seconds. :param int attr: Object to be serialized. + :return: Deserialized datetime :rtype: Datetime :raises: DeserializationError if format invalid """ @@ -1994,5 +2112,4 @@ def deserialize_unix(attr): except ValueError as err: msg = "Cannot deserialize to unix datetime object." raise DeserializationError(msg) from err - else: - return date_obj + return date_obj diff --git a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_vendor.py b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_vendor.py index 17d661da021a..4abf84acf980 100644 --- a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_vendor.py +++ b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_vendor.py @@ -7,7 +7,7 @@ from abc import ABC import json -from typing import Any, Dict, IO, List, Mapping, Optional, Sequence, TYPE_CHECKING, Tuple, Union +from typing import Any, Dict, IO, List, Mapping, Optional, TYPE_CHECKING, Tuple, Union from ._configuration import DocumentTranslationClientConfiguration, SingleDocumentTranslationClientConfiguration from ._model_base import Model, SdkJSONEncoder @@ -49,8 +49,6 @@ class SingleDocumentTranslationClientMixinABC(ABC): Tuple[Optional[str], FileContent, Optional[str]], ] -FilesType = Union[Mapping[str, FileType], Sequence[Tuple[str, FileType]]] - def serialize_multipart_data_entry(data_entry: Any) -> Any: if isinstance(data_entry, (list, tuple, dict, Model)): diff --git a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_version.py b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_version.py index f6cd39818b76..a1f432eddc4e 100644 --- a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_version.py +++ b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_version.py @@ -6,4 +6,4 @@ # Changes may cause incorrect behavior and will be lost if the code is regenerated. # -------------------------------------------------------------------------- -VERSION = "1.1.0b2" +VERSION = "1.1.0b1" diff --git a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/__init__.py b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/__init__.py index ddf8855b2d9b..0093a4eb585f 100644 --- a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/__init__.py +++ b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/__init__.py @@ -6,18 +6,20 @@ # Changes may cause incorrect behavior and will be lost if the code is regenerated. # -------------------------------------------------------------------------- -from ._patch import DocumentTranslationClient +from ._client import DocumentTranslationClient from ._client import SingleDocumentTranslationClient - -from ._patch import AsyncDocumentTranslationLROPoller +try: + from ._patch import __all__ as _patch_all + from ._patch import * # pylint: disable=unused-wildcard-import +except ImportError: + _patch_all = [] from ._patch import patch_sdk as _patch_sdk __all__ = [ - "AsyncDocumentTranslationLROPoller", "DocumentTranslationClient", "SingleDocumentTranslationClient", ] - +__all__.extend([p for p in _patch_all if p not in __all__]) _patch_sdk() diff --git a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/_client.py b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/_client.py index 88ebaf968d96..ad390f62d8ea 100644 --- a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/_client.py +++ b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/_client.py @@ -8,6 +8,7 @@ from copy import deepcopy from typing import Any, Awaitable, TYPE_CHECKING, Union +from typing_extensions import Self from azure.core import AsyncPipelineClient from azure.core.credentials import AzureKeyCredential @@ -101,7 +102,7 @@ def send_request( async def close(self) -> None: await self._client.close() - async def __aenter__(self) -> "DocumentTranslationClient": + async def __aenter__(self) -> Self: await self._client.__aenter__() return self @@ -185,7 +186,7 @@ def send_request( async def close(self) -> None: await self._client.close() - async def __aenter__(self) -> "SingleDocumentTranslationClient": + async def __aenter__(self) -> Self: await self._client.__aenter__() return self diff --git a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/_operations/__init__.py b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/_operations/__init__.py index 9e327bba3bf1..5f45db60645f 100644 --- a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/_operations/__init__.py +++ b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/_operations/__init__.py @@ -6,15 +6,16 @@ # Changes may cause incorrect behavior and will be lost if the code is regenerated. # -------------------------------------------------------------------------- -from ._patch import DocumentTranslationClientOperationsMixin -from ._patch import SingleDocumentTranslationClientOperationsMixin - +from ._operations import DocumentTranslationClientOperationsMixin +from ._operations import SingleDocumentTranslationClientOperationsMixin +from ._patch import __all__ as _patch_all +from ._patch import * # pylint: disable=unused-wildcard-import from ._patch import patch_sdk as _patch_sdk __all__ = [ "DocumentTranslationClientOperationsMixin", "SingleDocumentTranslationClientOperationsMixin", ] - +__all__.extend([p for p in _patch_all if p not in __all__]) _patch_sdk() diff --git a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/_operations/_operations.py b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/_operations/_operations.py index f801bc673f37..3335f74681b4 100644 --- a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/_operations/_operations.py +++ b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/_operations/_operations.py @@ -34,6 +34,8 @@ ResourceExistsError, ResourceNotFoundError, ResourceNotModifiedError, + StreamClosedError, + StreamConsumedError, map_error, ) from azure.core.pipeline import PipelineResponse @@ -44,18 +46,16 @@ from azure.core.tracing.decorator_async import distributed_trace_async from azure.core.utils import case_insensitive_dict -from ...models import _models -from ...models import _enums -from ... import _model_base +from ... import _model_base, models as _models from ..._model_base import SdkJSONEncoder, _deserialize from ..._operations._operations import ( + build_document_translation__begin_translation_request, build_document_translation_cancel_translation_request, build_document_translation_get_document_status_request, - build_document_translation_get_documents_status_request, build_document_translation_get_supported_formats_request, build_document_translation_get_translation_status_request, - build_document_translation_get_translations_status_request, - build_document_translation_start_translation_request, + build_document_translation_list_document_statuses_request, + build_document_translation_list_translation_statuses_request, build_single_document_translation_document_translate_request, ) from ..._vendor import prepare_multipart_form_data @@ -72,10 +72,10 @@ class DocumentTranslationClientOperationsMixin(DocumentTranslationClientMixinABC): - async def _start_translation_initial( # pylint: disable=inconsistent-return-statements + async def __begin_translation_initial( self, body: Union[_models.StartTranslationDetails, JSON, IO[bytes]], **kwargs: Any - ) -> None: - error_map: MutableMapping[int, Type[HttpResponseError]] = { + ) -> AsyncIterator[bytes]: + error_map: MutableMapping[int, Type[HttpResponseError]] = { # pylint: disable=unsubscriptable-object 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError, @@ -87,7 +87,7 @@ async def _start_translation_initial( # pylint: disable=inconsistent-return-sta _params = kwargs.pop("params", {}) or {} content_type: Optional[str] = kwargs.pop("content_type", _headers.pop("Content-Type", None)) - cls: ClsType[None] = kwargs.pop("cls", None) + cls: ClsType[AsyncIterator[bytes]] = kwargs.pop("cls", None) content_type = content_type or "application/json" _content = None @@ -96,7 +96,7 @@ async def _start_translation_initial( # pylint: disable=inconsistent-return-sta else: _content = json.dumps(body, cls=SdkJSONEncoder, exclude_readonly=True) # type: ignore - _request = build_document_translation_start_translation_request( + _request = build_document_translation__begin_translation_request( content_type=content_type, api_version=self._config.api_version, content=_content, @@ -108,7 +108,7 @@ async def _start_translation_initial( # pylint: disable=inconsistent-return-sta } _request.url = self._client.format_url(_request.url, **path_format_arguments) - _stream = False + _stream = True pipeline_response: PipelineResponse = await self._client._pipeline.run( # type: ignore # pylint: disable=protected-access _request, stream=_stream, **kwargs ) @@ -116,22 +116,27 @@ async def _start_translation_initial( # pylint: disable=inconsistent-return-sta response = pipeline_response.http_response if response.status_code not in [202]: - if _stream: + try: await response.read() # Load the body in memory and close the socket + except (StreamConsumedError, StreamClosedError): + pass map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response) response_headers = {} response_headers["Operation-Location"] = self._deserialize("str", response.headers.get("Operation-Location")) + deserialized = response.iter_bytes() + if cls: - return cls(pipeline_response, None, response_headers) # type: ignore + return cls(pipeline_response, deserialized, response_headers) # type: ignore + + return deserialized # type: ignore @overload - async def begin_start_translation( # pylint: disable=protected-access + async def _begin_translation( self, body: _models.StartTranslationDetails, *, content_type: str = "application/json", **kwargs: Any ) -> AsyncLROPoller[None]: - # pylint: disable=line-too-long """Submit a document translation request to the Document Translation service. Use this API to submit a bulk (batch) translation request to the Document @@ -154,76 +159,18 @@ async def begin_start_translation( # pylint: disable=protected-access destination, it will be overwritten. The targetUrl for each target language must be unique. - :param body: Required. - :type body: ~azure.ai.translation.document.models._models.StartTranslationDetails + :param body: Translation job submission batch request. Required. + :type body: ~azure.ai.translation.document.models.StartTranslationDetails :keyword content_type: Body Parameter content-type. Content type parameter for JSON body. Default value is "application/json". :paramtype content_type: str :return: An instance of AsyncLROPoller that returns None :rtype: ~azure.core.polling.AsyncLROPoller[None] :raises ~azure.core.exceptions.HttpResponseError: - - Example: - .. code-block:: python - - # JSON input template you can fill out and use as your body input. - body = { - "inputs": [ - { - "source": { - "sourceUrl": "str", # Location of the folder / - container or single file with your documents. Required. - "filter": { - "prefix": "str", # Optional. A - case-sensitive prefix string to filter documents in the source - path for translation. For example, when using a Azure storage - blob Uri, use the prefix to restrict sub folders for translation. - "suffix": "str" # Optional. A case-sensitive - suffix string to filter documents in the source path for - translation. This is most often use for file extensions. - }, - "language": "str", # Optional. Language code If none - is specified, we will perform auto detect on the document. - "storageSource": "str" # Optional. Storage Source. - "AzureBlob" - }, - "targets": [ - { - "language": "str", # Target Language. - Required. - "targetUrl": "str", # Location of the folder - / container with your documents. Required. - "category": "str", # Optional. Category / - custom system for translation request. - "glossaries": [ - { - "format": "str", # Format. - Required. - "glossaryUrl": "str", # - Location of the glossary. We will use the file extension - to extract the formatting if the format parameter is not - supplied. If the translation language pair is not - present in the glossary, it will not be applied. - Required. - "storageSource": "str", # - Optional. Storage Source. "AzureBlob" - "version": "str" # Optional. - Optional Version. If not specified, default is used. - } - ], - "storageSource": "str" # Optional. Storage - Source. "AzureBlob" - } - ], - "storageType": "str" # Optional. Storage type of the input - documents source string. Known values are: "Folder" and "File". - } - ] - } """ @overload - async def begin_start_translation( + async def _begin_translation( self, body: JSON, *, content_type: str = "application/json", **kwargs: Any ) -> AsyncLROPoller[None]: """Submit a document translation request to the Document Translation service. @@ -248,7 +195,7 @@ async def begin_start_translation( destination, it will be overwritten. The targetUrl for each target language must be unique. - :param body: Required. + :param body: Translation job submission batch request. Required. :type body: JSON :keyword content_type: Body Parameter content-type. Content type parameter for JSON body. Default value is "application/json". @@ -259,7 +206,7 @@ async def begin_start_translation( """ @overload - async def begin_start_translation( + async def _begin_translation( self, body: IO[bytes], *, content_type: str = "application/json", **kwargs: Any ) -> AsyncLROPoller[None]: """Submit a document translation request to the Document Translation service. @@ -284,7 +231,7 @@ async def begin_start_translation( destination, it will be overwritten. The targetUrl for each target language must be unique. - :param body: Required. + :param body: Translation job submission batch request. Required. :type body: IO[bytes] :keyword content_type: Body Parameter content-type. Content type parameter for binary body. Default value is "application/json". @@ -295,10 +242,9 @@ async def begin_start_translation( """ @distributed_trace_async - async def begin_start_translation( + async def _begin_translation( self, body: Union[_models.StartTranslationDetails, JSON, IO[bytes]], **kwargs: Any ) -> AsyncLROPoller[None]: - # pylint: disable=line-too-long """Submit a document translation request to the Document Translation service. Use this API to submit a bulk (batch) translation request to the Document @@ -321,70 +267,12 @@ async def begin_start_translation( destination, it will be overwritten. The targetUrl for each target language must be unique. - :param body: Is one of the following types: StartTranslationDetails, JSON, IO[bytes] Required. - :type body: ~azure.ai.translation.document.models._models.StartTranslationDetails or JSON or - IO[bytes] + :param body: Translation job submission batch request. Is one of the following types: + StartTranslationDetails, JSON, IO[bytes] Required. + :type body: ~azure.ai.translation.document.models.StartTranslationDetails or JSON or IO[bytes] :return: An instance of AsyncLROPoller that returns None :rtype: ~azure.core.polling.AsyncLROPoller[None] :raises ~azure.core.exceptions.HttpResponseError: - - Example: - .. code-block:: python - - # JSON input template you can fill out and use as your body input. - body = { - "inputs": [ - { - "source": { - "sourceUrl": "str", # Location of the folder / - container or single file with your documents. Required. - "filter": { - "prefix": "str", # Optional. A - case-sensitive prefix string to filter documents in the source - path for translation. For example, when using a Azure storage - blob Uri, use the prefix to restrict sub folders for translation. - "suffix": "str" # Optional. A case-sensitive - suffix string to filter documents in the source path for - translation. This is most often use for file extensions. - }, - "language": "str", # Optional. Language code If none - is specified, we will perform auto detect on the document. - "storageSource": "str" # Optional. Storage Source. - "AzureBlob" - }, - "targets": [ - { - "language": "str", # Target Language. - Required. - "targetUrl": "str", # Location of the folder - / container with your documents. Required. - "category": "str", # Optional. Category / - custom system for translation request. - "glossaries": [ - { - "format": "str", # Format. - Required. - "glossaryUrl": "str", # - Location of the glossary. We will use the file extension - to extract the formatting if the format parameter is not - supplied. If the translation language pair is not - present in the glossary, it will not be applied. - Required. - "storageSource": "str", # - Optional. Storage Source. "AzureBlob" - "version": "str" # Optional. - Optional Version. If not specified, default is used. - } - ], - "storageSource": "str" # Optional. Storage - Source. "AzureBlob" - } - ], - "storageType": "str" # Optional. Storage type of the input - documents source string. Known values are: "Folder" and "File". - } - ] - } """ _headers = case_insensitive_dict(kwargs.pop("headers", {}) or {}) _params = kwargs.pop("params", {}) or {} @@ -395,9 +283,10 @@ async def begin_start_translation( lro_delay = kwargs.pop("polling_interval", self._config.polling_interval) cont_token: Optional[str] = kwargs.pop("continuation_token", None) if cont_token is None: - raw_result = await self._start_translation_initial( # type: ignore + raw_result = await self.__begin_translation_initial( body=body, content_type=content_type, cls=lambda x, y, z: x, headers=_headers, params=_params, **kwargs ) + await raw_result.http_response.read() # type: ignore kwargs.pop("error_map", None) def get_long_running_output(pipeline_response): # pylint: disable=inconsistent-return-statements @@ -427,7 +316,7 @@ def get_long_running_output(pipeline_response): # pylint: disable=inconsistent- return AsyncLROPoller[None](self._client, raw_result, get_long_running_output, polling_method) # type: ignore @distributed_trace - def get_translations_status( + def list_translation_statuses( self, *, top: Optional[int] = None, @@ -439,7 +328,6 @@ def get_translations_status( orderby: Optional[List[str]] = None, **kwargs: Any ) -> AsyncIterable["_models.TranslationStatus"]: - # pylint: disable=line-too-long """Returns a list of batch requests submitted and the status for each request. Returns a list of batch requests submitted and the status for each @@ -535,60 +423,16 @@ def get_translations_status( :paramtype orderby: list[str] :return: An iterator like instance of TranslationStatus :rtype: - ~azure.core.async_paging.AsyncItemPaged[~azure.ai.translation.document.models._models.TranslationStatus] + ~azure.core.async_paging.AsyncItemPaged[~azure.ai.translation.document.models.TranslationStatus] :raises ~azure.core.exceptions.HttpResponseError: - - Example: - .. code-block:: python - - # response body for status code(s): 200 - response == { - "createdDateTimeUtc": "2020-02-20 00:00:00", # Operation created date time. - Required. - "id": "str", # Id of the operation. Required. - "lastActionDateTimeUtc": "2020-02-20 00:00:00", # Date time in which the - operation's status has been updated. Required. - "status": "str", # List of possible statuses for job or document. Required. - Known values are: "NotStarted", "Running", "Succeeded", "Failed", "Cancelled", - "Cancelling", and "ValidationFailed". - "summary": { - "cancelled": 0, # Number of cancelled. Required. - "failed": 0, # Failed count. Required. - "inProgress": 0, # Number of in progress. Required. - "notYetStarted": 0, # Count of not yet started. Required. - "success": 0, # Number of Success. Required. - "total": 0, # Total count. Required. - "totalCharacterCharged": 0 # Total characters charged by the API. - Required. - }, - "error": { - "code": "str", # Enums containing high level error codes. Required. - Known values are: "InvalidRequest", "InvalidArgument", "InternalServerError", - "ServiceUnavailable", "ResourceNotFound", "Unauthorized", and - "RequestRateTooHigh". - "message": "str", # Gets high level error message. Required. - "innerError": { - "code": "str", # Gets code error string. Required. - "message": "str", # Gets high level error message. Required. - "innerError": ..., - "target": "str" # Optional. Gets the source of the error. - For example it would be "documents" or "document id" in case of invalid - document. - }, - "target": "str" # Optional. Gets the source of the error. For - example it would be "documents" or "document id" in case of invalid document. - } - } """ _headers = kwargs.pop("headers", {}) or {} _params = kwargs.pop("params", {}) or {} maxpagesize = kwargs.pop("maxpagesize", None) - cls: ClsType[List[_models.TranslationStatus]] = kwargs.pop( # pylint: disable=protected-access - "cls", None - ) + cls: ClsType[List[_models.TranslationStatus]] = kwargs.pop("cls", None) - error_map: MutableMapping[int, Type[HttpResponseError]] = { + error_map: MutableMapping[int, Type[HttpResponseError]] = { # pylint: disable=unsubscriptable-object 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError, @@ -599,7 +443,7 @@ def get_translations_status( def prepare_request(next_link=None): if not next_link: - _request = build_document_translation_get_translations_status_request( + _request = build_document_translation_list_translation_statuses_request( top=top, skip=skip, maxpagesize=maxpagesize, @@ -658,8 +502,6 @@ async def get_next(next_link=None): response = pipeline_response.http_response if response.status_code not in [200]: - if _stream: - await response.read() # Load the body in memory and close the socket map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response) @@ -668,61 +510,21 @@ async def get_next(next_link=None): return AsyncItemPaged(get_next, extract_data) @distributed_trace_async - async def get_document_status( # pylint: disable=protected-access - self, id: str, document_id: str, **kwargs: Any - ) -> _models.DocumentStatus: - # pylint: disable=line-too-long + async def get_document_status(self, translation_id: str, document_id: str, **kwargs: Any) -> _models.DocumentStatus: """Returns the status for a specific document. Returns the translation status for a specific document based on the request Id and document Id. - :param id: Format - uuid. The batch id. Required. - :type id: str + :param translation_id: Format - uuid. The batch id. Required. + :type translation_id: str :param document_id: Format - uuid. The document id. Required. :type document_id: str :return: DocumentStatus. The DocumentStatus is compatible with MutableMapping - :rtype: ~azure.ai.translation.document.models._models.DocumentStatus + :rtype: ~azure.ai.translation.document.models.DocumentStatus :raises ~azure.core.exceptions.HttpResponseError: - - Example: - .. code-block:: python - - # response body for status code(s): 200 - response == { - "createdDateTimeUtc": "2020-02-20 00:00:00", # Operation created date time. - Required. - "id": "str", # Document Id. Required. - "lastActionDateTimeUtc": "2020-02-20 00:00:00", # Date time in which the - operation's status has been updated. Required. - "progress": 0.0, # Progress of the translation if available. Required. - "sourcePath": "str", # Location of the source document. Required. - "status": "str", # List of possible statuses for job or document. Required. - Known values are: "NotStarted", "Running", "Succeeded", "Failed", "Cancelled", - "Cancelling", and "ValidationFailed". - "to": "str", # To language. Required. - "characterCharged": 0, # Optional. Character charged by the API. - "error": { - "code": "str", # Enums containing high level error codes. Required. - Known values are: "InvalidRequest", "InvalidArgument", "InternalServerError", - "ServiceUnavailable", "ResourceNotFound", "Unauthorized", and - "RequestRateTooHigh". - "message": "str", # Gets high level error message. Required. - "innerError": { - "code": "str", # Gets code error string. Required. - "message": "str", # Gets high level error message. Required. - "innerError": ..., - "target": "str" # Optional. Gets the source of the error. - For example it would be "documents" or "document id" in case of invalid - document. - }, - "target": "str" # Optional. Gets the source of the error. For - example it would be "documents" or "document id" in case of invalid document. - }, - "path": "str" # Optional. Location of the document or folder. - } """ - error_map: MutableMapping[int, Type[HttpResponseError]] = { + error_map: MutableMapping[int, Type[HttpResponseError]] = { # pylint: disable=unsubscriptable-object 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError, @@ -733,10 +535,10 @@ async def get_document_status( # pylint: disable=protected-access _headers = kwargs.pop("headers", {}) or {} _params = kwargs.pop("params", {}) or {} - cls: ClsType[_models.DocumentStatus] = kwargs.pop("cls", None) # pylint: disable=protected-access + cls: ClsType[_models.DocumentStatus] = kwargs.pop("cls", None) _request = build_document_translation_get_document_status_request( - id=id, + translation_id=translation_id, document_id=document_id, api_version=self._config.api_version, headers=_headers, @@ -756,16 +558,17 @@ async def get_document_status( # pylint: disable=protected-access if response.status_code not in [200]: if _stream: - await response.read() # Load the body in memory and close the socket + try: + await response.read() # Load the body in memory and close the socket + except (StreamConsumedError, StreamClosedError): + pass map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response) if _stream: deserialized = response.iter_bytes() else: - deserialized = _deserialize( - _models.DocumentStatus, response.json() # pylint: disable=protected-access - ) + deserialized = _deserialize(_models.DocumentStatus, response.json()) if cls: return cls(pipeline_response, deserialized, {}) # type: ignore @@ -773,10 +576,7 @@ async def get_document_status( # pylint: disable=protected-access return deserialized # type: ignore @distributed_trace_async - async def get_translation_status( # pylint: disable=protected-access - self, id: str, **kwargs: Any - ) -> _models.TranslationStatus: - # pylint: disable=line-too-long + async def get_translation_status(self, translation_id: str, **kwargs: Any) -> _models.TranslationStatus: """Returns the status for a document translation request. Returns the status for a document translation request. @@ -784,55 +584,13 @@ async def get_translation_status( # pylint: disable=protected-access overall request status, as well as the status for documents that are being translated as part of that request. - :param id: Format - uuid. The operation id. Required. - :type id: str + :param translation_id: Format - uuid. The operation id. Required. + :type translation_id: str :return: TranslationStatus. The TranslationStatus is compatible with MutableMapping - :rtype: ~azure.ai.translation.document.models._models.TranslationStatus + :rtype: ~azure.ai.translation.document.models.TranslationStatus :raises ~azure.core.exceptions.HttpResponseError: - - Example: - .. code-block:: python - - # response body for status code(s): 200 - response == { - "createdDateTimeUtc": "2020-02-20 00:00:00", # Operation created date time. - Required. - "id": "str", # Id of the operation. Required. - "lastActionDateTimeUtc": "2020-02-20 00:00:00", # Date time in which the - operation's status has been updated. Required. - "status": "str", # List of possible statuses for job or document. Required. - Known values are: "NotStarted", "Running", "Succeeded", "Failed", "Cancelled", - "Cancelling", and "ValidationFailed". - "summary": { - "cancelled": 0, # Number of cancelled. Required. - "failed": 0, # Failed count. Required. - "inProgress": 0, # Number of in progress. Required. - "notYetStarted": 0, # Count of not yet started. Required. - "success": 0, # Number of Success. Required. - "total": 0, # Total count. Required. - "totalCharacterCharged": 0 # Total characters charged by the API. - Required. - }, - "error": { - "code": "str", # Enums containing high level error codes. Required. - Known values are: "InvalidRequest", "InvalidArgument", "InternalServerError", - "ServiceUnavailable", "ResourceNotFound", "Unauthorized", and - "RequestRateTooHigh". - "message": "str", # Gets high level error message. Required. - "innerError": { - "code": "str", # Gets code error string. Required. - "message": "str", # Gets high level error message. Required. - "innerError": ..., - "target": "str" # Optional. Gets the source of the error. - For example it would be "documents" or "document id" in case of invalid - document. - }, - "target": "str" # Optional. Gets the source of the error. For - example it would be "documents" or "document id" in case of invalid document. - } - } """ - error_map: MutableMapping[int, Type[HttpResponseError]] = { + error_map: MutableMapping[int, Type[HttpResponseError]] = { # pylint: disable=unsubscriptable-object 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError, @@ -843,10 +601,10 @@ async def get_translation_status( # pylint: disable=protected-access _headers = kwargs.pop("headers", {}) or {} _params = kwargs.pop("params", {}) or {} - cls: ClsType[_models.TranslationStatus] = kwargs.pop("cls", None) # pylint: disable=protected-access + cls: ClsType[_models.TranslationStatus] = kwargs.pop("cls", None) _request = build_document_translation_get_translation_status_request( - id=id, + translation_id=translation_id, api_version=self._config.api_version, headers=_headers, params=_params, @@ -865,16 +623,17 @@ async def get_translation_status( # pylint: disable=protected-access if response.status_code not in [200]: if _stream: - await response.read() # Load the body in memory and close the socket + try: + await response.read() # Load the body in memory and close the socket + except (StreamConsumedError, StreamClosedError): + pass map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response) if _stream: deserialized = response.iter_bytes() else: - deserialized = _deserialize( - _models.TranslationStatus, response.json() # pylint: disable=protected-access - ) + deserialized = _deserialize(_models.TranslationStatus, response.json()) if cls: return cls(pipeline_response, deserialized, {}) # type: ignore @@ -882,10 +641,7 @@ async def get_translation_status( # pylint: disable=protected-access return deserialized # type: ignore @distributed_trace_async - async def cancel_translation( # pylint: disable=protected-access - self, id: str, **kwargs: Any - ) -> _models.TranslationStatus: - # pylint: disable=line-too-long + async def cancel_translation(self, translation_id: str, **kwargs: Any) -> _models.TranslationStatus: """Cancel a currently processing or queued translation. Cancel a currently processing or queued translation. @@ -897,55 +653,13 @@ async def cancel_translation( # pylint: disable=protected-access All pending documents will be cancelled if possible. - :param id: Format - uuid. The operation-id. Required. - :type id: str + :param translation_id: Format - uuid. The operation-id. Required. + :type translation_id: str :return: TranslationStatus. The TranslationStatus is compatible with MutableMapping - :rtype: ~azure.ai.translation.document.models._models.TranslationStatus + :rtype: ~azure.ai.translation.document.models.TranslationStatus :raises ~azure.core.exceptions.HttpResponseError: - - Example: - .. code-block:: python - - # response body for status code(s): 200 - response == { - "createdDateTimeUtc": "2020-02-20 00:00:00", # Operation created date time. - Required. - "id": "str", # Id of the operation. Required. - "lastActionDateTimeUtc": "2020-02-20 00:00:00", # Date time in which the - operation's status has been updated. Required. - "status": "str", # List of possible statuses for job or document. Required. - Known values are: "NotStarted", "Running", "Succeeded", "Failed", "Cancelled", - "Cancelling", and "ValidationFailed". - "summary": { - "cancelled": 0, # Number of cancelled. Required. - "failed": 0, # Failed count. Required. - "inProgress": 0, # Number of in progress. Required. - "notYetStarted": 0, # Count of not yet started. Required. - "success": 0, # Number of Success. Required. - "total": 0, # Total count. Required. - "totalCharacterCharged": 0 # Total characters charged by the API. - Required. - }, - "error": { - "code": "str", # Enums containing high level error codes. Required. - Known values are: "InvalidRequest", "InvalidArgument", "InternalServerError", - "ServiceUnavailable", "ResourceNotFound", "Unauthorized", and - "RequestRateTooHigh". - "message": "str", # Gets high level error message. Required. - "innerError": { - "code": "str", # Gets code error string. Required. - "message": "str", # Gets high level error message. Required. - "innerError": ..., - "target": "str" # Optional. Gets the source of the error. - For example it would be "documents" or "document id" in case of invalid - document. - }, - "target": "str" # Optional. Gets the source of the error. For - example it would be "documents" or "document id" in case of invalid document. - } - } """ - error_map: MutableMapping[int, Type[HttpResponseError]] = { + error_map: MutableMapping[int, Type[HttpResponseError]] = { # pylint: disable=unsubscriptable-object 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError, @@ -956,10 +670,10 @@ async def cancel_translation( # pylint: disable=protected-access _headers = kwargs.pop("headers", {}) or {} _params = kwargs.pop("params", {}) or {} - cls: ClsType[_models.TranslationStatus] = kwargs.pop("cls", None) # pylint: disable=protected-access + cls: ClsType[_models.TranslationStatus] = kwargs.pop("cls", None) _request = build_document_translation_cancel_translation_request( - id=id, + translation_id=translation_id, api_version=self._config.api_version, headers=_headers, params=_params, @@ -978,16 +692,17 @@ async def cancel_translation( # pylint: disable=protected-access if response.status_code not in [200]: if _stream: - await response.read() # Load the body in memory and close the socket + try: + await response.read() # Load the body in memory and close the socket + except (StreamConsumedError, StreamClosedError): + pass map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response) if _stream: deserialized = response.iter_bytes() else: - deserialized = _deserialize( - _models.TranslationStatus, response.json() # pylint: disable=protected-access - ) + deserialized = _deserialize(_models.TranslationStatus, response.json()) if cls: return cls(pipeline_response, deserialized, {}) # type: ignore @@ -995,9 +710,9 @@ async def cancel_translation( # pylint: disable=protected-access return deserialized # type: ignore @distributed_trace - def get_documents_status( + def list_document_statuses( self, - id: str, + translation_id: str, *, top: Optional[int] = None, skip: Optional[int] = None, @@ -1008,7 +723,6 @@ def get_documents_status( orderby: Optional[List[str]] = None, **kwargs: Any ) -> AsyncIterable["_models.DocumentStatus"]: - # pylint: disable=line-too-long """Returns the status for all documents in a batch document translation request. Returns the status for all documents in a batch document translation request. @@ -1056,8 +770,8 @@ def get_documents_status( This reduces the risk of the client making assumptions about the data returned. - :param id: Format - uuid. The operation id. Required. - :type id: str + :param translation_id: Format - uuid. The operation id. Required. + :type translation_id: str :keyword top: top indicates the total number of records the user wants to be returned across all pages. @@ -1100,53 +814,16 @@ def get_documents_status( :paramtype orderby: list[str] :return: An iterator like instance of DocumentStatus :rtype: - ~azure.core.async_paging.AsyncItemPaged[~azure.ai.translation.document.models._models.DocumentStatus] + ~azure.core.async_paging.AsyncItemPaged[~azure.ai.translation.document.models.DocumentStatus] :raises ~azure.core.exceptions.HttpResponseError: - - Example: - .. code-block:: python - - # response body for status code(s): 200 - response == { - "createdDateTimeUtc": "2020-02-20 00:00:00", # Operation created date time. - Required. - "id": "str", # Document Id. Required. - "lastActionDateTimeUtc": "2020-02-20 00:00:00", # Date time in which the - operation's status has been updated. Required. - "progress": 0.0, # Progress of the translation if available. Required. - "sourcePath": "str", # Location of the source document. Required. - "status": "str", # List of possible statuses for job or document. Required. - Known values are: "NotStarted", "Running", "Succeeded", "Failed", "Cancelled", - "Cancelling", and "ValidationFailed". - "to": "str", # To language. Required. - "characterCharged": 0, # Optional. Character charged by the API. - "error": { - "code": "str", # Enums containing high level error codes. Required. - Known values are: "InvalidRequest", "InvalidArgument", "InternalServerError", - "ServiceUnavailable", "ResourceNotFound", "Unauthorized", and - "RequestRateTooHigh". - "message": "str", # Gets high level error message. Required. - "innerError": { - "code": "str", # Gets code error string. Required. - "message": "str", # Gets high level error message. Required. - "innerError": ..., - "target": "str" # Optional. Gets the source of the error. - For example it would be "documents" or "document id" in case of invalid - document. - }, - "target": "str" # Optional. Gets the source of the error. For - example it would be "documents" or "document id" in case of invalid document. - }, - "path": "str" # Optional. Location of the document or folder. - } """ _headers = kwargs.pop("headers", {}) or {} _params = kwargs.pop("params", {}) or {} maxpagesize = kwargs.pop("maxpagesize", None) - cls: ClsType[List[_models.DocumentStatus]] = kwargs.pop("cls", None) # pylint: disable=protected-access + cls: ClsType[List[_models.DocumentStatus]] = kwargs.pop("cls", None) - error_map: MutableMapping[int, Type[HttpResponseError]] = { + error_map: MutableMapping[int, Type[HttpResponseError]] = { # pylint: disable=unsubscriptable-object 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError, @@ -1157,8 +834,8 @@ def get_documents_status( def prepare_request(next_link=None): if not next_link: - _request = build_document_translation_get_documents_status_request( - id=id, + _request = build_document_translation_list_document_statuses_request( + translation_id=translation_id, top=top, skip=skip, maxpagesize=maxpagesize, @@ -1217,8 +894,6 @@ async def get_next(next_link=None): response = pipeline_response.http_response if response.status_code not in [200]: - if _stream: - await response.read() # Load the body in memory and close the socket map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response) @@ -1227,9 +902,9 @@ async def get_next(next_link=None): return AsyncItemPaged(get_next, extract_data) @distributed_trace_async - async def get_supported_formats( # pylint: disable=protected-access - self, *, type: Optional[Union[str, _enums.FileFormatType]] = None, **kwargs: Any - ) -> _models.SupportedFileFormats: + async def _get_supported_formats( # pylint: disable=protected-access + self, *, type: Optional[Union[str, _models.FileFormatType]] = None, **kwargs: Any + ) -> _models._models.SupportedFileFormats: """Returns a list of supported document formats. The list of supported formats supported by the Document Translation @@ -1243,34 +918,8 @@ async def get_supported_formats( # pylint: disable=protected-access :return: SupportedFileFormats. The SupportedFileFormats is compatible with MutableMapping :rtype: ~azure.ai.translation.document.models._models.SupportedFileFormats :raises ~azure.core.exceptions.HttpResponseError: - - Example: - .. code-block:: python - - # response body for status code(s): 200 - response == { - "value": [ - { - "contentTypes": [ - "str" # Supported Content-Types for this format. - Required. - ], - "fileExtensions": [ - "str" # Supported file extension for this format. - Required. - ], - "format": "str", # Name of the format. Required. - "defaultVersion": "str", # Optional. Default version if none - is specified. - "type": "str", # Optional. Supported Type for this format. - "versions": [ - "str" # Optional. Supported Version. - ] - } - ] - } """ - error_map: MutableMapping[int, Type[HttpResponseError]] = { + error_map: MutableMapping[int, Type[HttpResponseError]] = { # pylint: disable=unsubscriptable-object 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError, @@ -1281,7 +930,7 @@ async def get_supported_formats( # pylint: disable=protected-access _headers = kwargs.pop("headers", {}) or {} _params = kwargs.pop("params", {}) or {} - cls: ClsType[_models.SupportedFileFormats] = kwargs.pop("cls", None) # pylint: disable=protected-access + cls: ClsType[_models._models.SupportedFileFormats] = kwargs.pop("cls", None) # pylint: disable=protected-access _request = build_document_translation_get_supported_formats_request( type=type, @@ -1303,7 +952,10 @@ async def get_supported_formats( # pylint: disable=protected-access if response.status_code not in [200]: if _stream: - await response.read() # Load the body in memory and close the socket + try: + await response.read() # Load the body in memory and close the socket + except (StreamConsumedError, StreamClosedError): + pass map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response) @@ -1311,7 +963,7 @@ async def get_supported_formats( # pylint: disable=protected-access deserialized = response.iter_bytes() else: deserialized = _deserialize( - _models.SupportedFileFormats, response.json() # pylint: disable=protected-access + _models._models.SupportedFileFormats, response.json() # pylint: disable=protected-access ) if cls: @@ -1339,7 +991,7 @@ async def document_translate( Use this API to submit a single translation request to the Document Translation Service. - :param body: Required. + :param body: Document Translate Request Content. Required. :type body: ~azure.ai.translation.document.models.DocumentTranslateContent :keyword target_language: Specifies the language of the output document. The target language must be one of the supported languages included in the translation scope. @@ -1366,15 +1018,6 @@ async def document_translate( :return: AsyncIterator[bytes] :rtype: AsyncIterator[bytes] :raises ~azure.core.exceptions.HttpResponseError: - - Example: - .. code-block:: python - - # JSON input template you can fill out and use as your body input. - body = { - "document": filetype, - "glossary": [filetype] - } """ @overload @@ -1392,7 +1035,7 @@ async def document_translate( Use this API to submit a single translation request to the Document Translation Service. - :param body: Required. + :param body: Document Translate Request Content. Required. :type body: JSON :keyword target_language: Specifies the language of the output document. The target language must be one of the supported languages included in the translation scope. @@ -1436,7 +1079,8 @@ async def document_translate( Use this API to submit a single translation request to the Document Translation Service. - :param body: Is either a DocumentTranslateContent type or a JSON type. Required. + :param body: Document Translate Request Content. Is either a DocumentTranslateContent type or a + JSON type. Required. :type body: ~azure.ai.translation.document.models.DocumentTranslateContent or JSON :keyword target_language: Specifies the language of the output document. The target language must be one of the supported languages included in the translation scope. @@ -1463,17 +1107,8 @@ async def document_translate( :return: AsyncIterator[bytes] :rtype: AsyncIterator[bytes] :raises ~azure.core.exceptions.HttpResponseError: - - Example: - .. code-block:: python - - # JSON input template you can fill out and use as your body input. - body = { - "document": filetype, - "glossary": [filetype] - } """ - error_map: MutableMapping[int, Type[HttpResponseError]] = { + error_map: MutableMapping[int, Type[HttpResponseError]] = { # pylint: disable=unsubscriptable-object 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError, @@ -1516,7 +1151,10 @@ async def document_translate( if response.status_code not in [200]: if _stream: - await response.read() # Load the body in memory and close the socket + try: + await response.read() # Load the body in memory and close the socket + except (StreamConsumedError, StreamClosedError): + pass map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response) @@ -1524,6 +1162,7 @@ async def document_translate( response_headers["x-ms-client-request-id"] = self._deserialize( "str", response.headers.get("x-ms-client-request-id") ) + response_headers["content-type"] = self._deserialize("str", response.headers.get("content-type")) deserialized = response.iter_bytes() diff --git a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/_operations/_patch.py b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/_operations/_patch.py index c6893b2f1abd..f7dd32510333 100644 --- a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/_operations/_patch.py +++ b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/_operations/_patch.py @@ -6,322 +6,9 @@ Follow our quickstart for examples: https://aka.ms/azsdk/python/dpcodegen/python/customize """ -import sys -from typing import AsyncIterator, Callable, Dict, Type, TypeVar, overload, Any, IO, List, Optional, Union, cast -from azure.core.polling import AsyncNoPolling, AsyncPollingMethod -from azure.core.polling.async_base_polling import AsyncLROBasePolling -from azure.core.tracing.decorator import distributed_trace -from azure.core.tracing.decorator_async import distributed_trace_async -from azure.core.rest import AsyncHttpResponse, HttpRequest -from azure.core.exceptions import ( - ClientAuthenticationError, - HttpResponseError, - ResourceExistsError, - ResourceNotFoundError, - ResourceNotModifiedError, - map_error, -) -from azure.core.pipeline import PipelineResponse -from azure.core.utils import case_insensitive_dict +from typing import List -from ..._vendor import prepare_multipart_form_data -from ...models import _models -from ... import _model_base - -from ..._model_base import _deserialize -from ._operations import ( - DocumentTranslationClientOperationsMixin as GeneratedDocumentTranslationClientOperationsMixin, - SingleDocumentTranslationClientOperationsMixin as GeneratedSingleDocumentTranslationClientOperationsMixin, - build_single_document_translation_document_translate_request, - JSON, - ClsType, -) -from ...aio._patch import AsyncDocumentTranslationLROPoller - -if sys.version_info >= (3, 9): - from collections.abc import MutableMapping -else: - from typing import MutableMapping # type: ignore # pylint: disable=ungrouped-imports -JSON = MutableMapping[str, Any] # type: ignore[misc] # pylint: disable=unsubscriptable-object -T = TypeVar("T") -ClsType = Optional[ # type: ignore[misc] - Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any] -] - - -class DocumentTranslationClientOperationsMixin(GeneratedDocumentTranslationClientOperationsMixin): - - @distributed_trace - async def begin_start_translation( # type: ignore[override] - self, body: Union[_models.StartTranslationDetails, JSON, IO[bytes]], **kwargs: Any - ) -> AsyncDocumentTranslationLROPoller[_models.TranslationStatus]: - _headers = case_insensitive_dict(kwargs.pop("headers", {}) or {}) - _params = kwargs.pop("params", {}) or {} - - content_type: Optional[str] = kwargs.pop("content_type", _headers.pop("Content-Type", None)) - cls: ClsType[_models.TranslationStatus] = kwargs.pop("cls", None) - polling: Union[bool, AsyncPollingMethod] = kwargs.pop("polling", True) - lro_delay = kwargs.pop("polling_interval", self._config.polling_interval) - cont_token: Optional[str] = kwargs.pop("continuation_token", None) - if cont_token is None: - raw_result = await self._start_translation_initial( # type: ignore[func-returns-value] - body=body, content_type=content_type, cls=lambda x, y, z: x, headers=_headers, params=_params, **kwargs - ) - kwargs.pop("error_map", None) - - def get_long_running_output(pipeline_response): - response_headers = {} - response = pipeline_response.http_response - response_headers["Operation-Location"] = self._deserialize( - "str", response.headers.get("Operation-Location") - ) - - deserialized = _deserialize(_models.TranslationStatus, response.json()) - if cls: - return cls(pipeline_response, deserialized, response_headers) - return deserialized - - path_format_arguments = { - "endpoint": self._serialize.url("self._config.endpoint", self._config.endpoint, "str", skip_quote=True), - } - - if polling is True: - polling_method: AsyncPollingMethod = cast( - AsyncPollingMethod, - AsyncLROBasePolling(lro_delay, path_format_arguments=path_format_arguments, **kwargs), - ) - elif polling is False: - polling_method = cast(AsyncPollingMethod, AsyncNoPolling()) - else: - polling_method = polling - if cont_token: - return AsyncDocumentTranslationLROPoller[_models.TranslationStatus].from_continuation_token( - polling_method=polling_method, - continuation_token=cont_token, - client=self._client, - deserialization_callback=get_long_running_output, - ) - return AsyncDocumentTranslationLROPoller[_models.TranslationStatus]( - self._client, raw_result, get_long_running_output, polling_method - ) - - -class SingleDocumentTranslationClientOperationsMixin( - GeneratedSingleDocumentTranslationClientOperationsMixin -): # pylint: disable=name-too-long - - @overload - async def document_translate( - self, - body: _models.DocumentTranslateContent, - *, - target_language: str, - source_language: Optional[str] = None, - category: Optional[str] = None, - allow_fallback: Optional[bool] = None, - **kwargs: Any - ) -> AsyncIterator[bytes]: - """Submit a single document translation request to the Document Translation service. - - Use this API to submit a single translation request to the Document Translation Service. - - :param body: Required. - :type body: ~azure.ai.translation.document.models.DocumentTranslateContent - :keyword target_language: Specifies the language of the output document. - The target language must be one of the supported languages included in the translation scope. - For example if you want to translate the document in German language, then use - targetLanguage=de. Required. - :paramtype target_language: str - :keyword source_language: Specifies source language of the input document. - If this parameter isn't specified, automatic language detection is applied to determine the - source language. - For example if the source document is written in English, then use sourceLanguage=en. Default - value is None. - :paramtype source_language: str - :keyword category: A string specifying the category (domain) of the translation. This parameter - is used to get translations - from a customized system built with Custom Translator. Add the Category ID from your Custom - Translator - project details to this parameter to use your deployed customized system. Default value is: - general. Default value is None. - :paramtype category: str - :keyword allow_fallback: Specifies that the service is allowed to fall back to a general system - when a custom system doesn't exist. - Possible values are: true (default) or false. Default value is None. - :paramtype allow_fallback: bool - :return: AsyncIterator[bytes] - :rtype: AsyncIterator[bytes] - :raises ~azure.core.exceptions.HttpResponseError: - - Example: - .. code-block:: python - - # JSON input template you can fill out and use as your body input. - body = { - "document": filetype, - "glossary": [filetype] - } - """ - - @overload - async def document_translate( - self, - body: JSON, - *, - target_language: str, - source_language: Optional[str] = None, - category: Optional[str] = None, - allow_fallback: Optional[bool] = None, - **kwargs: Any - ) -> AsyncIterator[bytes]: - """Submit a single document translation request to the Document Translation service. - - Use this API to submit a single translation request to the Document Translation Service. - - :param body: Required. - :type body: JSON - :keyword target_language: Specifies the language of the output document. - The target language must be one of the supported languages included in the translation scope. - For example if you want to translate the document in German language, then use - targetLanguage=de. Required. - :paramtype target_language: str - :keyword source_language: Specifies source language of the input document. - If this parameter isn't specified, automatic language detection is applied to determine the - source language. - For example if the source document is written in English, then use sourceLanguage=en. Default - value is None. - :paramtype source_language: str - :keyword category: A string specifying the category (domain) of the translation. This parameter - is used to get translations - from a customized system built with Custom Translator. Add the Category ID from your Custom - Translator - project details to this parameter to use your deployed customized system. Default value is: - general. Default value is None. - :paramtype category: str - :keyword allow_fallback: Specifies that the service is allowed to fall back to a general system - when a custom system doesn't exist. - Possible values are: true (default) or false. Default value is None. - :paramtype allow_fallback: bool - :return: AsyncIterator[bytes] - :rtype: AsyncIterator[bytes] - :raises ~azure.core.exceptions.HttpResponseError: - """ - - @distributed_trace_async - async def document_translate( - self, - body: Union[_models.DocumentTranslateContent, JSON], - *, - target_language: str, - source_language: Optional[str] = None, - category: Optional[str] = None, - allow_fallback: Optional[bool] = None, - **kwargs: Any - ) -> AsyncIterator[bytes]: - """Submit a single document translation request to the Document Translation service. - - Use this API to submit a single translation request to the Document Translation Service. - - :param body: Is either a DocumentTranslateContent type or a JSON type. Required. - :type body: ~azure.ai.translation.document.models.DocumentTranslateContent or JSON - :keyword target_language: Specifies the language of the output document. - The target language must be one of the supported languages included in the translation scope. - For example if you want to translate the document in German language, then use - targetLanguage=de. Required. - :paramtype target_language: str - :keyword source_language: Specifies source language of the input document. - If this parameter isn't specified, automatic language detection is applied to determine the - source language. - For example if the source document is written in English, then use sourceLanguage=en. Default - value is None. - :paramtype source_language: str - :keyword category: A string specifying the category (domain) of the translation. This parameter - is used to get translations - from a customized system built with Custom Translator. Add the Category ID from your Custom - Translator - project details to this parameter to use your deployed customized system. Default value is: - general. Default value is None. - :paramtype category: str - :keyword allow_fallback: Specifies that the service is allowed to fall back to a general system - when a custom system doesn't exist. - Possible values are: true (default) or false. Default value is None. - :paramtype allow_fallback: bool - :return: AsyncIterator[bytes] - :rtype: AsyncIterator[bytes] - :raises ~azure.core.exceptions.HttpResponseError: - - Example: - .. code-block:: python - - # JSON input template you can fill out and use as your body input. - body = { - "document": filetype, - "glossary": [filetype] - } - """ - error_map: MutableMapping[int, Type[HttpResponseError]] = { - 401: ClientAuthenticationError, - 404: ResourceNotFoundError, - 409: ResourceExistsError, - 304: ResourceNotModifiedError, - } - error_map.update(kwargs.pop("error_map", {}) or {}) - - _headers = kwargs.pop("headers", {}) or {} - _params = kwargs.pop("params", {}) or {} - - cls: ClsType[AsyncIterator[bytes]] = kwargs.pop("cls", None) - - _body = body.as_dict() if isinstance(body, _model_base.Model) else body - _file_fields: List[str] = ["document", "glossary"] - _data_fields: List[str] = [] - _files, _data = prepare_multipart_form_data(_body, _file_fields, _data_fields) - - _request = build_single_document_translation_document_translate_request( - target_language=target_language, - source_language=source_language, - category=category, - allow_fallback=allow_fallback, - api_version=self._config.api_version, - files=_files, - data=_data, - headers=_headers, - params=_params, - ) - path_format_arguments = { - "endpoint": self._serialize.url("self._config.endpoint", self._config.endpoint, "str", skip_quote=True), - } - _request.url = self._client.format_url(_request.url, **path_format_arguments) - - _stream = kwargs.pop("stream", True) - pipeline_response: PipelineResponse = await self._client._pipeline.run( # type: ignore # pylint: disable=protected-access - _request, stream=_stream, **kwargs - ) - - response = pipeline_response.http_response - - if response.status_code not in [200]: - if _stream: - await response.read() # Load the body in memory and close the socket - map_error(status_code=response.status_code, response=response, error_map=error_map) - raise HttpResponseError(response=response) - - response_headers = {} - response_headers["x-ms-request-id"] = self._deserialize("str", response.headers.get("x-ms-request-id")) - - # deserialized = response.iter_bytes() - - if cls: - # return cls(pipeline_response, deserialized, response_headers) # type: ignore - return cls(pipeline_response, response.read(), response_headers) # type: ignore - - return await response.read() - - -__all__: List[str] = [ - "DocumentTranslationClientOperationsMixin", - "SingleDocumentTranslationClientOperationsMixin", -] # Add all objects you want publicly available to users at this package level +__all__: List[str] = [] # Add all objects you want publicly available to users at this package level def patch_sdk(): diff --git a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/_patch.py b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/_patch.py index c94fef4eb896..f7dd32510333 100644 --- a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/_patch.py +++ b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/_patch.py @@ -2,642 +2,13 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT License. # ------------------------------------ -# mypy: disable-error-code="attr-defined" +"""Customize generated code here. -import json -import datetime -from typing import Any, List, Union, overload, Optional, cast, Tuple, TypeVar -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 -from azure.core.credentials import AzureKeyCredential -from azure.core.credentials_async import AsyncTokenCredential +Follow our quickstart for examples: https://aka.ms/azsdk/python/dpcodegen/python/customize +""" +from typing import List -from azure.core.polling import AsyncLROPoller -from azure.core.polling.base_polling import ( - OperationFailed, - _raise_if_bad_http_status_and_method, -) -from azure.core.polling.async_base_polling import AsyncLROBasePolling - -from azure.ai.translation.document import ( - DocumentTranslationInput, - DocumentTranslationFileFormat, - TranslationGlossary, - TranslationStatus, - DocumentStatus, - StorageInputType, -) - -from ..models._models import ( - TranslationStatus as _TranslationStatus, - DocumentStatus as _DocumentStatus, - StartTranslationDetails, -) -from ...document._patch import ( - get_http_logging_policy, - get_translation_input, - convert_datetime, - convert_order_by, - TranslationPolling, - convert_status, -) - -POLLING_INTERVAL = 1 -PollingReturnType_co = TypeVar("PollingReturnType_co", covariant=True) -_FINISHED = frozenset(["succeeded", "cancelled", "cancelling", "failed"]) -_FAILED = frozenset(["validationfailed"]) - - -class AsyncDocumentTranslationLROPoller(AsyncLROPoller[PollingReturnType_co]): - """An async custom poller implementation for Document Translation. Call `result()` on the poller to return - a pageable of :class:`~azure.ai.translation.document.DocumentStatus`.""" - - @property - def id(self) -> str: - """The ID for the translation operation - - :return: The str ID for the translation operation. - :rtype: str - """ - if self._polling_method._current_body: # pylint: disable=protected-access - return self._polling_method._current_body.id # pylint: disable=protected-access - return self._polling_method._get_id_from_headers() # pylint: disable=protected-access - - @property - def details(self) -> TranslationStatus: - """The details for the translation operation - - :return: The details for the translation operation. - :rtype: ~azure.ai.translation.document.TranslationStatus - """ - if self._polling_method._current_body: # pylint: disable=protected-access - return TranslationStatus._from_generated( # pylint: disable=protected-access - self._polling_method._current_body # pylint: disable=protected-access - ) - return TranslationStatus(id=self._polling_method._get_id_from_headers()) # pylint: disable=protected-access - - @classmethod - def from_continuation_token( # pylint: disable=docstring-missing-return,docstring-missing-param,docstring-missing-rtype - cls, polling_method, continuation_token, **kwargs - ): - """ - :meta private: - """ - ( - client, - initial_response, - deserialization_callback, - ) = polling_method.from_continuation_token(continuation_token, **kwargs) - - return cls(client, initial_response, deserialization_callback, polling_method) - - -class AsyncDocumentTranslationLROPollingMethod(AsyncLROBasePolling): - """A custom polling method implementation for Document Translation.""" - - def __init__(self, *args, **kwargs): - self._cont_token_response = kwargs.pop("cont_token_response") - super().__init__(*args, **kwargs) - - @property - def _current_body(self) -> _TranslationStatus: - try: - return _TranslationStatus(self._pipeline_response.http_response.json()) - except json.decoder.JSONDecodeError: - return _TranslationStatus() # type: ignore[call-overload] - - def _get_id_from_headers(self) -> str: - return ( - self._initial_response.http_response.headers["Operation-Location"] - .split("/batches/")[1] - .split("?api-version")[0] - ) - - def finished(self) -> bool: - """Is this polling finished? - - :return: True/False for whether polling is complete. - :rtype: bool - """ - return self._finished(self.status()) - - @staticmethod - def _finished(status) -> bool: - if hasattr(status, "value"): - status = status.value - return str(status).lower() in _FINISHED - - @staticmethod - def _failed(status) -> bool: - if hasattr(status, "value"): - status = status.value - return str(status).lower() in _FAILED - - def get_continuation_token(self) -> str: - if self._current_body: - return self._current_body.id - return self._get_id_from_headers() - - # pylint: disable=arguments-differ - def from_continuation_token(self, continuation_token: str, **kwargs: Any) -> Tuple: # type: ignore[override] - try: - client = kwargs["client"] - except KeyError as exc: - raise ValueError("Need kwarg 'client' to be recreated from continuation_token") from exc - - try: - deserialization_callback = kwargs["deserialization_callback"] - except KeyError as exc: - raise ValueError("Need kwarg 'deserialization_callback' to be recreated from continuation_token") from exc - - return client, self._cont_token_response, deserialization_callback - - async def _poll(self) -> None: - """Poll status of operation so long as operation is incomplete and - we have an endpoint to query. - - :raises: OperationFailed if operation status 'Failed' or 'Canceled'. - :raises: BadStatus if response status invalid. - :raises: BadResponse if response invalid. - """ - while not self.finished(): - await self.update_status() - while not self.finished(): - await self._delay() - await self.update_status() - - if self._failed(self.status()): - raise OperationFailed("Operation failed or canceled") - - final_get_url = self._operation.get_final_get_url(self._pipeline_response) - if final_get_url: - self._pipeline_response = await self.request_status(final_get_url) - _raise_if_bad_http_status_and_method(self._pipeline_response.http_response) - - -class DocumentTranslationClient: - """DocumentTranslationClient. - - :param endpoint: Supported document Translation endpoint, protocol and hostname, for example: - https://{TranslatorResourceName}.cognitiveservices.azure.com/translator. Required. - :type endpoint: str - :param credential: Credential used to authenticate requests to the service. Is either a - AzureKeyCredential type or a TokenCredential type. Required. - :type credential: ~azure.core.credentials.AzureKeyCredential or - ~azure.core.credentials_async.AsyncTokenCredential - :keyword api_version: The API version to use for this operation. Default value is "2024-05-01". - Note that overriding this default value may result in unsupported behavior. - :paramtype api_version: str - :keyword int polling_interval: Default waiting time between two polls for LRO operations if no - Retry-After header is present. - """ - - def __init__( - self, - endpoint: str, - credential: Union[AzureKeyCredential, AsyncTokenCredential], # pylint: disable=used-before-assignment - **kwargs: Any - ) -> None: - """DocumentTranslationClient is your interface to the Document Translation service. - Use the client to translate whole documents while preserving source document - structure and text formatting. - - :param str endpoint: Supported Document Translation endpoint (protocol and hostname, for example: - https://.cognitiveservices.azure.com/). - :param credential: Credentials needed for the client to connect to Azure. - This is an instance of AzureKeyCredential if using an API key or a token - credential from :mod:`azure.identity`. - :type credential: :class:`~azure.core.credentials.AzureKeyCredential` or - :class:`~azure.core.credentials.TokenCredential` - :keyword api_version: - The API version of the service to use for requests. It defaults to the latest service version. - Setting to an older version may result in reduced feature compatibility. - :paramtype api_version: str or ~azure.ai.translation.document.DocumentTranslationApiVersion - - .. admonition:: Example: - - .. literalinclude:: ../samples/async_samples/sample_authentication_async.py - :start-after: [START create_dt_client_with_key_async] - :end-before: [END create_dt_client_with_key_async] - :language: python - :dedent: 4 - :caption: Creating the DocumentTranslationClient with an endpoint and API key. - - .. literalinclude:: ../samples/async_samples/sample_authentication_async.py - :start-after: [START create_dt_client_with_aad_async] - :end-before: [END create_dt_client_with_aad_async] - :language: python - :dedent: 4 - :caption: Creating the DocumentTranslationClient with a token credential. - """ - try: - self._endpoint = endpoint.rstrip("/") - except AttributeError as exc: - raise ValueError("Parameter 'endpoint' must be a string.") from exc - self._credential = credential - polling_interval = kwargs.pop("polling_interval", POLLING_INTERVAL) - from ._client import DocumentTranslationClient as _BatchDocumentTranslationClient - - self._client = _BatchDocumentTranslationClient( - endpoint=self._endpoint, - credential=credential, - http_logging_policy=kwargs.pop("http_logging_policy", get_http_logging_policy()), - polling_interval=polling_interval, - **kwargs - ) - - async def __aenter__(self) -> "DocumentTranslationClient": - await self._client.__aenter__() - return self - - async def __aexit__(self, *args: "Any") -> None: - await self._client.__aexit__(*args) - - async def close(self) -> None: - """Close the :class:`~azure.ai.translation.document.aio.DocumentTranslationClient` session.""" - await self._client.__aexit__() - - @overload - async def begin_translation( - self, - source_url: str, - target_url: str, - target_language: str, - *, - source_language: Optional[str] = None, - prefix: Optional[str] = None, - suffix: Optional[str] = None, - storage_type: Optional[Union[str, StorageInputType]] = None, - category_id: Optional[str] = None, - glossaries: Optional[List[TranslationGlossary]] = None, - **kwargs: Any - ) -> AsyncDocumentTranslationLROPoller[AsyncItemPaged[DocumentStatus]]: - """Begin translating the document(s) in your source container to your target container - in the given language. There are two ways to call this method: - - 1) To perform translation on documents from a single source container to a single target container, pass the - `source_url`, `target_url`, and `target_language` parameters including any optional keyword arguments. - - 2) To pass multiple inputs for translation (multiple sources or targets), pass the `inputs` parameter - as a list of :class:`~azure.ai.translation.document.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. See the service documentation for the supported SAS permissions for accessing - source storage containers/blobs: https://aka.ms/azsdk/documenttranslation/sas-permissions - :param str target_url: The target SAS URL to the Azure Blob container where the translated documents - should be written. See the service documentation for the supported SAS permissions for accessing - target storage containers/blobs: https://aka.ms/azsdk/documenttranslation/sas-permissions - :param str target_language: This is the language code you want your documents to be translated to. - See supported language codes here: - https://docs.microsoft.com/azure/cognitive-services/translator/language-support#translate - :keyword str source_language: Language code for the source documents. - If none is specified, the source language will be auto-detected for each document. - :keyword str prefix: A case-sensitive prefix string to filter documents in the source path for - translation. For example, when using a Azure storage blob Uri, use the prefix to restrict - sub folders for translation. - :keyword str suffix: A case-sensitive suffix string to filter documents in the source path for - translation. This is most often use for file extensions. - :keyword storage_type: Storage type of the input documents source string. Possible values - include: "Folder", "File". - :paramtype storage_type: str or ~azure.ai.translation.document.StorageInputType - :keyword str category_id: Category / custom model ID for using custom translation. - :keyword glossaries: Glossaries to apply to translation. - :paramtype glossaries: list[~azure.ai.translation.document.TranslationGlossary] - :return: An instance of an AsyncDocumentTranslationLROPoller. Call `result()` on the poller - object to return a pageable of DocumentStatus. A DocumentStatus will be - returned for each translation on a document. - :rtype: AsyncDocumentTranslationLROPoller[~azure.core.async_paging.AsyncItemPaged[DocumentStatus]] - :raises ~azure.core.exceptions.HttpResponseError: - """ - - @overload - async def begin_translation( - self, inputs: List[DocumentTranslationInput], **kwargs: Any - ) -> AsyncDocumentTranslationLROPoller[AsyncItemPaged[DocumentStatus]]: - """Begin translating the document(s) in your source container to your target container - in the given language. There are two ways to call this method: - - 1) To perform translation on documents from a single source container to a single target container, pass the - `source_url`, `target_url`, and `target_language` parameters including any optional keyword arguments. - - 2) To pass multiple inputs for translation (multiple sources or targets), pass the `inputs` parameter - as a list of :class:`~azure.ai.translation.document.DocumentTranslationInput`. - - For supported languages and document formats, see the service documentation: - https://docs.microsoft.com/azure/cognitive-services/translator/document-translation/overview - - :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. - :type inputs: List[~azure.ai.translation.document.DocumentTranslationInput] - :return: An instance of a AsyncDocumentTranslationLROPoller. Call `result()` on the poller - object to return a pageable of DocumentStatus. A DocumentStatus will be - returned for each translation on a document. - :rtype: AsyncDocumentTranslationLROPoller[~azure.core.async_paging.AsyncItemPaged[DocumentStatus]] - :raises ~azure.core.exceptions.HttpResponseError: - """ - - @distributed_trace_async - async def begin_translation( # pylint: disable=docstring-missing-param - self, *args: Union[str, List[DocumentTranslationInput]], **kwargs: Any - ) -> AsyncDocumentTranslationLROPoller[AsyncItemPaged[DocumentStatus]]: - """Begin translating the document(s) in your source container to your target container - in the given language. There are two ways to call this method: - - 1) To perform translation on documents from a single source container to a single target container, pass the - `source_url`, `target_url`, and `target_language` parameters including any optional keyword arguments. - - 2) To pass multiple inputs for translation (multiple sources or targets), pass the `inputs` parameter - as a list of :class:`~azure.ai.translation.document.DocumentTranslationInput`. - - For supported languages and document formats, see the service documentation: - https://docs.microsoft.com/azure/cognitive-services/translator/document-translation/overview - - :return: An instance of an AsyncDocumentTranslationLROPoller. Call `result()` on the poller - object to return a pageable of DocumentStatus. A DocumentStatus will be - returned for each translation on a document. - :rtype: AsyncDocumentTranslationLROPoller[~azure.core.async_paging.AsyncItemPaged[DocumentStatus]] - :raises ~azure.core.exceptions.HttpResponseError: - - .. admonition:: Example: - - .. literalinclude:: ../samples/async_samples/sample_begin_translation_async.py - :start-after: [START begin_translation_async] - :end-before: [END begin_translation_async] - :language: python - :dedent: 4 - :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 - translation_status = json.loads(raw_response.http_response.text()) - return self.list_document_statuses(translation_status["id"]) - - polling_interval = kwargs.pop( - "polling_interval", - self._client._config.polling_interval, # pylint: disable=protected-access - ) - - pipeline_response = None - if continuation_token: - pipeline_response = await self._client.get_translation_status( - continuation_token, - cls=lambda pipeline_response, _, response_headers: pipeline_response, - ) - - callback = kwargs.pop("cls", deserialization_callback) - return cast( - AsyncDocumentTranslationLROPoller[AsyncItemPaged[DocumentStatus]], - await self._client.begin_start_translation( - body=StartTranslationDetails(inputs=inputs), - polling=AsyncDocumentTranslationLROPollingMethod( - timeout=polling_interval, - lro_algorithms=[TranslationPolling()], - cont_token_response=pipeline_response, - **kwargs - ), - cls=callback, - continuation_token=continuation_token, - **kwargs - ), - ) - - @distributed_trace_async - async def get_translation_status(self, translation_id: str, **kwargs: Any) -> TranslationStatus: - """Gets the status of the translation operation. - - Includes the overall status, as well as a summary of - the documents that are being translated as part of that translation operation. - - :param str translation_id: The translation operation ID. - :return: A TranslationStatus with information on the status of the translation operation. - :rtype: ~azure.ai.translation.document.TranslationStatus - :raises ~azure.core.exceptions.HttpResponseError or ~azure.core.exceptions.ResourceNotFoundError: - """ - - translation_status = await self._client.get_translation_status(translation_id, **kwargs) - return TranslationStatus._from_generated( # pylint: disable=protected-access - _TranslationStatus(translation_status) # type: ignore[call-overload] - ) - - @distributed_trace_async - async def cancel_translation(self, translation_id: str, **kwargs: Any) -> None: - """Cancel a currently processing or queued translation operation. - - A translation will not be canceled if it is already completed, failed, or canceling. - All documents that have completed translation will not be canceled and will be charged. - If possible, all pending documents will be canceled. - - :param str translation_id: The translation operation ID. - :return: None - :rtype: None - :raises ~azure.core.exceptions.HttpResponseError or ~azure.core.exceptions.ResourceNotFoundError: - """ - - await self._client.cancel_translation(translation_id, **kwargs) - - @distributed_trace - def list_translation_statuses( - self, - *, - top: Optional[int] = None, - skip: Optional[int] = None, - translation_ids: Optional[List[str]] = None, - statuses: Optional[List[str]] = None, - created_after: Optional[Union[str, datetime.datetime]] = None, - created_before: Optional[Union[str, datetime.datetime]] = None, - order_by: Optional[List[str]] = None, - **kwargs: Any - ) -> AsyncItemPaged[TranslationStatus]: - """List all the submitted translation operations under the Document Translation resource. - - :keyword int top: The total number of operations to return (across all pages) from all submitted translations. - :keyword int skip: The number of operations to skip (from beginning of all submitted operations). - By default, we sort by all submitted operations in descending order by start time. - :keyword list[str] translation_ids: Translation operations ids to filter by. - :keyword list[str] statuses: Translation operation statuses to filter by. Options include - 'NotStarted', 'Running', 'Succeeded', 'Failed', 'Canceled', 'Canceling', - and 'ValidationFailed'. - :keyword created_after: Get operations created after a certain datetime. - :paramtype created_after: str or ~datetime.datetime - :keyword created_before: Get operations created before a certain datetime. - :paramtype created_before: str or ~datetime.datetime - :keyword list[str] order_by: The sorting query for the operations returned. Currently only - 'created_on' supported. - format: ["param1 asc/desc", "param2 asc/desc", ...] - (ex: 'created_on asc', 'created_on desc'). - :return: A pageable of TranslationStatus. - :rtype: ~azure.core.async_paging.AsyncItemPaged[TranslationStatus] - :raises ~azure.core.exceptions.HttpResponseError: - - .. admonition:: Example: - - .. literalinclude:: ../samples/async_samples/sample_list_translations_async.py - :start-after: [START list_translations_async] - :end-before: [END list_translations_async] - :language: python - :dedent: 4 - :caption: List all submitted translations under the resource. - """ - - if statuses: - statuses = [convert_status(status, ll=True) for status in statuses] - order_by = convert_order_by(order_by) - created_after = convert_datetime(created_after) if created_after else None - created_before = convert_datetime(created_before) if created_before else None - - def _convert_from_generated_model(generated_model): - # pylint: disable=protected-access - return TranslationStatus._from_generated(_TranslationStatus(generated_model)) - - model_conversion_function = kwargs.pop( - "cls", - lambda translation_statuses: [_convert_from_generated_model(status) for status in translation_statuses], - ) - - return cast( - AsyncItemPaged[TranslationStatus], - self._client.get_translations_status( - cls=model_conversion_function, - created_date_time_utc_start=created_after, - created_date_time_utc_end=created_before, - ids=translation_ids, - orderby=order_by, - statuses=statuses, - top=top, - skip=skip, - **kwargs - ), - ) - - @distributed_trace - def list_document_statuses( - self, - translation_id: str, - *, - top: Optional[int] = None, - skip: Optional[int] = None, - document_ids: Optional[List[str]] = None, - statuses: Optional[List[str]] = None, - created_after: Optional[Union[str, datetime.datetime]] = None, - created_before: Optional[Union[str, datetime.datetime]] = None, - order_by: Optional[List[str]] = None, - **kwargs: Any - ) -> AsyncItemPaged[DocumentStatus]: - """List all the document statuses for a given translation operation. - - :param str translation_id: ID of translation operation to list documents for. - :keyword int top: The total number of documents to return (across all pages). - :keyword int skip: The number of documents to skip (from beginning). - By default, we sort by all documents in descending order by start time. - :keyword list[str] document_ids: Document IDs to filter by. - :keyword list[str] statuses: Document statuses to filter by. Options include - 'NotStarted', 'Running', 'Succeeded', 'Failed', 'Canceled', 'Canceling', - and 'ValidationFailed'. - :keyword created_after: Get documents created after a certain datetime. - :paramtype created_after: str or ~datetime.datetime - :keyword created_before: Get documents created before a certain datetime. - :paramtype created_before: str or ~datetime.datetime - :keyword list[str] order_by: The sorting query for the documents. Currently only - 'created_on' is supported. - format: ["param1 asc/desc", "param2 asc/desc", ...] - (ex: 'created_on asc', 'created_on desc'). - :return: A pageable of DocumentStatus. - :rtype: ~azure.core.async_paging.AsyncItemPaged[DocumentStatus] - :raises ~azure.core.exceptions.HttpResponseError: - - .. admonition:: Example: - - .. literalinclude:: ../samples/async_samples/sample_check_document_statuses_async.py - :start-after: [START list_document_statuses_async] - :end-before: [END list_document_statuses_async] - :language: python - :dedent: 4 - :caption: List all the document statuses as they are being translated. - """ - - if statuses: - statuses = [convert_status(status, ll=True) for status in statuses] - order_by = convert_order_by(order_by) - created_after = convert_datetime(created_after) if created_after else None - created_before = convert_datetime(created_before) if created_before else None - - def _convert_from_generated_model(generated_model): - # pylint: disable=protected-access - return DocumentStatus._from_generated(_DocumentStatus(generated_model)) - - model_conversion_function = kwargs.pop( - "cls", - lambda doc_statuses: [_convert_from_generated_model(doc_status) for doc_status in doc_statuses], - ) - - return cast( - AsyncItemPaged[DocumentStatus], - self._client.get_documents_status( - id=translation_id, - cls=model_conversion_function, - created_date_time_utc_start=created_after, - created_date_time_utc_end=created_before, - ids=document_ids, - orderby=order_by, - statuses=statuses, - top=top, - skip=skip, - **kwargs - ), - ) - - @distributed_trace_async - async def get_document_status(self, translation_id: str, document_id: str, **kwargs: Any) -> DocumentStatus: - """Get the status of an individual document within a translation operation. - - :param str translation_id: The translation operation ID. - :param str document_id: The ID for the document. - :return: A DocumentStatus with information on the status of the document. - :rtype: ~azure.ai.translation.document.DocumentStatus - :raises ~azure.core.exceptions.HttpResponseError or ~azure.core.exceptions.ResourceNotFoundError: - """ - document_status = await self._client.get_document_status(translation_id, document_id, **kwargs) - # pylint: disable=protected-access - return DocumentStatus._from_generated(_DocumentStatus(document_status)) # type: ignore[call-overload] - - @distributed_trace_async - async def get_supported_glossary_formats(self, **kwargs: Any) -> List[DocumentTranslationFileFormat]: - """Get the list of the glossary formats supported by the Document Translation service. - - :return: A list of supported glossary formats. - :rtype: List[DocumentTranslationFileFormat] - :raises ~azure.core.exceptions.HttpResponseError: - """ - glossary_formats = await self._client.get_supported_formats(type="glossary", **kwargs) - # pylint: disable=protected-access - return DocumentTranslationFileFormat._from_generated_list(glossary_formats.value) - - @distributed_trace_async - async def get_supported_document_formats(self, **kwargs: Any) -> List[DocumentTranslationFileFormat]: - """Get the list of the document formats supported by the Document Translation service. - - :return: A list of supported document formats for translation. - :rtype: List[DocumentTranslationFileFormat] - :raises ~azure.core.exceptions.HttpResponseError: - """ - document_formats = await self._client.get_supported_formats(type="document", **kwargs) - # pylint: disable=protected-access - return DocumentTranslationFileFormat._from_generated_list(document_formats.value) - - -__all__: List[str] = [ - "DocumentTranslationClient", - "AsyncDocumentTranslationLROPoller", -] # Add all objects you want publicly available to users at this package level +__all__: List[str] = [] # Add all objects you want publicly available to users at this package level def patch_sdk(): diff --git a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/models/__init__.py b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/models/__init__.py index 8bd914e84a31..5198620922de 100644 --- a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/models/__init__.py +++ b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/models/__init__.py @@ -6,13 +6,50 @@ # Changes may cause incorrect behavior and will be lost if the code is regenerated. # -------------------------------------------------------------------------- +from ._models import BatchRequest +from ._models import DocumentFilter +from ._models import DocumentStatus from ._models import DocumentTranslateContent +from ._models import DocumentTranslationError +from ._models import DocumentTranslationFileFormat +from ._models import InnerTranslationError +from ._models import SourceInput +from ._models import StartTranslationDetails +from ._models import StatusSummary +from ._models import TranslationErrorResponse +from ._models import TranslationGlossary +from ._models import TranslationStatus +from ._models import TranslationTarget + +from ._enums import FileFormatType +from ._enums import Status +from ._enums import StorageInputType +from ._enums import StorageSource +from ._enums import TranslationErrorCode from ._patch import __all__ as _patch_all from ._patch import * # pylint: disable=unused-wildcard-import from ._patch import patch_sdk as _patch_sdk __all__ = [ + "BatchRequest", + "DocumentFilter", + "DocumentStatus", "DocumentTranslateContent", + "DocumentTranslationError", + "DocumentTranslationFileFormat", + "InnerTranslationError", + "SourceInput", + "StartTranslationDetails", + "StatusSummary", + "TranslationErrorResponse", + "TranslationGlossary", + "TranslationStatus", + "TranslationTarget", + "FileFormatType", + "Status", + "StorageInputType", + "StorageSource", + "TranslationErrorCode", ] __all__.extend([p for p in _patch_all if p not in __all__]) _patch_sdk() diff --git a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/models/_enums.py b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/models/_enums.py index fa7b87f694c5..e9191ebab4be 100644 --- a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/models/_enums.py +++ b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/models/_enums.py @@ -30,9 +30,9 @@ class Status(str, Enum, metaclass=CaseInsensitiveEnumMeta): """Succeeded""" FAILED = "Failed" """Failed""" - CANCELLED = "Cancelled" + CANCELED = "Cancelled" """Cancelled""" - CANCELLING = "Cancelling" + CANCELING = "Cancelling" """Cancelling""" VALIDATION_FAILED = "ValidationFailed" """ValidationFailed""" diff --git a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/models/_models.py b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/models/_models.py index cfc432b062bc..6f5ec6cde7f0 100644 --- a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/models/_models.py +++ b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/models/_models.py @@ -25,21 +25,40 @@ class BatchRequest(_model_base.Model): All required parameters must be populated in order to send to server. :ivar source: Source of the input documents. Required. - :vartype source: ~azure.ai.translation.document.models._models.SourceInput + :vartype source: ~azure.ai.translation.document.models.SourceInput :ivar targets: Location of the destination for the output. Required. - :vartype targets: list[~azure.ai.translation.document.models._models.TargetInput] + :vartype targets: list[~azure.ai.translation.document.models.TranslationTarget] :ivar storage_type: Storage type of the input documents source string. Known values are: "Folder" and "File". :vartype storage_type: str or ~azure.ai.translation.document.models.StorageInputType """ - source: "_models._models.SourceInput" = rest_field() + source: "_models.SourceInput" = rest_field() """Source of the input documents. Required.""" - targets: List["_models._models.TargetInput"] = rest_field() + targets: List["_models.TranslationTarget"] = rest_field() """Location of the destination for the output. Required.""" - storage_type: Optional[Union[str, "_models._enums.StorageInputType"]] = rest_field(name="storageType") + storage_type: Optional[Union[str, "_models.StorageInputType"]] = rest_field(name="storageType") """Storage type of the input documents source string. Known values are: \"Folder\" and \"File\".""" + @overload + def __init__( + self, + *, + source: "_models.SourceInput", + targets: List["_models.TranslationTarget"], + storage_type: Optional[Union[str, "_models.StorageInputType"]] = None, + ): ... + + @overload + def __init__(self, mapping: Mapping[str, Any]): + """ + :param mapping: raw JSON to initialize the model. + :type mapping: Mapping[str, Any] + """ + + def __init__(self, *args: Any, **kwargs: Any) -> None: # pylint: disable=useless-super-delegation + super().__init__(*args, **kwargs) + class DocumentFilter(_model_base.Model): """Document filter. @@ -65,79 +84,104 @@ class DocumentFilter(_model_base.Model): translation. This is most often use for file extensions.""" + @overload + def __init__( + self, + *, + prefix: Optional[str] = None, + suffix: Optional[str] = None, + ): ... -class DocumentsStatus(_model_base.Model): - """Documents Status Response. - - All required parameters must be populated in order to send to server. - - :ivar value: The detail status of individual documents. Required. - :vartype value: list[~azure.ai.translation.document.models._models.DocumentStatus] - :ivar next_link: Url for the next page. Null if no more pages available. - :vartype next_link: str - """ + @overload + def __init__(self, mapping: Mapping[str, Any]): + """ + :param mapping: raw JSON to initialize the model. + :type mapping: Mapping[str, Any] + """ - value: List["_models._models.DocumentStatus"] = rest_field() - """The detail status of individual documents. Required.""" - next_link: Optional[str] = rest_field(name="nextLink") - """Url for the next page. Null if no more pages available.""" + def __init__(self, *args: Any, **kwargs: Any) -> None: # pylint: disable=useless-super-delegation + super().__init__(*args, **kwargs) class DocumentStatus(_model_base.Model): """Document Status Response. - All required parameters must be populated in order to send to server. - :ivar path: Location of the document or folder. - :vartype path: str - :ivar source_path: Location of the source document. Required. - :vartype source_path: str - :ivar created_date_time_utc: Operation created date time. Required. - :vartype created_date_time_utc: ~datetime.datetime - :ivar last_action_date_time_utc: Date time in which the operation's status has been updated. - Required. - :vartype last_action_date_time_utc: ~datetime.datetime + :ivar translated_document_url: Location of the document or folder. + :vartype translated_document_url: str + :ivar source_document_url: Location of the source document. Required. + :vartype source_document_url: str + :ivar created_on: Operation created date time. Required. + :vartype created_on: ~datetime.datetime + :ivar last_updated_on: Date time in which the operation's status has been updated. Required. + :vartype last_updated_on: ~datetime.datetime :ivar status: List of possible statuses for job or document. Required. Known values are: "NotStarted", "Running", "Succeeded", "Failed", "Cancelled", "Cancelling", and "ValidationFailed". :vartype status: str or ~azure.ai.translation.document.models.Status - :ivar to: To language. Required. - :vartype to: str + :ivar translated_to: To language. Required. + :vartype translated_to: str :ivar error: This contains an outer error with error code, message, details, target and an inner error with more descriptive details. - :vartype error: ~azure.ai.translation.document.models._models.TranslationError - :ivar progress: Progress of the translation if available. Required. - :vartype progress: float + :vartype error: ~azure.ai.translation.document.models.DocumentTranslationError + :ivar translation_progress: Progress of the translation if available. Required. + :vartype translation_progress: float :ivar id: Document Id. Required. :vartype id: str - :ivar character_charged: Character charged by the API. - :vartype character_charged: int + :ivar characters_charged: Character charged by the API. + :vartype characters_charged: int """ - path: Optional[str] = rest_field() + translated_document_url: Optional[str] = rest_field(name="path") """Location of the document or folder.""" - source_path: str = rest_field(name="sourcePath") + source_document_url: str = rest_field(name="sourcePath") """Location of the source document. Required.""" - created_date_time_utc: datetime.datetime = rest_field(name="createdDateTimeUtc", format="rfc3339") + created_on: datetime.datetime = rest_field(name="createdDateTimeUtc", format="rfc3339") """Operation created date time. Required.""" - last_action_date_time_utc: datetime.datetime = rest_field(name="lastActionDateTimeUtc", format="rfc3339") + last_updated_on: datetime.datetime = rest_field(name="lastActionDateTimeUtc", format="rfc3339") """Date time in which the operation's status has been updated. Required.""" - status: Union[str, "_models._enums.Status"] = rest_field() + status: Union[str, "_models.Status"] = rest_field() """List of possible statuses for job or document. Required. Known values are: \"NotStarted\", \"Running\", \"Succeeded\", \"Failed\", \"Cancelled\", \"Cancelling\", and \"ValidationFailed\".""" - to: str = rest_field() + translated_to: str = rest_field(name="to") """To language. Required.""" - error: Optional["_models._models.TranslationError"] = rest_field() + error: Optional["_models.DocumentTranslationError"] = rest_field() """This contains an outer error with error code, message, details, target and an inner error with more descriptive details.""" - progress: float = rest_field() + translation_progress: float = rest_field(name="progress") """Progress of the translation if available. Required.""" id: str = rest_field() """Document Id. Required.""" - character_charged: Optional[int] = rest_field(name="characterCharged") + characters_charged: Optional[int] = rest_field(name="characterCharged") """Character charged by the API.""" + @overload + def __init__( + self, + *, + source_document_url: str, + created_on: datetime.datetime, + last_updated_on: datetime.datetime, + status: Union[str, "_models.Status"], + translated_to: str, + translation_progress: float, + id: str, # pylint: disable=redefined-builtin + translated_document_url: Optional[str] = None, + error: Optional["_models.DocumentTranslationError"] = None, + characters_charged: Optional[int] = None, + ): ... + + @overload + def __init__(self, mapping: Mapping[str, Any]): + """ + :param mapping: raw JSON to initialize the model. + :type mapping: Mapping[str, Any] + """ + + def __init__(self, *args: Any, **kwargs: Any) -> None: # pylint: disable=useless-super-delegation + super().__init__(*args, **kwargs) + class DocumentTranslateContent(_model_base.Model): """Document Translate Request Content. @@ -145,9 +189,9 @@ class DocumentTranslateContent(_model_base.Model): All required parameters must be populated in order to send to server. :ivar document: Document to be translated in the form. Required. - :vartype document: bytes + :vartype document: ~azure.ai.translation.document._vendor.FileType :ivar glossary: Glossary-translation memory will be used during translation in the form. - :vartype glossary: list[bytes] + :vartype glossary: list[~azure.ai.translation.document._vendor.FileType] """ document: FileType = rest_field(is_multipart_file_input=True) @@ -174,72 +218,122 @@ def __init__(self, *args: Any, **kwargs: Any) -> None: # pylint: disable=useles super().__init__(*args, **kwargs) -class FileFormat(_model_base.Model): +class DocumentTranslationError(_model_base.Model): + """This contains an outer error with error code, message, details, target and an + inner error with more descriptive details. + + Readonly variables are only populated by the server, and will be ignored when sending a request. + + + :ivar code: Enums containing high level error codes. Required. Known values are: + "InvalidRequest", "InvalidArgument", "InternalServerError", "ServiceUnavailable", + "ResourceNotFound", "Unauthorized", and "RequestRateTooHigh". + :vartype code: str or ~azure.ai.translation.document.models.TranslationErrorCode + :ivar message: Gets high level error message. Required. + :vartype message: str + :ivar target: Gets the source of the error. + For example it would be "documents" or + "document id" in case of invalid document. + :vartype target: str + :ivar inner_error: New Inner Error format which conforms to Cognitive Services API Guidelines + which is available at + https://microsoft.sharepoint.com/%3Aw%3A/t/CognitiveServicesPMO/EUoytcrjuJdKpeOKIK_QRC8BPtUYQpKBi8JsWyeDMRsWlQ?e=CPq8ow. + This + contains required properties ErrorCode, message and optional properties target, + details(key value pair), inner error(this can be nested). + :vartype inner_error: ~azure.ai.translation.document.models.InnerTranslationError + """ + + code: Union[str, "_models.TranslationErrorCode"] = rest_field() + """Enums containing high level error codes. Required. Known values are: \"InvalidRequest\", + \"InvalidArgument\", \"InternalServerError\", \"ServiceUnavailable\", \"ResourceNotFound\", + \"Unauthorized\", and \"RequestRateTooHigh\".""" + message: str = rest_field() + """Gets high level error message. Required.""" + target: Optional[str] = rest_field(visibility=["read"]) + """Gets the source of the error. + For example it would be \"documents\" or + \"document id\" in case of invalid document.""" + inner_error: Optional["_models.InnerTranslationError"] = rest_field(name="innerError") + """New Inner Error format which conforms to Cognitive Services API Guidelines + which is available at + https://microsoft.sharepoint.com/%3Aw%3A/t/CognitiveServicesPMO/EUoytcrjuJdKpeOKIK_QRC8BPtUYQpKBi8JsWyeDMRsWlQ?e=CPq8ow. # pylint: disable=line-too-long + This + contains required properties ErrorCode, message and optional properties target, + details(key value pair), inner error(this can be nested).""" + + @overload + def __init__( + self, + *, + code: Union[str, "_models.TranslationErrorCode"], + message: str, + inner_error: Optional["_models.InnerTranslationError"] = None, + ): ... + + @overload + def __init__(self, mapping: Mapping[str, Any]): + """ + :param mapping: raw JSON to initialize the model. + :type mapping: Mapping[str, Any] + """ + + def __init__(self, *args: Any, **kwargs: Any) -> None: # pylint: disable=useless-super-delegation + super().__init__(*args, **kwargs) + + +class DocumentTranslationFileFormat(_model_base.Model): """File Format. - All required parameters must be populated in order to send to server. - :ivar format: Name of the format. Required. - :vartype format: str + :ivar file_format: Name of the format. Required. + :vartype file_format: str :ivar file_extensions: Supported file extension for this format. Required. :vartype file_extensions: list[str] :ivar content_types: Supported Content-Types for this format. Required. :vartype content_types: list[str] - :ivar default_version: Default version if none is specified. - :vartype default_version: str - :ivar versions: Supported Version. - :vartype versions: list[str] + :ivar default_format_version: Default version if none is specified. + :vartype default_format_version: str + :ivar format_versions: Supported Version. + :vartype format_versions: list[str] :ivar type: Supported Type for this format. :vartype type: str """ - format: str = rest_field() + file_format: str = rest_field(name="format") """Name of the format. Required.""" file_extensions: List[str] = rest_field(name="fileExtensions") """Supported file extension for this format. Required.""" content_types: List[str] = rest_field(name="contentTypes") """Supported Content-Types for this format. Required.""" - default_version: Optional[str] = rest_field(name="defaultVersion") + default_format_version: Optional[str] = rest_field(name="defaultVersion") """Default version if none is specified.""" - versions: Optional[List[str]] = rest_field() + format_versions: Optional[List[str]] = rest_field(name="versions") """Supported Version.""" type: Optional[str] = rest_field() """Supported Type for this format.""" + @overload + def __init__( + self, + *, + file_format: str, + file_extensions: List[str], + content_types: List[str], + default_format_version: Optional[str] = None, + format_versions: Optional[List[str]] = None, + type: Optional[str] = None, + ): ... -class Glossary(_model_base.Model): - """Glossary / translation memory for the request. - - All required parameters must be populated in order to send to server. - - :ivar glossary_url: Location of the glossary. - We will use the file extension to extract the - formatting if the format parameter is not supplied. - - If the translation - language pair is not present in the glossary, it will not be applied. Required. - :vartype glossary_url: str - :ivar format: Format. Required. - :vartype format: str - :ivar version: Optional Version. If not specified, default is used. - :vartype version: str - :ivar storage_source: Storage Source. "AzureBlob" - :vartype storage_source: str or ~azure.ai.translation.document.models.StorageSource - """ + @overload + def __init__(self, mapping: Mapping[str, Any]): + """ + :param mapping: raw JSON to initialize the model. + :type mapping: Mapping[str, Any] + """ - glossary_url: str = rest_field(name="glossaryUrl") - """Location of the glossary. - We will use the file extension to extract the - formatting if the format parameter is not supplied. - - If the translation - language pair is not present in the glossary, it will not be applied. Required.""" - format: str = rest_field() - """Format. Required.""" - version: Optional[str] = rest_field() - """Optional Version. If not specified, default is used.""" - storage_source: Optional[Union[str, "_models._enums.StorageSource"]] = rest_field(name="storageSource") - """Storage Source. \"AzureBlob\"""" + def __init__(self, *args: Any, **kwargs: Any) -> None: # pylint: disable=useless-super-delegation + super().__init__(*args, **kwargs) class InnerTranslationError(_model_base.Model): @@ -252,7 +346,6 @@ class InnerTranslationError(_model_base.Model): Readonly variables are only populated by the server, and will be ignored when sending a request. - All required parameters must be populated in order to send to server. :ivar code: Gets code error string. Required. :vartype code: str @@ -268,7 +361,7 @@ class InnerTranslationError(_model_base.Model): This contains required properties ErrorCode, message and optional properties target, details(key value pair), inner error(this can be nested). - :vartype inner_error: ~azure.ai.translation.document.models._models.InnerTranslationError + :vartype inner_error: ~azure.ai.translation.document.models.InnerTranslationError """ code: str = rest_field() @@ -279,7 +372,7 @@ class InnerTranslationError(_model_base.Model): """Gets the source of the error. For example it would be \"documents\" or \"document id\" in case of invalid document.""" - inner_error: Optional["_models._models.InnerTranslationError"] = rest_field(name="innerError") + inner_error: Optional["_models.InnerTranslationError"] = rest_field(name="innerError") """New Inner Error format which conforms to Cognitive Services API Guidelines which is available at https://microsoft.sharepoint.com/%3Aw%3A/t/CognitiveServicesPMO/EUoytcrjuJdKpeOKIK_QRC8BPtUYQpKBi8JsWyeDMRsWlQ?e=CPq8ow. # pylint: disable=line-too-long @@ -287,6 +380,25 @@ class InnerTranslationError(_model_base.Model): contains required properties ErrorCode, message and optional properties target, details(key value pair), inner error(this can be nested).""" + @overload + def __init__( + self, + *, + code: str, + message: str, + inner_error: Optional["_models.InnerTranslationError"] = None, + ): ... + + @overload + def __init__(self, mapping: Mapping[str, Any]): + """ + :param mapping: raw JSON to initialize the model. + :type mapping: Mapping[str, Any] + """ + + def __init__(self, *args: Any, **kwargs: Any) -> None: # pylint: disable=useless-super-delegation + super().__init__(*args, **kwargs) + class SourceInput(_model_base.Model): """Source of the input documents. @@ -297,7 +409,7 @@ class SourceInput(_model_base.Model): Required. :vartype source_url: str :ivar filter: Document filter. - :vartype filter: ~azure.ai.translation.document.models._models.DocumentFilter + :vartype filter: ~azure.ai.translation.document.models.DocumentFilter :ivar language: Language code If none is specified, we will perform auto detect on the document. :vartype language: str @@ -307,14 +419,34 @@ class SourceInput(_model_base.Model): source_url: str = rest_field(name="sourceUrl") """Location of the folder / container or single file with your documents. Required.""" - filter: Optional["_models._models.DocumentFilter"] = rest_field() + filter: Optional["_models.DocumentFilter"] = rest_field() """Document filter.""" language: Optional[str] = rest_field() """Language code If none is specified, we will perform auto detect on the document.""" - storage_source: Optional[Union[str, "_models._enums.StorageSource"]] = rest_field(name="storageSource") + storage_source: Optional[Union[str, "_models.StorageSource"]] = rest_field(name="storageSource") """Storage Source. \"AzureBlob\"""" + @overload + def __init__( + self, + *, + source_url: str, + filter: Optional["_models.DocumentFilter"] = None, # pylint: disable=redefined-builtin + language: Optional[str] = None, + storage_source: Optional[Union[str, "_models.StorageSource"]] = None, + ): ... + + @overload + def __init__(self, mapping: Mapping[str, Any]): + """ + :param mapping: raw JSON to initialize the model. + :type mapping: Mapping[str, Any] + """ + + def __init__(self, *args: Any, **kwargs: Any) -> None: # pylint: disable=useless-super-delegation + super().__init__(*args, **kwargs) + class StartTranslationDetails(_model_base.Model): """Translation job submission batch request. @@ -322,17 +454,33 @@ class StartTranslationDetails(_model_base.Model): All required parameters must be populated in order to send to server. :ivar inputs: The input list of documents or folders containing documents. Required. - :vartype inputs: list[~azure.ai.translation.document.models._models.BatchRequest] + :vartype inputs: list[~azure.ai.translation.document.models.BatchRequest] """ - inputs: List["_models._models.BatchRequest"] = rest_field() + inputs: List["_models.BatchRequest"] = rest_field() """The input list of documents or folders containing documents. Required.""" + @overload + def __init__( + self, + *, + inputs: List["_models.BatchRequest"], + ): ... + + @overload + def __init__(self, mapping: Mapping[str, Any]): + """ + :param mapping: raw JSON to initialize the model. + :type mapping: Mapping[str, Any] + """ + + def __init__(self, *args: Any, **kwargs: Any) -> None: # pylint: disable=useless-super-delegation + super().__init__(*args, **kwargs) + class StatusSummary(_model_base.Model): """Status Summary. - All required parameters must be populated in order to send to server. :ivar total: Total count. Required. :vartype total: int @@ -344,10 +492,10 @@ class StatusSummary(_model_base.Model): :vartype in_progress: int :ivar not_yet_started: Count of not yet started. Required. :vartype not_yet_started: int - :ivar cancelled: Number of cancelled. Required. - :vartype cancelled: int - :ivar total_character_charged: Total characters charged by the API. Required. - :vartype total_character_charged: int + :ivar canceled: Number of cancelled. Required. + :vartype canceled: int + :ivar total_characters_charged: Total characters charged by the API. Required. + :vartype total_characters_charged: int """ total: int = rest_field() @@ -360,152 +508,242 @@ class StatusSummary(_model_base.Model): """Number of in progress. Required.""" not_yet_started: int = rest_field(name="notYetStarted") """Count of not yet started. Required.""" - cancelled: int = rest_field() + canceled: int = rest_field(name="cancelled") """Number of cancelled. Required.""" - total_character_charged: int = rest_field(name="totalCharacterCharged") + total_characters_charged: int = rest_field(name="totalCharacterCharged") """Total characters charged by the API. Required.""" + @overload + def __init__( + self, + *, + total: int, + failed: int, + success: int, + in_progress: int, + not_yet_started: int, + canceled: int, + total_characters_charged: int, + ): ... + + @overload + def __init__(self, mapping: Mapping[str, Any]): + """ + :param mapping: raw JSON to initialize the model. + :type mapping: Mapping[str, Any] + """ + + def __init__(self, *args: Any, **kwargs: Any) -> None: # pylint: disable=useless-super-delegation + super().__init__(*args, **kwargs) + class SupportedFileFormats(_model_base.Model): """List of supported file formats. - All required parameters must be populated in order to send to server. :ivar value: list of objects. Required. - :vartype value: list[~azure.ai.translation.document.models._models.FileFormat] + :vartype value: list[~azure.ai.translation.document.models.DocumentTranslationFileFormat] """ - value: List["_models._models.FileFormat"] = rest_field() + value: List["_models.DocumentTranslationFileFormat"] = rest_field() """list of objects. Required.""" -class TargetInput(_model_base.Model): - """Destination for the finished translated documents. - - All required parameters must be populated in order to send to server. +class TranslationErrorResponse(_model_base.Model): + """Contains unified error information used for HTTP responses across any Cognitive + Service. Instances + can be created either through + Microsoft.CloudAI.Containers.HttpStatusExceptionV2 or by returning it directly + from + a controller. - :ivar target_url: Location of the folder / container with your documents. Required. - :vartype target_url: str - :ivar category: Category / custom system for translation request. - :vartype category: str - :ivar language: Target Language. Required. - :vartype language: str - :ivar glossaries: List of Glossary. - :vartype glossaries: list[~azure.ai.translation.document.models._models.Glossary] - :ivar storage_source: Storage Source. "AzureBlob" - :vartype storage_source: str or ~azure.ai.translation.document.models.StorageSource + :ivar error: This contains an outer error with error code, message, details, target and an + inner error with more descriptive details. + :vartype error: ~azure.ai.translation.document.models.DocumentTranslationError """ - target_url: str = rest_field(name="targetUrl") - """Location of the folder / container with your documents. Required.""" - category: Optional[str] = rest_field() - """Category / custom system for translation request.""" - language: str = rest_field() - """Target Language. Required.""" - glossaries: Optional[List["_models._models.Glossary"]] = rest_field() - """List of Glossary.""" - storage_source: Optional[Union[str, "_models._enums.StorageSource"]] = rest_field(name="storageSource") - """Storage Source. \"AzureBlob\"""" - - -class TranslationError(_model_base.Model): + error: Optional["_models.DocumentTranslationError"] = rest_field() """This contains an outer error with error code, message, details, target and an - inner error with more descriptive details. - - Readonly variables are only populated by the server, and will be ignored when sending a request. + inner error with more descriptive details.""" - All required parameters must be populated in order to send to server. + @overload + def __init__( + self, + *, + error: Optional["_models.DocumentTranslationError"] = None, + ): ... - :ivar code: Enums containing high level error codes. Required. Known values are: - "InvalidRequest", "InvalidArgument", "InternalServerError", "ServiceUnavailable", - "ResourceNotFound", "Unauthorized", and "RequestRateTooHigh". - :vartype code: str or ~azure.ai.translation.document.models.TranslationErrorCode - :ivar message: Gets high level error message. Required. - :vartype message: str - :ivar target: Gets the source of the error. - For example it would be "documents" or - "document id" in case of invalid document. - :vartype target: str - :ivar inner_error: New Inner Error format which conforms to Cognitive Services API Guidelines - which is available at - https://microsoft.sharepoint.com/%3Aw%3A/t/CognitiveServicesPMO/EUoytcrjuJdKpeOKIK_QRC8BPtUYQpKBi8JsWyeDMRsWlQ?e=CPq8ow. - This - contains required properties ErrorCode, message and optional properties target, - details(key value pair), inner error(this can be nested). - :vartype inner_error: ~azure.ai.translation.document.models._models.InnerTranslationError - """ + @overload + def __init__(self, mapping: Mapping[str, Any]): + """ + :param mapping: raw JSON to initialize the model. + :type mapping: Mapping[str, Any] + """ - code: Union[str, "_models._enums.TranslationErrorCode"] = rest_field() - """Enums containing high level error codes. Required. Known values are: \"InvalidRequest\", - \"InvalidArgument\", \"InternalServerError\", \"ServiceUnavailable\", \"ResourceNotFound\", - \"Unauthorized\", and \"RequestRateTooHigh\".""" - message: str = rest_field() - """Gets high level error message. Required.""" - target: Optional[str] = rest_field(visibility=["read"]) - """Gets the source of the error. - For example it would be \"documents\" or - \"document id\" in case of invalid document.""" - inner_error: Optional["_models._models.InnerTranslationError"] = rest_field(name="innerError") - """New Inner Error format which conforms to Cognitive Services API Guidelines - which is available at - https://microsoft.sharepoint.com/%3Aw%3A/t/CognitiveServicesPMO/EUoytcrjuJdKpeOKIK_QRC8BPtUYQpKBi8JsWyeDMRsWlQ?e=CPq8ow. # pylint: disable=line-too-long - This - contains required properties ErrorCode, message and optional properties target, - details(key value pair), inner error(this can be nested).""" + def __init__(self, *args: Any, **kwargs: Any) -> None: # pylint: disable=useless-super-delegation + super().__init__(*args, **kwargs) -class TranslationsStatus(_model_base.Model): - """Translation job Status Response. +class TranslationGlossary(_model_base.Model): + """Glossary / translation memory for the request. All required parameters must be populated in order to send to server. - :ivar value: The summary status of individual operation. Required. - :vartype value: list[~azure.ai.translation.document.models._models.TranslationStatus] - :ivar next_link: Url for the next page. Null if no more pages available. - :vartype next_link: str + :ivar glossary_url: Location of the glossary. + We will use the file extension to extract the + formatting if the format parameter is not supplied. + + If the translation + language pair is not present in the glossary, it will not be applied. Required. + :vartype glossary_url: str + :ivar file_format: Format. Required. + :vartype file_format: str + :ivar format_version: Optional Version. If not specified, default is used. + :vartype format_version: str + :ivar storage_source: Storage Source. "AzureBlob" + :vartype storage_source: str or ~azure.ai.translation.document.models.StorageSource """ - value: List["_models._models.TranslationStatus"] = rest_field() - """The summary status of individual operation. Required.""" - next_link: Optional[str] = rest_field(name="nextLink") - """Url for the next page. Null if no more pages available.""" + glossary_url: str = rest_field(name="glossaryUrl") + """Location of the glossary. + We will use the file extension to extract the + formatting if the format parameter is not supplied. + + If the translation + language pair is not present in the glossary, it will not be applied. Required.""" + file_format: str = rest_field(name="format") + """Format. Required.""" + format_version: Optional[str] = rest_field(name="version") + """Optional Version. If not specified, default is used.""" + storage_source: Optional[Union[str, "_models.StorageSource"]] = rest_field(name="storageSource") + """Storage Source. \"AzureBlob\"""" + + @overload + def __init__( + self, + *, + glossary_url: str, + file_format: str, + format_version: Optional[str] = None, + storage_source: Optional[Union[str, "_models.StorageSource"]] = None, + ): ... + + @overload + def __init__(self, mapping: Mapping[str, Any]): + """ + :param mapping: raw JSON to initialize the model. + :type mapping: Mapping[str, Any] + """ + + def __init__(self, *args: Any, **kwargs: Any) -> None: # pylint: disable=useless-super-delegation + super().__init__(*args, **kwargs) class TranslationStatus(_model_base.Model): """Translation job status response. - All required parameters must be populated in order to send to server. :ivar id: Id of the operation. Required. :vartype id: str - :ivar created_date_time_utc: Operation created date time. Required. - :vartype created_date_time_utc: ~datetime.datetime - :ivar last_action_date_time_utc: Date time in which the operation's status has been updated. - Required. - :vartype last_action_date_time_utc: ~datetime.datetime + :ivar created_on: Operation created date time. Required. + :vartype created_on: ~datetime.datetime + :ivar last_updated_on: Date time in which the operation's status has been updated. Required. + :vartype last_updated_on: ~datetime.datetime :ivar status: List of possible statuses for job or document. Required. Known values are: "NotStarted", "Running", "Succeeded", "Failed", "Cancelled", "Cancelling", and "ValidationFailed". :vartype status: str or ~azure.ai.translation.document.models.Status :ivar error: This contains an outer error with error code, message, details, target and an inner error with more descriptive details. - :vartype error: ~azure.ai.translation.document.models._models.TranslationError + :vartype error: ~azure.ai.translation.document.models.DocumentTranslationError :ivar summary: Status Summary. Required. - :vartype summary: ~azure.ai.translation.document.models._models.StatusSummary + :vartype summary: ~azure.ai.translation.document.models.StatusSummary """ id: str = rest_field() """Id of the operation. Required.""" - created_date_time_utc: datetime.datetime = rest_field(name="createdDateTimeUtc", format="rfc3339") + created_on: datetime.datetime = rest_field(name="createdDateTimeUtc", format="rfc3339") """Operation created date time. Required.""" - last_action_date_time_utc: datetime.datetime = rest_field(name="lastActionDateTimeUtc", format="rfc3339") + last_updated_on: datetime.datetime = rest_field(name="lastActionDateTimeUtc", format="rfc3339") """Date time in which the operation's status has been updated. Required.""" - status: Union[str, "_models._enums.Status"] = rest_field() + status: Union[str, "_models.Status"] = rest_field() """List of possible statuses for job or document. Required. Known values are: \"NotStarted\", \"Running\", \"Succeeded\", \"Failed\", \"Cancelled\", \"Cancelling\", and \"ValidationFailed\".""" - error: Optional["_models._models.TranslationError"] = rest_field() + error: Optional["_models.DocumentTranslationError"] = rest_field() """This contains an outer error with error code, message, details, target and an inner error with more descriptive details.""" - summary: "_models._models.StatusSummary" = rest_field() + summary: "_models.StatusSummary" = rest_field() """Status Summary. Required.""" + + @overload + def __init__( + self, + *, + id: str, # pylint: disable=redefined-builtin + created_on: datetime.datetime, + last_updated_on: datetime.datetime, + status: Union[str, "_models.Status"], + summary: "_models.StatusSummary", + error: Optional["_models.DocumentTranslationError"] = None, + ): ... + + @overload + def __init__(self, mapping: Mapping[str, Any]): + """ + :param mapping: raw JSON to initialize the model. + :type mapping: Mapping[str, Any] + """ + + def __init__(self, *args: Any, **kwargs: Any) -> None: # pylint: disable=useless-super-delegation + super().__init__(*args, **kwargs) + + +class TranslationTarget(_model_base.Model): + """Destination for the finished translated documents. + + All required parameters must be populated in order to send to server. + + :ivar target_url: Location of the folder / container with your documents. Required. + :vartype target_url: str + :ivar category_id: Category / custom system for translation request. + :vartype category_id: str + :ivar language: Target Language. Required. + :vartype language: str + :ivar glossaries: List of Glossary. + :vartype glossaries: list[~azure.ai.translation.document.models.TranslationGlossary] + :ivar storage_source: Storage Source. "AzureBlob" + :vartype storage_source: str or ~azure.ai.translation.document.models.StorageSource + """ + + target_url: str = rest_field(name="targetUrl") + """Location of the folder / container with your documents. Required.""" + category_id: Optional[str] = rest_field(name="category") + """Category / custom system for translation request.""" + language: str = rest_field() + """Target Language. Required.""" + glossaries: Optional[List["_models.TranslationGlossary"]] = rest_field() + """List of Glossary.""" + storage_source: Optional[Union[str, "_models.StorageSource"]] = rest_field(name="storageSource") + """Storage Source. \"AzureBlob\"""" + + @overload + def __init__( + self, + *, + target_url: str, + language: str, + category_id: Optional[str] = None, + glossaries: Optional[List["_models.TranslationGlossary"]] = None, + storage_source: Optional[Union[str, "_models.StorageSource"]] = None, + ): ... + + @overload + def __init__(self, mapping: Mapping[str, Any]): + """ + :param mapping: raw JSON to initialize the model. + :type mapping: Mapping[str, Any] + """ + + def __init__(self, *args: Any, **kwargs: Any) -> None: # pylint: disable=useless-super-delegation + super().__init__(*args, **kwargs) diff --git a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/models/_patch.py b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/models/_patch.py index 455941122974..f7dd32510333 100644 --- a/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/models/_patch.py +++ b/sdk/translation/azure-ai-translation-document/azure/ai/translation/document/models/_patch.py @@ -6,7 +6,6 @@ Follow our quickstart for examples: https://aka.ms/azsdk/python/dpcodegen/python/customize """ - from typing import List __all__: List[str] = [] # Add all objects you want publicly available to users at this package level diff --git a/sdk/translation/azure-ai-translation-document/tests/perfstress_tests/perf_translation.py b/sdk/translation/azure-ai-translation-document/tests/perfstress_tests/perf_translation.py index cb1c76cc356f..fe009faf420d 100644 --- a/sdk/translation/azure-ai-translation-document/tests/perfstress_tests/perf_translation.py +++ b/sdk/translation/azure-ai-translation-document/tests/perfstress_tests/perf_translation.py @@ -43,9 +43,7 @@ def __init__(self, arguments): self.target_container_name = "target-perf-" + str(uuid.uuid4()) self.service_client = DocumentTranslationClient(endpoint, get_credential(), **self._client_kwargs) - self.async_service_client = AsyncDocumentTranslationClient( - endpoint, get_credential(), **self._client_kwargs - ) + self.async_service_client = AsyncDocumentTranslationClient(endpoint, get_credential(), **self._client_kwargs) async def create_source_container(self): container_client = ContainerClient(self.storage_endpoint, self.source_container_name, self.storage_key) diff --git a/sdk/translation/azure-ai-translation-document/tests/preparer.py b/sdk/translation/azure-ai-translation-document/tests/preparer.py index 5f33a9c8368f..a8abc3810b09 100644 --- a/sdk/translation/azure-ai-translation-document/tests/preparer.py +++ b/sdk/translation/azure-ai-translation-document/tests/preparer.py @@ -32,8 +32,6 @@ def create_resource(self, name, **kwargs): if not self.is_live: self.client_kwargs["polling_interval"] = 0 - client = self.client_cls( - doctranslation_test_endpoint, get_credential(), **self.client_kwargs - ) + client = self.client_cls(doctranslation_test_endpoint, get_credential(), **self.client_kwargs) kwargs.update({"client": client}) return kwargs diff --git a/sdk/translation/azure-ai-translation-document/tsp-location.yaml b/sdk/translation/azure-ai-translation-document/tsp-location.yaml index cd0188ee842d..79d63ba5ac89 100644 --- a/sdk/translation/azure-ai-translation-document/tsp-location.yaml +++ b/sdk/translation/azure-ai-translation-document/tsp-location.yaml @@ -1,4 +1,4 @@ -commit: c70d880019bbd107ca2423fb734a681cd1a75332 -repo: Azure/azure-rest-api-specs directory: specification/translation/Azure.AI.DocumentTranslation - +commit: c29452397f8d29456182b951987a765d1ae01bb1 +repo: test-repo-billy/azure-rest-api-specs +additionalDirectories: