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

TDL-14354 log request_id #135

Merged
merged 15 commits into from
May 27, 2022
13 changes: 13 additions & 0 deletions tap_stripe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import stripe
import stripe.error
from stripe.stripe_object import StripeObject
from stripe.api_requestor import APIRequestor
from stripe.util import convert_to_stripe_object
import singer
from singer import utils, Transformer, metrics
Expand Down Expand Up @@ -392,6 +393,18 @@ def reduce_foreign_keys(rec, stream_name):
rec['lines'][k] = [li.to_dict_recursive() for li in val]
return rec

def new_request(self, method, url, params=None, headers=None):
'''The new request function to overwrite the request() in the APIRequestor class.'''
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'''The new request function to overwrite the request() in the APIRequestor class.'''
'''The new request function to overwrite the request() function of the APIRequestor class of SDK.'''

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

U[dated the comment

rbody, rcode, rheaders, my_api_key = self.request_raw(
method.lower(), url, params, headers, is_streaming=False
)
resp = self.interpret_response(rbody, rcode, rheaders)
LOGGER.debug(f'request id : {resp.request_id}')
return resp, my_api_key

# To log the request_id, we replaced the request() function of the APIRequestor
# class, captured the response and logged the request_id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# class, captured the response and logged the request_id
# class of SDK, captured the response, and logged the request_id.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the comment

APIRequestor.request = new_request

def paginate(sdk_obj, filter_key, start_date, end_date, stream_name, request_args=None, limit=100):
yield from sdk_obj.list(
Expand Down
23 changes: 20 additions & 3 deletions tests/test_all_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,10 @@
# As for the `price` field added in the schema, the API doc doesn't mention any
# `trial_period_days` in the field, hence skipping the assertion error for the same.
KNOWN_NESTED_MISSING_FIELDS = {
'subscription_items': {'price': 'recurring.trial_period_days'}
'subscription_items': {'price': 'recurring.trial_period_days'},
'charges': {'payment_method_details': 'card.mandate'},
'payment_intents': {'charges': 'payment_method_details.card.mandate',
'payment_method_options': 'card.mandate_options'}
}

class ALlFieldsTest(BaseTapTest):
Expand Down Expand Up @@ -548,8 +551,22 @@ def all_fields_test(self, streams_to_test):
f"AssertionError({failure_1})")

nested_key = KNOWN_NESTED_MISSING_FIELDS.get(stream, {})
if self.find_nested_key(nested_key, expected_field_value, field):
continue
# Check whether expected_field_value is list or not.
# If expected_field_value is list then loop through each item of list
if type(expected_field_value) == list:
dbshah1212 marked this conversation as resolved.
Show resolved Hide resolved
is_fickle = True
for each_expected_field_value in expected_field_value:
if self.find_nested_key(nested_key, each_expected_field_value, field):
continue
else:
is_fickle = False
break

if is_fickle:
continue
else:
if self.find_nested_key(nested_key, expected_field_value, field):
continue

if field in KNOWN_FAILING_FIELDS[stream] or field in FIELDS_TO_NOT_CHECK[stream]:
continue # skip the following wokaround
Expand Down
33 changes: 33 additions & 0 deletions tests/unittests/test_log_request_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import unittest
from unittest import mock
from tap_stripe import new_request

class MockRequest():
'''Mock Request object'''
def __init__(self, response):
self.last_response = response

def request_raw(self, method, url, params=None, supplied_headers=None, is_streaming=False):
return {}, {}, {}, {}

def interpret_response(self, rbody, rcode, rheaders):
return get_request_id()

class MockResponse():
'''Mock response object which contains the request_id'''
def __init__(self, request_id):
self.request_id = request_id


def get_request_id():
'''Return the MockRequest object which contains request_id'''
response = MockResponse('dummy_request_id')
return response

class TestDebugLogger(unittest.TestCase):
@mock.patch('tap_stripe.LOGGER.debug')
def test_debug_logger(self, mock_debug):
'''Test that the debug is called with proper request id.'''
mock_request = MockRequest('url')
new_request(mock_request, 'GET', 'dummy_url')
mock_debug.assert_called_with('request id : dummy_request_id')