Skip to content

Commit

Permalink
Use debug logging for channel types errors, D3 and WA
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Nov 22, 2024
1 parent 0f1aaf7 commit b6cb315
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 30 deletions.
10 changes: 8 additions & 2 deletions temba/channels/types/dialog360_legacy/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,18 @@ def test_check_health(self):
with patch("requests.get") as mock_get:
mock_get.side_effect = [
MockResponse(200, '{"meta": {"api_status": "stable", "version": "2.35.4"}}'),
MockResponse(401, '{"meta": {"api_status": "stable", "version": "2.35.4"}}'),
MockResponse(401, ""),
]
channel.type.check_health(channel)
mock_get.assert_called_with(
"https://example.com/whatsapp/v1/health",
headers={"D360-API-KEY": "123456789", "Content-Type": "application/json"},
)
with self.assertRaises(Exception):

with patch("logging.Logger.debug") as mock_log_debug:
channel.type.check_health(channel)
self.assertEqual(1, mock_log_debug.call_count)
self.assertEqual(
"Error checking API health: b''",
mock_log_debug.call_args[0][0],
)
14 changes: 13 additions & 1 deletion temba/channels/types/dialog360_legacy/type.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

import requests

from django.forms import ValidationError
Expand All @@ -13,6 +15,8 @@

from ...models import ChannelType, ConfigUI

logger = logging.getLogger(__name__)


class Dialog360LegacyType(ChannelType):
"""
Expand Down Expand Up @@ -67,12 +71,20 @@ def fetch_templates(self, channel) -> list:
return response.json()["waba_templates"]

def check_health(self, channel):
start = timezone.now()
response = requests.get(
channel.config[Channel.CONFIG_BASE_URL] + "/v1/health", headers=self.get_headers(channel)
)

if response.status_code != 200:
raise requests.RequestException("Could not check api status", response=response)
HTTPLog.from_exception(
HTTPLog.WHATSAPP_CHECK_HEALTH,
requests.RequestException("Could not check api status", response=response),
start,
channel=channel,
)
logger.debug(f"Error checking API health: {response.content}")
return

return response

Expand Down
7 changes: 7 additions & 0 deletions temba/channels/types/whatsapp_legacy/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def fetch_templates(self, channel) -> list:

def check_health(self, channel):
headers = self.get_api_headers(channel)
start = timezone.now()

try:
response = requests.get(channel.config[Channel.CONFIG_BASE_URL] + "/v1/health", headers=headers)
Expand All @@ -124,6 +125,12 @@ def check_health(self, channel):
return

if response.status_code >= 400:
HTTPLog.from_exception(
HTTPLog.WHATSAPP_CHECK_HEALTH,
requests.RequestException(f"Error checking API health: {response.content}", response=response),
start,
channel=channel,
)
logger.debug(f"Error checking API health: {response.content}")
return

Expand Down
29 changes: 10 additions & 19 deletions temba/utils/whatsapp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
import logging

import requests

from django.utils import timezone

from temba.request_logs.models import HTTPLog

logger = logging.getLogger(__name__)


def update_api_version(channel):
start = timezone.now()
try:
response = channel.type.check_health(channel)
api_status = response.json()
version = api_status["meta"]["version"]
if not version.startswith("v"):
version = "v" + version
channel.config.update(version=version)
channel.save(update_fields=("config",))
except requests.RequestException as e:
HTTPLog.from_exception(HTTPLog.WHATSAPP_CHECK_HEALTH, e, start, channel=channel)
except Exception as e: # pragma: no cover
logger.error(f"Error retrieving WhatsApp API version: {str(e)}", exc_info=True)
response = channel.type.check_health(channel)
if not response:
logger.debug("Error retrieving WhatsApp API version: Failed to check health")
return
api_status = response.json()
version = api_status["meta"]["version"]
if not version.startswith("v"):
version = "v" + version
channel.config.update(version=version)
channel.save(update_fields=("config",))
26 changes: 18 additions & 8 deletions temba/utils/whatsapp/tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from unittest.mock import patch

import requests

from temba.channels.models import Channel
from temba.channels.types.whatsapp_legacy.type import (
CONFIG_FB_ACCESS_TOKEN,
Expand Down Expand Up @@ -45,9 +43,15 @@ def test_update_api_version_whatsapp(self, mock_health):
self.assertEqual("v2.35.2", channel.config.get("version"))

self.assertEqual(0, HTTPLog.objects.filter(log_type=HTTPLog.WHATSAPP_CHECK_HEALTH).count())
mock_health.side_effect = [requests.RequestException(response=MockResponse(401, "{}"))]
update_api_version(channel)
self.assertEqual(1, HTTPLog.objects.filter(log_type=HTTPLog.WHATSAPP_CHECK_HEALTH).count())
mock_health.return_value = None

with patch("logging.Logger.debug") as mock_log_debug:
update_api_version(channel)
self.assertEqual(1, mock_log_debug.call_count)
self.assertEqual(
"Error retrieving WhatsApp API version: Failed to check health",
mock_log_debug.call_args[0][0],
)

@patch("temba.channels.types.dialog360_legacy.Dialog360LegacyType.check_health")
def test_update_api_version_dialog360(self, mock_health):
Expand All @@ -72,6 +76,12 @@ def test_update_api_version_dialog360(self, mock_health):
self.assertEqual("v2.35.4", channel.config.get("version"))

self.assertEqual(0, HTTPLog.objects.filter(log_type=HTTPLog.WHATSAPP_CHECK_HEALTH).count())
mock_health.side_effect = [requests.RequestException(response=MockResponse(401, "{}"))]
update_api_version(channel)
self.assertEqual(1, HTTPLog.objects.filter(log_type=HTTPLog.WHATSAPP_CHECK_HEALTH).count())
mock_health.return_value = None

with patch("logging.Logger.debug") as mock_log_debug:
update_api_version(channel)
self.assertEqual(1, mock_log_debug.call_count)
self.assertEqual(
"Error retrieving WhatsApp API version: Failed to check health",
mock_log_debug.call_args[0][0],
)

0 comments on commit b6cb315

Please sign in to comment.