Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Convert list_shipping_methods_for_checkout to return Promise (#18858)
Maciej Korycinski9a0221138334
diff --git a/saleor/checkout/fetch.py b/saleor/checkout/fetch.pyindex 2a2bd5858d..8b0d33fd9c 100644--- a/saleor/checkout/fetch.py+++ b/saleor/checkout/fetch.py@@ -9,6 +9,7 @@ from django.conf import settingsfrom django.db.models import Qfrom django.utils import timezonefrom prices import Money+from promise import Promisefrom ..core.db.connection import allow_writerfrom ..core.prices import quantize_price@@ -553,7 +554,7 @@ def get_available_built_in_shipping_methods_for_checkout_info(def fetch_external_shipping_methods_for_checkout_info(checkout_info,available_built_in_methods: list[ShippingMethodData],-) -> list[ShippingMethodData]:+) -> Promise[list[ShippingMethodData]]:from .webhooks.list_shipping_methods import list_shipping_methods_for_checkoutallow_replica = not (@@ -740,7 +741,7 @@ def fetch_shipping_methods_for_checkout(for shipping_method in fetch_external_shipping_methods_for_checkout_info(checkout_info=checkout_info,available_built_in_methods=list(built_in_shipping_methods_dict.values()),- )+ ).get()}all_methods = list(built_in_shipping_methods_dict.values()) + list(external_shipping_methods_dict.values()diff --git a/saleor/checkout/tests/test_fetch.py b/saleor/checkout/tests/test_fetch.pyindex 363438281f..650f7fbd0a 100644--- a/saleor/checkout/tests/test_fetch.py+++ b/saleor/checkout/tests/test_fetch.py@@ -851,7 +851,7 @@ def test_fetch_shipping_methods_for_checkout_with_external_shipping_method(},)- mocked_webhook.return_value = [available_shipping_method]+ mocked_webhook.return_value = Promise.resolve([available_shipping_method])checkout = checkout_with_itemcheckout.shipping_address = address@@ -905,7 +905,7 @@ def test_fetch_shipping_methods_for_checkout_updates_existing_external_shipping_},)- mocked_webhook.return_value = [available_shipping_method]+ mocked_webhook.return_value = Promise.resolve([available_shipping_method])checkout = checkout_with_itemcheckout.shipping_methods.create(@@ -969,7 +969,7 @@ def test_fetch_shipping_methods_for_checkout_removes_non_applicable_external_shi},)- mocked_webhook.return_value = [available_shipping_method]+ mocked_webhook.return_value = Promise.resolve([available_shipping_method])checkout = checkout_with_itemcheckout.shipping_methods.create(@@ -1033,7 +1033,7 @@ def test_fetch_shipping_methods_for_checkout_non_applicable_assigned_external_sh},)- mocked_webhook.return_value = [available_shipping_method]+ mocked_webhook.return_value = Promise.resolve([available_shipping_method])checkout = checkout_with_itemexpired_app_id = to_shipping_app_id(external_app, "expired-shipping-method-id")@@ -1117,7 +1117,9 @@ def test_fetch_shipping_methods_for_checkout_with_excluded_external_shipping_met])- mocked_list_shipping_methods.return_value = [unavailable_shipping_method]+ mocked_list_shipping_methods.return_value = Promise.resolve(+ [unavailable_shipping_method]+ )checkout = checkout_with_itemcheckout.shipping_address = addressdiff --git a/saleor/checkout/tests/webhooks/test_list_shipping_methods.py b/saleor/checkout/tests/webhooks/test_list_shipping_methods.pyindex 2b5f103126..9f8cfcf023 100644--- a/saleor/checkout/tests/webhooks/test_list_shipping_methods.py+++ b/saleor/checkout/tests/webhooks/test_list_shipping_methods.py@@ -34,7 +34,7 @@ def test_list_shipping_methods_for_checkout_webhook_response_none([],allow_replica=False,requestor=None,- )+ ).get()# thenassert not response@@ -61,7 +61,7 @@ def test_list_shipping_methods_for_checkout_set_cache(# whenlist_shipping_methods_for_checkout(checkout_with_item, [], allow_replica=False, requestor=None- )+ ).get()# thenassert mocked_webhook.called@@ -92,7 +92,7 @@ def test_list_shipping_methods_no_webhook_response_sets_short_term_cache(# whenlist_shipping_methods_for_checkout(checkout_with_item, [], allow_replica=False, requestor=None- )+ ).get()# thenassert mocked_webhook.called@@ -124,7 +124,7 @@ def test_list_shipping_methods_for_checkout_use_cache(# whenlist_shipping_methods_for_checkout(checkout_with_item, [], allow_replica=False, requestor=None- )+ ).get()# thenassert not mocked_webhook.called@@ -145,7 +145,7 @@ def test_list_shipping_methods_for_checkout_use_cache_for_empty_list(# whenlist_shipping_methods_for_checkout(checkout_with_item, [], allow_replica=False, requestor=None- )+ ).get()# thenassert not mocked_webhook.called@@ -197,7 +197,7 @@ def test_checkout_change_invalidates_cache_key()list_shipping_methods_for_checkout(checkout_with_item, [], allow_replica=False, requestor=None- )+ ).get()# thenassert cache_key != new_cache_key@@ -254,7 +254,7 @@ def test_ignore_selected_fields_on_generating_cache_key()list_shipping_methods_for_checkout(checkout_with_item, [], allow_replica=False, requestor=None- )+ ).get()# thenassert cache_key == new_cache_keydiff --git a/saleor/checkout/webhooks/list_shipping_methods.py b/saleor/checkout/webhooks/list_shipping_methods.pyindex cc7529c457..70fb0f5784 100644--- a/saleor/checkout/webhooks/list_shipping_methods.py+++ b/saleor/checkout/webhooks/list_shipping_methods.py@@ -3,6 +3,7 @@ import loggingfrom typing import TYPE_CHECKING, Any, Unionfrom django.conf import settings+from promise import Promisefrom pydantic import ValidationErrorfrom ...shipping.interface import ShippingMethodData@@ -10,7 +11,7 @@ from ...webhook.event_types import WebhookEventSyncTypefrom ...webhook.payloads import generate_checkout_payloadfrom ...webhook.response_schemas.shipping import ListShippingMethodsSchemafrom ...webhook.transport.synchronous.transport import (- trigger_webhook_sync_if_not_cached,+ trigger_webhook_sync_promise_if_not_cached,)from ...webhook.utils import get_webhooks_for_eventfrom ..models import Checkout@@ -29,17 +30,21 @@ def list_shipping_methods_for_checkout(built_in_shipping_methods: list["ShippingMethodData"],allow_replica: bool,requestor: Union["App", "User", None],-) -> list["ShippingMethodData"]:+) -> Promise[list["ShippingMethodData"]]:methods: list[ShippingMethodData] = []event_type = WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUTwebhooks = get_webhooks_for_event(event_type)- if webhooks:- payload = generate_checkout_payload(checkout, requestor)- cache_data = _get_cache_data_for_shipping_list_methods_for_checkout(payload)- for webhook in webhooks:- response_data = trigger_webhook_sync_if_not_cached(+ if not webhooks:+ return Promise.resolve(methods)++ promised_responses = []+ payload = generate_checkout_payload(checkout, requestor)+ cache_data = _get_cache_data_for_shipping_list_methods_for_checkout(payload)+ for webhook in webhooks:+ promised_responses.append(+ trigger_webhook_sync_promise_if_not_cached(event_type=event_type,- payload=payload,+ static_payload=payload,webhook=webhook,cache_data=cache_data,allow_replica=allow_replica,@@ -48,13 +53,20 @@ def list_shipping_methods_for_checkout(cache_timeout=CACHE_TIME_SHIPPING_LIST_METHODS_FOR_CHECKOUT,requestor=requestor,)+ )+ def process_responses(responses: list[Any]):+ for response_data, webhook in zip(responses, webhooks, strict=True):if response_data:shipping_methods = _parse_list_shipping_methods_response(response_data, webhook.app, checkout.currency)methods.extend(shipping_methods)- return [method for method in methods if method.price.currency == checkout.currency]+ return [+ method for method in methods if method.price.currency == checkout.currency+ ]++ return Promise.all(promised_responses).then(process_responses)def _parse_list_shipping_methods_response(diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.py b/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.pyindex 71f61a7795..400538a99c 100644--- a/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.py+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.py@@ -8,6 +8,7 @@ import pytestfrom django.test import override_settingsfrom django.utils import timezonefrom freezegun import freeze_time+from promise import Promisefrom .....checkout.actions import call_checkout_info_eventfrom .....checkout.error_codes import CheckoutErrorCode@@ -1378,7 +1379,7 @@ def test_checkout_lines_marks_shipping_methods_as_stale(checkout_delivery,):# given- mocked_webhook.return_value = []+ mocked_webhook.return_value = Promise.resolve([])checkout = checkout_with_itemcheckout.assigned_delivery = checkout_delivery(checkout)checkout.save(update_fields=["assigned_delivery"])