Skip to content

Commit 0cd7507

Browse files
committed
Autopep8 linter used
1 parent fe948d8 commit 0cd7507

2 files changed

Lines changed: 34 additions & 21 deletions

File tree

src/nypl_py_utils/classes/avro_client.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@
99
from nypl_py_utils.functions.log_helper import create_log
1010
from requests.exceptions import JSONDecodeError, RequestException
1111

12+
1213
class AvroClient:
1314
"""
1415
Base class for Avro schema interaction. Takes as input the
1516
Platform API endpoint from which to fetch the schema in JSON format.
1617
"""
17-
18+
1819
def __init__(self, platform_schema_url):
1920
self.logger = create_log('avro_encoder')
2021
self.schema = avro.schema.parse(
2122
self.get_json_schema(platform_schema_url))
22-
23+
2324
def get_json_schema(self, platform_schema_url):
2425
"""
2526
Fetches a JSON response from the input Platform API endpoint and
@@ -48,7 +49,7 @@ def get_json_schema(self, platform_schema_url):
4849
raise AvroClientError(
4950
'Retrieved schema is malformed: {errorType} {errorMessage}'
5051
.format(errorType=type(e), errorMessage=e)) from None
51-
52+
5253

5354
class AvroEncoder(AvroClient):
5455
"""
@@ -64,7 +65,7 @@ def encode_record(self, record):
6465
"""
6566
self.logger.debug(
6667
'Encoding record using {schema} schema'.format(
67-
schema=self.schema.name))
68+
schema=self.schema.name))
6869
datum_writer = DatumWriter(self.schema)
6970
with BytesIO() as output_stream:
7071
encoder = BinaryEncoder(output_stream)
@@ -110,30 +111,33 @@ class AvroDecoder(AvroClient):
110111

111112
def decode_record(self, record, encoding="binary"):
112113
"""
113-
Decodes a single record represented either as a byte or
114+
Decodes a single record represented either as a byte or
114115
base64 string, using the given Avro schema.
115116
116117
Returns a dictionary where each key is a field in the schema.
117118
"""
118-
self.logger.info('Decoding {rec} of type {type} using {schema} schema'.format(
119-
rec=record, type=encoding, schema=self.schema.name))
120-
119+
self.logger.info('Decoding {rec} of type {type} using {schema} schema'
120+
.format(rec=record, type=encoding,
121+
schema=self.schema.name))
122+
121123
if encoding == "base64":
122124
return self._decode_base64(record)
123125
elif encoding == "binary":
124126
return self._decode_binary(record)
125127
else:
126-
self.logger.error('Failed to decode record due to encoding type: {}'.format(encoding))
128+
self.logger.error(
129+
'Failed to decode record due to encoding type: {}'
130+
.format(encoding))
127131
raise AvroClientError(
128132
'Invalid encoding type: {}'.format(encoding))
129-
133+
130134
def _decode_base64(self, record):
131135
decoded_data = base64.b64decode(record).decode("utf-8")
132136
try:
133137
return json.loads(decoded_data)
134138
except Exception as e:
135139
if isinstance(decoded_data, bytes):
136-
self._decode_binary(decoded_data)
140+
self._decode_binary(decoded_data)
137141
else:
138142
self.logger.error('Failed to decode record: {}'.format(e))
139143
raise AvroClientError(
@@ -153,4 +157,4 @@ def _decode_binary(self, record):
153157

154158
class AvroClientError(Exception):
155159
def __init__(self, message=None):
156-
self.message = message
160+
self.message = message

tests/test_avro_client.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import json
22
import pytest
33

4-
from nypl_py_utils.classes.avro_client import AvroDecoder, AvroEncoder, AvroClientError
4+
from nypl_py_utils.classes.avro_client import (
5+
AvroDecoder, AvroEncoder, AvroClientError)
56
from requests.exceptions import ConnectTimeout
67

78
_TEST_SCHEMA = {'data': {'schema': json.dumps({
@@ -19,6 +20,7 @@
1920
]
2021
})}}
2122

23+
2224
class TestAvroClient:
2325

2426
@pytest.fixture
@@ -33,9 +35,12 @@ def test_avro_decoder_instance(self, requests_mock):
3335
'https://test_schema_url', text=json.dumps(_TEST_SCHEMA))
3436
return AvroDecoder('https://test_schema_url')
3537

36-
def test_get_json_schema(self, test_avro_encoder_instance, test_avro_decoder_instance):
37-
assert test_avro_encoder_instance.schema == _TEST_SCHEMA['data']['schema']
38-
assert test_avro_decoder_instance.schema == _TEST_SCHEMA['data']['schema']
38+
def test_get_json_schema(self, test_avro_encoder_instance,
39+
test_avro_decoder_instance):
40+
assert test_avro_encoder_instance.schema == _TEST_SCHEMA['data'][
41+
'schema']
42+
assert test_avro_decoder_instance.schema == _TEST_SCHEMA['data'][
43+
'schema']
3944

4045
def test_request_error(self, requests_mock):
4146
requests_mock.get('https://test_schema_url', exc=ConnectTimeout)
@@ -54,18 +59,21 @@ def test_missing_key_error(self, requests_mock):
5459
with pytest.raises(AvroClientError):
5560
AvroEncoder('https://test_schema_url')
5661

57-
def test_encode_record(self, test_avro_encoder_instance, test_avro_decoder_instance):
62+
def test_encode_record(self, test_avro_encoder_instance,
63+
test_avro_decoder_instance):
5864
TEST_RECORD = {'patron_id': 123, 'library_branch': 'aa'}
5965
encoded_record = test_avro_encoder_instance.encode_record(TEST_RECORD)
6066
assert type(encoded_record) is bytes
61-
assert test_avro_decoder_instance.decode_record(encoded_record) == TEST_RECORD
67+
assert test_avro_decoder_instance.decode_record(
68+
encoded_record) == TEST_RECORD
6269

6370
def test_encode_record_error(self, test_avro_encoder_instance):
6471
TEST_RECORD = {'patron_id': 123, 'bad_field': 'bad'}
6572
with pytest.raises(AvroClientError):
6673
test_avro_encoder_instance.encode_record(TEST_RECORD)
6774

68-
def test_encode_batch(self, test_avro_encoder_instance, test_avro_decoder_instance):
75+
def test_encode_batch(self, test_avro_encoder_instance,
76+
test_avro_decoder_instance):
6977
TEST_BATCH = [
7078
{'patron_id': 123, 'library_branch': 'aa'},
7179
{'patron_id': 456, 'library_branch': None},
@@ -89,10 +97,11 @@ def test_decode_record_binary(self, test_avro_decoder_instance):
8997
TEST_ENCODED_RECORD = b'\xf6\x01\x02\x04aa'
9098
assert test_avro_decoder_instance.decode_record(
9199
TEST_ENCODED_RECORD) == TEST_DECODED_RECORD
92-
100+
93101
def test_decode_record_b64(self, test_avro_decoder_instance):
94102
TEST_DECODED_RECORD = {"patron_id'": 123, "library_branch": "aa"}
95-
TEST_ENCODED_RECORD = "eyJwYXRyb25faWQnIjogMTIzLCAibGlicmFyeV9icmFuY2giOiAiYWEifQ=="
103+
TEST_ENCODED_RECORD = (
104+
"eyJwYXRyb25faWQnIjogMTIzLCAibGlicmFyeV9icmFuY2giOiAiYWEifQ==")
96105
assert test_avro_decoder_instance.decode_record(
97106
TEST_ENCODED_RECORD, "base64") == TEST_DECODED_RECORD
98107

0 commit comments

Comments
 (0)