Skip to content

Commit

Permalink
support-for - MR comment applied
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Schielmann committed Aug 20, 2024
1 parent eb1bbff commit 19ac8af
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 114 deletions.
17 changes: 11 additions & 6 deletions shift4/request_options.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
class RequestOptions:
__idempotency_key = None
_idempotency_key_option_name = "idempotency_key"
_supported_options = [_idempotency_key_option_name]


def set_idempotency_key(self, idempotency_key):
self.__idempotency_key = idempotency_key
class RequestOptions:
def __init__(self, options):
if options is not None:
for key, value in options.items():
if key in _supported_options:
setattr(self, key, value)

def has_idempotency_key(self):
return self.__idempotency_key is not None
return hasattr(self, _idempotency_key_option_name)

def get_idempotency_key(self):
return self.__idempotency_key
return getattr(self, _idempotency_key_option_name)
1 change: 1 addition & 0 deletions shift4/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import shift4 as api
from shift4.__version__ import __version__
from shift4.request_options import RequestOptions


class Resource(object):
Expand Down
44 changes: 26 additions & 18 deletions tests/integration/test_cards.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,18 @@ def test_will_not_create_duplicate_if_same_idempotency_key_is_used(self, api):
"cvc": "123",
"cardholderName": (random_string()),
}
request_options = RequestOptions()
request_options.set_idempotency_key(random_string())
idempotency_key = random_string()

# when
first_call_response = api.cards.create(
customer["id"], card_req, request_options=request_options
customer["id"],
card_req,
request_options=RequestOptions({"idempotency_key": idempotency_key}),
)
second_call_response = api.cards.create(
customer["id"], card_req, request_options=request_options
customer["id"],
card_req,
request_options=RequestOptions({"idempotency_key": idempotency_key}),
)

# then
Expand All @@ -116,10 +119,6 @@ def test_will_create_two_instances_if_different_idempotency_keys_are_used(
self, api
):
# given
request_options = RequestOptions()
request_options.set_idempotency_key(random_string())
other_request_options = RequestOptions()
other_request_options.set_idempotency_key(random_string())
customer = api.customers.create({"email": random_email()})
card_req = {
"number": "4242424242424242",
Expand All @@ -131,10 +130,14 @@ def test_will_create_two_instances_if_different_idempotency_keys_are_used(

# when
first_call_response = api.cards.create(
customer["id"], card_req, request_options=request_options
customer["id"],
card_req,
request_options=RequestOptions({"idempotency_key": random_string()}),
)
second_call_response = api.cards.create(
customer["id"], card_req, request_options=other_request_options
customer["id"],
card_req,
request_options=RequestOptions({"idempotency_key": random_string()}),
)

# then
Expand Down Expand Up @@ -170,14 +173,20 @@ def test_will_throw_exception_if_same_idempotency_key_is_used_for_two_different_
"cvc": "123",
"cardholderName": (random_string()),
}
request_options = RequestOptions()
request_options.set_idempotency_key(random_string())
idempotency_key = random_string()

# when
api.cards.create(customer["id"], card_req, request_options=request_options)
api.cards.create(
customer["id"],
card_req,
request_options=RequestOptions({"idempotency_key": idempotency_key}),
)
card_req["cvc"] = "042"
exception = self.assert_shift4_exception(
api.cards.create, customer["id"], card_req, request_options=request_options
api.cards.create,
customer["id"],
card_req,
request_options=RequestOptions({"idempotency_key": idempotency_key}),
)

# then
Expand All @@ -192,8 +201,7 @@ def test_will_throw_exception_if_same_idempotency_key_is_used_for_two_different_
self, api
):
# given
request_options = RequestOptions()
request_options.set_idempotency_key(random_string())
idempotency_key = random_string()
customer = api.customers.create({"email": random_email()})
created = api.cards.create(customer["id"], valid_card_req())

Expand All @@ -214,15 +222,15 @@ def test_will_throw_exception_if_same_idempotency_key_is_used_for_two_different_
created["customerId"],
created["id"],
update_request,
request_options=request_options,
request_options=RequestOptions({"idempotency_key": idempotency_key}),
)
update_request["expMonth"] = "06"
exception = self.assert_shift4_exception(
api.cards.update,
created["customerId"],
created["id"],
update_request,
request_options=request_options,
request_options=RequestOptions({"idempotency_key": idempotency_key}),
)

# then
Expand Down
40 changes: 22 additions & 18 deletions tests/integration/test_charges.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,17 @@ def test_list(self, api):

def test_will_not_create_duplicate_if_same_idempotency_key_is_used(self, api):
# given
request_options = RequestOptions()
request_options.set_idempotency_key(random_string())
idempotency_key = random_string()
charge_req = valid_charge_req()

# when
first_call_response = api.charges.create(
charge_req, request_options=request_options
charge_req,
request_options=RequestOptions({"idempotency_key": idempotency_key}),
)
second_call_response = api.charges.create(
charge_req, request_options=request_options
charge_req,
request_options=RequestOptions({"idempotency_key": idempotency_key}),
)

# then
Expand All @@ -111,18 +112,16 @@ def test_will_create_two_instances_if_different_idempotency_keys_are_used(
self, api
):
# given
request_options = RequestOptions()
request_options.set_idempotency_key(random_string())
other_request_options = RequestOptions()
other_request_options.set_idempotency_key(random_string())
charge_req = valid_charge_req()

# when
first_call_response = api.charges.create(
charge_req, request_options=request_options
charge_req,
request_options=RequestOptions({"idempotency_key": random_string()}),
)
second_call_response = api.charges.create(
charge_req, request_options=other_request_options
charge_req,
request_options=RequestOptions({"idempotency_key": random_string()}),
)

# then
Expand All @@ -143,15 +142,19 @@ def test_will_throw_exception_if_same_idempotency_key_is_used_for_two_different_
self, api
):
# given
request_options = RequestOptions()
request_options.set_idempotency_key(random_string())
idempotency_key = random_string()
charge_req = valid_charge_req()

# when
api.charges.create(charge_req, request_options=request_options)
api.charges.create(
charge_req,
request_options=RequestOptions({"idempotency_key": idempotency_key}),
)
charge_req["amount"] = "42"
exception = self.assert_shift4_exception(
api.charges.create, charge_req, request_options=request_options
api.charges.create,
charge_req,
request_options=RequestOptions({"idempotency_key": idempotency_key}),
)

# then
Expand All @@ -166,8 +169,7 @@ def test_will_throw_exception_if_same_idempotency_key_is_used_for_two_different_
self, api
):
# given
request_options = RequestOptions()
request_options.set_idempotency_key(random_string())
idempotency_key = random_string()
charge_req = valid_charge_req()
created = api.charges.create(charge_req)
update_request_params = {
Expand All @@ -177,14 +179,16 @@ def test_will_throw_exception_if_same_idempotency_key_is_used_for_two_different_

# when
api.charges.update(
created["id"], update_request_params, request_options=request_options
created["id"],
update_request_params,
request_options=RequestOptions({"idempotency_key": idempotency_key}),
)
update_request_params["description"] = "other description"
exception = self.assert_shift4_exception(
api.charges.update,
created["id"],
update_request_params,
request_options=request_options,
request_options=RequestOptions({"idempotency_key": idempotency_key}),
)

# then
Expand Down
40 changes: 22 additions & 18 deletions tests/integration/test_credits.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,17 @@ def test_list(self, api):

def test_will_not_create_duplicate_if_same_idempotency_key_is_used(self, api):
# given
request_options = RequestOptions()
request_options.set_idempotency_key(random_string())
idempotency_key = random_string()
credit_req = valid_credit_req()

# when
first_call_response = api.credits.create(
credit_req, request_options=request_options
credit_req,
request_options=RequestOptions({"idempotency_key": idempotency_key}),
)
second_call_response = api.credits.create(
credit_req, request_options=request_options
credit_req,
request_options=RequestOptions({"idempotency_key": idempotency_key}),
)

# then
Expand All @@ -87,18 +88,16 @@ def test_will_create_two_instances_if_different_idempotency_keys_are_used(
self, api
):
# given
request_options = RequestOptions()
request_options.set_idempotency_key(random_string())
other_request_options = RequestOptions()
other_request_options.set_idempotency_key(random_string())
credit_req = valid_credit_req()

# when
first_call_response = api.credits.create(
credit_req, request_options=request_options
credit_req,
request_options=RequestOptions({"idempotency_key": random_string()}),
)
second_call_response = api.credits.create(
credit_req, request_options=other_request_options
credit_req,
request_options=RequestOptions({"idempotency_key": random_string()}),
)

# then
Expand All @@ -119,15 +118,19 @@ def test_will_throw_exception_if_same_idempotency_key_is_used_for_two_different_
self, api
):
# given
request_options = RequestOptions()
request_options.set_idempotency_key(random_string())
idempotency_key = random_string()
credit_req = valid_credit_req()

# when
api.credits.create(credit_req, request_options=request_options)
api.credits.create(
credit_req,
request_options=RequestOptions({"idempotency_key": idempotency_key}),
)
credit_req["amount"] = "42"
exception = self.assert_shift4_exception(
api.credits.create, credit_req, request_options=request_options
api.credits.create,
credit_req,
request_options=RequestOptions({"idempotency_key": idempotency_key}),
)

# then
Expand All @@ -142,8 +145,7 @@ def test_will_throw_exception_if_same_idempotency_key_is_used_for_two_different_
self, api
):
# given
request_options = RequestOptions()
request_options.set_idempotency_key(random_string())
idempotency_key = random_string()
credit_req = valid_credit_req()
created = api.credits.create(credit_req)
update_request_params = {
Expand All @@ -153,14 +155,16 @@ def test_will_throw_exception_if_same_idempotency_key_is_used_for_two_different_

# when
api.credits.update(
created["id"], update_request_params, request_options=request_options
created["id"],
update_request_params,
request_options=RequestOptions({"idempotency_key": idempotency_key}),
)
update_request_params["description"] = "other description"
exception = self.assert_shift4_exception(
api.credits.update,
created["id"],
update_request_params,
request_options=request_options,
request_options=RequestOptions({"idempotency_key": idempotency_key}),
)

# then
Expand Down
7 changes: 3 additions & 4 deletions tests/integration/test_disputes.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,20 @@ def test_will_throw_exception_if_same_idempotency_key_is_used_for_two_different_
self, api
):
# given
request_options = RequestOptions()
request_options.set_idempotency_key(random_string())
idempotency_key = random_string()
[dispute, _] = create_dispute(api)
evidence_customer_name = "Test Customer"
# when
api.disputes.update(
dispute["id"],
{"evidence": {"customerName": evidence_customer_name}},
request_options=request_options,
request_options=RequestOptions({"idempotency_key": idempotency_key}),
)
exception = self.assert_shift4_exception(
api.disputes.update,
dispute["id"],
{"evidence": {"customerName": "other name"}},
request_options=request_options,
request_options=RequestOptions({"idempotency_key": idempotency_key}),
)

# then
Expand Down
Loading

0 comments on commit 19ac8af

Please sign in to comment.