Skip to content

fix: improve ct call in program price api #379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 57 additions & 29 deletions commerce_coordinator/apps/commercetools/http_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
API clients for commerceetool app.
"""
import logging
import time
from typing import Dict, List, Optional, Union

import requests
from django.conf import settings
from requests.exceptions import HTTPError

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -41,51 +41,76 @@
return response.json()["access_token"]

def _make_request(
self,
method: str,
endpoint: str,
params: Optional[Dict] = None,
json: Optional[Dict] = None,
) -> Union[Dict, List]:
self,
method: str,
endpoint: str,
params: Optional[Dict] = None,
json: Optional[Dict] = None,
total_retries: int = 2,
base_backoff: int = 1,
) -> Union[Dict, None]:
"""
Make an HTTP request to the Commercetools API.
Make an HTTP request to the Commercetools API with retry logic.

Args:
method (str): HTTP method (e.g., "GET", "POST").
endpoint (str): API endpoint (e.g., "/cart-discounts").
params (Optional[Dict]): Query parameters.
json (Optional[Dict]): JSON payload for POST/PUT requests.
total_retries (int): Number of retries after the first attempt.
base_backoff (int): Base backoff time in seconds.

Returns:
Union[Dict, List]: JSON response from the API or None if the request fails.
Union[Dict, None]: JSON response from the API or None if all retries fail.
"""
url = f"{self.config['apiUrl']}/{self.config['projectKey']}/{endpoint}"
headers = {
"Authorization": f"Bearer {self.access_token}",
"Content-Type": "application/json",
}
try:
response = requests.request(
method,
url,
headers=headers,
params=params,
json=json,
timeout=settings.REQUEST_READ_TIMEOUT_SECONDS,
)
response.raise_for_status()
return response.json()
except HTTPError as err:
if response is not None:
response_message = response.json().get('message', 'No message provided.')
logger.error(
"API request for endpoint: %s failed with error: %s and message: %s",
endpoint, err, response_message

def attempt(attempt_number: int) -> Union[Dict, List, None]:
try:
response = requests.request(
method,
url,
headers=headers,
params=params,
json=json,
timeout=settings.REQUEST_READ_TIMEOUT_SECONDS,
)

response.raise_for_status()
return response.json()
except requests.RequestException as err:
response = getattr(err, 'response', None)
if response is not None:
try:
response_message = response.json().get('message', 'No message provided.')
except ValueError:
response_message = getattr(response, 'text', None) or 'No message provided.'
else:
response_message = str(err)

if attempt_number >= total_retries:
logger.error(
"CTCustomAPIClient: API request failed for endpoint: %s after attempt #%s"
" with error: %s and message: %s",
endpoint, attempt_number, err, response_message
)
return None

next_attempt = attempt_number + 1
next_backoff = base_backoff * next_attempt
logger.warning(
"API request failed for endpoint: %s with error: %s and message: %s. "
"Retrying attempt #%s in %s seconds...",
endpoint, err, response_message, next_attempt, next_backoff
)
else:
logger.error("API request for endpoint: %s failed with error: %s", endpoint, err)
time.sleep(next_backoff)
return attempt(next_attempt)

return None
return attempt(0)

def get_ct_bundle_offers_without_code(self) -> Dict:
"""
Expand Down Expand Up @@ -129,6 +154,9 @@
"products",
params=params,
)
if program is None:
return []

Check failure on line 158 in commerce_coordinator/apps/commercetools/http_api_client.py

View workflow job for this annotation

GitHub Actions / tests (ubuntu-latest, 3.12, django42)

Missing coverage

Missing coverage on line 158

entitlement_products = []
results = program.get("results")
if not results:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import requests_mock
from django.test import TestCase
from requests import Response
from requests.exceptions import HTTPError

from commerce_coordinator.apps.commercetools.http_api_client import CTCustomAPIClient
Expand Down Expand Up @@ -60,6 +61,71 @@ def test_make_request_failure(self):
response = self.client._make_request("GET", "products")
self.assertIsNone(response)

def test_make_request_retries_and_fails(self):
"""Test that the function retries the correct number of times and fails."""
with requests_mock.Mocker() as mocker, patch("time.sleep", return_value=None) as mock_sleep:
mocker.get(
f"{self.client.config['apiUrl']}/{self.client.config['projectKey']}/products",
status_code=502,
json={"message": "Bad Gateway"}
)

with patch("requests.Response.raise_for_status", side_effect=HTTPError("502 Bad Gateway")):
# pylint: disable=protected-access
response = self.client._make_request("GET", "products")
self.assertIsNone(response)

self.assertEqual(mock_sleep.call_count, 2) # 2 retries

def test_make_request_retries_and_succeeds(self):
"""Test that the function retries and eventually succeeds."""
with requests_mock.Mocker() as mocker, patch("time.sleep", return_value=None) as mock_sleep:
# Simulate failure on the first attempt and success on the second
mocker.get(
f"{self.client.config['apiUrl']}/{self.client.config['projectKey']}/products",
[
{"status_code": 500, "json": {"message": "Internal Server Error"}},
{"status_code": 200, "json": {"results": [{"id": "mock_id"}]}},
]
)

response = self.client._make_request("GET", "products") # pylint: disable=protected-access
self.assertIsNotNone(response)
self.assertEqual(response, {"results": [{"id": "mock_id"}]})

self.assertEqual(mock_sleep.call_count, 1) # 1 retry

@patch("commerce_coordinator.apps.commercetools.http_api_client.logger.error")
def test_make_request_response_json_throws_value_error(self, mock_logger):
"""Test that the function handles ValueError when response.json() raises an exception."""
with requests_mock.Mocker() as mocker, patch("time.sleep", return_value=None):
url = f"{self.client.config['apiUrl']}/{self.client.config['projectKey']}/products"

mocker.get(
url,
status_code=502,
text="Bad Gateway",
)

bad_response = Response()
bad_response.status_code = 502
error_with_response = HTTPError("502 Bad Gateway")
error_with_response.response = bad_response

with patch("requests.Response.raise_for_status", side_effect=error_with_response):
# pylint: disable=protected-access
self.client._make_request("GET", "products")

mock_logger.assert_called_with(
"CTCustomAPIClient: API request failed for endpoint: %s after attempt #%s with "
"error: %s and message: %s", "products", 2, error_with_response, 'No message provided.'
)

# expects that ValueError is handled properly and
# Verify that 'No message provided.' was part of the log
log_message = mock_logger.call_args[0][4]
self.assertIn("No message provided.", str(log_message))

def test_get_ct_bundle_offers_without_code(self):
with requests_mock.Mocker() as mocker:
mock_response = {"results": [{"id": "mock_id"}]}
Expand Down
Loading