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 19, 2024
1 parent eb1bbff commit 64fbf0e
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 128 deletions.
14 changes: 14 additions & 0 deletions shift4/_request_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class RequestOptions:
__idempotency_key = None

def __init__(self, options):
self.__dict__.update(options)

def set_idempotency_key(self, idempotency_key):
self.__dict__["idempotency_key"] = idempotency_key

def has_idempotency_key(self):
return self.__dict__["idempotency_key"] is not None

def get_idempotency_key(self):
return self.__dict__["idempotency_key"]
11 changes: 0 additions & 11 deletions shift4/request_options.py

This file was deleted.

6 changes: 4 additions & 2 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 Expand Up @@ -70,6 +71,7 @@ def __create_headers(cls, request_options=None):
sys.version_info.micro,
)
headers = {"User-Agent": user_agent}
if request_options is not None and request_options.has_idempotency_key():
headers["Idempotency-Key"] = request_options.get_idempotency_key()
parsed_request_options = RequestOptions(request_options)
if parsed_request_options.has_idempotency_key():
headers["Idempotency-Key"] = parsed_request_options.get_idempotency_key()
return headers
45 changes: 26 additions & 19 deletions tests/integration/test_cards.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from shift4.request_options import RequestOptions
from . import random_email, random_string
from .data.cards import valid_card_req
from .testcase import TestCase
Expand Down Expand Up @@ -98,15 +97,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={"idempotency_key": idempotency_key},
)
second_call_response = api.cards.create(
customer["id"], card_req, request_options=request_options
customer["id"],
card_req,
request_options={"idempotency_key": idempotency_key},
)

# then
Expand All @@ -116,10 +118,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 +129,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={"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={"idempotency_key": random_string()},
)

# then
Expand Down Expand Up @@ -170,14 +172,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={"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={"idempotency_key": idempotency_key},
)

# then
Expand All @@ -192,8 +200,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 +221,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={"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={"idempotency_key": idempotency_key},
)

# then
Expand Down
40 changes: 21 additions & 19 deletions tests/integration/test_charges.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from shift4.request_options import RequestOptions
from . import random_string
from .data.charges import valid_charge_req
from .data.customers import valid_customer_req
Expand Down Expand Up @@ -92,16 +91,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={"idempotency_key": idempotency_key},
)
second_call_response = api.charges.create(
charge_req, request_options=request_options
charge_req,
request_options={"idempotency_key": idempotency_key},
)

# then
Expand All @@ -111,18 +111,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={"idempotency_key": random_string()},
)
second_call_response = api.charges.create(
charge_req, request_options=other_request_options
charge_req,
request_options={"idempotency_key": random_string()},
)

# then
Expand All @@ -143,15 +141,18 @@ 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={"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={"idempotency_key": idempotency_key},
)

# then
Expand All @@ -166,8 +167,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 +177,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={"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={"idempotency_key": idempotency_key},
)

# then
Expand Down
41 changes: 22 additions & 19 deletions tests/integration/test_credits.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from shift4.request_options import RequestOptions
from . import random_string
from .data.credits import valid_credit_req
from .data.customers import valid_customer_req
Expand Down Expand Up @@ -68,16 +67,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={"idempotency_key": idempotency_key},
)
second_call_response = api.credits.create(
credit_req, request_options=request_options
credit_req,
request_options={"idempotency_key": idempotency_key},
)

# then
Expand All @@ -87,18 +87,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={"idempotency_key": random_string()},
)
second_call_response = api.credits.create(
credit_req, request_options=other_request_options
credit_req,
request_options={"idempotency_key": random_string()},
)

# then
Expand All @@ -119,15 +117,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={"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={"idempotency_key": idempotency_key},
)

# then
Expand All @@ -142,8 +144,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 +154,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={"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={"idempotency_key": idempotency_key},
)

# then
Expand Down
8 changes: 3 additions & 5 deletions tests/integration/test_disputes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from waiting import wait

from shift4.request_options import RequestOptions
from . import random_string
from .data.cards import disputed_card_req
from .data.charges import valid_charge_req
Expand Down Expand Up @@ -62,21 +61,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={"idempotency_key": idempotency_key},
)
exception = self.assert_shift4_exception(
api.disputes.update,
dispute["id"],
{"evidence": {"customerName": "other name"}},
request_options=request_options,
request_options={"idempotency_key": idempotency_key},
)

# then
Expand Down
Loading

0 comments on commit 64fbf0e

Please sign in to comment.