-
Notifications
You must be signed in to change notification settings - Fork 433
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #371 from sedouard/success-response-headers
Allow access to request metadata on success
- Loading branch information
Showing
9 changed files
with
159 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from stripe import util | ||
|
||
|
||
class StripeResponse: | ||
|
||
def __init__(self, body, code, headers): | ||
self.body = body | ||
self.code = code | ||
self.headers = headers | ||
self.data = util.json.loads(body) | ||
|
||
@property | ||
def idempotency_key(self): | ||
try: | ||
return self.headers['idempotency-key'] | ||
except KeyError: | ||
return None | ||
|
||
@property | ||
def request_id(self): | ||
try: | ||
return self.headers['request-id'] | ||
except KeyError: | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from __future__ import absolute_import, division, print_function | ||
|
||
import time | ||
|
||
from stripe import util | ||
from stripe.stripe_response import StripeResponse | ||
from tests.helper import StripeUnitTestCase | ||
|
||
class StripeResponseTests(StripeUnitTestCase): | ||
def test_idempotency_key(self): | ||
response, headers, body, code = StripeResponseTests.mock_stripe_response() | ||
self.assertEqual(response.idempotency_key, headers['idempotency-key']) | ||
|
||
def test_request_id(self): | ||
response, headers, body, code = StripeResponseTests.mock_stripe_response() | ||
self.assertEqual(response.request_id, headers['request-id']) | ||
|
||
def test_code(self): | ||
response, headers, body, code = StripeResponseTests.mock_stripe_response() | ||
self.assertEqual(response.code, code) | ||
|
||
def test_headers(self): | ||
response, headers, body, code = StripeResponseTests.mock_stripe_response() | ||
self.assertEqual(response.headers, headers) | ||
|
||
def test_body(self): | ||
response, headers, body, code = StripeResponseTests.mock_stripe_response() | ||
self.assertEqual(response.body, body) | ||
|
||
def test_data(self): | ||
response, headers, body, code = StripeResponseTests.mock_stripe_response() | ||
self.assertEqual(response.data, util.json.loads(body)) | ||
|
||
@staticmethod | ||
def mock_stripe_response(): | ||
code = 200 | ||
headers = StripeResponseTests.mock_headers() | ||
body = StripeResponseTests.mock_body() | ||
response = StripeResponse(body, code, headers) | ||
return response, headers, body, code | ||
@staticmethod | ||
def mock_headers(): | ||
return { | ||
'idempotency-key': '123456', | ||
'request-id': 'req_123456' | ||
} | ||
|
||
@staticmethod | ||
def mock_body(): | ||
return '{ "id": "ch_12345", "object": "charge", "amount": 1 }' |