From 4645a9ee4b502ca0d8c6db099ff6aac213bf75ef Mon Sep 17 00:00:00 2001 From: Renan Butkeraites Date: Thu, 5 Dec 2024 18:41:21 -0300 Subject: [PATCH] Refactor schema loading and property handling in KlaviyoStream 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. --- tap_klaviyo/client.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/tap_klaviyo/client.py b/tap_klaviyo/client.py index cf70d77..6c4b3e4 100644 --- a/tap_klaviyo/client.py +++ b/tap_klaviyo/client.py @@ -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: @@ -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