Skip to content

Commit

Permalink
fix: update settings to use forum v2 APIs
Browse files Browse the repository at this point in the history
Updated settings to use Forum v2. All requests will be handled by Forum v2.
The Forum v2 includes a proxy for redirecting all requests to the CS Commerce app
that are not implemented within Forum v2. This ensures that any unhandled requests
are appropriately forwarded, maintaining seamless integration with the CS Commerce application.
  • Loading branch information
Ali-Salman29 committed Aug 9, 2024
1 parent f7be991 commit dc86f57
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
# lint-amnesty, pylint: disable=cyclic-import, missing-module-docstring
from django.conf import settings

# FORUM V1 SERVICE HOST
if hasattr(settings, "COMMENTS_SERVICE_URL"):
SERVICE_HOST = settings.COMMENTS_SERVICE_URL
COMMENTS_SERVICE_SERVICE_HOST = settings.COMMENTS_SERVICE_URL
else:
SERVICE_HOST = 'http://localhost:4567'
COMMENTS_SERVICE_SERVICE_HOST = "http://localhost:4567"

PREFIX = SERVICE_HOST + '/api/v1'
COMMENTS_SERVICE_PREFIX = COMMENTS_SERVICE_SERVICE_HOST + "/api/v1"

# V2 url support for differential logging
if hasattr(settings, "COMMENTS_SERVICE_V2_URL"):
SERVICE_HOST_V2 = settings.COMMENTS_SERVICE_V2_URL

if hasattr(settings, "FORUM_V2_SERVICE_URL"):
SERVICE_HOST = settings.FORUM_V2_SERVICE_URL
else:
SERVICE_HOST_V2 = 'http://localhost:8000'
SERVICE_HOST = "http://localhost:8000"

PREFIX_V2 = SERVICE_HOST_V2 + '/forum/forum_proxy/api/v1'
PREFIX = SERVICE_HOST + "/forum/api/v2"
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import requests
from django.utils.translation import get_language

from .settings import PREFIX, PREFIX_V2, SERVICE_HOST as COMMENTS_SERVICE
from .settings import PREFIX, COMMENTS_SERVICE_PREFIX, SERVICE_HOST as COMMENTS_SERVICE

log = logging.getLogger(__name__)

Expand All @@ -30,8 +30,8 @@ def extract(dic, keys):
return strip_none({k: dic.get(k) for k in keys})


def _get_forum_v2_url(url):
return url.replace(PREFIX, PREFIX_V2)
def _get_comment_service_url(url):
return url.replace(PREFIX, COMMENTS_SERVICE_PREFIX)


def perform_request(method, url, data_or_params=None, raw=False,
Expand Down Expand Up @@ -75,22 +75,37 @@ def perform_request(method, url, data_or_params=None, raw=False,
timeout=config.connection_timeout
)

# For the better logging
log.info(
"""
======> FORUM <======
method: {method}
url: {url}
params: {params}
data: {data}
response: {response}
======> END <======
""".format(method=method, url=url, params=params, data=data, response=response.json())
)

if method == "get":
forum_v2_url = _get_forum_v2_url(url)
response_v2 = requests.request(
forum_v1_url = _get_comment_service_url(url)
forum_v1_response = requests.request(
method,
forum_v2_url,
forum_v1_url,
data=data,
params=params,
headers=headers,
timeout=config.connection_timeout,
)
log.info(f"requested forum v1 url: {url}")
log.info(f"requested forum v2 url: {forum_v2_url}")
if response_v2.json() != response.json():
log.info(f"requested forum proxey url: {url}")
log.info(f"requested forum v1 url: {forum_v1_url}")
if forum_v1_response.json() != response.json():
log.error(
f"Forum v2 difference, for endpoint {url} with params={params}. \
Expected: {response.json()}. Got: {response_v2.json()}."
f"Forum v2 difference, for endpoint {forum_v1_url} with params={params}. \
Expected: {forum_v1_response.json()}. Got: {response.json()}."
)

metric_tags.append(f'status_code:{response.status_code}')
Expand Down

0 comments on commit dc86f57

Please sign in to comment.