FC-0001: Remove old EdxRestAPIClient usage across the platform (#30301)

* refactor: remove EdxRestAPIClient

* test: update tests according to EdxRestAPIClient removal

* fix: remove unused import
This commit is contained in:
Eugene Dyudyunov
2022-05-09 19:48:26 +03:00
committed by GitHub
parent baf1cbc6fb
commit 289e682b8f
36 changed files with 869 additions and 698 deletions

View File

@@ -2,15 +2,14 @@
import logging
from urllib.parse import urljoin
from django.conf import settings
from django.utils.translation import gettext_lazy as _
from urllib.parse import urljoin # lint-amnesty, pylint: disable=wrong-import-order
from requests.exceptions import ConnectionError, Timeout # pylint: disable=redefined-builtin
from slumber.exceptions import SlumberBaseException
from requests.exceptions import RequestException
from common.djangoapps.course_modes.models import CourseMode
from openedx.core.djangoapps.commerce.utils import ecommerce_api_client
from openedx.core.djangoapps.commerce.utils import get_ecommerce_api_base_url, get_ecommerce_api_client
DISPLAY_VERIFIED = "verified"
DISPLAY_HONOR = "honor"
@@ -89,11 +88,17 @@ def get_course_final_price(user, sku, course_price):
"""
price_details = {}
try:
price_details = ecommerce_api_client(user).baskets.calculate.get(
sku=[sku],
username=user.username,
api_url = urljoin(f"{get_ecommerce_api_base_url()}/", "baskets/calculate/")
response = get_ecommerce_api_client(user).get(
api_url,
params={
"sku": [sku],
"username": user.username,
}
)
except (SlumberBaseException, ConnectionError, Timeout) as exc:
response.raise_for_status()
price_details = response.json()
except RequestException as exc:
LOGGER.info(
'[e-commerce calculate endpoint] Exception raise for sku [%s] - user [%s] and exception: %s',
sku,

View File

@@ -11,8 +11,6 @@ file and check it in at the same time as your model changes. To do that,
3. Add the migration file created in edx-platform/common/djangoapps/student/migrations/
"""
import crum
import hashlib # lint-amnesty, pylint: disable=wrong-import-order
import json # lint-amnesty, pylint: disable=wrong-import-order
import logging # lint-amnesty, pylint: disable=wrong-import-order
@@ -21,8 +19,9 @@ from collections import defaultdict, namedtuple # lint-amnesty, pylint: disable
from datetime import date, datetime, timedelta # lint-amnesty, pylint: disable=wrong-import-order
from functools import total_ordering # lint-amnesty, pylint: disable=wrong-import-order
from importlib import import_module # lint-amnesty, pylint: disable=wrong-import-order
from urllib.parse import urlencode # lint-amnesty, pylint: disable=wrong-import-order
from urllib.parse import urlencode, urljoin
import crum
from config_models.models import ConfigurationModel
from django.apps import apps
from django.conf import settings
@@ -37,42 +36,35 @@ from django.db.models import Count, Index, Q
from django.db.models.signals import post_save, pre_save
from django.db.utils import ProgrammingError
from django.dispatch import receiver
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _
from django.utils.translation import gettext_noop
from django_countries.fields import CountryField
from edx_django_utils.cache import RequestCache, TieredCache, get_cache_key
from edx_django_utils import monitoring
from edx_rest_api_client.exceptions import SlumberBaseException
from edx_django_utils.cache import RequestCache, TieredCache, get_cache_key
from eventtracking import tracker
from model_utils.models import TimeStampedModel
from opaque_keys.edx.django.models import CourseKeyField, LearningContextKeyField
from opaque_keys.edx.keys import CourseKey
from pytz import UTC, timezone
from simple_history.models import HistoricalRecords
from slumber.exceptions import HttpClientError, HttpServerError
from user_util import user_util
from openedx_events.learning.data import (
CourseData,
CourseEnrollmentData,
UserData,
UserPersonalData,
)
from openedx_events.learning.data import CourseData, CourseEnrollmentData, UserData, UserPersonalData
from openedx_events.learning.signals import (
COURSE_ENROLLMENT_CHANGED,
COURSE_ENROLLMENT_CREATED,
COURSE_UNENROLLMENT_COMPLETED,
)
from openedx_filters.learning.filters import CourseEnrollmentStarted, CourseUnenrollmentStarted
from pytz import UTC, timezone
from requests.exceptions import HTTPError, RequestException
from simple_history.models import HistoricalRecords
from user_util import user_util
import openedx.core.djangoapps.django_comment_common.comment_client as cc
from common.djangoapps.course_modes.models import CourseMode, get_cosmetic_verified_display_price
from common.djangoapps.student.emails import send_proctoring_requirements_email
from common.djangoapps.student.email_helpers import (
generate_proctoring_requirements_email_context,
should_send_proctoring_requirements_email
should_send_proctoring_requirements_email,
)
from common.djangoapps.student.emails import send_proctoring_requirements_email
from common.djangoapps.student.signals import ENROLL_STATUS_CHANGE, ENROLLMENT_TRACK_UPDATED, UNENROLL_DONE
from common.djangoapps.track import contexts, segment
from common.djangoapps.util.model_utils import emit_field_changed_events, get_changed_fields_dict
@@ -1972,6 +1964,7 @@ class CourseEnrollment(models.Model):
# Due to circular import issues this import was placed close to usage. To move this to the
# top of the file would require a large scale refactor of the refund code.
import lms.djangoapps.certificates.api
# If the student has already been given a certificate in a non refundable status they should not be refunded
certificate = lms.djangoapps.certificates.api.get_certificate_for_user_id(
self.user,
@@ -2052,7 +2045,7 @@ class CourseEnrollment(models.Model):
"""
# NOTE: This is here to avoid circular references
from openedx.core.djangoapps.commerce.utils import ecommerce_api_client
from openedx.core.djangoapps.commerce.utils import get_ecommerce_api_base_url, get_ecommerce_api_client
order_number = self.get_order_attribute_value('order_number')
if not order_number:
return None
@@ -2065,23 +2058,21 @@ class CourseEnrollment(models.Model):
else:
try:
# response is not cached, so make a call to ecommerce to fetch order details
order = ecommerce_api_client(self.user).orders(order_number).get()
except HttpClientError:
api_url = urljoin(f"{get_ecommerce_api_base_url()}/", f"orders/{order_number}/")
response = get_ecommerce_api_client(self.user).get(api_url)
response.raise_for_status()
order = response.json()
except HTTPError as err:
log.warning(
"Encountered HttpClientError while getting order details from ecommerce. "
"Order={number} and user {user}".format(number=order_number, user=self.user.id))
"Encountered HTTPError while getting order details from ecommerce. "
"Status code was %d, Order=%s and user %s", err.response.status_code, order_number, self.user.id
)
return None
except HttpServerError:
log.warning(
"Encountered HttpServerError while getting order details from ecommerce. "
"Order={number} and user {user}".format(number=order_number, user=self.user.id))
return None
except SlumberBaseException:
except RequestException:
log.warning(
"Encountered an error while getting order details from ecommerce. "
"Order={number} and user {user}".format(number=order_number, user=self.user.id))
"Order=%s and user %s", order_number, self.user.id
)
return None
cache_time_out = getattr(settings, 'ECOMMERCE_ORDERS_API_CACHE_TIMEOUT', 3600)

View File

@@ -235,7 +235,7 @@ class RefundableTest(SharedModuleStoreTestCase):
)
assert self.enrollment.is_order_voucher_refundable() is False
@patch('openedx.core.djangoapps.commerce.utils.ecommerce_api_client')
@patch('openedx.core.djangoapps.commerce.utils.get_ecommerce_api_client')
def test_get_order_attribute_from_ecommerce(self, mock_ecommerce_api_client):
"""
Assert that the get_order_attribute_from_ecommerce method returns order details if it's already cached,
@@ -254,7 +254,7 @@ class RefundableTest(SharedModuleStoreTestCase):
assert self.enrollment.get_order_attribute_from_ecommerce("vouchers") == order_details["vouchers"]
mock_ecommerce_api_client.assert_not_called()
@patch('openedx.core.djangoapps.commerce.utils.ecommerce_api_client')
@patch('openedx.core.djangoapps.commerce.utils.get_ecommerce_api_client')
def test_refund_cutoff_date_with_date_placed_attr(self, mock_ecommerce_api_client):
"""
Assert that the refund_cutoff_date returns order placement date if order:date_placed