Skip to content

Commit

Permalink
feat: call python methods from forum v2
Browse files Browse the repository at this point in the history
- add a method named `check_service_enabled` for checking if the forum service
is enabled or not as it was being checked in v1
- call python methods instead of HTTPs APIs for getting response/data
for pin, unpin thread, commentables count_stats and get user's data by user_id
- add a method named `handle_response` for handling returned results from
forum v2 methods as same as v1 responses were handled
  • Loading branch information
Muhammad Faraz Maqsood committed Sep 13, 2024
1 parent a480618 commit e3cc16a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from edx_django_utils.monitoring import function_trace
from opaque_keys.edx.keys import CourseKey

from forum import api as forum_api
from openedx.core.djangoapps.django_comment_common.comment_client import settings
from openedx.core.djangoapps.django_comment_common.comment_client.utils import perform_request

Expand All @@ -29,17 +30,8 @@ def get_course_commentable_counts(course_key: CourseKey) -> Dict[str, Dict[str,
}
"""
url = f"{settings.PREFIX}/commentables/{course_key}/counts"
response = perform_request(
'get',
url,
metric_tags=[
f"course_key:{course_key}",
"function:get_course_commentable_counts",
],
metric_action='commentable_stats.retrieve',
)
return response
commentable_stats = forum_api.retrieve_commentables_stats(str(course_key))
return commentable_stats


@function_trace("get_course_user_stats")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from eventtracking import tracker

from . import models, settings, utils
from forum import api as forum_api

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -193,27 +194,11 @@ def unFlagAbuse(self, user, voteable, removeAll):
voteable._update_from_response(response)

def pin(self, user, thread_id):
url = _url_for_pin_thread(thread_id)
params = {'user_id': user.id}
response = utils.perform_request(
'put',
url,
params,
metric_tags=self._metric_tags,
metric_action='thread.pin'
)
self._update_from_response(response)
thread_data = forum_api.pin_thread(user.id, thread_id)
self._update_from_response(thread_data)

def un_pin(self, user, thread_id):
url = _url_for_un_pin_thread(thread_id)
params = {'user_id': user.id}
response = utils.perform_request(
'put',
url,
params,
metric_tags=self._metric_tags,
metric_action='thread.unpin'
)
response = forum_api.unpin_thread(user.id, thread_id)
self._update_from_response(response)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@


from . import models, settings, utils

from forum import api as forum_api
from forum.utils import ForumV2RequestError

class User(models.Model):

Expand Down Expand Up @@ -149,27 +150,9 @@ def _retrieve(self, *args, **kwargs):
if self.attributes.get('group_id'):
retrieve_params['group_id'] = self.group_id
try:
response = utils.perform_request(
'get',
url,
retrieve_params,
metric_action='model.retrieve',
metric_tags=self._metric_tags,
)
except utils.CommentClientRequestError as e:
if e.status_code == 404:
# attempt to gracefully recover from a previous failure
# to sync this user to the comments service.
self.save()
response = utils.perform_request(
'get',
url,
retrieve_params,
metric_action='model.retrieve',
metric_tags=self._metric_tags,
)
else:
raise
response = forum_api.retrieve_user(self.attributes["id"], retrieve_params)
except ForumV2RequestError as e:
raise str(e)
self._update_from_response(response)

def retire(self, retired_username):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ def perform_request(method, url, data_or_params=None, raw=False,
)

# For the better logging
response_to_log_and_compare = (
response.json() if response.content else response.content
)
log.info(
"""
======> FORUM <======
Expand All @@ -87,7 +90,13 @@ def perform_request(method, url, data_or_params=None, raw=False,
response: {response}
======> END <======
""".format(method=method, url=url, params=params, data=data, response=response.json())
""".format(
method=method,
url=url,
params=params,
data=data,
response=response_to_log_and_compare,
)
)

if method == "get":
Expand All @@ -100,12 +109,17 @@ def perform_request(method, url, data_or_params=None, raw=False,
headers=headers,
timeout=config.connection_timeout,
)
forum_v1_response_to_log_and_compare = (
forum_v1_response.json()
if forum_v1_response.content
else forum_v1_response.content
)
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():
if forum_v1_response_to_log_and_compare != response_to_log_and_compare:
log.error(
f"Forum v2 difference, for endpoint {forum_v1_url} with params={params}. \
Expected: {forum_v1_response.json()}. Got: {response.json()}."
Expected: {forum_v1_response_to_log_and_compare}. Got: {response_to_log_and_compare}."
)

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

0 comments on commit e3cc16a

Please sign in to comment.