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

🎉 Source Chargebee: add "custom fields" to the relevant data streams #17056

Merged
Show file tree
Hide file tree
Changes from 2 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,20 @@
}
}
}
},
"custom_fields": {
"type": ["null", "array"],
"items" : {
"type" : ["null", "object"],
"properties": {
"name" : {
"type" : ["null", "string"]
},
"value": {
"type": ["null", "string"]
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,19 @@
"object": {
"type": ["string", "null"]
}
},
"custom_fields": {
"type": ["null", "array"],
"items": {
"type": ["null", "object"],
"properties" : {
"name" : {
"type" : ["null", "string"]
},
"value" : {
"type" : ["null", "string"]
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,20 @@
}
}
}
},
"custom_fields": {
"type": ["null", "array"],
"items": {
"type": ["null", "object"],
"properties": {
"name": {
"type": ["null", "string"]
},
"value": {
"type": ["null", "string"]
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,20 @@
}
}
}
},
"custom_fields": {
"type": ["null", "array"],
"items": {
"type": ["null", "object"],
"properties": {
"name": {
"type": ["null", "string"]
},
"value": {
"type": ["null", "string"]
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,20 @@
"meta_data": {
"type": ["object", "null"],
"properties": {}
},
"custom_fields": {
"type": ["null", "array"],
"items": {
"type": ["null", "object"],
"properties": {
"name": {
"type": ["null", "string"]
},
"value": {
"type": ["null", "string"]
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@
}
}
}
},
"custom_fields": {
"type": ["null", "array"],
"items": {
"type": ["null", "object"],
"properties": {
"name": {
"type": ["null", "string"]
},
"value": {
"type": ["null", "string"]
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,20 @@
"enum": ["not_validated", "valid", "partially_valid", "invalid"]
}
}
},
"custom_fields": {
"type": ["null", "array"],
"items": {
"type": ["null", "object"],
"properties": {
"name": {
"type": ["null", "string"]
},
"value": {
"type": ["null", "string"]
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@
},
"object": {
"type": ["string", "null"]
},
"custom_fields": {
"type": ["null", "array"],
"items": {
"type": ["null", "object"],
"properties": {
"name": {
"type": ["null", "string"]
},
"value": {
"type": ["null", "string"]
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,20 @@
},
"object": {
"type": ["string", "null"]
},
"custom_fields": {
"type": ["null", "array"],
"items": {
"type": ["null", "object"],
"properties": {
"name": {
"type": ["null", "string"]
},
"value": {
"type": ["null", "string"]
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,20 @@
}
}
}
},
"custom_fields": {
"type": ["null", "array"],
"items": {
"type": ["null", "object"],
"properties": {
"name": {
"type": ["null", "string"]
},
"value": {
"type": ["null", "string"]
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,20 @@
}
}
}
},
"custom_fields": {
"type": ["null", "array"],
"items": {
"type": ["null", "object"],
"properties": {
"name": {
"type": ["null", "string"]
},
"value": {
"type": ["null", "string"]
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,20 @@
}
}
}
},
"custom_fields": {
"type": ["null", "array"],
"items": {
"type": ["null", "object"],
"properties": {
"name": {
"type": ["null", "string"]
},
"value": {
"type": ["null", "string"]
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,20 @@
}
}
}
},
"custom_fields": {
"type": ["null", "array"],
"items": {
"type": ["null", "object"],
"properties": {
"name": {
"type": ["null", "string"]
},
"value": {
"type": ["null", "string"]
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from chargebee.models import Transaction as TransactionModel

from .rate_limiting import default_backoff_handler
from .utils import transform_custom_fields

# Backoff params below according to Chargebee's guidance on rate limit.
# https://apidocs.chargebee.com/docs/api?prod_cat_ver=2#api_rate_limits
Expand Down Expand Up @@ -59,7 +60,7 @@ def request_params(

def parse_response(self, list_result: ListResult, **kwargs) -> Iterable[Mapping]:
for message in list_result:
yield message._response[self.name]
yield from transform_custom_fields(message._response[self.name])

@default_backoff_handler(max_tries=MAX_TRIES, factor=MAX_TIME)
def _send_request(self, **kwargs) -> ListResult:
Expand Down Expand Up @@ -133,7 +134,7 @@ def parse_response(
for message in list_result:
record = message._response[self.name]
if record[self.cursor_field] > starting_point:
yield record
yield from transform_custom_fields(record)

def get_updated_state(self, current_stream_state: MutableMapping[str, Any], latest_record: Mapping[str, Any]):
"""
Expand Down Expand Up @@ -179,7 +180,7 @@ def request_params(

def parse_response(self, list_result: ListResult, **kwargs) -> Iterable[Mapping]:
for message in list_result:
yield message._response[self.name]
yield from transform_custom_fields(message._response[self.name])

def get_updated_state(self, current_stream_state: MutableMapping[str, Any], latest_record: Mapping[str, Any]):
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from typing import Any, Dict


def transform_custom_fields(record: Dict[str, Any]) -> Dict[str, Any]:
"""
Method to detect custom fields that start with 'cf_' from chargbee models.
Args:
record:
{
...
'cf_custom_fields': 'some_value',
...
}

Returns:
record:
{
...
'custom_fields': [{
'name': 'cf_custom_fields',
'value': some_value'
}],
...
}
"""

custom_fields = [key for key in record.keys() if key.startswith('cf_')]
new_custom_fields = []

if custom_fields:
for custom_field in custom_fields:
new_custom_fields.append({"name": custom_field, "value": record[custom_field]})

record['custom_fields'] = new_custom_fields

yield record