Skip to content

Commit d32de70

Browse files
authored
[SchemaRegistry] update positional args to req kwargs (#20763)
* make params required kwargs * update changelog * adams comments * update avro tests + samples * fix samples * pylint error * adams comments
1 parent 55526df commit d32de70

22 files changed

+189
-173
lines changed

sdk/schemaregistry/azure-schemaregistry-avroserializer/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Features Added
66

77
- `auto_register_schemas` keyword argument has been added to `SchemaRegistryAvroSerializer`, which will allow for automatically registering schemas passed in to the `serialize`.
8+
- `value` parameter in `serialize` on `SchemaRegistryAvroSerializer` takes type `Mapping` rather than `Dict`.
89

910
### Breaking Changes
1011

@@ -13,6 +14,9 @@
1314
- `data` parameter in the `serialize` and `deserialize` methods on `SchemaRegistryAvroSerializer` has been renamed `value`.
1415
- `schema` parameter in the `serialize` method on `SchemaRegistryAvroSerializer` no longer accepts argument of type `bytes`.
1516
- `SchemaRegistryAvroSerializer` constructor no longer takes in the `codec` keyword argument.
17+
- The following positional arguments are now required keyword arguments:
18+
- `client` and `group_name` in `SchemaRegistryAvroSerializer` constructor
19+
- `schema` in `serialize` on `SchemaRegistryAvroSerializer`
1620

1721
### Bugs Fixed
1822

sdk/schemaregistry/azure-schemaregistry-avroserializer/azure/schemaregistry/serializer/avroserializer/_schema_registry_avro_serializer.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#
2525
# --------------------------------------------------------------------------
2626
from io import BytesIO
27-
from typing import Any, Dict, Union
27+
from typing import Any, Dict, Mapping
2828
import avro
2929

3030
from ._constants import SCHEMA_ID_START_INDEX, SCHEMA_ID_LENGTH, DATA_START_INDEX
@@ -36,20 +36,23 @@ class SchemaRegistryAvroSerializer(object):
3636
SchemaRegistryAvroSerializer provides the ability to serialize and deserialize data according
3737
to the given avro schema. It would automatically register, get and cache the schema.
3838
39-
:param client: The schema registry client
39+
:keyword client: Required. The schema registry client
4040
which is used to register schema and retrieve schema from the service.
41-
:type client: ~azure.schemaregistry.SchemaRegistryClient
42-
:param str group_name: Schema group under which schema should be registered.
41+
:paramtype client: ~azure.schemaregistry.SchemaRegistryClient
42+
:keyword str group_name: Required. Schema group under which schema should be registered.
4343
:keyword bool auto_register_schemas: When true, register new schemas passed to serialize.
4444
Otherwise, and by default, fail if it has not been pre-registered in the registry.
4545
4646
"""
4747

48-
def __init__(self, client, group_name, **kwargs):
49-
# type: ("SchemaRegistryClient", str, Any) -> None
50-
self._schema_group = group_name
48+
def __init__(self, **kwargs):
49+
# type: (Any) -> None
50+
try:
51+
self._schema_group = kwargs.pop("group_name")
52+
self._schema_registry_client = kwargs.pop("client") # type: "SchemaRegistryClient"
53+
except KeyError as e:
54+
raise TypeError("'{}' is a required keyword.".format(e.args[0]))
5155
self._avro_serializer = AvroObjectSerializer(codec=kwargs.get("codec"))
52-
self._schema_registry_client = client # type: "SchemaRegistryClient"
5356
self._auto_register_schemas = kwargs.get("auto_register_schemas", False)
5457
self._auto_register_schema_func = (
5558
self._schema_registry_client.register_schema
@@ -119,20 +122,23 @@ def _get_schema(self, schema_id, **kwargs):
119122
self._schema_to_id[schema_str] = schema_id
120123
return schema_str
121124

122-
def serialize(self, value, schema, **kwargs):
123-
# type: (Dict[str, Any], Union[str, bytes], Any) -> bytes
125+
def serialize(self, value, **kwargs):
126+
# type: (Mapping[str, Any], Any) -> bytes
124127
"""
125128
Encode data with the given schema. The returns bytes are consisted of: The first 4 bytes
126129
denoting record format identifier. The following 32 bytes denoting schema id returned by schema registry
127130
service. The remaining bytes are the real data payload.
128131
129132
:param value: The data to be encoded.
130-
:type value: Dict[str, Any]
131-
:param schema: The schema used to encode the data.
132-
:type schema: str
133+
:type value: Mapping[str, Any]
134+
:keyword schema: Required. The schema used to encode the data.
135+
:paramtype schema: str
133136
:rtype: bytes
134137
"""
135-
raw_input_schema = schema
138+
try:
139+
raw_input_schema = kwargs.pop("schema")
140+
except KeyError as e:
141+
raise TypeError("'{}' is a required keyword.".format(e.args[0]))
136142
try:
137143
cached_schema = self._user_input_schema_cache[raw_input_schema]
138144
except KeyError:

sdk/schemaregistry/azure-schemaregistry-avroserializer/samples/avro_serializer.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
CLIENT_ID=os.environ['SCHEMA_REGISTRY_AZURE_CLIENT_ID']
3434
CLIENT_SECRET=os.environ['SCHEMA_REGISTRY_AZURE_CLIENT_SECRET']
3535

36-
SCHEMA_REGISTRY_ENDPOINT=os.environ['SCHEMA_REGISTRY_ENDPOINT']
36+
SCHEMA_REGISTRY_FULLY_QUALIFIED_NAMESPACE=os.environ['SCHEMA_REGISTRY_FULLY_QUALIFIED_NAMESPACE']
3737
GROUP_NAME=os.environ['SCHEMA_REGISTRY_GROUP']
3838
SCHEMA_STRING = """
3939
{"namespace": "example.avro",
@@ -59,9 +59,9 @@ def serialize(serializer):
5959
dict_data_alice = {"name": u"Alice", "favorite_number": 15, "favorite_color": u"green"}
6060

6161
# Schema would be automatically registered into Schema Registry and cached locally.
62-
payload_ben = serializer.serialize(dict_data_ben, SCHEMA_STRING)
62+
payload_ben = serializer.serialize(dict_data_ben, schema=SCHEMA_STRING)
6363
# The second call won't trigger a service call.
64-
payload_alice = serializer.serialize(dict_data_alice, SCHEMA_STRING)
64+
payload_alice = serializer.serialize(dict_data_alice, schema=SCHEMA_STRING)
6565

6666
print('Encoded bytes are: ', payload_ben)
6767
print('Encoded bytes are: ', payload_alice)
@@ -79,8 +79,8 @@ def deserialize(serializer, bytes_payload):
7979

8080

8181
if __name__ == '__main__':
82-
schema_registry = SchemaRegistryClient(endpoint=SCHEMA_REGISTRY_ENDPOINT, credential=token_credential)
83-
serializer = SchemaRegistryAvroSerializer(schema_registry, GROUP_NAME)
82+
schema_registry = SchemaRegistryClient(endpoint=SCHEMA_REGISTRY_FULLY_QUALIFIED_NAMESPACE, credential=token_credential)
83+
serializer = SchemaRegistryAvroSerializer(client=schema_registry, group_name=GROUP_NAME, auto_register_schemas=True)
8484
bytes_data_ben, bytes_data_alice = serialize(serializer)
8585
dict_data_ben = deserialize(serializer, bytes_data_ben)
8686
dict_data_alice = deserialize(serializer, bytes_data_alice)

sdk/schemaregistry/azure-schemaregistry-avroserializer/samples/eventhub_receive_integration.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
EVENTHUB_CONNECTION_STR = os.environ['EVENT_HUB_CONN_STR']
2020
EVENTHUB_NAME = os.environ['EVENT_HUB_NAME']
2121

22-
SCHEMA_REGISTRY_ENDPOINT = os.environ['SCHEMA_REGISTRY_ENDPOINT']
22+
SCHEMA_REGISTRY_FULLY_QUALIFIED_NAMESPACE = os.environ['SCHEMA_REGISTRY_FULLY_QUALIFIED_NAMESPACE']
2323
GROUP_NAME = os.environ['SCHEMA_REGISTRY_GROUP']
2424

2525

@@ -45,12 +45,14 @@ def on_event(partition_context, event):
4545

4646

4747
# create a SchemaRegistryAvroSerializer instance
48+
# TODO: after 'azure-schemaregistry==1.0.0b3' is released, update 'endpoint' to 'fully_qualified_namespace'
4849
avro_serializer = SchemaRegistryAvroSerializer(
4950
client=SchemaRegistryClient(
50-
endpoint=SCHEMA_REGISTRY_ENDPOINT,
51+
endpoint=SCHEMA_REGISTRY_FULLY_QUALIFIED_NAMESPACE,
5152
credential=DefaultAzureCredential()
5253
),
53-
group_name=GROUP_NAME
54+
group_name=GROUP_NAME,
55+
auto_register_schemas=True
5456
)
5557

5658

sdk/schemaregistry/azure-schemaregistry-avroserializer/samples/eventhub_send_integration.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
EVENTHUB_CONNECTION_STR = os.environ['EVENT_HUB_CONN_STR']
2121
EVENTHUB_NAME = os.environ['EVENT_HUB_NAME']
2222

23-
SCHEMA_REGISTRY_ENDPOINT = os.environ['SCHEMA_REGISTRY_ENDPOINT']
23+
SCHEMA_REGISTRY_FULLY_QUALIFIED_NAMESPACE = os.environ['SCHEMA_REGISTRY_FULLY_QUALIFIED_NAMESPACE']
2424
GROUP_NAME = os.environ['SCHEMA_REGISTRY_GROUP']
2525

2626
SCHEMA_STRING = """
@@ -59,12 +59,14 @@ def send_event_data_batch(producer, serializer):
5959

6060

6161
# create a SchemaRegistryAvroSerializer instance
62+
# TODO: after 'azure-schemaregistry==1.0.0b3' is released, update 'endpoint' to 'fully_qualified_namespace'
6263
avro_serializer = SchemaRegistryAvroSerializer(
6364
client=SchemaRegistryClient(
64-
endpoint=SCHEMA_REGISTRY_ENDPOINT,
65+
endpoint=SCHEMA_REGISTRY_FULLY_QUALIFIED_NAMESPACE,
6566
credential=DefaultAzureCredential()
6667
),
67-
group_name=GROUP_NAME
68+
group_name=GROUP_NAME,
69+
auto_register_schemas=True
6870
)
6971

7072

sdk/schemaregistry/azure-schemaregistry-avroserializer/tests/recordings/test_avro_serializer.test_basic_sr_avro_serializer_with_auto_register_schemas.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ interactions:
2323
uri: https://fake_resource.servicebus.windows.net/$schemagroups/fakegroup/schemas/example.avro.User?api-version=2017-04
2424
response:
2525
body:
26-
string: '{"id":"041afcdb34a546faa3aa26a991567e32"}'
26+
string: '{"id":"fc61e4d3e31b46f6a758fa1b67f35cc5"}'
2727
headers:
2828
content-type:
2929
- application/json
3030
date:
31-
- Wed, 08 Sep 2021 22:17:05 GMT
31+
- Fri, 24 Sep 2021 19:54:45 GMT
3232
location:
3333
- https://swathip-test-eventhubs.servicebus.windows.net:443/$schemagroups/fakegroup/schemas/example.avro.User/versions/1?api-version=2017-04
3434
server:
@@ -38,9 +38,9 @@ interactions:
3838
transfer-encoding:
3939
- chunked
4040
x-schema-id:
41-
- 041afcdb34a546faa3aa26a991567e32
41+
- fc61e4d3e31b46f6a758fa1b67f35cc5
4242
x-schema-id-location:
43-
- https://swathip-test-eventhubs.servicebus.windows.net:443/$schemagroups/getschemabyid/041afcdb34a546faa3aa26a991567e32?api-version=2017-04
43+
- https://swathip-test-eventhubs.servicebus.windows.net:443/$schemagroups/getschemabyid/fc61e4d3e31b46f6a758fa1b67f35cc5?api-version=2017-04
4444
x-schema-type:
4545
- Avro
4646
x-schema-version:

sdk/schemaregistry/azure-schemaregistry-avroserializer/tests/recordings/test_avro_serializer.test_basic_sr_avro_serializer_without_auto_register_schemas.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ interactions:
2323
uri: https://fake_resource.servicebus.windows.net/$schemagroups/fakegroup/schemas/example.avro.User?api-version=2017-04
2424
response:
2525
body:
26-
string: '{"id":"041afcdb34a546faa3aa26a991567e32"}'
26+
string: '{"id":"fc61e4d3e31b46f6a758fa1b67f35cc5"}'
2727
headers:
2828
content-type:
2929
- application/json
3030
date:
31-
- Wed, 08 Sep 2021 22:17:06 GMT
31+
- Fri, 24 Sep 2021 19:54:47 GMT
3232
location:
3333
- https://swathip-test-eventhubs.servicebus.windows.net:443/$schemagroups/fakegroup/schemas/example.avro.User/versions/1?api-version=2017-04
3434
server:
@@ -38,9 +38,9 @@ interactions:
3838
transfer-encoding:
3939
- chunked
4040
x-schema-id:
41-
- 041afcdb34a546faa3aa26a991567e32
41+
- fc61e4d3e31b46f6a758fa1b67f35cc5
4242
x-schema-id-location:
43-
- https://swathip-test-eventhubs.servicebus.windows.net:443/$schemagroups/getschemabyid/041afcdb34a546faa3aa26a991567e32?api-version=2017-04
43+
- https://swathip-test-eventhubs.servicebus.windows.net:443/$schemagroups/getschemabyid/fc61e4d3e31b46f6a758fa1b67f35cc5?api-version=2017-04
4444
x-schema-type:
4545
- Avro
4646
x-schema-version:

sdk/schemaregistry/azure-schemaregistry-avroserializer/tests/schemaregistry_preparer.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
)
3131

3232

33-
SCHEMA_REGISTRY_ENDPOINT_PARAM = "schemaregistry_endpoint"
33+
SCHEMA_REGISTRY_FULLY_QUALIFIED_NAMESPACE_PARAM = "schemaregistry_fully_qualified_namespace"
3434
SCHEMA_REGISTRY_GROUP_PARAM = "schemaregistry_group"
35-
SCHEMA_REGISTRY_ENDPOINT_ENV_KEY_NAME = 'SCHEMA_REGISTRY_ENDPOINT'
35+
SCHEMA_REGISTRY_FULLY_QUALIFIED_NAMESPACE_ENV_KEY_NAME = 'SCHEMA_REGISTRY_FULLY_QUALIFIED_NAMESPACE'
3636
SCHEMA_REGISTRY_GROUP_ENV_KEY_NAME = 'SCHEMA_REGISTRY_GROUP'
3737

3838

@@ -59,13 +59,13 @@ def create_resource(self, name, **kwargs):
5959
# TODO: right now the endpoint/group is fixed, as there is no way to create/delete resources using api, in the future we should be able to dynamically create and remove resources
6060
if self.is_live:
6161
return {
62-
SCHEMA_REGISTRY_ENDPOINT_PARAM: os.environ[SCHEMA_REGISTRY_ENDPOINT_ENV_KEY_NAME],
62+
SCHEMA_REGISTRY_FULLY_QUALIFIED_NAMESPACE_PARAM: os.environ[SCHEMA_REGISTRY_FULLY_QUALIFIED_NAMESPACE_ENV_KEY_NAME],
6363
SCHEMA_REGISTRY_GROUP_PARAM: os.environ[SCHEMA_REGISTRY_GROUP_ENV_KEY_NAME]
6464
}
6565

6666
else:
6767
return {
68-
SCHEMA_REGISTRY_ENDPOINT_PARAM: "sr-playground.servicebus.windows.net",
68+
SCHEMA_REGISTRY_FULLY_QUALIFIED_NAMESPACE_PARAM: "sr-playground.servicebus.windows.net",
6969
SCHEMA_REGISTRY_GROUP_PARAM: "azsdk_python_test_group"
7070
}
7171

sdk/schemaregistry/azure-schemaregistry-avroserializer/tests/test_avro_serializer.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
from devtools_testutils import AzureTestCase, PowerShellPreparer
3434

35-
SchemaRegistryPowerShellPreparer = functools.partial(PowerShellPreparer, "schemaregistry", schemaregistry_endpoint="fake_resource.servicebus.windows.net/", schemaregistry_group="fakegroup")
35+
SchemaRegistryPowerShellPreparer = functools.partial(PowerShellPreparer, "schemaregistry", schemaregistry_fully_qualified_namespace="fake_resource.servicebus.windows.net/", schemaregistry_group="fakegroup")
3636

3737
class SchemaRegistryAvroSerializerTests(AzureTestCase):
3838

@@ -75,15 +75,16 @@ def test_raw_avro_serializer_negative(self):
7575
raw_avro_object_serializer.serialize(dict_data_missing_required_field, schema)
7676

7777
@SchemaRegistryPowerShellPreparer()
78-
def test_basic_sr_avro_serializer_with_auto_register_schemas(self, schemaregistry_endpoint, schemaregistry_group, **kwargs):
79-
sr_client = self.create_basic_client(SchemaRegistryClient, endpoint=schemaregistry_endpoint)
80-
sr_avro_serializer = SchemaRegistryAvroSerializer(sr_client, schemaregistry_group, auto_register_schemas=True)
78+
def test_basic_sr_avro_serializer_with_auto_register_schemas(self, schemaregistry_fully_qualified_namespace, schemaregistry_group, **kwargs):
79+
# TODO: AFTER RELEASING azure-schemaregistry=1.0.0b3, UPDATE 'endpoint' to 'fully_qualified_namespace'
80+
sr_client = self.create_basic_client(SchemaRegistryClient, endpoint=schemaregistry_fully_qualified_namespace)
81+
sr_avro_serializer = SchemaRegistryAvroSerializer(client=sr_client, group_name=schemaregistry_group, auto_register_schemas=True)
8182

8283
schema_str = """{"namespace":"example.avro","type":"record","name":"User","fields":[{"name":"name","type":"string"},{"name":"favorite_number","type":["int","null"]},{"name":"favorite_color","type":["string","null"]}]}"""
8384
schema = avro.schema.parse(schema_str)
8485

8586
dict_data = {"name": u"Ben", "favorite_number": 7, "favorite_color": u"red"}
86-
encoded_data = sr_avro_serializer.serialize(dict_data, schema_str)
87+
encoded_data = sr_avro_serializer.serialize(dict_data, schema=schema_str)
8788

8889
assert schema_str in sr_avro_serializer._user_input_schema_cache
8990
assert str(avro.schema.parse(schema_str)) in sr_avro_serializer._schema_to_id
@@ -102,15 +103,16 @@ def test_basic_sr_avro_serializer_with_auto_register_schemas(self, schemaregistr
102103
sr_avro_serializer.close()
103104

104105
@SchemaRegistryPowerShellPreparer()
105-
def test_basic_sr_avro_serializer_without_auto_register_schemas(self, schemaregistry_endpoint, schemaregistry_group, **kwargs):
106-
sr_client = self.create_basic_client(SchemaRegistryClient, endpoint=schemaregistry_endpoint)
107-
sr_avro_serializer = SchemaRegistryAvroSerializer(sr_client, schemaregistry_group)
106+
def test_basic_sr_avro_serializer_without_auto_register_schemas(self, schemaregistry_fully_qualified_namespace, schemaregistry_group, **kwargs):
107+
# TODO: AFTER RELEASING azure-schemaregistry=1.0.0b3, UPDATE 'endpoint' to 'fully_qualified_namespace'
108+
sr_client = self.create_basic_client(SchemaRegistryClient, endpoint=schemaregistry_fully_qualified_namespace)
109+
sr_avro_serializer = SchemaRegistryAvroSerializer(client=sr_client, group_name=schemaregistry_group)
108110

109111
schema_str = """{"namespace":"example.avro","type":"record","name":"User","fields":[{"name":"name","type":"string"},{"name":"favorite_number","type":["int","null"]},{"name":"favorite_color","type":["string","null"]}]}"""
110112
schema = avro.schema.parse(schema_str)
111113

112114
dict_data = {"name": u"Ben", "favorite_number": 7, "favorite_color": u"red"}
113-
encoded_data = sr_avro_serializer.serialize(dict_data, schema_str)
115+
encoded_data = sr_avro_serializer.serialize(dict_data, schema=schema_str)
114116

115117
assert schema_str in sr_avro_serializer._user_input_schema_cache
116118
assert str(avro.schema.parse(schema_str)) in sr_avro_serializer._schema_to_id

sdk/schemaregistry/azure-schemaregistry/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
- `schema_definition`, which has been renamed from `schema_content`
1515
- `format`, which has been renamed from `serialization_type`
1616
- `endpoint` parameter in `SchemaRegistryClient` constructor has been renamed `fully_qualified_namespace`
17+
- `location` instance variable in `SchemaProperties` has been removed.
18+
- `Schema` and `SchemaProperties` no longer have positional parameters, as they will not be constructed by the user.
1719

1820
### Bugs Fixed
1921

0 commit comments

Comments
 (0)