From 316f087bdeba4f8fb1d45f4c4144970dc9e928b0 Mon Sep 17 00:00:00 2001 From: Santiago Grangetto Date: Thu, 10 Jun 2021 14:59:24 -0300 Subject: [PATCH] Deprecate HealthCheck Activity --- .../botbuilder/core/__init__.py | 2 - .../botbuilder/core/activity_handler.py | 23 -------- .../botbuilder/core/healthcheck.py | 31 ----------- .../tests/test_activity_handler.py | 52 +------------------ .../botbuilder/schema/__init__.py | 4 -- .../botbuilder/schema/health_results.py | 32 ------------ .../botbuilder/schema/healthcheck_response.py | 16 ------ 7 files changed, 1 insertion(+), 159 deletions(-) delete mode 100644 libraries/botbuilder-core/botbuilder/core/healthcheck.py delete mode 100644 libraries/botbuilder-schema/botbuilder/schema/health_results.py delete mode 100644 libraries/botbuilder-schema/botbuilder/schema/healthcheck_response.py diff --git a/libraries/botbuilder-core/botbuilder/core/__init__.py b/libraries/botbuilder-core/botbuilder/core/__init__.py index fcc867fb4..32178f3e7 100644 --- a/libraries/botbuilder-core/botbuilder/core/__init__.py +++ b/libraries/botbuilder-core/botbuilder/core/__init__.py @@ -45,7 +45,6 @@ from .user_state import UserState from .register_class_middleware import RegisterClassMiddleware from .adapter_extensions import AdapterExtensions -from .healthcheck import HealthCheck __all__ = [ "ActivityHandler", @@ -68,7 +67,6 @@ "ConversationState", "conversation_reference_extension", "ExtendedUserTokenProvider", - "HealthCheck", "IntentScore", "InvokeResponse", "MemoryStorage", diff --git a/libraries/botbuilder-core/botbuilder/core/activity_handler.py b/libraries/botbuilder-core/botbuilder/core/activity_handler.py index 28c924e0f..426c95e62 100644 --- a/libraries/botbuilder-core/botbuilder/core/activity_handler.py +++ b/libraries/botbuilder-core/botbuilder/core/activity_handler.py @@ -9,12 +9,9 @@ ChannelAccount, MessageReaction, SignInConstants, - HealthCheckResponse, ) from .bot import Bot -from .bot_adapter import BotAdapter -from .healthcheck import HealthCheck from .serializer_helper import serializer_helper from .bot_framework_adapter import BotFrameworkAdapter from .invoke_response import InvokeResponse @@ -453,11 +450,6 @@ async def on_invoke_activity( # pylint: disable=unused-argument await self.on_sign_in_invoke(turn_context) return self._create_invoke_response() - if turn_context.activity.name == "healthcheck": - return self._create_invoke_response( - await self.on_healthcheck(turn_context) - ) - raise _InvokeResponseException(HTTPStatus.NOT_IMPLEMENTED) except _InvokeResponseException as invoke_exception: return invoke_exception.create_invoke_response() @@ -478,21 +470,6 @@ async def on_sign_in_invoke( # pylint: disable=unused-argument """ raise _InvokeResponseException(HTTPStatus.NOT_IMPLEMENTED) - async def on_healthcheck(self, turn_context: TurnContext) -> HealthCheckResponse: - """ - Invoked when the bot is sent a health check from the hosting infrastructure or, in the case of - Skills the parent bot. By default, this method acknowledges the health state of the bot. - - When the on_invoke_activity method receives an Invoke with a Activity.name of `healthCheck`, it - calls this method. - - :param turn_context: A context object for this turn. - :return: The HealthCheckResponse object - """ - return HealthCheck.create_healthcheck_response( - turn_context.turn_state.get(BotAdapter.BOT_CONNECTOR_CLIENT_KEY) - ) - @staticmethod def _create_invoke_response(body: object = None) -> InvokeResponse: return InvokeResponse(status=int(HTTPStatus.OK), body=serializer_helper(body)) diff --git a/libraries/botbuilder-core/botbuilder/core/healthcheck.py b/libraries/botbuilder-core/botbuilder/core/healthcheck.py deleted file mode 100644 index c9f5afb49..000000000 --- a/libraries/botbuilder-core/botbuilder/core/healthcheck.py +++ /dev/null @@ -1,31 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from botbuilder.schema import HealthCheckResponse, HealthResults -from botbuilder.core.bot_framework_adapter import USER_AGENT -from botframework.connector import ConnectorClient - - -class HealthCheck: - @staticmethod - def create_healthcheck_response( - connector_client: ConnectorClient, - ) -> HealthCheckResponse: - # A derived class may override this, however, the default is that the bot is healthy given - # we have got to here. - health_results = HealthResults(success=True) - - if connector_client: - health_results.authorization = "{} {}".format( - "Bearer", connector_client.config.credentials.get_access_token() - ) - health_results.user_agent = USER_AGENT - - success_message = "Health check succeeded." - health_results.messages = ( - [success_message] - if health_results.authorization - else [success_message, "Callbacks are not authorized."] - ) - - return HealthCheckResponse(health_results=health_results) diff --git a/libraries/botbuilder-core/tests/test_activity_handler.py b/libraries/botbuilder-core/tests/test_activity_handler.py index 69ccfa830..fedc03e96 100644 --- a/libraries/botbuilder-core/tests/test_activity_handler.py +++ b/libraries/botbuilder-core/tests/test_activity_handler.py @@ -5,7 +5,7 @@ from botframework.connector import ConnectorClient from botframework.connector.auth import AppCredentials -from botbuilder.core import ActivityHandler, BotAdapter, TurnContext, InvokeResponse +from botbuilder.core import ActivityHandler, BotAdapter, TurnContext from botbuilder.schema import ( Activity, ActivityTypes, @@ -13,11 +13,8 @@ ConversationReference, MessageReaction, ResourceResponse, - HealthCheckResponse, ) -from botbuilder.core.bot_framework_adapter import USER_AGENT - class TestingActivityHandler(ActivityHandler): __test__ = False @@ -102,10 +99,6 @@ async def on_sign_in_invoke( # pylint: disable=unused-argument self.record.append("on_sign_in_invoke") return - async def on_healthcheck(self, turn_context: TurnContext) -> HealthCheckResponse: - self.record.append("on_healthcheck") - return HealthCheckResponse() - class NotImplementedAdapter(BotAdapter): async def delete_activity( @@ -313,46 +306,3 @@ async def test_on_installation_update_remove_upgrade(self): assert len(bot.record) == 2 assert bot.record[0] == "on_installation_update" assert bot.record[1] == "on_installation_update_remove" - - async def test_healthcheck(self): - activity = Activity(type=ActivityTypes.invoke, name="healthcheck",) - - adapter = TestInvokeAdapter() - turn_context = TurnContext(adapter, activity) - - bot = ActivityHandler() - await bot.on_turn(turn_context) - - self.assertIsNotNone(adapter.activity) - self.assertIsInstance(adapter.activity.value, InvokeResponse) - self.assertEqual(adapter.activity.value.status, 200) - - response = HealthCheckResponse.deserialize(adapter.activity.value.body) - self.assertTrue(response.health_results.success) - self.assertTrue(response.health_results.messages) - self.assertEqual(response.health_results.messages[0], "Health check succeeded.") - - async def test_healthcheck_with_connector(self): - activity = Activity(type=ActivityTypes.invoke, name="healthcheck",) - - adapter = TestInvokeAdapter() - turn_context = TurnContext(adapter, activity) - - mock_connector_client = MockConnectorClient() - turn_context.turn_state[ - BotAdapter.BOT_CONNECTOR_CLIENT_KEY - ] = mock_connector_client - - bot = ActivityHandler() - await bot.on_turn(turn_context) - - self.assertIsNotNone(adapter.activity) - self.assertIsInstance(adapter.activity.value, InvokeResponse) - self.assertEqual(adapter.activity.value.status, 200) - - response = HealthCheckResponse.deserialize(adapter.activity.value.body) - self.assertTrue(response.health_results.success) - self.assertEqual(response.health_results.authorization, "Bearer awesome") - self.assertEqual(response.health_results.user_agent, USER_AGENT) - self.assertTrue(response.health_results.messages) - self.assertEqual(response.health_results.messages[0], "Health check succeeded.") diff --git a/libraries/botbuilder-schema/botbuilder/schema/__init__.py b/libraries/botbuilder-schema/botbuilder/schema/__init__.py index 734d6d91c..44b68d3e8 100644 --- a/libraries/botbuilder-schema/botbuilder/schema/__init__.py +++ b/libraries/botbuilder-schema/botbuilder/schema/__init__.py @@ -69,8 +69,6 @@ from ._sign_in_enums import SignInConstants from .callerid_constants import CallerIdConstants -from .health_results import HealthResults -from .healthcheck_response import HealthCheckResponse from .speech_constants import SpeechConstants __all__ = [ @@ -140,7 +138,5 @@ "ContactRelationUpdateActionTypes", "InstallationUpdateActionTypes", "CallerIdConstants", - "HealthResults", - "HealthCheckResponse", "SpeechConstants", ] diff --git a/libraries/botbuilder-schema/botbuilder/schema/health_results.py b/libraries/botbuilder-schema/botbuilder/schema/health_results.py deleted file mode 100644 index 6205e68cb..000000000 --- a/libraries/botbuilder-schema/botbuilder/schema/health_results.py +++ /dev/null @@ -1,32 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from typing import List -from msrest.serialization import Model - - -class HealthResults(Model): - _attribute_map = { - "success": {"key": "success", "type": "bool"}, - "authorization": {"key": "authorization", "type": "str"}, - "user_agent": {"key": "user-agent", "type": "str"}, - "messages": {"key": "messages", "type": "[str]"}, - "diagnostics": {"key": "diagnostics", "type": "object"}, - } - - def __init__( - self, - *, - success: bool = None, - authorization: str = None, - user_agent: str = None, - messages: List[str] = None, - diagnostics: object = None, - **kwargs - ) -> None: - super(HealthResults, self).__init__(**kwargs) - self.success = success - self.authorization = authorization - self.user_agent = user_agent - self.messages = messages - self.diagnostics = diagnostics diff --git a/libraries/botbuilder-schema/botbuilder/schema/healthcheck_response.py b/libraries/botbuilder-schema/botbuilder/schema/healthcheck_response.py deleted file mode 100644 index e5ebea7e3..000000000 --- a/libraries/botbuilder-schema/botbuilder/schema/healthcheck_response.py +++ /dev/null @@ -1,16 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from msrest.serialization import Model - -from botbuilder.schema import HealthResults - - -class HealthCheckResponse(Model): - _attribute_map = { - "health_results": {"key": "healthResults", "type": "HealthResults"}, - } - - def __init__(self, *, health_results: HealthResults = None, **kwargs) -> None: - super(HealthCheckResponse, self).__init__(**kwargs) - self.health_results = health_results