Skip to content

Commit 1f2218a

Browse files
authored
[SchemaRegistry] update API for consistency (Azure#20538)
* changes * updated failing tests * keep Dict for now * pylint errors * nit * add recordings * tests recordings
1 parent 40c9060 commit 1f2218a

25 files changed

+400
-470
lines changed

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

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,21 @@ 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 schema_registry: The schema registry client
39+
:param client: The schema registry client
4040
which is used to register schema and retrieve schema from the service.
41-
:type schema_registry: ~azure.schemaregistry.SchemaRegistryClient
42-
:param str schema_group: Schema group under which schema should be registered.
41+
:type client: ~azure.schemaregistry.SchemaRegistryClient
42+
:param str group_name: 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
:keyword str codec: The writer codec. If None, let the avro library decides.
4646
4747
"""
4848

49-
def __init__(self, schema_registry, schema_group, **kwargs):
49+
def __init__(self, client, group_name, **kwargs):
5050
# type: ("SchemaRegistryClient", str, Any) -> None
51-
self._schema_group = schema_group
51+
self._schema_group = group_name
5252
self._avro_serializer = AvroObjectSerializer(codec=kwargs.get("codec"))
53-
self._schema_registry_client = schema_registry # type: "SchemaRegistryClient"
53+
self._schema_registry_client = client # type: "SchemaRegistryClient"
5454
self._auto_register_schemas = kwargs.get("auto_register_schemas", False)
5555
self._auto_register_schema_func = (
5656
self._schema_registry_client.register_schema
@@ -120,17 +120,18 @@ def _get_schema(self, schema_id, **kwargs):
120120
self._schema_to_id[schema_str] = schema_id
121121
return schema_str
122122

123-
def serialize(self, data, schema, **kwargs):
123+
def serialize(self, value, schema, **kwargs):
124124
# type: (Dict[str, Any], Union[str, bytes], Any) -> bytes
125125
"""
126-
Encode dict data with the given schema. The returns bytes are consisted of: The first 4 bytes
126+
Encode data with the given schema. The returns bytes are consisted of: The first 4 bytes
127127
denoting record format identifier. The following 32 bytes denoting schema id returned by schema registry
128128
service. The remaining bytes are the real data payload.
129129
130-
:param data: The dict data to be encoded.
130+
:param value: The data to be encoded.
131+
:type value: Dict[str, Any]
131132
:param schema: The schema used to encode the data.
132133
:type schema: Union[str, bytes]
133-
:return:
134+
:rtype: bytes
134135
"""
135136
raw_input_schema = schema
136137
try:
@@ -142,7 +143,7 @@ def serialize(self, data, schema, **kwargs):
142143

143144
record_format_identifier = b"\0\0\0\0"
144145
schema_id = self._get_schema_id(cached_schema.fullname, cached_schema, **kwargs)
145-
data_bytes = self._avro_serializer.serialize(data, cached_schema)
146+
data_bytes = self._avro_serializer.serialize(value, cached_schema)
146147

147148
stream = BytesIO()
148149

@@ -155,21 +156,21 @@ def serialize(self, data, schema, **kwargs):
155156
stream.close()
156157
return payload
157158

158-
def deserialize(self, data, **kwargs):
159+
def deserialize(self, value, **kwargs):
159160
# type: (bytes, Any) -> Dict[str, Any]
160161
"""
161162
Decode bytes data.
162163
163-
:param bytes data: The bytes data needs to be decoded.
164+
:param bytes value: The bytes data needs to be decoded.
164165
:rtype: Dict[str, Any]
165166
"""
166167
# record_format_identifier = data[0:4] # The first 4 bytes are retained for future record format identifier.
167-
schema_id = data[
168+
schema_id = value[
168169
SCHEMA_ID_START_INDEX : (SCHEMA_ID_START_INDEX + SCHEMA_ID_LENGTH)
169170
].decode("utf-8")
170171
schema_content = self._get_schema(schema_id, **kwargs)
171172

172-
dict_data = self._avro_serializer.deserialize(
173-
data[DATA_START_INDEX:], schema_content
173+
dict_value = self._avro_serializer.deserialize(
174+
value[DATA_START_INDEX:], schema_content
174175
)
175-
return dict_data
176+
return dict_value

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ def on_event(partition_context, event):
4646

4747
# create a SchemaRegistryAvroSerializer instance
4848
avro_serializer = SchemaRegistryAvroSerializer(
49-
schema_registry=SchemaRegistryClient(
49+
client=SchemaRegistryClient(
5050
endpoint=SCHEMA_REGISTRY_ENDPOINT,
5151
credential=DefaultAzureCredential()
5252
),
53-
schema_group=SCHEMA_GROUP
53+
group_name=SCHEMA_GROUP
5454
)
5555

5656

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def send_event_data_batch(producer, serializer):
4242
# Use the serialize method to convert dict object to bytes with the given avro schema.
4343
# The serialize method would automatically register the schema into the Schema Registry Service and
4444
# schema would be cached locally for future usage.
45-
payload_bytes = serializer.serialize(data=dict_data, schema=SCHEMA_STRING)
45+
payload_bytes = serializer.serialize(value=dict_data, schema=SCHEMA_STRING)
4646
print('The bytes of serialized dict data is {}.'.format(payload_bytes))
4747

4848
event_data = EventData(body=payload_bytes) # pass the bytes data to the body of an EventData
@@ -60,11 +60,11 @@ def send_event_data_batch(producer, serializer):
6060

6161
# create a SchemaRegistryAvroSerializer instance
6262
avro_serializer = SchemaRegistryAvroSerializer(
63-
schema_registry=SchemaRegistryClient(
63+
client=SchemaRegistryClient(
6464
endpoint=SCHEMA_REGISTRY_ENDPOINT,
6565
credential=DefaultAzureCredential()
6666
),
67-
schema_group=SCHEMA_GROUP
67+
group_name=SCHEMA_GROUP
6868
)
6969

7070

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

Lines changed: 0 additions & 53 deletions
This file was deleted.

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":"576838e0558c43f8b85cdaadbd4561f5"}'
26+
string: '{"id":"041afcdb34a546faa3aa26a991567e32"}'
2727
headers:
2828
content-type:
2929
- application/json
3030
date:
31-
- Wed, 01 Sep 2021 17:06:46 GMT
31+
- Wed, 08 Sep 2021 22:17:05 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-
- 576838e0558c43f8b85cdaadbd4561f5
41+
- 041afcdb34a546faa3aa26a991567e32
4242
x-schema-id-location:
43-
- https://swathip-test-eventhubs.servicebus.windows.net:443/$schemagroups/getschemabyid/576838e0558c43f8b85cdaadbd4561f5?api-version=2017-04
43+
- https://swathip-test-eventhubs.servicebus.windows.net:443/$schemagroups/getschemabyid/041afcdb34a546faa3aa26a991567e32?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":"576838e0558c43f8b85cdaadbd4561f5"}'
26+
string: '{"id":"041afcdb34a546faa3aa26a991567e32"}'
2727
headers:
2828
content-type:
2929
- application/json
3030
date:
31-
- Wed, 01 Sep 2021 17:08:24 GMT
31+
- Wed, 08 Sep 2021 22:17:06 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-
- 576838e0558c43f8b85cdaadbd4561f5
41+
- 041afcdb34a546faa3aa26a991567e32
4242
x-schema-id-location:
43-
- https://swathip-test-eventhubs.servicebus.windows.net:443/$schemagroups/getschemabyid/576838e0558c43f8b85cdaadbd4561f5?api-version=2017-04
43+
- https://swathip-test-eventhubs.servicebus.windows.net:443/$schemagroups/getschemabyid/041afcdb34a546faa3aa26a991567e32?api-version=2017-04
4444
x-schema-type:
4545
- Avro
4646
x-schema-version:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ 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(self, schemaregistry_endpoint, schemaregistry_group, **kwargs):
78+
def test_basic_sr_avro_serializer_with_auto_register_schemas(self, schemaregistry_endpoint, schemaregistry_group, **kwargs):
7979
sr_client = self.create_basic_client(SchemaRegistryClient, endpoint=schemaregistry_endpoint)
8080
sr_avro_serializer = SchemaRegistryAvroSerializer(sr_client, schemaregistry_group, auto_register_schemas=True)
8181

sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_common/_response_handlers.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,22 @@
2929
def _parse_schema_properties_dict(response):
3030
return {
3131
'location': response.headers.get('location'),
32-
'location_by_id': response.headers.get('schema-id-location'),
33-
'schema_id': response.headers.get('schema-id'),
32+
'id': response.headers.get('schema-id'),
3433
'serialization_type': response.headers.get('serialization-type'),
3534
'version': int(response.headers.get('schema-version'))
3635
}
3736

3837

3938
def _parse_response_schema_properties(response):
4039
properties_dict = _parse_schema_properties_dict(response)
41-
properties_dict['schema_id'] = response.json()["id"]
40+
properties_dict['id'] = response.json()["id"]
4241
return SchemaProperties(
4342
**properties_dict
4443
)
4544

4645

4746
def _parse_response_schema(response):
4847
return Schema(
49-
schema_content=response.text(),
50-
schema_properties=SchemaProperties(**_parse_schema_properties_dict(response))
48+
content=response.text(),
49+
properties=SchemaProperties(**_parse_schema_properties_dict(response))
5150
)

sdk/schemaregistry/azure-schemaregistry/azure/schemaregistry/_common/_schema.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,10 @@ class SchemaProperties(object):
3030
"""
3131
Meta properties of a schema.
3232
33-
:ivar schema_id: References specific schema in registry namespace.
34-
:type schema_id: str
33+
:ivar id: References specific schema in registry namespace.
34+
:type id: str
3535
:ivar location: URL location of schema, identified by schema group, schema name, and version.
3636
:type location: str
37-
:ivar location_by_id: URL location of schema, identified by schema ID.
38-
:type location_by_id: str
3937
:ivar serialization_type: Serialization type for the schema being stored.
4038
:type serialization_type: str
4139
:ivar version: Version of the returned schema.
@@ -54,13 +52,12 @@ class SchemaProperties(object):
5452

5553
def __init__(
5654
self,
57-
schema_id=None,
55+
id=None, # pylint:disable=redefined-builtin
5856
**kwargs
5957
):
6058
# type: (Optional[str], Any) -> None
61-
self.schema_id = schema_id
59+
self.id = id
6260
self.location = kwargs.get('location')
63-
self.location_by_id = kwargs.get('location_by_id')
6461
self.serialization_type = kwargs.get('serialization_type')
6562
self.version = kwargs.get('version')
6663

@@ -69,10 +66,10 @@ class Schema(object):
6966
"""
7067
The schema content of a schema, along with id and meta properties.
7168
72-
:ivar schema_content: The content of the schema.
73-
:type schema_content: str
74-
:ivar schema_properties: The properties of the schema.
75-
:type schema_properties: SchemaProperties
69+
:ivar content: The content of the schema.
70+
:type content: str
71+
:ivar properties: The properties of the schema.
72+
:type properties: SchemaProperties
7673
7774
.. admonition:: Example:
7875
@@ -87,9 +84,9 @@ class Schema(object):
8784

8885
def __init__(
8986
self,
90-
schema_content,
91-
schema_properties,
87+
content,
88+
properties,
9289
):
9390
# type: (str, SchemaProperties) -> None
94-
self.schema_content = schema_content
95-
self.schema_properties = schema_properties
91+
self.content = content
92+
self.properties = properties

0 commit comments

Comments
 (0)