From 76ae50fdb7b13cdfe90c48b09568b854a3fd2cfa Mon Sep 17 00:00:00 2001 From: SDKAuto Date: Thu, 17 Oct 2024 16:47:05 +0000 Subject: [PATCH] CodeGen from PR 3537 in test-repo-billy/azure-rest-api-specs Merge 6296b8e5c14516850f6b39a7fee10b2f0db170a5 into 6c6e1713f03acaf37e5c89652b023dd2c9eae16e --- .../azure-schemaregistry/_meta.json | 6 + .../azure/schemaregistry/__init__.py | 1 + .../azure/schemaregistry/_client.py | 3 +- .../azure/schemaregistry/_configuration.py | 4 +- .../azure/schemaregistry/_model_base.py | 346 +++++++++-- .../schemaregistry/_operations/__init__.py | 2 + .../schemaregistry/_operations/_operations.py | 271 +++++++-- .../schemaregistry/_operations/_patch.py | 261 +------- .../azure/schemaregistry/_patch.py | 558 +----------------- .../azure/schemaregistry/_serialization.py | 415 ++++++++----- .../azure/schemaregistry/_vendor.py | 11 - .../azure/schemaregistry/_version.py | 2 +- .../azure/schemaregistry/aio/__init__.py | 2 + .../azure/schemaregistry/aio/_client.py | 3 +- .../schemaregistry/aio/_configuration.py | 4 +- .../aio/_operations/__init__.py | 2 + .../aio/_operations/_operations.py | 197 ++++++- .../schemaregistry/aio/_operations/_patch.py | 202 +------ .../azure/schemaregistry/aio/_patch.py | 346 +---------- .../azure/schemaregistry/aio/_vendor.py | 11 - .../azure/schemaregistry/encoder/__init__.py | 26 - .../encoder/jsonencoder/__init__.py | 36 -- .../encoder/jsonencoder/_constants.py | 27 - .../encoder/jsonencoder/_exceptions.py | 44 -- .../_schema_registry_json_encoder.py | 386 ------------ .../encoder/jsonencoder/_utils.py | 261 -------- .../encoder/jsonencoder/_version.py | 27 - .../encoder/jsonencoder/aio/__init__.py | 30 - .../encoder/jsonencoder/aio/_async_lru.py | 228 ------- .../_schema_registry_json_encoder_async.py | 388 ------------ .../encoder/jsonencoder/py.typed | 1 - .../azure/schemaregistry/models/_enums.py | 2 + .../azure/schemaregistry/models/_patch.py | 28 - .../generated_tests/conftest.py | 39 ++ .../generated_tests/test_schema_registry.py | 34 ++ .../test_schema_registry_async.py | 35 ++ .../generated_tests/testpreparer.py | 26 + .../testpreparer_async.py} | 20 +- .../azure-schemaregistry/setup.py | 6 +- .../tests/async_tests/mock_transport_async.py | 42 +- .../async_tests/test_schema_registry_async.py | 24 +- .../tests/mock_transport.py | 38 +- .../tests/test_schema_registry.py | 16 +- .../azure-schemaregistry/tsp-location.yaml | 6 +- 44 files changed, 1211 insertions(+), 3206 deletions(-) create mode 100644 sdk/schemaregistry/azure-schemaregistry/_meta.json delete mode 100644 sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/__init__.py delete mode 100644 sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/__init__.py delete mode 100644 sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/_constants.py delete mode 100644 sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/_exceptions.py delete mode 100644 sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/_schema_registry_json_encoder.py delete mode 100644 sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/_utils.py delete mode 100644 sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/_version.py delete mode 100644 sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/aio/__init__.py delete mode 100644 sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/aio/_async_lru.py delete mode 100644 sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/aio/_schema_registry_json_encoder_async.py delete mode 100644 sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/py.typed create mode 100644 sdk/schemaregistry/azure-schemaregistry/generated_tests/conftest.py create mode 100644 sdk/schemaregistry/azure-schemaregistry/generated_tests/test_schema_registry.py create mode 100644 sdk/schemaregistry/azure-schemaregistry/generated_tests/test_schema_registry_async.py create mode 100644 sdk/schemaregistry/azure-schemaregistry/generated_tests/testpreparer.py rename sdk/schemaregistry/azure-schemaregistry/{azure/schemaregistry/_types.py => generated_tests/testpreparer_async.py} (50%) diff --git a/sdk/schemaregistry/azure-schemaregistry/_meta.json b/sdk/schemaregistry/azure-schemaregistry/_meta.json new file mode 100644 index 000000000000..316d17e888b3 --- /dev/null +++ b/sdk/schemaregistry/azure-schemaregistry/_meta.json @@ -0,0 +1,6 @@ +{ + "commit": "c29452397f8d29456182b951987a765d1ae01bb1", + "repository_url": "https://github.com/test-repo-billy/azure-rest-api-specs", + "typespec_src": "specification/schemaregistry/SchemaRegistry", + "@azure-tools/typespec-python": "0.31.1" +} \ No newline at end of file diff --git a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/__init__.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/__init__.py index 55df830b7b75..d7fa5e6b7c1a 100644 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/__init__.py +++ b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/__init__.py @@ -6,6 +6,7 @@ # Changes may cause incorrect behavior and will be lost if the code is regenerated. # -------------------------------------------------------------------------- +from ._client import SchemaRegistryClient from ._version import VERSION __version__ = VERSION diff --git a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_client.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_client.py index e6bb94829520..b62b4f3be303 100644 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_client.py +++ b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_client.py @@ -32,13 +32,12 @@ class SchemaRegistryClient(SchemaRegistryClientOperationsMixin): # pylint: disa :type fully_qualified_namespace: str :param credential: Credential used to authenticate requests to the service. Required. :type credential: ~azure.core.credentials.TokenCredential - :keyword api_version: The API version to use for this operation. Default value is "2022-10". + :keyword api_version: The API version to use for this operation. Default value is "2023-07-01". Note that overriding this default value may result in unsupported behavior. :paramtype api_version: str """ def __init__(self, fully_qualified_namespace: str, credential: "TokenCredential", **kwargs: Any) -> None: - super().__init__() _endpoint = "https://{fullyQualifiedNamespace}" self._config = SchemaRegistryClientConfiguration( fully_qualified_namespace=fully_qualified_namespace, credential=credential, **kwargs diff --git a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_configuration.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_configuration.py index a52f460189e3..f99007f123cd 100644 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_configuration.py +++ b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_configuration.py @@ -28,13 +28,13 @@ class SchemaRegistryClientConfiguration: # pylint: disable=too-many-instance-at :type fully_qualified_namespace: str :param credential: Credential used to authenticate requests to the service. Required. :type credential: ~azure.core.credentials.TokenCredential - :keyword api_version: The API version to use for this operation. Default value is "2022-10". + :keyword api_version: The API version to use for this operation. Default value is "2023-07-01". Note that overriding this default value may result in unsupported behavior. :paramtype api_version: str """ def __init__(self, fully_qualified_namespace: str, credential: "TokenCredential", **kwargs: Any) -> None: - api_version: str = kwargs.pop("api_version", "2022-10") + api_version: str = kwargs.pop("api_version", "2023-07-01") if fully_qualified_namespace is None: raise ValueError("Parameter 'fully_qualified_namespace' must not be None.") diff --git a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_model_base.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_model_base.py index 43fd8c7e9b1b..12ad7f29c71e 100644 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_model_base.py +++ b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_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, ) @@ -884,5 +984,175 @@ 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) + 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: + 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/schemaregistry/azure-schemaregistry/azure/schemaregistry/_operations/__init__.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_operations/__init__.py index ab44a5020c31..c3dc71695d85 100644 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_operations/__init__.py +++ b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_operations/__init__.py @@ -6,6 +6,8 @@ # Changes may cause incorrect behavior and will be lost if the code is regenerated. # -------------------------------------------------------------------------- +from ._operations import SchemaRegistryClientOperationsMixin + from ._patch import __all__ as _patch_all from ._patch import * # pylint: disable=unused-wildcard-import from ._patch import patch_sdk as _patch_sdk diff --git a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_operations/_operations.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_operations/_operations.py index af8ea36e8fd1..e0421b0feba4 100644 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_operations/_operations.py +++ b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_operations/_operations.py @@ -6,6 +6,7 @@ # Code generated by Microsoft (R) Python Code Generator. # Changes may cause incorrect behavior and will be lost if the code is regenerated. # -------------------------------------------------------------------------- +import json import sys from typing import Any, Callable, Dict, Iterable, Iterator, List, Optional, Type, TypeVar import urllib.parse @@ -16,6 +17,8 @@ ResourceExistsError, ResourceNotFoundError, ResourceNotModifiedError, + StreamClosedError, + StreamConsumedError, map_error, ) from azure.core.paging import ItemPaged @@ -24,9 +27,9 @@ from azure.core.tracing.decorator import distributed_trace from azure.core.utils import case_insensitive_dict -from .._model_base import _deserialize +from .._model_base import SdkJSONEncoder, _deserialize from .._serialization import Serializer -from .._vendor import SchemaRegistryClientMixinABC, raise_if_not_implemented +from .._vendor import SchemaRegistryClientMixinABC if sys.version_info >= (3, 9): from collections.abc import MutableMapping @@ -43,7 +46,7 @@ def build_schema_registry_list_schema_groups_request(**kwargs: Any) -> HttpReque _headers = case_insensitive_dict(kwargs.pop("headers", {}) or {}) _params = case_insensitive_dict(kwargs.pop("params", {}) or {}) - api_version: str = kwargs.pop("api_version", _params.pop("api-version", "2022-10")) + api_version: str = kwargs.pop("api_version", _params.pop("api-version", "2023-07-01")) accept = _headers.pop("Accept", "application/json") # Construct URL @@ -64,7 +67,7 @@ def build_schema_registry_list_schema_versions_request( # pylint: disable=name- _headers = case_insensitive_dict(kwargs.pop("headers", {}) or {}) _params = case_insensitive_dict(kwargs.pop("params", {}) or {}) - api_version: str = kwargs.pop("api_version", _params.pop("api-version", "2022-10")) + api_version: str = kwargs.pop("api_version", _params.pop("api-version", "2023-07-01")) accept = _headers.pop("Accept", "application/json") # Construct URL @@ -91,8 +94,9 @@ def build_schema_registry_get_schema_by_id_request( # pylint: disable=name-too- _headers = case_insensitive_dict(kwargs.pop("headers", {}) or {}) _params = case_insensitive_dict(kwargs.pop("params", {}) or {}) - api_version: str = kwargs.pop("api_version", _params.pop("api-version", "2022-10")) - accept = _headers.pop("Accept", "application/json; serialization=Avro") + api_version: str = kwargs.pop("api_version", _params.pop("api-version", "2023-07-01")) + accept = _headers.pop("Accept", None) + # Construct URL _url = "/$schemaGroups/$schemas/{id}" path_format_arguments = { @@ -105,7 +109,8 @@ def build_schema_registry_get_schema_by_id_request( # pylint: disable=name-too- _params["api-version"] = _SERIALIZER.query("api_version", api_version, "str") # Construct headers - _headers["Accept"] = _SERIALIZER.header("accept", accept, "str") + if accept is not None: + _headers["Accept"] = _SERIALIZER.header("accept", accept, "str") return HttpRequest(method="GET", url=_url, params=_params, headers=_headers, **kwargs) @@ -116,8 +121,9 @@ def build_schema_registry_get_schema_by_version_request( # pylint: disable=name _headers = case_insensitive_dict(kwargs.pop("headers", {}) or {}) _params = case_insensitive_dict(kwargs.pop("params", {}) or {}) - api_version: str = kwargs.pop("api_version", _params.pop("api-version", "2022-10")) - accept = _headers.pop("Accept", "application/json; serialization=Avro") + api_version: str = kwargs.pop("api_version", _params.pop("api-version", "2023-07-01")) + accept = _headers.pop("Accept", None) + # Construct URL _url = "/$schemaGroups/{groupName}/schemas/{schemaName}/versions/{schemaVersion}" path_format_arguments = { @@ -132,21 +138,71 @@ def build_schema_registry_get_schema_by_version_request( # pylint: disable=name _params["api-version"] = _SERIALIZER.query("api_version", api_version, "str") # Construct headers - _headers["Accept"] = _SERIALIZER.header("accept", accept, "str") + if accept is not None: + _headers["Accept"] = _SERIALIZER.header("accept", accept, "str") return HttpRequest(method="GET", url=_url, params=_params, headers=_headers, **kwargs) -class SchemaRegistryClientOperationsMixin(SchemaRegistryClientMixinABC): # pylint: disable=abstract-class-instantiated +def build_schema_registry_get_schema_properties_by_content_request( # pylint: disable=name-too-long + group_name: str, schema_name: str, **kwargs: Any +) -> HttpRequest: + _headers = case_insensitive_dict(kwargs.pop("headers", {}) or {}) + _params = case_insensitive_dict(kwargs.pop("params", {}) or {}) - def __init__(self): - raise_if_not_implemented( - self.__class__, - [ - "_get_schema_properties_by_content", - "_register_schema", - ], - ) + content_type: str = kwargs.pop("content_type") + api_version: str = kwargs.pop("api_version", _params.pop("api-version", "2023-07-01")) + accept = _headers.pop("Accept", "application/json") + + # Construct URL + _url = "/$schemaGroups/{groupName}/schemas/{schemaName}:get-id" + path_format_arguments = { + "groupName": _SERIALIZER.url("group_name", group_name, "str"), + "schemaName": _SERIALIZER.url("schema_name", schema_name, "str"), + } + + _url: str = _url.format(**path_format_arguments) # type: ignore + + # Construct parameters + _params["api-version"] = _SERIALIZER.query("api_version", api_version, "str") + + # Construct headers + _headers["Content-Type"] = _SERIALIZER.header("content_type", content_type, "str") + _headers["Accept"] = _SERIALIZER.header("accept", accept, "str") + + return HttpRequest(method="POST", url=_url, params=_params, headers=_headers, **kwargs) + + +def build_schema_registry_register_schema_request( # pylint: disable=name-too-long + group_name: str, schema_name: str, **kwargs: Any +) -> HttpRequest: + _headers = case_insensitive_dict(kwargs.pop("headers", {}) or {}) + _params = case_insensitive_dict(kwargs.pop("params", {}) or {}) + + content_type: str = kwargs.pop("content_type") + api_version: str = kwargs.pop("api_version", _params.pop("api-version", "2023-07-01")) + accept = _headers.pop("Accept", "application/json") + + # Construct URL + _url = "/$schemaGroups/{groupName}/schemas/{schemaName}" + path_format_arguments = { + "groupName": _SERIALIZER.url("group_name", group_name, "str"), + "schemaName": _SERIALIZER.url("schema_name", schema_name, "str"), + } + + _url: str = _url.format(**path_format_arguments) # type: ignore + + # Construct parameters + _params["api-version"] = _SERIALIZER.query("api_version", api_version, "str") + + # Construct headers + _headers["Content-Type"] = _SERIALIZER.header("content_type", content_type, "str") + _headers["Accept"] = _SERIALIZER.header("accept", accept, "str") + + return HttpRequest(method="PUT", url=_url, params=_params, headers=_headers, **kwargs) + + +class SchemaRegistryClientOperationsMixin(SchemaRegistryClientMixinABC): @distributed_trace def list_schema_groups(self, **kwargs: Any) -> Iterable[str]: @@ -157,19 +213,13 @@ def list_schema_groups(self, **kwargs: Any) -> Iterable[str]: :return: An iterator like instance of str :rtype: ~azure.core.paging.ItemPaged[str] :raises ~azure.core.exceptions.HttpResponseError: - - Example: - .. code-block:: python - - # response body for status code(s): 200 - response == "str" """ _headers = kwargs.pop("headers", {}) or {} _params = kwargs.pop("params", {}) or {} cls: ClsType[List[str]] = 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, @@ -257,19 +307,13 @@ def list_schema_versions(self, group_name: str, schema_name: str, **kwargs: Any) :return: An iterator like instance of int :rtype: ~azure.core.paging.ItemPaged[int] :raises ~azure.core.exceptions.HttpResponseError: - - Example: - .. code-block:: python - - # response body for status code(s): 200 - response == 0 """ _headers = kwargs.pop("headers", {}) or {} _params = kwargs.pop("params", {}) or {} cls: ClsType[List[int]] = 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, @@ -359,7 +403,7 @@ def _get_schema_by_id(self, id: str, **kwargs: Any) -> Iterator[bytes]: :rtype: Iterator[bytes] :raises ~azure.core.exceptions.HttpResponseError: """ - error_map: MutableMapping[int, Type[HttpResponseError]] = { + error_map: MutableMapping[int, Type[HttpResponseError]] = { # pylint: disable=unsubscriptable-object 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError, @@ -394,7 +438,10 @@ def _get_schema_by_id(self, id: str, **kwargs: Any) -> Iterator[bytes]: 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) @@ -432,7 +479,7 @@ def _get_schema_by_version( :rtype: Iterator[bytes] :raises ~azure.core.exceptions.HttpResponseError: """ - error_map: MutableMapping[int, Type[HttpResponseError]] = { + error_map: MutableMapping[int, Type[HttpResponseError]] = { # pylint: disable=unsubscriptable-object 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError, @@ -469,7 +516,10 @@ def _get_schema_by_version( 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) @@ -488,3 +538,150 @@ def _get_schema_by_version( return cls(pipeline_response, deserialized, response_headers) # type: ignore return deserialized # type: ignore + + @distributed_trace + def _get_schema_properties_by_content( # pylint: disable=inconsistent-return-statements + self, group_name: str, schema_name: str, schema_content: bytes, **kwargs: Any + ) -> None: + """Get properties for existing schema. + + Gets the properties referencing an existing schema within the specified schema group, as + matched by schema content comparison. + + :param group_name: Name of schema group. Required. + :type group_name: str + :param schema_name: Name of schema. Required. + :type schema_name: str + :param schema_content: String representation (UTF-8) of the schema. Required. + :type schema_content: bytes + :return: None + :rtype: None + :raises ~azure.core.exceptions.HttpResponseError: + """ + error_map: MutableMapping[int, Type[HttpResponseError]] = { # pylint: disable=unsubscriptable-object + 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 {} + + content_type: str = kwargs.pop("content_type") + cls: ClsType[None] = kwargs.pop("cls", None) + + _content = schema_content + + _request = build_schema_registry_get_schema_properties_by_content_request( + group_name=group_name, + schema_name=schema_name, + content_type=content_type, + api_version=self._config.api_version, + content=_content, + headers=_headers, + params=_params, + ) + path_format_arguments = { + "fullyQualifiedNamespace": self._serialize.url( + "self._config.fully_qualified_namespace", self._config.fully_qualified_namespace, "str", skip_quote=True + ), + } + _request.url = self._client.format_url(_request.url, **path_format_arguments) + + _stream = False + 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 [204]: + map_error(status_code=response.status_code, response=response, error_map=error_map) + raise HttpResponseError(response=response) + + response_headers = {} + response_headers["Location"] = self._deserialize("str", response.headers.get("Location")) + response_headers["Schema-Id"] = self._deserialize("str", response.headers.get("Schema-Id")) + response_headers["Schema-Id-Location"] = self._deserialize("str", response.headers.get("Schema-Id-Location")) + response_headers["Schema-Group-Name"] = self._deserialize("str", response.headers.get("Schema-Group-Name")) + response_headers["Schema-Name"] = self._deserialize("str", response.headers.get("Schema-Name")) + response_headers["Schema-Version"] = self._deserialize("int", response.headers.get("Schema-Version")) + + if cls: + return cls(pipeline_response, None, response_headers) # type: ignore + + @distributed_trace + def _register_schema( # pylint: disable=inconsistent-return-statements + self, group_name: str, schema_name: str, schema_content: bytes, **kwargs: Any + ) -> None: + """Register new schema. + + Register new schema. If schema of specified name does not exist in specified group, schema is + created at version 1. If schema of specified name exists already in specified group, schema is + created at latest version + 1. + + :param group_name: Name of schema group. Required. + :type group_name: str + :param schema_name: Name of schema. Required. + :type schema_name: str + :param schema_content: String representation (UTF-8) of the schema. Required. + :type schema_content: bytes + :return: None + :rtype: None + :raises ~azure.core.exceptions.HttpResponseError: + """ + error_map: MutableMapping[int, Type[HttpResponseError]] = { # pylint: disable=unsubscriptable-object + 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 {} + + content_type: str = kwargs.pop("content_type") + cls: ClsType[None] = kwargs.pop("cls", None) + + _content = schema_content + + _request = build_schema_registry_register_schema_request( + group_name=group_name, + schema_name=schema_name, + content_type=content_type, + api_version=self._config.api_version, + content=_content, + headers=_headers, + params=_params, + ) + path_format_arguments = { + "fullyQualifiedNamespace": self._serialize.url( + "self._config.fully_qualified_namespace", self._config.fully_qualified_namespace, "str", skip_quote=True + ), + } + _request.url = self._client.format_url(_request.url, **path_format_arguments) + + _stream = False + 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 [204]: + map_error(status_code=response.status_code, response=response, error_map=error_map) + raise HttpResponseError(response=response) + + response_headers = {} + response_headers["Location"] = self._deserialize("str", response.headers.get("Location")) + response_headers["Schema-Id"] = self._deserialize("str", response.headers.get("Schema-Id")) + response_headers["Schema-Id-Location"] = self._deserialize("str", response.headers.get("Schema-Id-Location")) + response_headers["Schema-Group-Name"] = self._deserialize("str", response.headers.get("Schema-Group-Name")) + response_headers["Schema-Name"] = self._deserialize("str", response.headers.get("Schema-Name")) + response_headers["Schema-Version"] = self._deserialize("int", response.headers.get("Schema-Version")) + + if cls: + return cls(pipeline_response, None, response_headers) # type: ignore diff --git a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_operations/_patch.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_operations/_patch.py index b9844c735a89..f7dd32510333 100644 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_operations/_patch.py +++ b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_operations/_patch.py @@ -6,266 +6,9 @@ Follow our quickstart for examples: https://aka.ms/azsdk/python/dpcodegen/python/customize """ -from typing import List, IO, Any, TYPE_CHECKING +from typing import List -from azure.core.exceptions import ( - ClientAuthenticationError, - HttpResponseError, - ResourceExistsError, - ResourceNotFoundError, - ResourceNotModifiedError, - map_error, -) -from azure.core.rest import HttpRequest -from azure.core.utils import case_insensitive_dict -from azure.core.tracing.decorator import distributed_trace - -from ._operations import ( - ClsType, - SchemaRegistryClientOperationsMixin as GeneratedClientOperationsMixin, -) -from .._serialization import Serializer - -if TYPE_CHECKING: - from azure.core.pipeline import PipelineResponse - -_SERIALIZER = Serializer() - - -def build_schema_registry_get_schema_properties_by_content_request( # pylint: disable=name-too-long - group_name: str, schema_name: str, **kwargs: Any -) -> HttpRequest: - _headers = case_insensitive_dict(kwargs.pop("headers", {}) or {}) - _params = case_insensitive_dict(kwargs.pop("params", {}) or {}) - - content_type: str = kwargs.pop("content_type") - api_version: str = kwargs.pop("api_version", _params.pop("api-version", "2022-10")) - accept = _headers.pop("Accept", "application/json") - - # Construct URL - _url = "/$schemaGroups/{groupName}/schemas/{schemaName}:get-id" - path_format_arguments = { - "groupName": _SERIALIZER.url("group_name", group_name, "str"), - "schemaName": _SERIALIZER.url("schemaName", schema_name, "str"), - } - - _url: str = _url.format(**path_format_arguments) # type: ignore - - # Construct parameters - _params["api-version"] = _SERIALIZER.query("api_version", api_version, "str") - - # Construct headers - _headers["Content-Type"] = _SERIALIZER.header("content_type", content_type, "str") - _headers["Accept"] = _SERIALIZER.header("accept", accept, "str") - - return HttpRequest(method="POST", url=_url, params=_params, headers=_headers, **kwargs) - - -def build_schema_registry_register_schema_request( # pylint: disable=name-too-long - group_name: str, schema_name: str, **kwargs: Any -) -> HttpRequest: - _headers = case_insensitive_dict(kwargs.pop("headers", {}) or {}) - _params = case_insensitive_dict(kwargs.pop("params", {}) or {}) - - content_type: str = kwargs.pop("content_type") - api_version: str = kwargs.pop("api_version", _params.pop("api-version", "2022-10")) - accept = _headers.pop("Accept", "application/json") - - # Construct URL - _url = "/$schemaGroups/{groupName}/schemas/{schemaName}" - path_format_arguments = { - "groupName": _SERIALIZER.url("group_name", group_name, "str"), - "schemaName": _SERIALIZER.url("name", schema_name, "str"), - } - - _url: str = _url.format(**path_format_arguments) # type: ignore - - # Construct parameters - _params["api-version"] = _SERIALIZER.query("api_version", api_version, "str") - - # Construct headers - _headers["Content-Type"] = _SERIALIZER.header("content_type", content_type, "str") - _headers["Accept"] = _SERIALIZER.header("accept", accept, "str") - - return HttpRequest(method="PUT", url=_url, params=_params, headers=_headers, **kwargs) - - -class SchemaRegistryClientOperationsMixin(GeneratedClientOperationsMixin): - """ - Implement custom operations. - """ - - @distributed_trace - def _get_schema_properties_by_content( # pylint: disable=inconsistent-return-statements - self, group_name: str, schema_name: str, schema_content: IO, **kwargs: Any - ) -> None: - """Get properties for existing schema. - - Gets the properties referencing an existing schema within the specified schema group, as matched by - schema content comparison. - - :param group_name: Name of schema group. Required. - :type group_name: str - :param schema_name: Name of schema. Required. - :type schema_name: str - :param schema_content: String representation (UTF-8) of the registered schema. Required. - :type schema_content: IO - :keyword content_type: The content type for given schema. Default value is "text/plain; - charset=utf-8". - :paramtype content_type: str - :keyword bool stream: Whether to stream the response of this operation. Defaults to False. You - will have to context manage the returned stream. - :return: None - :rtype: None - :raises ~azure.core.exceptions.HttpResponseError: - """ - error_map = { - 401: ClientAuthenticationError, - 404: ResourceNotFoundError, - 409: ResourceExistsError, - 304: ResourceNotModifiedError, - } - error_map.update(kwargs.pop("error_map", {}) or {}) - - _headers = case_insensitive_dict(kwargs.pop("headers", {}) or {}) - _params = kwargs.pop("params", {}) or {} - - content_type: str = kwargs.pop("content_type", _headers.pop("Content-Type", "text/plain; charset=utf-8")) - cls: ClsType[None] = kwargs.pop("cls", None) - - _content = schema_content - - request = build_schema_registry_get_schema_properties_by_content_request( - group_name=group_name, - schema_name=schema_name, - content_type=content_type, - api_version=self._config.api_version, - content=_content, - headers=_headers, - params=_params, - ) - path_format_arguments = { - "fullyQualifiedNamespace": self._serialize.url( - "self._config.fully_qualified_namespace", - self._config.fully_qualified_namespace, - "str", - skip_quote=True, - ), - } - request.url = self._client.format_url(request.url, **path_format_arguments) - - _stream = kwargs.pop("stream", False) - 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 [204]: - 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["Location"] = self._deserialize("str", response.headers.get("Location")) - response_headers["Schema-Id"] = self._deserialize("str", response.headers.get("Schema-Id")) - response_headers["Schema-Id-Location"] = self._deserialize("str", response.headers.get("Schema-Id-Location")) - response_headers["Schema-Group-Name"] = self._deserialize("str", response.headers.get("Schema-Group-Name")) - response_headers["Schema-Name"] = self._deserialize("str", response.headers.get("Schema-Name")) - response_headers["Schema-Version"] = self._deserialize("int", response.headers.get("Schema-Version")) - - if cls: - return cls(pipeline_response, None, response_headers) - - @distributed_trace - def _register_schema( # pylint: disable=inconsistent-return-statements - self, group_name: str, schema_name: str, content: IO, **kwargs: Any - ) -> None: - """Register new schema. - - Register new schema. If schema of specified name does not exist in specified group, schema is - created at version 1. If schema of specified name exists already in specified group, schema is - created at latest version + 1. - - :param group_name: Name of schema group. Required. - :type group_name: str - :param schema_name: Name of schema. Required. - :type schema_name: str - :param content: String representation (UTF-8) of the schema. Required. - :type content: IO - :keyword content_type: The content type for given schema. Default value is "text/plain; - charset=utf-8". - :paramtype content_type: str - :keyword bool stream: Whether to stream the response of this operation. Defaults to False. You - will have to context manage the returned stream. - :return: None - :rtype: None - :raises ~azure.core.exceptions.HttpResponseError: - """ - error_map = { - 401: ClientAuthenticationError, - 404: ResourceNotFoundError, - 409: ResourceExistsError, - 304: ResourceNotModifiedError, - } - error_map.update(kwargs.pop("error_map", {}) or {}) - - _headers = case_insensitive_dict(kwargs.pop("headers", {}) or {}) - _params = kwargs.pop("params", {}) or {} - - content_type: str = kwargs.pop("content_type", _headers.pop("Content-Type", "text/plain; charset=utf-8")) - cls: ClsType[None] = kwargs.pop("cls", None) - - _content = content - - request = build_schema_registry_register_schema_request( - group_name=group_name, - schema_name=schema_name, - content_type=content_type, - api_version=self._config.api_version, - content=_content, - headers=_headers, - params=_params, - ) - path_format_arguments = { - "fullyQualifiedNamespace": self._serialize.url( - "self._config.fully_qualified_namespace", - self._config.fully_qualified_namespace, - "str", - skip_quote=True, - ), - } - request.url = self._client.format_url(request.url, **path_format_arguments) - - _stream = kwargs.pop("stream", False) - 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 [204]: - 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["Location"] = self._deserialize("str", response.headers.get("Location")) - response_headers["Schema-Id"] = self._deserialize("str", response.headers.get("Schema-Id")) - response_headers["Schema-Id-Location"] = self._deserialize("str", response.headers.get("Schema-Id-Location")) - response_headers["Schema-Group-Name"] = self._deserialize("str", response.headers.get("Schema-Group-Name")) - response_headers["Schema-Name"] = self._deserialize("str", response.headers.get("Schema-Name")) - response_headers["Schema-Version"] = self._deserialize("int", response.headers.get("Schema-Version")) - - if cls: - return cls(pipeline_response, None, response_headers) - - -__all__: List[str] = [ - "SchemaRegistryClientOperationsMixin", -] # 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/schemaregistry/azure-schemaregistry/azure/schemaregistry/_patch.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_patch.py index f4082d516a62..f7dd32510333 100644 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_patch.py +++ b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_patch.py @@ -6,551 +6,9 @@ Follow our quickstart for examples: https://aka.ms/azsdk/python/dpcodegen/python/customize """ -from __future__ import annotations -from functools import partial -from typing import ( - cast, - Tuple, - Mapping, - Dict, - TYPE_CHECKING, - Iterator, - AsyncIterator, - Union, - List, - Any, - overload, - IO, -) -from enum import Enum -from typing_extensions import Protocol, TypedDict, Self -from azure.core import CaseInsensitiveEnumMeta -from azure.core.tracing.decorator import distributed_trace +from typing import List -from ._client import SchemaRegistryClient as GeneratedServiceClient -from .models._patch import SchemaFormat, NormalizedSchemaContentTypes - -if TYPE_CHECKING: - from azure.core.credentials import TokenCredential - from azure.core.pipeline import PipelineResponse - from azure.core.rest import HttpResponse, AsyncHttpResponse - - class SchemaPropertiesDict(TypedDict): # needed for use with spread operator - """ - Typing for SchemaProperties dict. - """ - - id: str - format: "SchemaFormat" - group_name: str - name: str - version: int - - -###### Response Handlers ###### - - -def _parse_schema_properties_dict(response_headers: Mapping[str, Union[str, int]]) -> Dict[str, Union[str, int]]: - return { - "id": response_headers["Schema-Id"], - "group_name": response_headers["Schema-Group-Name"], - "name": response_headers["Schema-Name"], - "version": int(response_headers["Schema-Version"]), - } - -def _normalize_content_type(content_type: str) -> str: - return content_type.replace(" ", "").lower() - -def _get_format(content_type: str) -> Union[SchemaFormat, str]: - # pylint:disable=redefined-builtin - # Exception cases may be due to forward compatibility. - # i.e. Getting a schema with a content type from a future API version. - # In this case, we default to returning the content type string. - - # remove whitespace and case from string - normalized_content_type = _normalize_content_type(content_type) - if normalized_content_type == NormalizedSchemaContentTypes.AVRO.value: - return SchemaFormat.AVRO - if normalized_content_type == NormalizedSchemaContentTypes.JSON.value: - return SchemaFormat.JSON - if normalized_content_type == NormalizedSchemaContentTypes.CUSTOM.value: - return SchemaFormat.CUSTOM - return content_type - -def prepare_schema_properties_result( # pylint:disable=unused-argument,redefined-builtin - format: str, - pipeline_response: "PipelineResponse", - deserialized: Union[Iterator[bytes], AsyncIterator[bytes]], - response_headers: Mapping[str, Union[str, int]], -) -> Dict[str, Union[str, int]]: - properties_dict = _parse_schema_properties_dict(response_headers) - properties_dict["format"] = SchemaFormat(format) - pipeline_response.http_response.raise_for_status() - return properties_dict - - -def prepare_schema_result( # pylint:disable=unused-argument - pipeline_response: "PipelineResponse", - deserialized: Union[Iterator[bytes], AsyncIterator[bytes]], - response_headers: Mapping[str, Union[str, int]], -) -> Tuple[Union["HttpResponse", "AsyncHttpResponse"], Dict[str, Union[int, str]]]: - properties_dict = _parse_schema_properties_dict(response_headers) - # re-generate after multi-content type response fix: https://github.com/Azure/autorest.python/issues/2122 - properties_dict["format"] = _get_format(cast(str, response_headers.get("Content-Type"))) - pipeline_response.http_response.raise_for_status() - return pipeline_response.http_response, properties_dict - - -###### Request Helper Functions ###### - - -def get_http_request_kwargs(kwargs): - http_request_keywords = ["params", "headers", "json", "data", "files"] - http_request_kwargs = {key: kwargs.pop(key, None) for key in http_request_keywords if key in kwargs} - return http_request_kwargs - - -def get_content_type(format: str): # pylint:disable=redefined-builtin - if format.lower() == SchemaFormat.CUSTOM.value.lower(): - return "text/plain; charset=utf-8" - return f"application/json; serialization={format}" - - -def get_case_insensitive_format(format: Union[str, SchemaFormat]) -> str: # pylint:disable=redefined-builtin - try: - format = cast(SchemaFormat, format) - format = format.value - except AttributeError: - pass - return format.capitalize() - - -###### Wrapper Class ###### - - -class SchemaRegistryClient: - """ - SchemaRegistryClient is a client for registering and retrieving schemas from the - Azure Schema Registry service. - - :param str fully_qualified_namespace: The Schema Registry service fully qualified host name. - For example: my-namespace.servicebus.windows.net. - :param credential: To authenticate managing the entities of the SchemaRegistry namespace. - :type credential: ~azure.core.credentials.TokenCredential - :keyword str api_version: The Schema Registry service API version to use for requests. - Default value is "2022-10". - - .. admonition:: Example: - - .. literalinclude:: ../samples/sync_samples/sample_code_schemaregistry.py - :start-after: [START create_sr_client_sync] - :end-before: [END create_sr_client_sync] - :language: python - :dedent: 4 - :caption: Create a new instance of the SchemaRegistryClient. - - """ - - def __init__(self, fully_qualified_namespace: str, credential: TokenCredential, **kwargs: Any) -> None: - # using composition (not inheriting from generated client) to allow - # calling different operations conditionally within one method - self._generated_client = GeneratedServiceClient( - fully_qualified_namespace=fully_qualified_namespace, - credential=credential, - **kwargs, - ) - - def __enter__(self) -> Self: - self._generated_client.__enter__() - return self - - def __exit__(self, *exc_details: Any) -> None: - self._generated_client.__exit__(*exc_details) - - def close(self) -> None: - """This method is to close the sockets opened by the client. - It need not be used when using with a context manager. - """ - self._generated_client.close() - - @distributed_trace - def register_schema( # pylint:disable=arguments-differ - self, - group_name: str, - name: str, - definition: str, - format: Union[str, SchemaFormat], # pylint:disable=redefined-builtin - **kwargs: Any, - ) -> SchemaProperties: - """ - Register new schema. If schema of specified name does not exist in specified group, - schema is created at version 1. If schema of specified name exists already in specified group, - schema is created at latest version + 1. - - :param str group_name: Schema group under which schema should be registered. - :param str name: Name of schema being registered. - :param str definition: String representation of the schema being registered. - :param format: Format for the schema being registered. - :type format: Union[str, SchemaFormat] - :return: The SchemaProperties associated with the registered schema. - :rtype: ~azure.schemaregistry.SchemaProperties - :raises: :class:`~azure.core.exceptions.HttpResponseError` - - .. admonition:: Example: - - .. literalinclude:: ../samples/sync_samples/sample_code_schemaregistry.py - :start-after: [START register_schema_sync] - :end-before: [END register_schema_sync] - :language: python - :dedent: 4 - :caption: Register a new schema. - - """ - format = get_case_insensitive_format(format) - http_request_kwargs = get_http_request_kwargs(kwargs) - # ignoring return type because the generated client operations are not annotated w/ cls return type - schema_properties: Dict[str, Union[int, str]] = ( - self._generated_client._register_schema( # type:ignore # pylint:disable=protected-access - group_name=group_name, - schema_name=name, - content=cast(IO[Any], definition), - content_type=kwargs.pop("content_type", get_content_type(format)), - cls=partial(prepare_schema_properties_result, format), - **http_request_kwargs, - ) - ) - properties = cast("SchemaPropertiesDict", schema_properties) - return SchemaProperties(**properties) - - @overload - def get_schema(self, schema_id: str, **kwargs: Any) -> Schema: - """Gets a registered schema. - - To get a registered schema by its unique ID, pass the `schema_id` parameter and any optional - keyword arguments. Azure Schema Registry guarantees that ID is unique within a namespace. - - WARNING: If retrieving a schema format that is unsupported by this client version, upgrade to a client - version that supports the schema format. Otherwise, the content MIME type string will be returned as - the `format` value in the `properties` of the returned Schema. - - :param str schema_id: References specific schema in registry namespace. Required if `group_name`, - `name`, and `version` are not provided. - :return: The schema stored in the registry associated with the provided arguments. - :rtype: ~azure.schemaregistry.Schema - :raises: :class:`~azure.core.exceptions.HttpResponseError` - - .. admonition:: Example: - - .. literalinclude:: ../samples/sync_samples/sample_code_schemaregistry.py - :start-after: [START get_schema_sync] - :end-before: [END get_schema_sync] - :language: python - :dedent: 4 - :caption: Get schema by id. - - """ - ... - - @overload - def get_schema(self, *, group_name: str, name: str, version: int, **kwargs: Any) -> Schema: - """Gets a registered schema. - - To get a specific version of a schema within the specified schema group, pass in the required - keyword arguments `group_name`, `name`, and `version` and any optional keyword arguments. - - WARNING: If retrieving a schema format that is unsupported by this client version, upgrade to a client - version that supports the schema format. Otherwise, the content MIME type string will be returned as - the `format` value in the `properties` of the returned Schema. - - :keyword str group_name: Name of schema group that contains the registered schema. - :keyword str name: Name of schema which should be retrieved. - :keyword int version: Version of schema which should be retrieved. - :return: The schema stored in the registry associated with the provided arguments. - :rtype: ~azure.schemaregistry.Schema - :raises: :class:`~azure.core.exceptions.HttpResponseError` - - .. admonition:: Example: - - .. literalinclude:: ../samples/sync_samples/sample_code_schemaregistry.py - :start-after: [START get_schema_by_version_sync] - :end-before: [END get_schema_by_version_sync] - :language: python - :dedent: 4 - :caption: Get schema by version. - """ - ... - - @distributed_trace - def get_schema( # pylint: disable=docstring-missing-param,docstring-should-be-keyword - self, *args: str, **kwargs: Any - ) -> Schema: - """Gets a registered schema. There are two ways to call this method: - - 1) To get a registered schema by its unique ID, pass the `schema_id` parameter and any optional - keyword arguments. Azure Schema Registry guarantees that ID is unique within a namespace. - - 2) To get a specific version of a schema within the specified schema group, pass in the required - keyword arguments `group_name`, `name`, and `version` and any optional keyword arguments. - - WARNING: If retrieving a schema format that is unsupported by this client version, upgrade to a client - version that supports the schema format. Otherwise, the content MIME type string will be returned as - the `format` value in the `properties` of the returned Schema. - - :param str schema_id: References specific schema in registry namespace. Required if `group_name`, - `name`, and `version` are not provided. - :keyword str group_name: Name of schema group that contains the registered schema. - :keyword str name: Name of schema which should be retrieved. - :keyword int version: Version of schema which should be retrieved. - :return: The schema stored in the registry associated with the provided arguments. - :rtype: ~azure.schemaregistry.Schema - :raises: :class:`~azure.core.exceptions.HttpResponseError` - - .. admonition:: Example: - - .. literalinclude:: ../samples/sync_samples/sample_code_schemaregistry.py - :start-after: [START get_schema_sync] - :end-before: [END get_schema_sync] - :language: python - :dedent: 4 - :caption: Get schema by id. - - .. literalinclude:: ../samples/sync_samples/sample_code_schemaregistry.py - :start-after: [START get_schema_by_version_sync] - :end-before: [END get_schema_by_version_sync] - :language: python - :dedent: 4 - :caption: Get schema by version. - """ - http_request_kwargs = get_http_request_kwargs(kwargs) - http_response: "HttpResponse" - schema_properties: Dict[str, Union[int, str]] - try: - # Check positional args for schema_id. - # Else, check if schema_id was passed in with keyword. - try: - schema_id = args[0] - except IndexError: - schema_id = kwargs.pop("schema_id") - schema_id = cast(str, schema_id) - # ignoring return type because the generated client operations are not annotated w/ cls return type - ( - http_response, - schema_properties, - ) = self._generated_client._get_schema_by_id( # type:ignore # pylint:disable=protected-access - id=schema_id, - cls=prepare_schema_result, - headers={ # TODO: remove when multiple content types in response are supported - "Accept": """application/json; serialization=Avro, application/json; """ - """serialization=json, text/plain; charset=utf-8""" - }, - stream=True, - **http_request_kwargs, - ) - except KeyError: - # If group_name, name, and version aren't passed in as kwargs, raise error. - try: - group_name = kwargs.pop("group_name") - name = kwargs.pop("name") - version = kwargs.pop("version") - except KeyError: - raise TypeError( # pylint:disable=raise-missing-from - """Missing required argument(s). Specify either `schema_id` """ - """or `group_name`, `name`, `version.""" - ) - # ignoring return type because the generated client operations are not annotated w/ cls return type - http_response, schema_properties = self._generated_client._get_schema_by_version( # type: ignore # pylint:disable=protected-access - group_name=group_name, - schema_name=name, - schema_version=version, - cls=prepare_schema_result, - headers={ # TODO: remove when multiple content types in response are supported - "Accept": """application/json; serialization=Avro, application/json; """ - """serialization=json, text/plain; charset=utf-8""" - }, - stream=True, - **http_request_kwargs, - ) # type:ignore - http_response.read() - properties = cast("SchemaPropertiesDict", schema_properties) - return Schema( - definition=http_response.text(), - properties=SchemaProperties(**properties), - ) - - @distributed_trace - def get_schema_properties( - self, - group_name: str, - name: str, - definition: str, - format: Union[str, SchemaFormat], # pylint:disable=redefined-builtin - **kwargs: Any, - ) -> SchemaProperties: - """ - Gets the schema properties corresponding to an existing schema within the specified schema group, - as matched by schema definition comparison. - - :param str group_name: Schema group under which schema should be registered. - :param str name: Name of schema for which properties should be retrieved. - :param str definition: String representation of the schema for which properties should be retrieved. - :param format: Format for the schema for which properties should be retrieved. - :type format: Union[str, SchemaFormat] - :return: The SchemaProperties associated with the provided schema metadata. - :rtype: ~azure.schemaregistry.SchemaProperties - :raises: :class:`~azure.core.exceptions.HttpResponseError` - - .. admonition:: Example: - - .. literalinclude:: ../samples/sync_samples/sample_code_schemaregistry.py - :start-after: [START get_schema_id_sync] - :end-before: [END get_schema_id_sync] - :language: python - :dedent: 4 - :caption: Get schema id. - - """ - format = get_case_insensitive_format(format) - http_request_kwargs = get_http_request_kwargs(kwargs) - # ignoring return type because the generated client operations are not annotated w/ cls return type - schema_properties: Dict[str, Union[int, str]] = ( - self._generated_client._get_schema_properties_by_content( # type: ignore # pylint:disable=protected-access - group_name=group_name, - schema_name=name, - schema_content=cast(IO[Any], definition), - content_type=kwargs.pop("content_type", get_content_type(format)), - cls=partial(prepare_schema_properties_result, format), - **http_request_kwargs, - ) - ) # type:ignore - properties = cast("SchemaPropertiesDict", schema_properties) - return SchemaProperties(**properties) - - -class SchemaProperties: - """ - Meta properties of a schema. - - :ivar id: References specific schema in registry namespace. - :vartype id: str - :ivar format: Format for the schema being stored. - :vartype format: ~azure.schemaregistry.SchemaFormat - :ivar group_name: Schema group under which schema is stored. - :vartype group_name: str - :ivar name: Name of schema. - :vartype name: str - :ivar version: Version of schema. - :vartype version: int - """ - - def __init__(self, **kwargs: Any) -> None: - self.id: str = kwargs.pop("id") - self.format: SchemaFormat = kwargs.pop("format") - self.group_name: str = kwargs.pop("group_name") - self.name: str = kwargs.pop("name") - self.version: int = kwargs.pop("version") - - def __repr__(self) -> str: - return ( - f"SchemaProperties(id={self.id}, format={self.format}, " - f"group_name={self.group_name}, name={self.name}, version={self.version})"[:1024] - ) - - -class Schema: - """ - The schema content of a schema, along with id and meta properties. - - :ivar definition: The content of the schema. - :vartype definition: str - :ivar properties: The properties of the schema. - :vartype properties: SchemaProperties - """ - - def __init__(self, **kwargs: Any) -> None: - self.definition: str = kwargs.pop("definition") - self.properties: SchemaProperties = kwargs.pop("properties") - - def __repr__(self) -> str: - return f"Schema(definition={self.definition}, properties={self.properties})"[:1024] - -# ApiVersion was added to a previously GA'd version. However, newer libraries should not -# accept ApiVersion enums and only take strings. Leaving this here for backwards compatibility. -class ApiVersion(str, Enum, metaclass=CaseInsensitiveEnumMeta): - """ - Represents the Schema Registry API version to use for requests. - """ - - V2021_10 = "2021-10" - V2022_10 = "2022-10" - """This is the default version.""" - -###### Encoder Protocols ###### - - -class SchemaContentValidate(Protocol): - # TODO: make __call__ a public API so that docs show, until then, keep below docstring - - """ - Callable protocol which validates content against provided schema. If invalid, raises Exception. - Else, returns None. - - :param mapping[str, any] schema: The schema to validate against. - :param mapping[str, any] content: The content to validate. - - :rtype: None - :returns: None if valid. - :raises: Exception if content is invalid against provided schema. - """ - - def __call__(self, schema: Mapping[str, Any], content: Mapping[str, Any]) -> None: - """ - Validates content against provided schema. If invalid, raises Exception. - Else, returns None. - - :param mapping[str, any] schema: The schema to validate against. - :param mapping[str, any] content: The content to validate. - - :rtype: None - :returns: None if valid. - :raises: Exception if content is invalid against provided schema. - """ - - -class MessageContent(TypedDict): - """A dict with required keys: - - `content`: bytes - - `content_type`: str - """ - - content: bytes - content_type: str - - -class OutboundMessageContent(Protocol): - """Protocol for classes that set content and content type values internally.""" - - @classmethod - def from_message_content(cls, content: bytes, content_type: str, **kwargs: Any) -> Self: - """Creates an object that is a subtype of OutboundMessageContent, given content type and - a content value to be set on the object. - - :param bytes content: The content value to be set as the body of the message. - :param str content_type: The content type to be set on the message. - :rtype: ~azure.schemaregistry.OutboundMessageContent - :returns: The OutboundMessageContent object with encoded content and content type. - """ - - -class InboundMessageContent(Protocol): - """Message Types that get content and content type values internally.""" - - def __message_content__(self) -> MessageContent: - """A MessageContent object, with `content` and `content_type` set to - the values of their respective properties on the InboundMessageContent object. - - :rtype: ~azure.schemaregistry.MessageContent - :returns: TypedDict of the content and content type from the InboundMessageContent object. - """ +__all__: List[str] = [] # Add all objects you want publicly available to users at this package level def patch_sdk(): @@ -560,15 +18,3 @@ def patch_sdk(): you can't accomplish using the techniques described in https://aka.ms/azsdk/python/dpcodegen/python/customize """ - - -__all__: List[str] = [ - "SchemaRegistryClient", - "SchemaProperties", - "Schema", - "SchemaFormat", - "SchemaContentValidate", - "MessageContent", - "OutboundMessageContent", - "InboundMessageContent", -] # Add all objects you want publicly available to users at this package level diff --git a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_serialization.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_serialization.py index 8139854b97bb..01a226bd7f14 100644 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_serialization.py +++ b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_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: @@ -155,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 @@ -184,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) @@ -235,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): @@ -300,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: @@ -326,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: @@ -346,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, @@ -380,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): @@ -395,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 @@ -408,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 @@ -426,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 @@ -448,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 @@ -456,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 @@ -501,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"} @@ -560,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) @@ -592,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"] == "": @@ -633,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 @@ -664,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 @@ -703,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 @@ -712,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 """ @@ -728,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 @@ -759,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]"]: @@ -780,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") @@ -805,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 @@ -821,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 @@ -841,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 @@ -871,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. @@ -882,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.") @@ -945,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 = {} @@ -971,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 @@ -979,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 @@ -1003,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: @@ -1034,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) @@ -1091,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) @@ -1105,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], @@ -1141,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) @@ -1172,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 @@ -1186,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 @@ -1211,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 @@ -1232,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) @@ -1279,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 @@ -1331,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: @@ -1363,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 = { @@ -1403,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 @@ -1416,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) @@ -1440,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 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"... @@ -1476,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: @@ -1505,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 @@ -1516,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 @@ -1531,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 ) @@ -1552,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", {}) @@ -1579,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: @@ -1596,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(): @@ -1605,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 @@ -1627,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) @@ -1647,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: @@ -1671,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): @@ -1681,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. """ @@ -1720,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 @@ -1732,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. """ @@ -1743,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): @@ -1768,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, @@ -1781,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): @@ -1794,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: @@ -1804,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: @@ -1822,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. """ @@ -1834,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. """ @@ -1849,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 @@ -1865,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. """ @@ -1877,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. """ @@ -1887,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. """ @@ -1910,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. """ @@ -1924,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. """ @@ -1939,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. """ @@ -1976,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): @@ -1985,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 """ @@ -1996,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/schemaregistry/azure-schemaregistry/azure/schemaregistry/_vendor.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_vendor.py index 63d1cf697e73..3ec09f29812f 100644 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_vendor.py +++ b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_vendor.py @@ -24,14 +24,3 @@ class SchemaRegistryClientMixinABC(ABC): _config: SchemaRegistryClientConfiguration _serialize: "Serializer" _deserialize: "Deserializer" - - -def raise_if_not_implemented(cls, abstract_methods): - not_implemented = [f for f in abstract_methods if not callable(getattr(cls, f, None))] - if not_implemented: - raise NotImplementedError( - "The following methods on operation group '{}' are not implemented: '{}'." - " Please refer to https://aka.ms/azsdk/python/dpcodegen/python/customize to learn how to customize.".format( - cls.__name__, "', '".join(not_implemented) - ) - ) diff --git a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_version.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_version.py index e616621bef1a..95a5f0e6b3c2 100644 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_version.py +++ b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_version.py @@ -6,4 +6,4 @@ # Changes may cause incorrect behavior and will be lost if the code is regenerated. # -------------------------------------------------------------------------- -VERSION = "1.3.0" +VERSION = "1.3.0b4" diff --git a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/__init__.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/__init__.py index 5dc2b452a8d8..32f4240fe712 100644 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/__init__.py +++ b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/__init__.py @@ -6,6 +6,8 @@ # Changes may cause incorrect behavior and will be lost if the code is regenerated. # -------------------------------------------------------------------------- +from ._client import SchemaRegistryClient + try: from ._patch import __all__ as _patch_all from ._patch import * # pylint: disable=unused-wildcard-import diff --git a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/_client.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/_client.py index 1d0345ce3fb5..17276bf58ad0 100644 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/_client.py +++ b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/_client.py @@ -32,13 +32,12 @@ class SchemaRegistryClient(SchemaRegistryClientOperationsMixin): # pylint: disa :type fully_qualified_namespace: str :param credential: Credential used to authenticate requests to the service. Required. :type credential: ~azure.core.credentials_async.AsyncTokenCredential - :keyword api_version: The API version to use for this operation. Default value is "2022-10". + :keyword api_version: The API version to use for this operation. Default value is "2023-07-01". Note that overriding this default value may result in unsupported behavior. :paramtype api_version: str """ def __init__(self, fully_qualified_namespace: str, credential: "AsyncTokenCredential", **kwargs: Any) -> None: - super().__init__() _endpoint = "https://{fullyQualifiedNamespace}" self._config = SchemaRegistryClientConfiguration( fully_qualified_namespace=fully_qualified_namespace, credential=credential, **kwargs diff --git a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/_configuration.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/_configuration.py index 997119725c59..d2ef5163de64 100644 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/_configuration.py +++ b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/_configuration.py @@ -28,13 +28,13 @@ class SchemaRegistryClientConfiguration: # pylint: disable=too-many-instance-at :type fully_qualified_namespace: str :param credential: Credential used to authenticate requests to the service. Required. :type credential: ~azure.core.credentials_async.AsyncTokenCredential - :keyword api_version: The API version to use for this operation. Default value is "2022-10". + :keyword api_version: The API version to use for this operation. Default value is "2023-07-01". Note that overriding this default value may result in unsupported behavior. :paramtype api_version: str """ def __init__(self, fully_qualified_namespace: str, credential: "AsyncTokenCredential", **kwargs: Any) -> None: - api_version: str = kwargs.pop("api_version", "2022-10") + api_version: str = kwargs.pop("api_version", "2023-07-01") if fully_qualified_namespace is None: raise ValueError("Parameter 'fully_qualified_namespace' must not be None.") diff --git a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/_operations/__init__.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/_operations/__init__.py index ab44a5020c31..c3dc71695d85 100644 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/_operations/__init__.py +++ b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/_operations/__init__.py @@ -6,6 +6,8 @@ # Changes may cause incorrect behavior and will be lost if the code is regenerated. # -------------------------------------------------------------------------- +from ._operations import SchemaRegistryClientOperationsMixin + from ._patch import __all__ as _patch_all from ._patch import * # pylint: disable=unused-wildcard-import from ._patch import patch_sdk as _patch_sdk diff --git a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/_operations/_operations.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/_operations/_operations.py index 2b53e448657c..782f61d98816 100644 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/_operations/_operations.py +++ b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/_operations/_operations.py @@ -6,6 +6,7 @@ # Code generated by Microsoft (R) Python Code Generator. # Changes may cause incorrect behavior and will be lost if the code is regenerated. # -------------------------------------------------------------------------- +import json import sys from typing import Any, AsyncIterable, AsyncIterator, Callable, Dict, List, Optional, Type, TypeVar import urllib.parse @@ -17,6 +18,8 @@ ResourceExistsError, ResourceNotFoundError, ResourceNotModifiedError, + StreamClosedError, + StreamConsumedError, map_error, ) from azure.core.pipeline import PipelineResponse @@ -25,14 +28,16 @@ from azure.core.tracing.decorator_async import distributed_trace_async from azure.core.utils import case_insensitive_dict -from ..._model_base import _deserialize +from ..._model_base import SdkJSONEncoder, _deserialize from ..._operations._operations import ( build_schema_registry_get_schema_by_id_request, build_schema_registry_get_schema_by_version_request, + build_schema_registry_get_schema_properties_by_content_request, build_schema_registry_list_schema_groups_request, build_schema_registry_list_schema_versions_request, + build_schema_registry_register_schema_request, ) -from .._vendor import SchemaRegistryClientMixinABC, raise_if_not_implemented +from .._vendor import SchemaRegistryClientMixinABC if sys.version_info >= (3, 9): from collections.abc import MutableMapping @@ -42,16 +47,7 @@ ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]] -class SchemaRegistryClientOperationsMixin(SchemaRegistryClientMixinABC): # pylint: disable=abstract-class-instantiated - - def __init__(self) -> None: - raise_if_not_implemented( - self.__class__, - [ - "_get_schema_properties_by_content", - "_register_schema", - ], - ) +class SchemaRegistryClientOperationsMixin(SchemaRegistryClientMixinABC): @distributed_trace def list_schema_groups(self, **kwargs: Any) -> AsyncIterable[str]: @@ -62,19 +58,13 @@ def list_schema_groups(self, **kwargs: Any) -> AsyncIterable[str]: :return: An iterator like instance of str :rtype: ~azure.core.async_paging.AsyncItemPaged[str] :raises ~azure.core.exceptions.HttpResponseError: - - Example: - .. code-block:: python - - # response body for status code(s): 200 - response == "str" """ _headers = kwargs.pop("headers", {}) or {} _params = kwargs.pop("params", {}) or {} cls: ClsType[List[str]] = 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, @@ -162,19 +152,13 @@ def list_schema_versions(self, group_name: str, schema_name: str, **kwargs: Any) :return: An iterator like instance of int :rtype: ~azure.core.async_paging.AsyncItemPaged[int] :raises ~azure.core.exceptions.HttpResponseError: - - Example: - .. code-block:: python - - # response body for status code(s): 200 - response == 0 """ _headers = kwargs.pop("headers", {}) or {} _params = kwargs.pop("params", {}) or {} cls: ClsType[List[int]] = 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, @@ -264,7 +248,7 @@ async def _get_schema_by_id(self, id: str, **kwargs: Any) -> AsyncIterator[bytes :rtype: AsyncIterator[bytes] :raises ~azure.core.exceptions.HttpResponseError: """ - error_map: MutableMapping[int, Type[HttpResponseError]] = { + error_map: MutableMapping[int, Type[HttpResponseError]] = { # pylint: disable=unsubscriptable-object 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError, @@ -299,7 +283,10 @@ async def _get_schema_by_id(self, id: str, **kwargs: Any) -> AsyncIterator[bytes 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) @@ -337,7 +324,7 @@ async def _get_schema_by_version( :rtype: AsyncIterator[bytes] :raises ~azure.core.exceptions.HttpResponseError: """ - error_map: MutableMapping[int, Type[HttpResponseError]] = { + error_map: MutableMapping[int, Type[HttpResponseError]] = { # pylint: disable=unsubscriptable-object 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError, @@ -374,7 +361,10 @@ async def _get_schema_by_version( 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) @@ -393,3 +383,150 @@ async def _get_schema_by_version( return cls(pipeline_response, deserialized, response_headers) # type: ignore return deserialized # type: ignore + + @distributed_trace_async + async def _get_schema_properties_by_content( # pylint: disable=inconsistent-return-statements + self, group_name: str, schema_name: str, schema_content: bytes, **kwargs: Any + ) -> None: + """Get properties for existing schema. + + Gets the properties referencing an existing schema within the specified schema group, as + matched by schema content comparison. + + :param group_name: Name of schema group. Required. + :type group_name: str + :param schema_name: Name of schema. Required. + :type schema_name: str + :param schema_content: String representation (UTF-8) of the schema. Required. + :type schema_content: bytes + :return: None + :rtype: None + :raises ~azure.core.exceptions.HttpResponseError: + """ + error_map: MutableMapping[int, Type[HttpResponseError]] = { # pylint: disable=unsubscriptable-object + 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 {} + + content_type: str = kwargs.pop("content_type") + cls: ClsType[None] = kwargs.pop("cls", None) + + _content = schema_content + + _request = build_schema_registry_get_schema_properties_by_content_request( + group_name=group_name, + schema_name=schema_name, + content_type=content_type, + api_version=self._config.api_version, + content=_content, + headers=_headers, + params=_params, + ) + path_format_arguments = { + "fullyQualifiedNamespace": self._serialize.url( + "self._config.fully_qualified_namespace", self._config.fully_qualified_namespace, "str", skip_quote=True + ), + } + _request.url = self._client.format_url(_request.url, **path_format_arguments) + + _stream = False + 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 [204]: + map_error(status_code=response.status_code, response=response, error_map=error_map) + raise HttpResponseError(response=response) + + response_headers = {} + response_headers["Location"] = self._deserialize("str", response.headers.get("Location")) + response_headers["Schema-Id"] = self._deserialize("str", response.headers.get("Schema-Id")) + response_headers["Schema-Id-Location"] = self._deserialize("str", response.headers.get("Schema-Id-Location")) + response_headers["Schema-Group-Name"] = self._deserialize("str", response.headers.get("Schema-Group-Name")) + response_headers["Schema-Name"] = self._deserialize("str", response.headers.get("Schema-Name")) + response_headers["Schema-Version"] = self._deserialize("int", response.headers.get("Schema-Version")) + + if cls: + return cls(pipeline_response, None, response_headers) # type: ignore + + @distributed_trace_async + async def _register_schema( # pylint: disable=inconsistent-return-statements + self, group_name: str, schema_name: str, schema_content: bytes, **kwargs: Any + ) -> None: + """Register new schema. + + Register new schema. If schema of specified name does not exist in specified group, schema is + created at version 1. If schema of specified name exists already in specified group, schema is + created at latest version + 1. + + :param group_name: Name of schema group. Required. + :type group_name: str + :param schema_name: Name of schema. Required. + :type schema_name: str + :param schema_content: String representation (UTF-8) of the schema. Required. + :type schema_content: bytes + :return: None + :rtype: None + :raises ~azure.core.exceptions.HttpResponseError: + """ + error_map: MutableMapping[int, Type[HttpResponseError]] = { # pylint: disable=unsubscriptable-object + 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 {} + + content_type: str = kwargs.pop("content_type") + cls: ClsType[None] = kwargs.pop("cls", None) + + _content = schema_content + + _request = build_schema_registry_register_schema_request( + group_name=group_name, + schema_name=schema_name, + content_type=content_type, + api_version=self._config.api_version, + content=_content, + headers=_headers, + params=_params, + ) + path_format_arguments = { + "fullyQualifiedNamespace": self._serialize.url( + "self._config.fully_qualified_namespace", self._config.fully_qualified_namespace, "str", skip_quote=True + ), + } + _request.url = self._client.format_url(_request.url, **path_format_arguments) + + _stream = False + 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 [204]: + map_error(status_code=response.status_code, response=response, error_map=error_map) + raise HttpResponseError(response=response) + + response_headers = {} + response_headers["Location"] = self._deserialize("str", response.headers.get("Location")) + response_headers["Schema-Id"] = self._deserialize("str", response.headers.get("Schema-Id")) + response_headers["Schema-Id-Location"] = self._deserialize("str", response.headers.get("Schema-Id-Location")) + response_headers["Schema-Group-Name"] = self._deserialize("str", response.headers.get("Schema-Group-Name")) + response_headers["Schema-Name"] = self._deserialize("str", response.headers.get("Schema-Name")) + response_headers["Schema-Version"] = self._deserialize("int", response.headers.get("Schema-Version")) + + if cls: + return cls(pipeline_response, None, response_headers) # type: ignore diff --git a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/_operations/_patch.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/_operations/_patch.py index a5a2bd6d1903..f7dd32510333 100644 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/_operations/_patch.py +++ b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/_operations/_patch.py @@ -6,207 +6,9 @@ Follow our quickstart for examples: https://aka.ms/azsdk/python/dpcodegen/python/customize """ -from typing import List, IO, Any, TYPE_CHECKING -from azure.core.tracing.decorator_async import distributed_trace_async +from typing import List -from azure.core.exceptions import ( - ClientAuthenticationError, - HttpResponseError, - ResourceExistsError, - ResourceNotFoundError, - ResourceNotModifiedError, - map_error, -) -from azure.core.utils import case_insensitive_dict -from ..._operations._patch import ( - build_schema_registry_get_schema_properties_by_content_request, - build_schema_registry_register_schema_request, -) -from ._operations import ( - ClsType, - SchemaRegistryClientOperationsMixin as GeneratedClientOperationsMixin, -) - -if TYPE_CHECKING: - from azure.core.pipeline import PipelineResponse - - -class SchemaRegistryClientOperationsMixin(GeneratedClientOperationsMixin): - """ - Generated client to add custom methods. - """ - - @distributed_trace_async - async def _get_schema_properties_by_content( # pylint: disable=inconsistent-return-statements - self, group_name: str, schema_name: str, schema_content: IO, **kwargs: Any - ) -> None: - """Get properties for existing schema. - - Gets the properties referencing an existing schema within the specified schema group, as matched by - schema content comparison. - - :param group_name: Name of schema group. Required. - :type group_name: str - :param schema_name: Name of schema. Required. - :type schema_name: str - :param schema_content: String representation (UTF-8) of the registered schema. Required. - :type schema_content: IO - :keyword content_type: The content type for given schema. Default value is "text/plain; - charset=utf-8". - :paramtype content_type: str - :keyword bool stream: Whether to stream the response of this operation. Defaults to False. You - will have to context manage the returned stream. - :return: None - :rtype: None - :raises ~azure.core.exceptions.HttpResponseError: - """ - error_map = { - 401: ClientAuthenticationError, - 404: ResourceNotFoundError, - 409: ResourceExistsError, - 304: ResourceNotModifiedError, - } - error_map.update(kwargs.pop("error_map", {}) or {}) - - _headers = case_insensitive_dict(kwargs.pop("headers", {}) or {}) - _params = kwargs.pop("params", {}) or {} - - content_type: str = kwargs.pop("content_type", _headers.pop("Content-Type", "text/plain; charset=utf-8")) - cls: ClsType[None] = kwargs.pop("cls", None) - - _content = schema_content - - request = build_schema_registry_get_schema_properties_by_content_request( - group_name=group_name, - schema_name=schema_name, - content_type=content_type, - api_version=self._config.api_version, - content=_content, - headers=_headers, - params=_params, - ) - path_format_arguments = { - "fullyQualifiedNamespace": self._serialize.url( - "self._config.fully_qualified_namespace", - self._config.fully_qualified_namespace, - "str", - skip_quote=True, - ), - } - request.url = self._client.format_url(request.url, **path_format_arguments) - - _stream = kwargs.pop("stream", False) - 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 [204]: - 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["Location"] = self._deserialize("str", response.headers.get("Location")) - response_headers["Schema-Id"] = self._deserialize("str", response.headers.get("Schema-Id")) - response_headers["Schema-Id-Location"] = self._deserialize("str", response.headers.get("Schema-Id-Location")) - response_headers["Schema-Group-Name"] = self._deserialize("str", response.headers.get("Schema-Group-Name")) - response_headers["Schema-Name"] = self._deserialize("str", response.headers.get("Schema-Name")) - response_headers["Schema-Version"] = self._deserialize("int", response.headers.get("Schema-Version")) - - if cls: - return cls(pipeline_response, None, response_headers) - - @distributed_trace_async - async def _register_schema( # pylint: disable=inconsistent-return-statements - self, group_name: str, schema_name: str, content: IO, **kwargs: Any - ) -> None: - """Register new schema. - - Register new schema. If schema of specified name does not exist in specified group, schema is - created at version 1. If schema of specified name exists already in specified group, schema is - created at latest version + 1. - - :param group_name: Name of schema group. Required. - :type group_name: str - :param schema_name: Name of schema. Required. - :type schema_name: str - :param content: String representation (UTF-8) of the schema. Required. - :type content: IO - :keyword content_type: The content type for given schema. Default value is "text/plain; - charset=utf-8". - :paramtype content_type: str - :keyword bool stream: Whether to stream the response of this operation. Defaults to False. You - will have to context manage the returned stream. - :return: None - :rtype: None - :raises ~azure.core.exceptions.HttpResponseError: - """ - error_map = { - 401: ClientAuthenticationError, - 404: ResourceNotFoundError, - 409: ResourceExistsError, - 304: ResourceNotModifiedError, - } - error_map.update(kwargs.pop("error_map", {}) or {}) - - _headers = case_insensitive_dict(kwargs.pop("headers", {}) or {}) - _params = kwargs.pop("params", {}) or {} - - content_type: str = kwargs.pop("content_type", _headers.pop("Content-Type", "text/plain; charset=utf-8")) - cls: ClsType[None] = kwargs.pop("cls", None) - - _content = content - - request = build_schema_registry_register_schema_request( - group_name=group_name, - schema_name=schema_name, - content_type=content_type, - api_version=self._config.api_version, - content=_content, - headers=_headers, - params=_params, - ) - path_format_arguments = { - "fullyQualifiedNamespace": self._serialize.url( - "self._config.fully_qualified_namespace", - self._config.fully_qualified_namespace, - "str", - skip_quote=True, - ), - } - request.url = self._client.format_url(request.url, **path_format_arguments) - - _stream = kwargs.pop("stream", False) - 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 [204]: - 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["Location"] = self._deserialize("str", response.headers.get("Location")) - response_headers["Schema-Id"] = self._deserialize("str", response.headers.get("Schema-Id")) - response_headers["Schema-Id-Location"] = self._deserialize("str", response.headers.get("Schema-Id-Location")) - response_headers["Schema-Group-Name"] = self._deserialize("str", response.headers.get("Schema-Group-Name")) - response_headers["Schema-Name"] = self._deserialize("str", response.headers.get("Schema-Name")) - response_headers["Schema-Version"] = self._deserialize("int", response.headers.get("Schema-Version")) - - if cls: - return cls(pipeline_response, None, response_headers) - - -__all__: List[str] = [ - "SchemaRegistryClientOperationsMixin" -] # 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/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/_patch.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/_patch.py index e1a859567cce..f7dd32510333 100644 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/_patch.py +++ b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/_patch.py @@ -6,346 +6,9 @@ Follow our quickstart for examples: https://aka.ms/azsdk/python/dpcodegen/python/customize """ -from __future__ import annotations -from typing import ( - List, - Any, - TYPE_CHECKING, - Union, - overload, - cast, - IO, - Dict, -) -from functools import partial -from typing_extensions import Self +from typing import List -from azure.core.tracing.decorator_async import distributed_trace_async - -from .._patch import ( - get_http_request_kwargs, - get_case_insensitive_format, - get_content_type, - Schema, - SchemaProperties, - prepare_schema_result, - prepare_schema_properties_result, -) -from ..models._patch import SchemaFormat -from ._client import SchemaRegistryClient as GeneratedServiceClient - -if TYPE_CHECKING: - from azure.core.credentials_async import AsyncTokenCredential - from azure.core.rest import AsyncHttpResponse - from .._patch import SchemaPropertiesDict - - -###### Wrapper Class ###### - - -class SchemaRegistryClient: - """ - SchemaRegistryClient is a client for registering and retrieving schemas from the Azure Schema Registry service. - - :param str fully_qualified_namespace: The Schema Registry service fully qualified host name. - For example: my-namespace.servicebus.windows.net. - :param credential: To authenticate managing the entities of the SchemaRegistry namespace. - :type credential: ~azure.core.credentials_async.AsyncTokenCredential - :keyword str api_version: The Schema Registry service API version to use for requests. - Default value is "2022-10". - - .. admonition:: Example: - - .. literalinclude:: ../samples/async_samples/sample_code_schemaregistry_async.py - :start-after: [START create_sr_client_async] - :end-before: [END create_sr_client_async] - :language: python - :dedent: 4 - :caption: Create a new instance of the SchemaRegistryClient. - - """ - - def __init__( - self, - fully_qualified_namespace: str, - credential: "AsyncTokenCredential", - **kwargs: Any, - ) -> None: - # using composition (not inheriting from generated client) to allow - # calling different operations conditionally within one method - self._generated_client = GeneratedServiceClient( - fully_qualified_namespace=fully_qualified_namespace, - credential=credential, - **kwargs, - ) - - async def __aenter__(self) -> Self: - await self._generated_client.__aenter__() - return self - - async def __aexit__(self, *args: Any) -> None: - await self._generated_client.__aexit__(*args) - - async def close(self) -> None: - """This method is to close the sockets opened by the client. - It need not be used when using with a context manager. - """ - await self._generated_client.close() - - @distributed_trace_async - async def register_schema( - self, - group_name: str, - name: str, - definition: str, - format: Union[str, SchemaFormat], # pylint:disable=redefined-builtin - **kwargs: Any, - ) -> SchemaProperties: - """ - Register new schema. If schema of specified name does not exist in specified group, - schema is created at version 1. If schema of specified name exists already in specified group, - schema is created at latest version + 1. - - :param str group_name: Schema group under which schema should be registered. - :param str name: Name of schema being registered. - :param str definition: String representation of the schema being registered. - :param format: Format for the schema being registered. - :type format: Union[str, ~azure.schemaregistry.SchemaFormat] - :return: The SchemaProperties associated with the registered schema. - :rtype: ~azure.schemaregistry.SchemaProperties - :raises: :class:`~azure.core.exceptions.HttpResponseError` - - .. admonition:: Example: - - .. literalinclude:: ../samples/async_samples/sample_code_schemaregistry_async.py - :start-after: [START register_schema_async] - :end-before: [END register_schema_async] - :language: python - :dedent: 4 - :caption: Register a new schema. - - """ - format = get_case_insensitive_format(format) - http_request_kwargs = get_http_request_kwargs(kwargs) - # ignoring return type because the generated client operations are not annotated w/ cls return type - schema_properties: Dict[str, Union[int, str]] = ( - await self._generated_client._register_schema( # type: ignore # pylint:disable=protected-access - group_name=group_name, - schema_name=name, - content=cast(IO[Any], definition), - content_type=kwargs.pop("content_type", get_content_type(format)), - cls=partial(prepare_schema_properties_result, format), - **http_request_kwargs, - ) - ) - - properties = cast("SchemaPropertiesDict", schema_properties) - return SchemaProperties(**properties) - - @overload - async def get_schema(self, schema_id: str, **kwargs: Any) -> Schema: - """Gets a registered schema. - - To get a registered schema by its unique ID, pass the `schema_id` parameter and any optional - keyword arguments. Azure Schema Registry guarantees that ID is unique within a namespace. - - WARNING: If retrieving a schema format that is unsupported by this client version, upgrade to a client - version that supports the schema format. Otherwise, the content MIME type string will be returned as - the `format` value in the `properties` of the returned Schema. - - :param str schema_id: References specific schema in registry namespace. Required if `group_name`, - `name`, and `version` are not provided. - :return: The schema stored in the registry associated with the provided arguments. - :rtype: ~azure.schemaregistry.Schema - :raises: :class:`~azure.core.exceptions.HttpResponseError` - - .. admonition:: Example: - - .. literalinclude:: ../samples/async_samples/sample_code_schemaregistry_async.py - :start-after: [START get_schema_async] - :end-before: [END get_schema_async] - :language: python - :dedent: 4 - :caption: Get schema by id. - - """ - ... - - @overload - async def get_schema(self, *, group_name: str, name: str, version: int, **kwargs: Any) -> Schema: - """Gets a registered schema. - - To get a specific version of a schema within the specified schema group, pass in the required - keyword arguments `group_name`, `name`, and `version` and any optional keyword arguments. - - WARNING: If retrieving a schema format that is unsupported by this client version, upgrade to a client - version that supports the schema format. Otherwise, the content MIME type string will be returned as - the `format` value in the `properties` of the returned Schema. - - :keyword str group_name: Name of schema group that contains the registered schema. - :keyword str name: Name of schema which should be retrieved. - :keyword int version: Version of schema which should be retrieved. - :return: The schema stored in the registry associated with the provided arguments. - :rtype: ~azure.schemaregistry.Schema - :raises: :class:`~azure.core.exceptions.HttpResponseError` - - .. admonition:: Example: - - .. literalinclude:: ../samples/async_samples/sample_code_schemaregistry_async.py - :start-after: [START get_schema_by_version_async] - :end-before: [END get_schema_by_version_async] - :language: python - :dedent: 4 - :caption: Get schema by version. - """ - ... - - @distributed_trace_async - async def get_schema( # pylint: disable=docstring-missing-param,docstring-should-be-keyword - self, *args: str, **kwargs: Any - ) -> Schema: - """Gets a registered schema. There are two ways to call this method: - - 1) To get a registered schema by its unique ID, pass the `schema_id` parameter and any optional - keyword arguments. Azure Schema Registry guarantees that ID is unique within a namespace. - - 2) To get a specific version of a schema within the specified schema group, pass in the required - keyword arguments `group_name`, `name`, and `version` and any optional keyword arguments. - - WARNING: If retrieving a schema format that is unsupported by this client version, upgrade to a client - version that supports the schema format. Otherwise, the content MIME type string will be returned as - the `format` value in the `properties` of the returned Schema. - - :param str schema_id: References specific schema in registry namespace. Required if `group_name`, - `name`, and `version` are not provided. - :keyword str group_name: Name of schema group that contains the registered schema. - :keyword str name: Name of schema which should be retrieved. - :keyword int version: Version of schema which should be retrieved. - :return: The schema stored in the registry associated with the provided arguments. - :rtype: ~azure.schemaregistry.Schema - :raises: :class:`~azure.core.exceptions.HttpResponseError` - - .. admonition:: Example: - - .. literalinclude:: ../samples/async_samples/sample_code_schemaregistry_async.py - :start-after: [START get_schema_async] - :end-before: [END get_schema_async] - :language: python - :dedent: 4 - :caption: Get schema by id. - - .. literalinclude:: ../samples/async_samples/sample_code_schemaregistry_async.py - :start-after: [START get_schema_by_version_async] - :end-before: [END get_schema_by_version_async] - :language: python - :dedent: 4 - :caption: Get schema by version. - """ - http_request_kwargs = get_http_request_kwargs(kwargs) - http_response: "AsyncHttpResponse" - schema_properties: Dict[str, Union[int, str]] - try: - # Check positional args for schema_id. - # Else, check if schema_id was passed in with keyword. - try: - schema_id = args[0] - except IndexError: - schema_id = kwargs.pop("schema_id") - schema_id = cast(str, schema_id) - # ignoring return type because the generated client operations are not annotated w/ cls return type - ( - http_response, - schema_properties, - ) = await self._generated_client._get_schema_by_id( # type: ignore # pylint:disable=protected-access - id=schema_id, - cls=prepare_schema_result, - headers={ # TODO: remove when multiple content types are supported - "Accept": """application/json; serialization=Avro, application/json; """ - """serialization=json, text/plain; charset=utf-8""" - }, - stream=True, - **http_request_kwargs, - ) - - except KeyError: - # If group_name, name, and version aren't passed in as kwargs, raise error. - try: - group_name = kwargs.pop("group_name") - name = kwargs.pop("name") - version = kwargs.pop("version") - except KeyError: - raise TypeError( # pylint:disable=raise-missing-from - """Missing required argument(s). Specify either `schema_id` """ - """or `group_name`, `name`, `version.""" - ) - # ignoring return type because the generated client operations are not annotated w/ cls return type - http_response, schema_properties = await self._generated_client._get_schema_by_version( # type: ignore # pylint:disable=protected-access - group_name=group_name, - schema_name=name, - schema_version=version, - cls=prepare_schema_result, - headers={ # TODO: remove when multiple content types are supported - "Accept": """application/json; serialization=Avro, application/json; """ - """serialization=json, text/plain; charset=utf-8""" - }, - stream=True, - **http_request_kwargs, - ) - - await http_response.read() - properties = cast("SchemaPropertiesDict", schema_properties) - return Schema( - definition=http_response.text(), - properties=SchemaProperties(**properties), - ) - - @distributed_trace_async - async def get_schema_properties( - self, - group_name: str, - name: str, - definition: str, - format: Union[str, SchemaFormat], # pylint:disable=redefined-builtin - **kwargs: Any, - ) -> SchemaProperties: - """ - Gets the schema properties corresponding to an existing schema within the specified schema group, - as matched by schema defintion comparison. - - :param str group_name: Schema group under which schema should be registered. - :param str name: Name of schema for which properties should be retrieved. - :param str definition: String representation of the schema for which properties should be retrieved. - :param format: Format for the schema for which properties should be retrieved. - :type format: Union[str, SchemaFormat] - :return: The SchemaProperties associated with the provided schema metadata. - :rtype: ~azure.schemaregistry.SchemaProperties - :raises: :class:`~azure.core.exceptions.HttpResponseError` - - .. admonition:: Example: - - .. literalinclude:: ../samples/async_samples/sample_code_schemaregistry_async.py - :start-after: [START get_schema_id_async] - :end-before: [END get_schema_id_async] - :language: python - :dedent: 4 - :caption: Get schema by id. - - """ - format = get_case_insensitive_format(format) - http_request_kwargs = get_http_request_kwargs(kwargs) - # ignoring return type because the generated client operations are not annotated w/ cls return type - schema_properties: Dict[str, Union[int, str]] = ( - await self._generated_client._get_schema_properties_by_content( # type: ignore # pylint:disable=protected-access - group_name=group_name, - schema_name=name, - schema_content=cast(IO[Any], definition), - content_type=kwargs.pop("content_type", get_content_type(format)), - cls=partial(prepare_schema_properties_result, format), - **http_request_kwargs, - ) - ) - properties = cast("SchemaPropertiesDict", schema_properties) - return SchemaProperties(**properties) +__all__: List[str] = [] # Add all objects you want publicly available to users at this package level def patch_sdk(): @@ -355,8 +18,3 @@ def patch_sdk(): you can't accomplish using the techniques described in https://aka.ms/azsdk/python/dpcodegen/python/customize """ - - -__all__: List[str] = [ - "SchemaRegistryClient", -] # Add all objects you want publicly available to users at this package level diff --git a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/_vendor.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/_vendor.py index 33c8f3794fbf..abb32a43b97d 100644 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/_vendor.py +++ b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/aio/_vendor.py @@ -24,14 +24,3 @@ class SchemaRegistryClientMixinABC(ABC): _config: SchemaRegistryClientConfiguration _serialize: "Serializer" _deserialize: "Deserializer" - - -def raise_if_not_implemented(cls, abstract_methods): - not_implemented = [f for f in abstract_methods if not callable(getattr(cls, f, None))] - if not_implemented: - raise NotImplementedError( - "The following methods on operation group '{}' are not implemented: '{}'." - " Please refer to https://aka.ms/azsdk/python/dpcodegen/python/customize to learn how to customize.".format( - cls.__name__, "', '".join(not_implemented) - ) - ) diff --git a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/__init__.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/__init__.py deleted file mode 100644 index 80f86cb969ec..000000000000 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/__init__.py +++ /dev/null @@ -1,26 +0,0 @@ -# -------------------------------------------------------------------------- -# -# Copyright (c) Microsoft Corporation. All rights reserved. -# -# The MIT License (MIT) -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the ""Software""), to -# deal in the Software without restriction, including without limitation the -# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -# sell copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS -# IN THE SOFTWARE. -# -# -------------------------------------------------------------------------- -__path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore diff --git a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/__init__.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/__init__.py deleted file mode 100644 index b3bae0b73832..000000000000 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/__init__.py +++ /dev/null @@ -1,36 +0,0 @@ -# -------------------------------------------------------------------------- -# -# Copyright (c) Microsoft Corporation. All rights reserved. -# -# The MIT License (MIT) -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the ""Software""), to -# deal in the Software without restriction, including without limitation the -# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -# sell copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS -# IN THE SOFTWARE. -# -# -------------------------------------------------------------------------- -from ._version import VERSION # pylint: disable=import-error - -__version__ = VERSION - -from ._schema_registry_json_encoder import ( - JsonSchemaEncoder, -) # pylint: disable=import-error -from ._exceptions import InvalidContentError # pylint: disable=import-error - - -__all__ = ["JsonSchemaEncoder", "InvalidContentError"] diff --git a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/_constants.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/_constants.py deleted file mode 100644 index 6ae93df1712d..000000000000 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/_constants.py +++ /dev/null @@ -1,27 +0,0 @@ -# -------------------------------------------------------------------------- -# -# Copyright (c) Microsoft Corporation. All rights reserved. -# -# The MIT License (MIT) -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the ""Software""), to -# deal in the Software without restriction, including without limitation the -# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -# sell copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS -# IN THE SOFTWARE. -# -# -------------------------------------------------------------------------- - -JSON_MIME_TYPE = "application/json;serialization=Json" diff --git a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/_exceptions.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/_exceptions.py deleted file mode 100644 index d301978eed94..000000000000 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/_exceptions.py +++ /dev/null @@ -1,44 +0,0 @@ -# -------------------------------------------------------------------------- -# -# Copyright (c) Microsoft Corporation. All rights reserved. -# -# The MIT License (MIT) -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the ""Software""), to -# deal in the Software without restriction, including without limitation the -# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -# sell copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS -# IN THE SOFTWARE. -# -# -------------------------------------------------------------------------- -from typing import Any, Dict, Optional - - -class InvalidContentError(ValueError): - """Error during encoding or decoding content with a schema. - - :param str message: The message object stringified as 'message' attribute - :keyword dict[str, str] or None details: The error details. Depending on the error, this may include - information like: `schema_id`, `schema_definition`, `message_content`. - - :ivar str message: A stringified version of the message parameter - :ivar dict[str, str] details: The error details. Depending on the error, this may include - information like: `schema_id`, `schema_definition`, `message_content`. - """ - - def __init__(self, message: str, *args: Any, details: Optional[Dict[str, str]] = None) -> None: - self.message: str = str(message) - self.details: Dict[str, str] = details or {} - super(InvalidContentError, self).__init__(self.message, *args) diff --git a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/_schema_registry_json_encoder.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/_schema_registry_json_encoder.py deleted file mode 100644 index 27656864d4b3..000000000000 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/_schema_registry_json_encoder.py +++ /dev/null @@ -1,386 +0,0 @@ -# -------------------------------------------------------------------------- -# -# Copyright (c) Microsoft Corporation. All rights reserved. -# -# The MIT License (MIT) -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the ""Software""), to -# deal in the Software without restriction, including without limitation the -# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -# sell copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS -# IN THE SOFTWARE. -# -# -------------------------------------------------------------------------- -from __future__ import annotations -import logging -import json -from functools import lru_cache -from typing import ( - TYPE_CHECKING, - Any, - cast, - Dict, - Mapping, - Optional, - Type, - overload, - Union, -) -from ..._patch import SchemaFormat -from ._utils import ( # pylint: disable=import-error - create_message_content, - parse_message, - decode_content, - get_loaded_schema, - get_jsonschema_validator, - InboundMessageContent, - OutboundMessageContent, -) - -if TYPE_CHECKING: - import azure.schemaregistry - from ..._patch import MessageContent, SchemaContentValidate - -_LOGGER = logging.getLogger(__name__) - - -class JsonSchemaEncoder(object): - """ - JsonSchemaEncoder provides the ability to encode and decode content according to the given JSON schema. - It will check the registry for the pre-registered schema and cache the schema locally. - - :keyword client: Required. The schema registry client which is used to retrieve schema from the service. - :paramtype client: ~azure.schemaregistry.SchemaRegistryClient - :keyword validate: Required. Used for validation in encode and decode. - If a JSON Schema meta-schema `$schema` string is provided, for example - `"https://json-schema.org/draft/2020-12/schema"`, the corresponding validator from `jsonschema` will be used. - In this case, `jsonschema` must be installed with `jsonencoder` extras: - `pip install azure-schemaregistry[jsonencoder]`. For a list of supported `jsonschema` validators, - please refer to the `jsonschema` documentation: - https://python-jsonschema.readthedocs.io/en/stable/api/jsonschema/validators/index.html - If a callable is provided, the schema and content will be passed in for validation. - :paramtype validate: str or ~azure.schemaregistry.SchemaContentValidate - :keyword Optional[str] group_name: Schema group under which schema should be registered. - Required if `schema`, not `schema_id`, is provided to `encode`. - """ - - def __init__( - self, - *, - client: "azure.schemaregistry.SchemaRegistryClient", - validate: Union[str, "SchemaContentValidate"], - group_name: Optional[str] = None, - ) -> None: - self._schema_registry_client = client - if isinstance(validate, str): - self._validate = get_jsonschema_validator(validate) - else: - self._validate = validate - self._schema_group = group_name - - def __enter__(self) -> "JsonSchemaEncoder": - self._schema_registry_client.__enter__() - return self - - def __exit__(self, *exc_details: Any) -> None: - self._schema_registry_client.__exit__(*exc_details) - - def close(self) -> None: - """This method is to close the sockets opened by the client. - It need not be used when using with a context manager. - """ - self._schema_registry_client.close() - - @lru_cache(maxsize=128) - def _get_schema_id(self, schema_name: str, schema_str: str, **kwargs: Any) -> str: - """ - Get schema id from local cache with the given schema. - If there is no item in the local cache, get schema id from the service and cache it. - - :param schema_name: Name of the schema - :type schema_name: str - :param str schema_str: Schema string - :return: Schema Id - :rtype: str - """ - schema_id = self._schema_registry_client.get_schema_properties( - cast(str, self._schema_group), - schema_name, - schema_str, - SchemaFormat.JSON.value, - **kwargs, - ).id - return schema_id - - @lru_cache(maxsize=128) - def _get_schema(self, schema_id: str, **kwargs: Any) -> str: - """ - Get schema content from local cache with the given schema id. - If there is no item in the local cache, get schema from the service and cache it. - - :param str schema_id: Schema id - :return: Schema content - :rtype: str - """ - schema_str = self._schema_registry_client.get_schema(schema_id, **kwargs).definition - return schema_str - - @overload - def encode( - self, - content: Mapping[str, Any], - *, - schema: str, - message_type: Type[OutboundMessageContent], - request_options: Optional[Dict[str, Any]] = None, - **kwargs: Any, - ) -> OutboundMessageContent: - """Encodes content after validating against the pre-registered JSON schema. Encoded content and content - type will be passed to the provided OutboundMessageContent class to create the message object. - - If `message_type` is set, then additional keyword arguments for building the OutboundMessageContent object - will be passed to the OutboundMessageContent.from_message_content() method. - - :param content: The content to be encoded. - :type content: mapping[str, any] - :keyword schema: Required. The pre-registered schema used to validate the content. `schema_id` - must not be passed. - :paramtype schema: str - :keyword message_type: The message class to construct the message. Must be a subtype of the - azure.schemaregistry.OutboundMessageContent protocol. - :paramtype message_type: type[OutboundMessageContent] - :keyword request_options: The keyword arguments for http requests to be passed to the client. - :paramtype request_options: dict[str, any] or None - :returns: The OutboundMessageContent object with encoded content and content type. - :rtype: OutboundMessageContent - :raises: ~azure.schemaregistry.encoder.InvalidContentError if there is an issue with encoding content - or validating it against the schema. - :raises: ~azure.core.exceptions.HttpResponseError if there is an issue with the request to get the schema - from the registry. - """ - - @overload - def encode( - self, - content: Mapping[str, Any], - *, - schema_id: str, - message_type: Type[OutboundMessageContent], - request_options: Optional[Dict[str, Any]] = None, - **kwargs: Any, - ) -> OutboundMessageContent: - """Encodes content after validating against the pre-registered JSON schema corresponding to - the provided schema ID. Encoded content and content type will be passed to the provided - OutboundMessageContent class to create message object. - - If `message_type` is set, then additional keyword arguments for building the OutboundMessageContent object - will be passed to the OutboundMessageContent.from_message_content() method. - - :param content: The content to be encoded. - :type content: mapping[str, any] - :keyword schema_id: Required. The schema ID corresponding to the pre-registered schema to be used - for validation. `schema` must not be passed. - :paramtype schema_id: str - :keyword message_type: The message class to construct the message. Must be a subtype of the - azure.schemaregistry.OutboundMessageContent protocol. - :paramtype message_type: type[OutboundMessageContent] - :keyword request_options: The keyword arguments for http requests to be passed to the client. - :paramtype request_options: dict[str, any] or None - :returns: The OutboundMessageContent object with encoded content and content type. - :rtype: OutboundMessageContent - :raises: ~azure.schemaregistry.encoder.InvalidContentError if there is an issue with encoding content - or validating it against the schema. - :raises: ~azure.core.exceptions.HttpResponseError if there is an issue with the request to get the schema - from the registry. - """ - - @overload - def encode( - self, - content: Mapping[str, Any], - *, - schema: str, - request_options: Optional[Dict[str, Any]] = None, - **kwargs: Any, - ) -> "MessageContent": - """Encodes content after validating against the pre-registered schema. The following dict will be returned: - {"content": JSON encoded value, "content_type": JSON mime type string + schema ID}. - - :param content: The content to be encoded. - :type content: mapping[str, any] - :keyword schema: Required. The pre-registered schema used to validate the content. `schema_id` - must not be passed. - :paramtype schema: str - :keyword request_options: The keyword arguments for http requests to be passed to the client. - :paramtype request_options: dict[str, any] or None - :returns: TypedDict of encoded content and content type. - :rtype: MessageContent - :raises: ~azure.schemaregistry.encoder.InvalidContentError if there is an issue with encoding content - or validating it against the schema. - :raises: ~azure.core.exceptions.HttpResponseError if there is an issue with the request to get the schema - from the registry. - """ - - @overload - def encode( - self, - content: Mapping[str, Any], - *, - schema_id: str, - request_options: Optional[Dict[str, Any]] = None, - **kwargs: Any, - ) -> "MessageContent": - """Encodes content after validating against the pre-registered schema corresponding to - the provided schema ID. The following dict will be returned: - {"content": JSON encoded value, "content_type": JSON mime type string + schema ID}. - - :param content: The content to be encoded. - :type content: mapping[str, any] - :keyword schema: None. - :paramtype schema: None - :keyword schema_id: Required. The schema ID corresponding to the pre-registered schema to be used - for validation. `schema` must not be passed. - :paramtype schema_id: str - :keyword message_type: None. - :paramtype message_type: None - :keyword request_options: The keyword arguments for http requests to be passed to the client. - :paramtype request_options: dict[str, any] or None - :returns: TypedDict of encoded content and content type. - :rtype: MessageContent - :raises: ~azure.schemaregistry.encoder.InvalidContentError if there is an issue with encoding content - or validating it against the schema. - :raises: ~azure.core.exceptions.HttpResponseError if there is an issue with the request to get the schema - from the registry. - """ - - def encode( - self, - content: Mapping[str, Any], - *, - schema: Optional[str] = None, - schema_id: Optional[str] = None, - message_type: Optional[Type[OutboundMessageContent]] = None, - request_options: Optional[Dict[str, Any]] = None, - **kwargs: Any, - ) -> Union[OutboundMessageContent, "MessageContent"]: - """Encodes content after validating against the provided pre-registered schema or the one corresponding to - the provided schema ID. If provided with a OutboundMessageContent subtype, encoded content and - content type will be passed to create message object. If not provided, the following dict will be returned: - {"content": JSON encoded value, "content_type": JSON mime type string + schema ID}. - - If `message_type` is set, then additional keyword arguments for building OutboundMessageContent will be - passed to the OutboundMessageContent.from_message_content() method. - - :param content: The content to be encoded. - :type content: mapping[str, any] - :keyword schema: The pre-registered schema used to validate the content. Exactly one of - `schema` or `schema_id` must be passed. - :paramtype schema: str or None - :keyword schema_id: The schema ID corresponding to the pre-registered schema to be used - for validation. Exactly one of `schema` or `schema_id` must be passed. - :paramtype schema_id: str or None - :keyword message_type: The message class to construct the message. If passed, must be a subtype of the - azure.schemaregistry.OutboundMessageContent protocol. - :paramtype message_type: type[OutboundMessageContent] or None - :keyword request_options: The keyword arguments for http requests to be passed to the client. - :paramtype request_options: dict[str, any] or None - :returns: TypedDict of encoded content and content type if `message_type` is not set, otherwise the - constructed message object. - :rtype: OutboundMessageContent or MessageContent - :raises: ~azure.schemaregistry.encoder.InvalidContentError if there is an issue with encoding content - or validating it against the schema. - :raises: ~azure.core.exceptions.HttpResponseError if there is an issue with the request to get the schema - from the registry. - """ - request_options = request_options or {} - - # If schema_id, get schema for validation. If schema, get schema_id for content type. - if schema_id and not schema: - cache_misses = self._get_schema.cache_info().misses # pylint: disable=no-value-for-parameter - schema_str = self._get_schema(schema_id, **request_options) - new_cache_misses = self._get_schema.cache_info().misses # pylint: disable=no-value-for-parameter - if new_cache_misses > cache_misses: - cache_info = self._get_schema.cache_info() # pylint: disable=no-value-for-parameter - _LOGGER.info( - "New entry has been added to schema cache. Cache info: %s", - str(cache_info), - ) - schema_dict = json.loads(schema_str) - elif schema and not schema_id: - if not self._schema_group: - raise TypeError("'group_name' is required in constructor, if 'schema' is passed to encode.") - - schema_fullname, schema_str, schema_dict = get_loaded_schema(schema, content) - cache_misses = self._get_schema_id.cache_info().misses # pylint: disable=no-value-for-parameter - schema_id = self._get_schema_id(schema_fullname, schema_str, **request_options) - new_cache_misses = self._get_schema_id.cache_info().misses # pylint: disable=no-value-for-parameter - if new_cache_misses > cache_misses: - cache_info = self._get_schema_id.cache_info() # pylint: disable=no-value-for-parameter - _LOGGER.info( - "New entry has been added to schema ID cache. Cache info: %s", - str(cache_info), - ) - else: - raise TypeError("Exactly one of 'schema' or 'schema_id' is required.") - - return create_message_content( - content=content, - schema=schema_dict, - schema_id=schema_id, - validate=self._validate, - message_type=message_type, - **kwargs, - ) - - def decode( - self, - message: Union["MessageContent", InboundMessageContent], - *, - request_options: Optional[Dict[str, Any]] = None, - **kwargs: Any, - ) -> Dict[str, Any]: - """Decode bytes content using schema ID in the content type field. - - :param message: The message object which holds the content to be decoded and content type - containing the schema ID. - :type message: InboundMessageContent or MessageContent - :keyword request_options: The keyword arguments for http requests to be passed to the client. - :paramtype request_options: dict[str, any] or None - :returns: The decoded content. - :rtype: dict[str, any] - :raises: ~azure.schemaregistry.encoder.jsonencoder.InvalidContentError if there is - an issue with decoding content or validating it with the schema. - :raises: ~azure.core.exceptions.HttpResponseError if there is an issue with the request to get the schema - from the registry. - """ - schema_id, content = parse_message(message) - cache_misses = self._get_schema.cache_info().misses # pylint: disable=no-value-for-parameter - request_options = request_options or {} - schema_definition = self._get_schema(schema_id, **request_options) - new_cache_misses = self._get_schema.cache_info().misses # pylint: disable=no-value-for-parameter - if new_cache_misses > cache_misses: - cache_info = self._get_schema.cache_info() # pylint: disable=no-value-for-parameter - _LOGGER.info( - "New entry has been added to schema cache. Cache info: %s", - str(cache_info), - ) - - return decode_content( - content=content, - schema_id=schema_id, - schema_definition=schema_definition, - validate=self._validate, - **kwargs, - ) diff --git a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/_utils.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/_utils.py deleted file mode 100644 index 1cae614aeae0..000000000000 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/_utils.py +++ /dev/null @@ -1,261 +0,0 @@ -# -------------------------------------------------------------------------------------------- -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License.txt in the project root for license information. -# -------------------------------------------------------------------------------------------- - -from typing import ( - Any, - Callable, - Optional, - Type, - Union, - cast, - Mapping, - TYPE_CHECKING, - Tuple, - TypeVar, - overload, -) -import json - -try: - import jsonschema -except ImportError: - jsonschema = None - -from functools import partial - - -from ..._patch import ( # pylint: disable=import-error - MessageContent, - InboundMessageContent as InboundMessageContentProtocol, - OutboundMessageContent as OutboundMessageContentProtocol, -) -from ._exceptions import ( # pylint: disable=import-error - InvalidContentError, -) -from ._constants import JSON_MIME_TYPE # pylint: disable=import-error - -if TYPE_CHECKING: - try: - from jsonschema.protocols import Validator - except ImportError: - pass - from ..._patch import SchemaContentValidate - -# TypeVar ties the return type to the exact OutboundMessageContent/InboundMessageContent class, -# rather than the "OutboundMessageContent/InboundMessageContent" Protocol. -# Otherwise, mypy will complain that the return type is not compatible with the type annotation when -# the OutboundMessageContent/InboundMessageContent object is returned and passed around. -InboundMessageContent = TypeVar("InboundMessageContent", bound=InboundMessageContentProtocol) -OutboundMessageContent = TypeVar("OutboundMessageContent", bound=OutboundMessageContentProtocol) - - -def get_jsonschema_validator(draft_identifier: str) -> "Validator": - - # get validator - try: - validator = jsonschema.validators.validator_for({"$schema": draft_identifier}, default=False) - except AttributeError: - raise ValueError( - "To use a provided JSON Schema Validator, please install the " - "package with extras: `pip install azure-schemaregistry[jsonencoder]`." - ) from None - - if not validator: - raise ValueError( - f"{draft_identifier} is not a supported `jsonschema` meta-schema `$schema` value. " - "Please pass in a `$schema` value from the list of supported `jsonschema` validators: " - "https://python-jsonschema.readthedocs.io/en/stable/api/jsonschema/validators/index.html" - ) - - return partial(jsonschema_validate, validator=validator) - - -def jsonschema_validate(validator: "Validator", schema: Mapping[str, Any], content: Mapping[str, Any]) -> None: - """ - Validates content against provided schema using `jsonschema.Draft4Validator`. - If invalid, raises Exception. Else, returns None. - If jsonschema is not installed, raises ValueError. - :param jsonschema.protocols.Validator validator: The validator to use. - :param mapping[str, any] schema: The schema to validate against. - :param mapping[str, any] content: The content to validate. - :return: None - :rtype: None - """ - validator(schema).validate(content) - - -def get_loaded_schema( - schema: Union[str, Callable], - content: Mapping[str, Any], -) -> Tuple[str, str, Mapping[str, Any]]: - """Returns the tuple: (schema name, schema string, schema dict). - :param str or callable schema: The schema to load. - :param mapping[str, any] content: The content to validate. - :return: The schema name, schema string, and schema dict. - :rtype: tuple[str, str, mapping[str, any]] - """ - # get schema string - schema_dict: Mapping[str, Any] - schema_str: str - schema_fullname: str - try: - # str - schema = cast(str, schema) - schema_dict = json.loads(schema) - schema_str = schema - except TypeError: - # callable - schema = cast(Callable, schema) - try: - schema_dict = schema(content) - except Exception as exc: - raise InvalidContentError( - f"Cannot generate schema with callable given the following content: {content}" - ) from exc - schema_str = json.dumps(schema_dict) - - # get schema name for get_schema_properties operation - try: - schema_fullname = schema_dict["title"] - except KeyError: - raise ValueError("Schema must have 'title' property.") from None - - return schema_fullname, schema_str, schema_dict - - -@overload -def create_message_content( - content: Mapping[str, Any], - schema: Mapping[str, Any], - schema_id: str, - validate: "Validator", - message_type: Type[OutboundMessageContent], - **kwargs: Any, -) -> OutboundMessageContent: ... - - -@overload -def create_message_content( - content: Mapping[str, Any], - schema: Mapping[str, Any], - schema_id: str, - validate: "Validator", - message_type: None = None, -) -> MessageContent: ... - - -def create_message_content( - content: Mapping[str, Any], - schema: Mapping[str, Any], - schema_id: str, - validate: Union["Validator", "SchemaContentValidate"], - message_type: Optional[Type[OutboundMessageContent]] = None, - **kwargs: Any, -) -> Union[OutboundMessageContent, MessageContent]: - content_type = f"{JSON_MIME_TYPE}+{schema_id}" - try: - # validate content - validate(schema=schema, content=content) - except Exception as exc: # pylint:disable=broad-except - raise InvalidContentError( - f"Invalid content value '{content}' for the following schema with schema ID {schema_id}:" - f"{json.dumps(schema)}", - details={"schema_id": f"{schema_id}"}, - ) from exc - - try: - content_bytes = json.dumps(content, separators=(",", ":")).encode() - except Exception as exc: - raise InvalidContentError( - f"Cannot encode value '{content}' for the following schema with schema ID {schema_id}:" - f"{json.dumps(schema)}", - details={"schema_id": f"{schema_id}"}, - ) from exc - - if message_type: - try: - return cast( - OutboundMessageContent, - message_type.from_message_content(content_bytes, content_type, **kwargs), - ) - except AttributeError as exc: - raise TypeError( - f"""Cannot set content and content type on model object. The content model - {str(message_type)} must be a subtype of the OutboundMessageContent protocol. - If using an Azure SDK model class, please check the README.md for the full list - of supported Azure SDK models and their corresponding versions.""", - {"content": content_bytes, "content_type": content_type}, - ) from exc - - return MessageContent({"content": content_bytes, "content_type": content_type}) - - -def parse_message(message: Union[InboundMessageContent, MessageContent]): - try: - message = cast("InboundMessageContent", message) - message_content_dict = message.__message_content__() - content = message_content_dict["content"] - content_type = message_content_dict["content_type"] - except AttributeError: - message = cast(MessageContent, message) - try: - content = message["content"] - content_type = message["content_type"] - except (KeyError, TypeError) as exc: - raise TypeError( - f"""The content model {str(message)} must be a subtype of the InboundMessageContent protocol or type - MessageContent. If using an Azure SDK model class, please check the README.md - for the full list of supported Azure SDK models and their corresponding versions.""" - ) from exc - - try: - content_type_parts = content_type.split("+") - if len(content_type_parts) != 2 or content_type_parts[0] != JSON_MIME_TYPE: - raise InvalidContentError( - f"Content type {content_type} was not in the expected format of JSON MIME type + schema ID." - ) - schema_id = content_type_parts[1] - except AttributeError: - raise InvalidContentError( - f"Content type {content_type} was not in the expected format of JSON MIME type + schema ID." - ) from None - - return schema_id, content - - -def decode_content( - content: bytes, - schema_id: str, - schema_definition: str, - validate: Union["Validator", "SchemaContentValidate"], -): - try: - content = json.loads(content) - except Exception as exc: - error_message = f"""Cannot decode value '{content!r}' for schema - with schema ID {schema_id}: {schema_definition}""" - raise InvalidContentError( - error_message, - details={ - "schema_id": f"{schema_id}", - "schema_definition": f"{schema_definition}", - }, - ) from exc - try: - validate( - schema=json.loads(schema_definition), - content=cast(Mapping[str, Any], content), - ) - except Exception as exc: - error_message = f"""Invalid content value '{content!r}' for schema - with schema ID {schema_id}: {schema_definition}""" - raise InvalidContentError( - error_message, - details={ - "schema_id": f"{schema_id}", - "schema_definition": f"{schema_definition}", - }, - ) from exc - return content diff --git a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/_version.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/_version.py deleted file mode 100644 index d316addb36cb..000000000000 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/_version.py +++ /dev/null @@ -1,27 +0,0 @@ -# -------------------------------------------------------------------------- -# -# Copyright (c) Microsoft Corporation. All rights reserved. -# -# The MIT License (MIT) -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the ""Software""), to -# deal in the Software without restriction, including without limitation the -# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -# sell copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS -# IN THE SOFTWARE. -# -# -------------------------------------------------------------------------- - -VERSION = "1.0.0b1" diff --git a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/aio/__init__.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/aio/__init__.py deleted file mode 100644 index 197eac7f2c1b..000000000000 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/aio/__init__.py +++ /dev/null @@ -1,30 +0,0 @@ -# -------------------------------------------------------------------------- -# -# Copyright (c) Microsoft Corporation. All rights reserved. -# -# The MIT License (MIT) -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the ""Software""), to -# deal in the Software without restriction, including without limitation the -# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -# sell copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS -# IN THE SOFTWARE. -# -# -------------------------------------------------------------------------- -from ._schema_registry_json_encoder_async import ( - JsonSchemaEncoder, -) # pylint: disable=import-error - -__all__ = ["JsonSchemaEncoder"] diff --git a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/aio/_async_lru.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/aio/_async_lru.py deleted file mode 100644 index ce4d1936eaa2..000000000000 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/aio/_async_lru.py +++ /dev/null @@ -1,228 +0,0 @@ -# -------------------------------------------------------------------------- -# The MIT License -# -# Copyright (c) 2018 aio-libs team https://github.com/aio-libs/ -# Copyright (c) 2017 Ocean S. A. https://ocean.io/ -# Copyright (c) 2016-2017 WikiBusiness Corporation http://wikibusiness.org/ -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -# THE SOFTWARE. -# -------------------------------------------------------------------------- -# Copying over `async_lru.py`[https://github.com/aio-libs/async-lru/blob/master/async_lru.py] -# from `aio-libs`[https://github.com/aio-libs/async-lru] for the following reasons: -# 1. There has not been an official release of `async_lru` in 2 years. -# 2. The last update to the library was a year ago, so it seems the library is -# not being actively maintained. - -import asyncio -from collections import OrderedDict -from functools import _CacheInfo, _make_key, partial, wraps - - -__version__ = "1.0.2" - -__all__ = ("alru_cache",) - - -def unpartial(fn): - while hasattr(fn, "func"): - fn = fn.func - - return fn - - -def _done_callback(fut, task): - if task.cancelled(): - fut.cancel() - return - - exc = task.exception() - if exc is not None: - fut.set_exception(exc) - return - - fut.set_result(task.result()) - - -def _cache_invalidate(wrapped, typed, *args, **kwargs): - # pylint: disable=protected-access - key = _make_key(args, kwargs, typed) - - exists = key in wrapped._cache - - if exists: - wrapped._cache.pop(key) - - return exists - - -def _cache_clear(wrapped): - # pylint: disable=protected-access - wrapped.hits = wrapped.misses = 0 - wrapped._cache = OrderedDict() - wrapped.tasks = set() - - -def _open(wrapped): - if not wrapped.closed: - raise RuntimeError("alru_cache is not closed") - - # pylint: disable=protected-access - was_closed = wrapped.hits == wrapped.misses == len(wrapped.tasks) == len(wrapped._cache) == 0 - - if not was_closed: - raise RuntimeError("alru_cache was not closed correctly") - - wrapped.closed = False - - -def _close(wrapped, *, cancel=False, return_exceptions=True): - if wrapped.closed: - raise RuntimeError("alru_cache is closed") - - wrapped.closed = True - - if cancel: - for task in wrapped.tasks: - if not task.done(): # not sure is it possible - task.cancel() - - return _wait_closed(wrapped, return_exceptions=return_exceptions) - - -async def _wait_closed(wrapped, *, return_exceptions): - wait_closed = asyncio.gather(*wrapped.tasks, return_exceptions=return_exceptions) - - wait_closed.add_done_callback(partial(_close_waited, wrapped)) - - ret = await wait_closed - - # hack to get _close_waited callback to be executed - await asyncio.sleep(0) - - return ret - - -def _close_waited(wrapped, _): - wrapped.cache_clear() - - -def _cache_info(wrapped, maxsize): - # pylint: disable=protected-access - return _CacheInfo( - wrapped.hits, - wrapped.misses, - maxsize, - len(wrapped._cache), - ) - - -def __cache_touch(wrapped, key): - # pylint: disable=protected-access - try: - wrapped._cache.move_to_end(key) - except KeyError: # not sure is it possible - pass - - -def _cache_hit(wrapped, key): - wrapped.hits += 1 - __cache_touch(wrapped, key) - - -def _cache_miss(wrapped, key): - wrapped.misses += 1 - __cache_touch(wrapped, key) - - -def alru_cache( - fn=None, - maxsize=128, - typed=False, - *, - cache_exceptions=True, -): - def wrapper(fn): - # pylint: disable=protected-access - _origin = unpartial(fn) - - if not asyncio.iscoroutinefunction(_origin): - raise RuntimeError("Coroutine function is required, got {}".format(fn)) - - # functools.partialmethod support - if hasattr(fn, "_make_unbound_method"): - fn = fn._make_unbound_method() - - @wraps(fn) - async def wrapped(*fn_args, **fn_kwargs): - if wrapped.closed: - raise RuntimeError("alru_cache is closed for {}".format(wrapped)) - - loop = asyncio.get_event_loop() - - key = _make_key(fn_args, fn_kwargs, typed) - - fut = wrapped._cache.get(key) - - if fut is not None: - if not fut.done(): - _cache_hit(wrapped, key) - return await asyncio.shield(fut) - - exc = fut._exception - - if exc is None or cache_exceptions: - _cache_hit(wrapped, key) - return fut.result() - - # exception here and cache_exceptions == False - wrapped._cache.pop(key) - - fut = loop.create_future() - task = loop.create_task(fn(*fn_args, **fn_kwargs)) - task.add_done_callback(partial(_done_callback, fut)) - - wrapped.tasks.add(task) - task.add_done_callback(wrapped.tasks.remove) - - wrapped._cache[key] = fut - - if maxsize is not None and len(wrapped._cache) > maxsize: - wrapped._cache.popitem(last=False) - - _cache_miss(wrapped, key) - return await asyncio.shield(fut) - - _cache_clear(wrapped) - wrapped._origin = _origin - wrapped.closed = False - wrapped.cache_info = partial(_cache_info, wrapped, maxsize) - wrapped.cache_clear = partial(_cache_clear, wrapped) - wrapped.invalidate = partial(_cache_invalidate, wrapped, typed) - wrapped.close = partial(_close, wrapped) - wrapped.open = partial(_open, wrapped) - - return wrapped - - if fn is None: - return wrapper - - if callable(fn) or hasattr(fn, "_make_unbound_method"): - return wrapper(fn) - - raise NotImplementedError("{} decorating is not supported".format(fn)) diff --git a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/aio/_schema_registry_json_encoder_async.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/aio/_schema_registry_json_encoder_async.py deleted file mode 100644 index 0da917ecc0fd..000000000000 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/aio/_schema_registry_json_encoder_async.py +++ /dev/null @@ -1,388 +0,0 @@ -# -------------------------------------------------------------------------- -# -# Copyright (c) Microsoft Corporation. All rights reserved. -# -# The MIT License (MIT) -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the ""Software""), to -# deal in the Software without restriction, including without limitation the -# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -# sell copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS -# IN THE SOFTWARE. -# -# -------------------------------------------------------------------------- -from __future__ import annotations -import json -import logging -from typing import ( - TYPE_CHECKING, - Any, - Dict, - Mapping, - Optional, - overload, - Type, - Union, - cast, -) -from ...._patch import SchemaFormat - -from .._utils import ( - create_message_content, - parse_message, - decode_content, - get_loaded_schema, - get_jsonschema_validator, - InboundMessageContent, - OutboundMessageContent, -) -from ._async_lru import alru_cache # pylint: disable=import-error - -if TYPE_CHECKING: - import azure.schemaregistry.aio - from ...._patch import MessageContent, SchemaContentValidate - -_LOGGER = logging.getLogger(__name__) - - -class JsonSchemaEncoder(object): - """ - JsonSchemaEncoder provides the ability to encode and decode content according to the given JSON schema. - It will check the registry for the pre-registered schema and cache the schema locally. - - :keyword client: Required. The schema registry client which is used to retrieve schema from the service. - and retrieve schema from the service. - :paramtype client: ~azure.schemaregistry.aio.SchemaRegistryClient - :keyword validate: Required. Used for validation in encode and decode. - If a JSON Schema meta-schema `$schema` string is provided, for example - `"https://json-schema.org/draft/2020-12/schema"`, the corresponding validator from `jsonschema` will be used. - In this case, `jsonschema` must be installed with `jsonencoder` extras: - `pip install azure-schemaregistry[jsonencoder]`. For a list of supported `jsonschema` validators, - please refer to the `jsonschema` documentation: - https://python-jsonschema.readthedocs.io/en/stable/api/jsonschema/validators/index.html - If a callable is provided, the schema and content will be passed in for validation. - :paramtype validate: str or ~azure.schemaregistry.SchemaContentValidate - :keyword Optional[str] group_name: Schema group under which schema should be registered. - Required if `schema`, not `schema_id`, is provided to `encode`. - """ - - def __init__( - self, - *, - client: "azure.schemaregistry.aio.SchemaRegistryClient", - validate: Union[str, "SchemaContentValidate"], - group_name: Optional[str] = None, - ) -> None: - self._schema_registry_client = client - if isinstance(validate, str): - self._validate = get_jsonschema_validator(validate) - else: - self._validate = validate - self._schema_group = group_name - - async def __aenter__(self) -> "JsonSchemaEncoder": - await self._schema_registry_client.__aenter__() - return self - - async def __aexit__(self, *exc_details: Any) -> None: - await self._schema_registry_client.__aexit__(*exc_details) - - async def close(self) -> None: - """This method is to close the sockets opened by the client. - It need not be used when using with a context manager. - """ - await self._schema_registry_client.close() - - @alru_cache(maxsize=128, cache_exceptions=False) - async def _get_schema_id(self, schema_name: str, schema_str: str, **kwargs: Any) -> str: - """ - Get schema id from local cache with the given schema. - If there is no item in the local cache, get schema id from the service and cache it. - - :param schema_name: Name of the schema - :type schema_name: str - :param str schema_str: Schema string - :return: Schema Id - :rtype: str - """ - schema_properties = await self._schema_registry_client.get_schema_properties( - cast(str, self._schema_group), - schema_name, - schema_str, - SchemaFormat.JSON.value, - **kwargs, - ) - return schema_properties.id - - @alru_cache(maxsize=128, cache_exceptions=False) - async def _get_schema(self, schema_id: str, **kwargs: Any) -> str: - """ - Get schema definition from local cache with the given schema id. - If there is no item in the local cache, get schema from the service and cache it. - - :param str schema_id: Schema id - :return: Schema definition - :rtype: str - """ - schema = await self._schema_registry_client.get_schema(schema_id, **kwargs) - return schema.definition - - @overload - async def encode( - self, - content: Mapping[str, Any], - *, - schema: str, - message_type: Type[OutboundMessageContent], - request_options: Optional[Dict[str, Any]] = None, - **kwargs: Any, - ) -> OutboundMessageContent: - """Encodes content after validating against the pre-registered JSON schema. Encoded content and content - type will be passed to the provided OutboundMessageContent class to create message object. - - If `message_type` is set, then additional keyword arguments for building OutboundMessageContent will - be passed to the OutboundMessageContent.from_message_content() method. - - :param content: The content to be encoded. - :type content: mapping[str, any] - :keyword schema: Required. The pre-registered schema used to validate the content. `schema_id` - must not be passed. - :paramtype schema: str - :keyword message_type: The message class to construct the message. Must be a subtype of the - azure.schemaregistry.OutboundMessageContent protocol. - :paramtype message_type: type[OutboundMessageContent] - :keyword request_options: The keyword arguments for http requests to be passed to the client. - :paramtype request_options: dict[str, any] or None - :returns: The OutboundMessageContent object with encoded content and content type. - :rtype: OutboundMessageContent - :raises: ~azure.schemaregistry.encoder.InvalidContentError if there is an issue with encoding content - or validating it against the schema. - :raises: ~azure.core.exceptions.HttpResponseError if there is an issue with the request to get the schema - from the registry. - """ - - @overload - async def encode( - self, - content: Mapping[str, Any], - *, - schema_id: str, - message_type: Type[OutboundMessageContent], - request_options: Optional[Dict[str, Any]] = None, - **kwargs: Any, - ) -> OutboundMessageContent: - """Encodes content after validating against the pre-registered JSON schema corresponding to - the provided schema ID. Encoded content and content type will be passed to the provided - OutboundMessageContent class to create message object. - - If `message_type` is set, then additional keyword arguments for building OutboundMessageContent - will be passed to the OutboundMessageContent.from_message_content() method. - - :param content: The content to be encoded. - :type content: mapping[str, any] - :keyword schema_id: Required. The schema ID corresponding to the pre-registered schema to be used - for validation. `schema` must not be passed. - :paramtype schema_id: str - :keyword message_type: The message class to construct the message. Must be a subtype of the - azure.schemaregistry.OutboundMessageContent protocol. - :paramtype message_type: type[OutboundMessageContent] - :keyword request_options: The keyword arguments for http requests to be passed to the client. - :paramtype request_options: dict[str, any] or None - :returns: The OutboundMessageContent object with encoded content and content type. - :rtype: OutboundMessageContent - :raises: ~azure.schemaregistry.encoder.InvalidContentError if there is an issue with encoding content - or validating it against the schema. - :raises: ~azure.core.exceptions.HttpResponseError if there is an issue with the request to get the schema - from the registry. - """ - - @overload - async def encode( - self, - content: Mapping[str, Any], - *, - schema: str, - request_options: Optional[Dict[str, Any]] = None, - **kwargs: Any, - ) -> "MessageContent": - """Encodes content after validating against the pre-registered schema. The following dict will be returned: - {"content": JSON encoded value, "content_type": JSON mime type string + schema ID}. - - :param content: The content to be encoded. - :type content: mapping[str, any] - :keyword schema: Required. The pre-registered schema used to validate the content. `schema_id` - must not be passed. - :paramtype schema: str - :keyword request_options: The keyword arguments for http requests to be passed to the client. - :paramtype request_options: dict[str, any] or None - :returns: TypedDict of encoded content and content type. - :rtype: MessageContent - :raises: ~azure.schemaregistry.encoder.InvalidContentError if there is an issue with encoding content - or validating it against the schema. - :raises: ~azure.core.exceptions.HttpResponseError if there is an issue with the request to get the schema - from the registry. - """ - - @overload - async def encode( - self, - content: Mapping[str, Any], - *, - schema_id: str, - request_options: Optional[Dict[str, Any]] = None, - **kwargs: Any, - ) -> "MessageContent": - """Encodes content after validating against the pre-registered schema corresponding to - the provided schema ID. The following dict will be returned: - {"content": JSON encoded value, "content_type": JSON mime type string + schema ID}. - - :param content: The content to be encoded. - :type content: mapping[str, any] - :keyword schema_id: Required. The schema ID corresponding to the pre-registered schema to be used - for validation. `schema` must not be passed. - :paramtype schema_id: str - :keyword request_options: The keyword arguments for http requests to be passed to the client. - :paramtype request_options: dict[str, any] or None - :returns: TypedDict of encoded content and content type. - :rtype: MessageContent - :raises: ~azure.schemaregistry.encoder.InvalidContentError if there is an issue with encoding content - or validating it against the schema. - :raises: ~azure.core.exceptions.HttpResponseError if there is an issue with the request to get the schema - from the registry. - """ - - async def encode( - self, - content: Mapping[str, Any], - *, - schema: Optional[str] = None, - schema_id: Optional[str] = None, - message_type: Optional[Type[OutboundMessageContent]] = None, - request_options: Optional[Dict[str, Any]] = None, - **kwargs: Any, - ) -> Union[OutboundMessageContent, "MessageContent"]: - """Encodes content after validating against the provided pre-registered schema or the one corresponding to - the provided schema ID. If provided with a OutboundMessageContent subtype, encoded content and content type - will be passed to create message object. If not provided, the following dict will be returned: - {"content": JSON encoded value, "content_type": JSON mime type string + schema ID}. - - If `message_type` is set, then additional keyword arguments for building OutboundMessageContent will be passed - to the OutboundMessageContent.from_message_content() method. - - :param content: The content to be encoded. - :type content: mapping[str, any] - :keyword schema: The pre-registered schema used to validate the content. Exactly one of - `schema` or `schema_id` must be passed. - :paramtype schema: str or None - :keyword schema_id: The schema ID corresponding to the pre-registered schema to be used - for validation. Exactly one of `schema` or `schema_id` must be passed. - :paramtype schema_id: str or None - :keyword message_type: The message class to construct the message. Must be a subtype of the - azure.schemaregistry.encoder.OutboundMessageContent protocol. - :keyword message_type: The message class to construct the message. If passed, must be a subtype of the - azure.schemaregistry.OutboundMessageContent protocol. - :paramtype message_type: type[OutboundMessageContent] or None - :keyword request_options: The keyword arguments for http requests to be passed to the client. - :paramtype request_options: dict[str, any] or None - :returns: TypedDict of encoded content and content type if `message_type` is not set, otherwise the - constructed message object. - :rtype: OutboundMessageContent or MessageContent - :raises: ~azure.schemaregistry.encoder.InvalidContentError if there is an issue with encoding content - or validating it against the schema. - :raises: ~azure.core.exceptions.HttpResponseError if there is an issue with the request to get the schema - from the registry. - """ - request_options = request_options or {} - - # If schema_id, get schema for validation. If schema, get schema_id for content type. - if schema_id and not schema: - cache_misses = self._get_schema.cache_info().misses # pylint: disable=no-value-for-parameter,no-member - schema_str = await self._get_schema(schema_id, **request_options) - new_cache_misses = self._get_schema.cache_info().misses # pylint: disable=no-value-for-parameter,no-member - if new_cache_misses > cache_misses: - cache_info = self._get_schema.cache_info() # pylint: disable=no-value-for-parameter,no-member - _LOGGER.info( - "New entry has been added to schema cache. Cache info: %s", - str(cache_info), - ) - schema_dict = json.loads(schema_str) - elif schema and not schema_id: - if not self._schema_group: - raise TypeError("'group_name' is required in constructor, if 'schema' is passed to encode.") - - schema_fullname, schema_str, schema_dict = get_loaded_schema(schema, content) - cache_misses = self._get_schema_id.cache_info().misses # pylint: disable=no-value-for-parameter,no-member - schema_id = await self._get_schema_id(schema_fullname, schema_str, **request_options) - new_cache_misses = ( - self._get_schema_id.cache_info().misses # pylint: disable=no-value-for-parameter,no-member - ) - if new_cache_misses > cache_misses: - cache_info = self._get_schema_id.cache_info() # pylint: disable=no-value-for-parameter,no-member - _LOGGER.info( - "New entry has been added to schema ID cache. Cache info: %s", - str(cache_info), - ) - else: - raise TypeError("Exactly one of 'schema' or 'schema_id' is required.") - - return create_message_content( - content=content, - schema=schema_dict, - schema_id=cast(str, schema_id), - validate=self._validate, - message_type=message_type, - **kwargs, - ) - - async def decode( - self, - message: Union["MessageContent", InboundMessageContent], - *, - request_options: Optional[Dict[str, Any]] = None, - **kwargs: Any, - ) -> Dict[str, Any]: - """Decode bytes content using schema ID in the content type field. - - :param message: The message object which holds the content to be decoded and content type - containing the schema ID. - :type message: InboundMessageContent or MessageContent - :keyword request_options: The keyword arguments for http requests to be passed to the client. - :paramtype request_options: dict[str, any] or None - :returns: The decoded content. - :rtype: dict[str, any] - :raises: ~azure.schemaregistry.encoder.jsonencoder.InvalidContentError if there is - an issue with decoding content or validating it with the schema. - :raises: ~azure.core.exceptions.HttpResponseError if there is an issue with the request to get the schema - from the registry. - """ - schema_id, content = parse_message(message) - cache_misses = self._get_schema.cache_info().misses # pylint: disable=no-value-for-parameter,no-member - request_options = request_options or {} - schema_definition = await self._get_schema(schema_id, **request_options) - new_cache_misses = self._get_schema.cache_info().misses # pylint: disable=no-value-for-parameter,no-member - if new_cache_misses > cache_misses: - cache_info = self._get_schema.cache_info() # pylint: disable=no-value-for-parameter,no-member - _LOGGER.info( - "New entry has been added to schema cache. Cache info: %s", - str(cache_info), - ) - - return decode_content( - content=content, - schema_id=schema_id, - schema_definition=schema_definition, - validate=self._validate, - **kwargs, - ) diff --git a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/py.typed b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/py.typed deleted file mode 100644 index e5aff4f83af8..000000000000 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/encoder/jsonencoder/py.typed +++ /dev/null @@ -1 +0,0 @@ -# Marker file for PEP 561. \ No newline at end of file diff --git a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/models/_enums.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/models/_enums.py index fdf1307c2382..658a7df97c01 100644 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/models/_enums.py +++ b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/models/_enums.py @@ -19,3 +19,5 @@ class SchemaContentTypeValues(str, Enum, metaclass=CaseInsensitiveEnumMeta): """JSON encoding""" CUSTOM = "text/plain; charset=utf-8" """Plain text custom encoding.""" + PROTOBUF = "text/vnd.ms.protobuf" + """Protobuf encoding.""" diff --git a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/models/_patch.py b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/models/_patch.py index 1cfb6e02993c..f7dd32510333 100644 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/models/_patch.py +++ b/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/models/_patch.py @@ -8,34 +8,6 @@ """ from typing import List -from enum import Enum - -from azure.core import CaseInsensitiveEnumMeta -from ._enums import SchemaContentTypeValues - - -class SchemaFormat(str, Enum, metaclass=CaseInsensitiveEnumMeta): - """Represents the format of the schema to be stored by the Schema Registry service.""" - - AVRO = "Avro" - """Represents the Apache Avro schema format.""" - JSON = "Json" - """Represents the JSON schema format.""" - CUSTOM = "Custom" - """Represents a custom schema format.""" - -# Normalizing the schema content type strings for whitespace and case insensitive comparison. -class NormalizedSchemaContentTypes(str, Enum, metaclass=CaseInsensitiveEnumMeta): - """Describes closed list of normalized schema content type values.""" - - AVRO = SchemaContentTypeValues.AVRO.value.replace(" ", "").lower() - """Avro encoding.""" - JSON = SchemaContentTypeValues.JSON.value.replace(" ", "").lower() - """JSON encoding""" - CUSTOM = SchemaContentTypeValues.CUSTOM.value.replace(" ", "").lower() - """Plain text custom encoding.""" - - __all__: List[str] = [] # Add all objects you want publicly available to users at this package level diff --git a/sdk/schemaregistry/azure-schemaregistry/generated_tests/conftest.py b/sdk/schemaregistry/azure-schemaregistry/generated_tests/conftest.py new file mode 100644 index 000000000000..5941387fbe2f --- /dev/null +++ b/sdk/schemaregistry/azure-schemaregistry/generated_tests/conftest.py @@ -0,0 +1,39 @@ +# coding=utf-8 +# -------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for license information. +# Code generated by Microsoft (R) Python Code Generator. +# Changes may cause incorrect behavior and will be lost if the code is regenerated. +# -------------------------------------------------------------------------- +import os +import pytest +from dotenv import load_dotenv +from devtools_testutils import ( + test_proxy, + add_general_regex_sanitizer, + add_body_key_sanitizer, + add_header_regex_sanitizer, +) + +load_dotenv() + + +# aovid record sensitive identity information in recordings +@pytest.fixture(scope="session", autouse=True) +def add_sanitizers(test_proxy): + schemaregistry_subscription_id = os.environ.get( + "SCHEMAREGISTRY_SUBSCRIPTION_ID", "00000000-0000-0000-0000-000000000000" + ) + schemaregistry_tenant_id = os.environ.get("SCHEMAREGISTRY_TENANT_ID", "00000000-0000-0000-0000-000000000000") + schemaregistry_client_id = os.environ.get("SCHEMAREGISTRY_CLIENT_ID", "00000000-0000-0000-0000-000000000000") + schemaregistry_client_secret = os.environ.get( + "SCHEMAREGISTRY_CLIENT_SECRET", "00000000-0000-0000-0000-000000000000" + ) + add_general_regex_sanitizer(regex=schemaregistry_subscription_id, value="00000000-0000-0000-0000-000000000000") + add_general_regex_sanitizer(regex=schemaregistry_tenant_id, value="00000000-0000-0000-0000-000000000000") + add_general_regex_sanitizer(regex=schemaregistry_client_id, value="00000000-0000-0000-0000-000000000000") + add_general_regex_sanitizer(regex=schemaregistry_client_secret, value="00000000-0000-0000-0000-000000000000") + + add_header_regex_sanitizer(key="Set-Cookie", value="[set-cookie;]") + add_header_regex_sanitizer(key="Cookie", value="cookie;") + add_body_key_sanitizer(json_path="$..access_token", value="access_token") diff --git a/sdk/schemaregistry/azure-schemaregistry/generated_tests/test_schema_registry.py b/sdk/schemaregistry/azure-schemaregistry/generated_tests/test_schema_registry.py new file mode 100644 index 000000000000..10821e4a965c --- /dev/null +++ b/sdk/schemaregistry/azure-schemaregistry/generated_tests/test_schema_registry.py @@ -0,0 +1,34 @@ +# coding=utf-8 +# -------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for license information. +# Code generated by Microsoft (R) Python Code Generator. +# Changes may cause incorrect behavior and will be lost if the code is regenerated. +# -------------------------------------------------------------------------- +import pytest +from devtools_testutils import recorded_by_proxy +from testpreparer import SchemaRegistryClientTestBase, SchemaRegistryPreparer + + +@pytest.mark.skip("you may need to update the auto-generated test case before run it") +class TestSchemaRegistry(SchemaRegistryClientTestBase): + @SchemaRegistryPreparer() + @recorded_by_proxy + def test_list_schema_groups(self, schemaregistry_endpoint): + client = self.create_client(endpoint=schemaregistry_endpoint) + response = client.list_schema_groups() + result = [r for r in response] + # please add some check logic here by yourself + # ... + + @SchemaRegistryPreparer() + @recorded_by_proxy + def test_list_schema_versions(self, schemaregistry_endpoint): + client = self.create_client(endpoint=schemaregistry_endpoint) + response = client.list_schema_versions( + group_name="str", + schema_name="str", + ) + result = [r for r in response] + # please add some check logic here by yourself + # ... diff --git a/sdk/schemaregistry/azure-schemaregistry/generated_tests/test_schema_registry_async.py b/sdk/schemaregistry/azure-schemaregistry/generated_tests/test_schema_registry_async.py new file mode 100644 index 000000000000..42446f6231c4 --- /dev/null +++ b/sdk/schemaregistry/azure-schemaregistry/generated_tests/test_schema_registry_async.py @@ -0,0 +1,35 @@ +# coding=utf-8 +# -------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for license information. +# Code generated by Microsoft (R) Python Code Generator. +# Changes may cause incorrect behavior and will be lost if the code is regenerated. +# -------------------------------------------------------------------------- +import pytest +from devtools_testutils.aio import recorded_by_proxy_async +from testpreparer import SchemaRegistryPreparer +from testpreparer_async import SchemaRegistryClientTestBaseAsync + + +@pytest.mark.skip("you may need to update the auto-generated test case before run it") +class TestSchemaRegistryAsync(SchemaRegistryClientTestBaseAsync): + @SchemaRegistryPreparer() + @recorded_by_proxy_async + async def test_list_schema_groups(self, schemaregistry_endpoint): + client = self.create_async_client(endpoint=schemaregistry_endpoint) + response = client.list_schema_groups() + result = [r async for r in response] + # please add some check logic here by yourself + # ... + + @SchemaRegistryPreparer() + @recorded_by_proxy_async + async def test_list_schema_versions(self, schemaregistry_endpoint): + client = self.create_async_client(endpoint=schemaregistry_endpoint) + response = client.list_schema_versions( + group_name="str", + schema_name="str", + ) + result = [r async for r in response] + # please add some check logic here by yourself + # ... diff --git a/sdk/schemaregistry/azure-schemaregistry/generated_tests/testpreparer.py b/sdk/schemaregistry/azure-schemaregistry/generated_tests/testpreparer.py new file mode 100644 index 000000000000..08c05c39d9b8 --- /dev/null +++ b/sdk/schemaregistry/azure-schemaregistry/generated_tests/testpreparer.py @@ -0,0 +1,26 @@ +# coding=utf-8 +# -------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for license information. +# Code generated by Microsoft (R) Python Code Generator. +# Changes may cause incorrect behavior and will be lost if the code is regenerated. +# -------------------------------------------------------------------------- +from azure.schemaregistry import SchemaRegistryClient +from devtools_testutils import AzureRecordedTestCase, PowerShellPreparer +import functools + + +class SchemaRegistryClientTestBase(AzureRecordedTestCase): + + def create_client(self, endpoint): + credential = self.get_credential(SchemaRegistryClient) + return self.create_client_from_credential( + SchemaRegistryClient, + credential=credential, + endpoint=endpoint, + ) + + +SchemaRegistryPreparer = functools.partial( + PowerShellPreparer, "schemaregistry", schemaregistry_endpoint="https://fake_schemaregistry_endpoint.com" +) diff --git a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_types.py b/sdk/schemaregistry/azure-schemaregistry/generated_tests/testpreparer_async.py similarity index 50% rename from sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_types.py rename to sdk/schemaregistry/azure-schemaregistry/generated_tests/testpreparer_async.py index f2b3319751b6..3b3c633aac49 100644 --- a/sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_types.py +++ b/sdk/schemaregistry/azure-schemaregistry/generated_tests/testpreparer_async.py @@ -1,18 +1,20 @@ # coding=utf-8 -# pylint: disable=too-many-lines # -------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for license information. # Code generated by Microsoft (R) Python Code Generator. # Changes may cause incorrect behavior and will be lost if the code is regenerated. # -------------------------------------------------------------------------- +from azure.schemaregistry.aio import SchemaRegistryClient +from devtools_testutils import AzureRecordedTestCase -from typing import Literal, Union -SchemaContentTypeValues = Union[ - Literal["application/octet-stream"], - Literal["text/plain; charset=utf-8"], - Literal["application/json; serialization=Avro"], - Literal["application/json; serialization=Json"], - Literal["text/vnd.ms.protobuf"], -] +class SchemaRegistryClientTestBaseAsync(AzureRecordedTestCase): + + def create_async_client(self, endpoint): + credential = self.get_credential(SchemaRegistryClient, is_async=True) + return self.create_client_from_credential( + SchemaRegistryClient, + credential=credential, + endpoint=endpoint, + ) diff --git a/sdk/schemaregistry/azure-schemaregistry/setup.py b/sdk/schemaregistry/azure-schemaregistry/setup.py index 8a14178d0468..6e31d628d744 100644 --- a/sdk/schemaregistry/azure-schemaregistry/setup.py +++ b/sdk/schemaregistry/azure-schemaregistry/setup.py @@ -67,11 +67,7 @@ python_requires=">=3.8", zip_safe=False, packages=find_packages(exclude=exclude_packages), - install_requires=[ - "azure-core<2.0.0,>=1.28.0", - "isodate>=0.6.0", - "typing-extensions>=4.6.0" - ], + install_requires=["azure-core<2.0.0,>=1.28.0", "isodate>=0.6.0", "typing-extensions>=4.6.0"], extras_require={ "jsonencoder": [ "jsonschema>=4.10.3", diff --git a/sdk/schemaregistry/azure-schemaregistry/tests/async_tests/mock_transport_async.py b/sdk/schemaregistry/azure-schemaregistry/tests/async_tests/mock_transport_async.py index a6fee6cb9f72..ec59ca69dc58 100644 --- a/sdk/schemaregistry/azure-schemaregistry/tests/async_tests/mock_transport_async.py +++ b/sdk/schemaregistry/azure-schemaregistry/tests/async_tests/mock_transport_async.py @@ -26,17 +26,10 @@ from azure.core.pipeline.transport import AsyncHttpTransport + class AsyncMockResponse: def __init__( - self, - schema_id, - schema_name, - schema_group_name, - schema_version, - content_type, - *, - content=b"", - status_code=200 + self, schema_id, schema_name, schema_group_name, schema_version, content_type, *, content=b"", status_code=200 ) -> None: self._schema_id = schema_id self._schema_name = schema_name @@ -65,13 +58,13 @@ def headers(self): @property def content(self): return self._content - + def iter_bytes(self): pass def raise_for_status(self): pass - + async def read(self): pass @@ -81,22 +74,23 @@ def text(self, encoding=None): def reason(self): pass + class AsyncMockTransport(AsyncHttpTransport): - def __init__(self, response): - self._response = response + def __init__(self, response): + self._response = response - async def __aenter__(self): - return self + async def __aenter__(self): + return self - async def __aexit__(self, *args): - pass + async def __aexit__(self, *args): + pass - async def open(self): - pass + async def open(self): + pass - async def close(self): - pass + async def close(self): + pass - async def send(self, request, **kwargs): - response = self._response - return response \ No newline at end of file + async def send(self, request, **kwargs): + response = self._response + return response diff --git a/sdk/schemaregistry/azure-schemaregistry/tests/async_tests/test_schema_registry_async.py b/sdk/schemaregistry/azure-schemaregistry/tests/async_tests/test_schema_registry_async.py index 341a36764313..00c41ba58a47 100644 --- a/sdk/schemaregistry/azure-schemaregistry/tests/async_tests/test_schema_registry_async.py +++ b/sdk/schemaregistry/azure-schemaregistry/tests/async_tests/test_schema_registry_async.py @@ -472,13 +472,15 @@ async def test_get_schema_unknown_content_type(self, **kwargs): avro_group_name = "avro_group" avro_schema_version = "1" avro_content_type = "application/json; serialization=Avro" - transport = AsyncMockTransport(response=AsyncMockResponse( - schema_id=avro_schema_id, - schema_name=avro_schema_name, - schema_group_name=avro_group_name, - schema_version=avro_schema_version, - content_type=avro_content_type, - )) + transport = AsyncMockTransport( + response=AsyncMockResponse( + schema_id=avro_schema_id, + schema_name=avro_schema_name, + schema_group_name=avro_group_name, + schema_version=avro_schema_version, + content_type=avro_content_type, + ) + ) mock_fqn = f"schemaregistry_fqn" credential = self.get_credential(SchemaRegistryClient) mock_client = SchemaRegistryClient( @@ -508,7 +510,9 @@ async def test_get_schema_unknown_content_type(self, **kwargs): schema = await mock_client.get_schema(foo_schema_id) assert schema.properties.format == foo_content_type # get unknown schema by version should return format of the content type string - schema = await mock_client.get_schema(group_name=foo_group_name, name=foo_schema_name, version=foo_schema_version) + schema = await mock_client.get_schema( + group_name=foo_group_name, name=foo_schema_name, version=foo_schema_version + ) assert schema.properties.format == foo_content_type # get unknown schema with content type of format "contenttype/" @@ -530,5 +534,7 @@ async def test_get_schema_unknown_content_type(self, **kwargs): schema = await mock_client.get_schema(bar_schema_id) assert schema.properties.format == bar_content_type # get unknown schema by version should return format of the content type string - schema = await mock_client.get_schema(group_name=bar_group_name, name=bar_schema_name, version=bar_schema_version) + schema = await mock_client.get_schema( + group_name=bar_group_name, name=bar_schema_name, version=bar_schema_version + ) assert schema.properties.format == bar_content_type diff --git a/sdk/schemaregistry/azure-schemaregistry/tests/mock_transport.py b/sdk/schemaregistry/azure-schemaregistry/tests/mock_transport.py index 4b5f4f27e32b..90c0da70a8dc 100644 --- a/sdk/schemaregistry/azure-schemaregistry/tests/mock_transport.py +++ b/sdk/schemaregistry/azure-schemaregistry/tests/mock_transport.py @@ -26,17 +26,10 @@ from azure.core.pipeline.transport import HttpTransport + class MockResponse: def __init__( - self, - schema_id, - schema_name, - schema_group_name, - schema_version, - content_type, - *, - content=b"", - status_code=200 + self, schema_id, schema_name, schema_group_name, schema_version, content_type, *, content=b"", status_code=200 ) -> None: self._schema_id = schema_id self._schema_name = schema_name @@ -65,13 +58,13 @@ def headers(self): @property def content(self): return self._content - + def iter_bytes(self): pass def raise_for_status(self): pass - + def read(self): pass @@ -81,19 +74,20 @@ def text(self, encoding=None): def reason(self): pass + class MockTransport(HttpTransport): - def __init__(self, response): - self._response = response + def __init__(self, response): + self._response = response - def __exit__(self, exc_type, exc_val, exc_tb): - pass + def __exit__(self, exc_type, exc_val, exc_tb): + pass - def close(self): - pass + def close(self): + pass - def open(self): - pass + def open(self): + pass - def send(self, request, **kwargs): - response = self._response - return response + def send(self, request, **kwargs): + response = self._response + return response diff --git a/sdk/schemaregistry/azure-schemaregistry/tests/test_schema_registry.py b/sdk/schemaregistry/azure-schemaregistry/tests/test_schema_registry.py index 281e15fa2088..81a601962ef6 100644 --- a/sdk/schemaregistry/azure-schemaregistry/tests/test_schema_registry.py +++ b/sdk/schemaregistry/azure-schemaregistry/tests/test_schema_registry.py @@ -442,13 +442,15 @@ def test_get_schema_unknown_content_type(self, **kwargs): avro_group_name = "avro_group" avro_schema_version = "1" avro_content_type = "application/json; serialization=Avro" - transport = MockTransport(response=MockResponse( - schema_id=avro_schema_id, - schema_name=avro_schema_name, - schema_group_name=avro_group_name, - schema_version=avro_schema_version, - content_type=avro_content_type, - )) + transport = MockTransport( + response=MockResponse( + schema_id=avro_schema_id, + schema_name=avro_schema_name, + schema_group_name=avro_group_name, + schema_version=avro_schema_version, + content_type=avro_content_type, + ) + ) mock_fqn = f"schemaregistry_fqn" credential = self.get_credential(SchemaRegistryClient) mock_client = SchemaRegistryClient( diff --git a/sdk/schemaregistry/azure-schemaregistry/tsp-location.yaml b/sdk/schemaregistry/azure-schemaregistry/tsp-location.yaml index 8eedcfcac455..338681e9dab7 100644 --- a/sdk/schemaregistry/azure-schemaregistry/tsp-location.yaml +++ b/sdk/schemaregistry/azure-schemaregistry/tsp-location.yaml @@ -1,4 +1,4 @@ -cleanup: false -commit: 44cbf51b9eee8a2dbf55ced61a61032aaed0148b directory: specification/schemaregistry/SchemaRegistry -repo: Azure/azure-rest-api-specs \ No newline at end of file +commit: c29452397f8d29456182b951987a765d1ae01bb1 +repo: test-repo-billy/azure-rest-api-specs +additionalDirectories: