Skip to content

Commit

Permalink
Refactor schema loading and property handling in KlaviyoStream
Browse files Browse the repository at this point in the history
This commit improves the schema loading process by using a context manager for file handling, ensuring proper resource management. Additionally, it updates the handling of the replication key, ensuring it is included in the property list only if it is defined. These changes enhance code readability and maintainability while ensuring that the schema properties are correctly populated.
  • Loading branch information
butkeraites-hotglue committed Dec 5, 2024
1 parent 9419efe commit da966c7
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions tap_klaviyo/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ def get_abs_path(self, path):
return os.path.join(os.path.dirname(os.path.realpath(__file__)), path)

def load_schema(self, name):
return json.load(open(self.get_abs_path('schemas/{}.json'.format(name))))
schema_path = self.get_abs_path(f'schemas/{name}.json')
with open(schema_path, 'r') as schema_file:
return json.load(schema_file)

def _fill_missing_properties(self, property_list):
try:
Expand Down Expand Up @@ -233,22 +235,15 @@ def get_schema(self) -> dict:
properties.append(
th.Property(name, self.get_jsonschema_type(record[name]))
)
# if the rep_key is not at a header level add updated as default
if (
self.replication_key is not None
and self.replication_key not in record.keys()
):
properties.append(
th.Property(self.replication_key, th.DateTimeType)
)
# Return the list as a JSON Schema dictionary object
property_list = th.PropertiesList(*properties).to_dict()
else:
property_list = th.PropertiesList(
th.Property("id", th.StringType),
th.Property(self.replication_key, th.DateTimeType),
).to_dict()
property_list = self._fill_missing_properties(property_list)
if self.replication_key is not None:
property_list["properties"].update(th.Property(self.replication_key,th.DateTimeType).to_dict())
return property_list

@cached_property
Expand Down

0 comments on commit da966c7

Please sign in to comment.