Skip to content

Commit

Permalink
WIP - Allow access to request metadata on success
Browse files Browse the repository at this point in the history
  • Loading branch information
sedouard committed Nov 9, 2017
1 parent 8629450 commit d9f3976
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
8 changes: 8 additions & 0 deletions stripe/api_requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,14 @@ def interpret_response(self, rbody, rcode, rheaders):
rbody, rcode, rheaders)
if not (200 <= rcode < 300):
self.handle_error_response(rbody, rcode, resp, rheaders)
resp['response'] = {
'headers': rheaders,
'status_code': rcode,
}
if 'Idempotency-Key' in rheaders.keys():
resp['response']['idempotency_key'] = rheaders['Idempotency-Key']
if 'Request-Id' in rheaders.keys():
resp['response']['request_id'] = rheaders['Request-Id']
return resp

# Deprecated request handling. Will all be removed in 2.0
Expand Down
7 changes: 7 additions & 0 deletions tests/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
'source': 'tok_visa'
}

DUMMY_CHARGE_IDEMPOTENT = {
'amount': 100,
'currency': 'usd',
'source': 'tok_visa',
'idempotency_key': '12345'
}

DUMMY_PLAN = {
'amount': 2000,
'interval': 'month',
Expand Down
35 changes: 33 additions & 2 deletions tests/test_api_requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,32 @@ def test_empty_methods(self):
post_data = None

self.check_call(meth, post_data=post_data)
self.assertEqual({}, body)
self.assertEqual({
'response': {
'headers': {},
'status_code': 200
}
}, body)

def test_success_headers(self):
for meth in VALID_API_METHODS:
self.mock_response('{}', 200)

body, key = self.requestor.request(meth, self.valid_path, {})

if meth == 'post':
post_data = ''
else:
post_data = None

self.check_call(meth, post_data=post_data)
# remove headers
self.assertEqual({
'response': {
'headers': {},
'status_code': 200
}
}, body)

def test_methods_with_params_and_response(self):
for meth in VALID_API_METHODS:
Expand All @@ -297,7 +322,13 @@ def test_methods_with_params_and_response(self):

body, key = self.requestor.request(meth, self.valid_path,
params)
self.assertEqual({'foo': 'bar', 'baz': 6}, body)
self.assertEqual({
'foo': 'bar',
'baz': 6,
'response': {
'headers': {},
'status_code': 200
}}, body)

if meth == 'post':
self.check_call(
Expand Down
17 changes: 16 additions & 1 deletion tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from stripe import six

from tests.helper import (StripeTestCase, DUMMY_CHARGE)
from tests.helper import (StripeTestCase, DUMMY_CHARGE, DUMMY_CHARGE_IDEMPOTENT)


class FunctionalTests(StripeTestCase):
Expand Down Expand Up @@ -55,6 +55,7 @@ def test_refresh(self):
charge2.junk = 'junk'
charge2.refresh()
self.assertRaises(AttributeError, lambda: charge2.junk)


def test_list_accessors(self):
customer = stripe.Customer.create(source='tok_visa')
Expand All @@ -75,6 +76,20 @@ def test_response_headers(self):
except stripe.error.CardError as e:
self.assertTrue(e.request_id.startswith('req_'))

def test_success_response_headers(self):
charge = stripe.Charge.create(**DUMMY_CHARGE_IDEMPOTENT)
self.assertTrue(charge.response != None)
self.assertTrue(charge.response.headers != None)
self.assertEqual(charge.response.status_code, 200)
self.assertEqual(charge.response.headers['idempotency-key'], '12345')
self.assertTrue(charge.response.headers['request-id'].startswith('req_'))
# ensure case insentitivity
self.assertEqual(charge.response.headers['Idempotency-Key'], '12345')
self.assertTrue(charge.response.headers['Request-Id'].startswith('req_'))
# ensure helper keys
self.assertEqual(charge.response.idempotency_key, '12345')
self.assertTrue(charge.response.request_id.startswith('req_'))

def test_unicode(self):
# Make sure unicode requests can be sent
self.assertRaises(stripe.error.InvalidRequestError,
Expand Down

0 comments on commit d9f3976

Please # to comment.