Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename and segment avro_client functionality #29

Merged
merged 16 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Changelog
## v1.1.6 6/26/24
## v1.2.0 7/8/24
- Generalized Avro functions and separated encoding/decoding behavior.

## v1.1.5 6/6/24
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "nypl_py_utils"
version = "1.1.5"
version = "1.2.0"
authors = [
{ name="Aaron Friedman", email="[email protected]" },
]
Expand Down
49 changes: 21 additions & 28 deletions src/nypl_py_utils/classes/avro_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import avro.schema
import base64
import json
import requests

from avro.errors import AvroException
Expand Down Expand Up @@ -109,39 +108,18 @@ class AvroDecoder(AvroClient):
Platform API endpoint from which to fetch the schema in JSON format.
"""

def decode_record(self, record, encoding="binary"):
def decode_record(self, record):
"""
Decodes a single record represented either as a byte or
fatimarahman marked this conversation as resolved.
Show resolved Hide resolved
base64 string, using the given Avro schema.

Returns a dictionary where each key is a field in the schema.
"""
self.logger.info('Decoding {rec} of type {type} using {schema} schema'
.format(rec=record, type=encoding,
schema=self.schema.name))

if encoding == "base64":
return self._decode_base64(record)
elif encoding == "binary":
return self._decode_binary(record)
else:
self.logger.error(
'Failed to decode record due to encoding type: {}'
.format(encoding))
raise AvroClientError(
'Invalid encoding type: {}'.format(encoding))

def _decode_base64(self, record):
decoded_data = base64.b64decode(record).decode("utf-8")
try:
return json.loads(decoded_data)
except Exception as e:
if isinstance(decoded_data, bytes):
self._decode_binary(decoded_data)
else:
self.logger.error('Failed to decode record: {}'.format(e))
raise AvroClientError(
'Failed to decode record: {}'.format(e)) from None
self.logger.info('Decoding {rec} using {schema} schema'
.format(rec=record, schema=self.schema.name))
bytes_input = base64.b64decode(record) if (
fatimarahman marked this conversation as resolved.
Show resolved Hide resolved
isinstance(record, str)) else record
return self._decode_binary(bytes_input)

def _decode_binary(self, record):
fatimarahman marked this conversation as resolved.
Show resolved Hide resolved
datum_reader = DatumReader(self.schema)
Expand All @@ -154,6 +132,21 @@ def _decode_binary(self, record):
raise AvroClientError(
'Failed to decode record: {}'.format(e)) from None

def decode_batch(self, record_list):
"""
Decodes a list of JSON records using the given Avro schema.

Returns a list of strings where each string is an decoded record.
fatimarahman marked this conversation as resolved.
Show resolved Hide resolved
"""
self.logger.info(
'Encoding ({num_rec}) records using {schema} schema'.format(
fatimarahman marked this conversation as resolved.
Show resolved Hide resolved
num_rec=len(record_list), schema=self.schema.name))
decoded_records = []
for record in record_list:
decoded_record = self._decode_binary(record)
decoded_records.append(decoded_record)
return decoded_records


class AvroClientError(Exception):
def __init__(self, message=None):
Expand Down
32 changes: 22 additions & 10 deletions tests/test_avro_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest

from nypl_py_utils.classes.avro_client import (
AvroDecoder, AvroEncoder, AvroClientError)
AvroClientError, AvroDecoder, AvroEncoder)
from requests.exceptions import ConnectTimeout

_TEST_SCHEMA = {'data': {'schema': json.dumps({
Expand Down Expand Up @@ -39,8 +39,8 @@ def test_get_json_schema(self, test_avro_encoder_instance,
test_avro_decoder_instance):
assert test_avro_encoder_instance.schema == _TEST_SCHEMA['data'][
'schema']
assert test_avro_decoder_instance.schema == _TEST_SCHEMA['data'][
'schema']
# assert test_avro_decoder_instance.schema == _TEST_SCHEMA['data'][
fatimarahman marked this conversation as resolved.
Show resolved Hide resolved
# 'schema']

def test_request_error(self, requests_mock):
requests_mock.get('https://test_schema_url', exc=ConnectTimeout)
Expand Down Expand Up @@ -98,14 +98,26 @@ def test_decode_record_binary(self, test_avro_decoder_instance):
assert test_avro_decoder_instance.decode_record(
TEST_ENCODED_RECORD) == TEST_DECODED_RECORD

def test_decode_record_b64(self, test_avro_decoder_instance):
TEST_DECODED_RECORD = {"patron_id'": 123, "library_branch": "aa"}
TEST_ENCODED_RECORD = (
"eyJwYXRyb25faWQnIjogMTIzLCAibGlicmFyeV9icmFuY2giOiAiYWEifQ==")
assert test_avro_decoder_instance.decode_record(
TEST_ENCODED_RECORD, "base64") == TEST_DECODED_RECORD

def test_decode_record_error(self, test_avro_decoder_instance):
TEST_ENCODED_RECORD = b'bad-encoding'
with pytest.raises(AvroClientError):
test_avro_decoder_instance.decode_record(TEST_ENCODED_RECORD)

def test_decode_batch(self, test_avro_decoder_instance):
TEST_ENCODED_BATCH = [
b'\xf6\x01\x02\x04aa',
b'\x90\x07\x00',
b'\xaa\x0c\x02\x04bb']
TEST_DECODED_BATCH = [
{'patron_id': 123, 'library_branch': 'aa'},
{'patron_id': 456, 'library_branch': None},
{'patron_id': 789, 'library_branch': 'bb'}]
assert test_avro_decoder_instance.decode_batch(
TEST_ENCODED_BATCH) == TEST_DECODED_BATCH

def test_decode_batch_error(self, test_avro_decoder_instance):
BAD_BATCH = [
b'\xf6\x01\x02\x04aa',
b'bad-encoding']
with pytest.raises(AvroClientError):
test_avro_decoder_instance.decode_batch(BAD_BATCH)
Loading