Skip to content

Commit

Permalink
feat: record blackboard API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
hamzawaleed01 committed Jan 29, 2024
1 parent a161565 commit ddc58d9
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ Change Log
Unreleased
----------

[4.10.13]
---------

feat: update blackboard client to store API calls in DB

[4.10.12]
---------

Expand Down
2 changes: 1 addition & 1 deletion enterprise/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
Your project description goes here.
"""

__version__ = "4.10.12"
__version__ = "4.10.13"
73 changes: 72 additions & 1 deletion integrated_channels/blackboard/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import copy
import json
import logging
import time
from http import HTTPStatus
from urllib.parse import urljoin

Expand All @@ -16,7 +17,11 @@
from integrated_channels.blackboard.exporters.content_metadata import BLACKBOARD_COURSE_CONTENT_NAME
from integrated_channels.exceptions import ClientError
from integrated_channels.integrated_channel.client import IntegratedChannelApiClient
from integrated_channels.utils import generate_formatted_log, refresh_session_if_expired
from integrated_channels.utils import (
generate_formatted_log,
refresh_session_if_expired,
store_api_call,
)

LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -594,11 +599,49 @@ def generate_course_content_delete_url(self, course_id, content_id):
path=COURSE_CONTENT_DELETE_PATH.format(course_id=course_id, content_id=content_id)
)

def stringify_and_store_api_record(
self, url, data, time_taken, status_code, response_body
):
if data is not None:
# Convert data to string if it's not already a string
if not isinstance(data, str):
try:
# Check if data is a dictionary, list, or tuple then convert to JSON string
if isinstance(data, (dict, list, tuple)):
data = json.dumps(data)
else:
# If it's another type, simply convert to string
data = str(data)
except Exception as e:
pass
# Store stringified data in the database
try:
store_api_call(
enterprise_customer=self.enterprise_configuration.enterprise_customer,
enterprise_customer_configuration_id=self.enterprise_configuration.id,
endpoint=url,
payload=data,
time_taken=time_taken,
status_code=status_code,
response_body=response_body,
)
except Exception as e:
print(f"Failed to store data in the database: {e}")

def _get(self, url, data=None):
"""
Returns request's get response and raises Client Errors if appropriate.
"""
start_time = time.time()
get_response = self.session.get(url, params=data)
time_taken = time.time() - start_time
self.stringify_and_store_api_record(
url=url,
data=data,
time_taken=time_taken,
status_code=get_response.status_code,
response_body=get_response.text,
)
if get_response.status_code >= 400:
raise ClientError(get_response.text, get_response.status_code)
return get_response
Expand All @@ -607,7 +650,16 @@ def _patch(self, url, data):
"""
Returns request's patch response and raises Client Errors if appropriate.
"""
start_time = time.time()
patch_response = self.session.patch(url, json=data)
time_taken = time.time() - start_time
self.stringify_and_store_api_record(
url=url,
data=data,
time_taken=time_taken,
status_code=patch_response.status_code,
response_body=patch_response.text,
)
if patch_response.status_code >= 400:
raise ClientError(patch_response.text, patch_response.status_code)
return patch_response
Expand All @@ -616,7 +668,17 @@ def _post(self, url, data):
"""
Returns request's post response and raises Client Errors if appropriate.
"""
start_time = time.time()
post_response = self.session.post(url, json=data)
time_taken = time.time() - start_time
self.stringify_and_store_api_record(
url=url,
data=data,
time_taken=time_taken,
status_code=post_response.status_code,
response_body=post_response.text,
)

if post_response.status_code >= 400:
raise ClientError(post_response.text, post_response.status_code)
return post_response
Expand All @@ -625,7 +687,16 @@ def _delete(self, url):
"""
Returns request's delete response and raises Client Errors if appropriate.
"""
start_time = time.time()
response = self.session.delete(url)
time_taken = time.time() - start_time
self.stringify_and_store_api_record(
url=url,
data='',
time_taken=time_taken,
status_code=response.status_code,
response_body=response.text,
)
if response.status_code >= 400:
raise ClientError(response.text, response.status_code)
return response
Expand Down

0 comments on commit ddc58d9

Please sign in to comment.