From 73ecc4478976ce740a2e0e664d21955818683b7e Mon Sep 17 00:00:00 2001 From: Rodrigo Brandao Date: Fri, 24 Jul 2026 12:09:37 -0700 Subject: [PATCH 1/2] Small refactoring --- .../aiohttp/_aiohttp_request_adapter.py | 28 +++++++++++++++++ .../hosting/aiohttp/_start_agent_process.py | 3 ++ .../hosting/aiohttp/cloud_adapter.py | 25 +--------------- .../hosting/fastapi/__init__.py | 3 ++ .../fastapi/_fastapi_request_adapter.py | 28 +++++++++++++++++ .../hosting/fastapi/_start_agent_process.py | 3 ++ .../fastapi/channel_service_route_table.py | 24 +-------------- .../hosting/fastapi/cloud_adapter.py | 30 +++---------------- 8 files changed, 71 insertions(+), 73 deletions(-) create mode 100644 libraries/microsoft-agents-hosting-aiohttp/microsoft_agents/hosting/aiohttp/_aiohttp_request_adapter.py create mode 100644 libraries/microsoft-agents-hosting-fastapi/microsoft_agents/hosting/fastapi/_fastapi_request_adapter.py diff --git a/libraries/microsoft-agents-hosting-aiohttp/microsoft_agents/hosting/aiohttp/_aiohttp_request_adapter.py b/libraries/microsoft-agents-hosting-aiohttp/microsoft_agents/hosting/aiohttp/_aiohttp_request_adapter.py new file mode 100644 index 000000000..97362cee4 --- /dev/null +++ b/libraries/microsoft-agents-hosting-aiohttp/microsoft_agents/hosting/aiohttp/_aiohttp_request_adapter.py @@ -0,0 +1,28 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from aiohttp.web import Request + + +class AiohttpRequestAdapter: + """Adapter to make aiohttp Request compatible with HttpRequestProtocol.""" + + def __init__(self, request: Request): + self._request = request + + @property + def method(self) -> str: + return self._request.method + + @property + def headers(self): + return self._request.headers + + async def json(self): + return await self._request.json() + + def get_claims_identity(self): + return self._request.get("claims_identity") + + def get_path_param(self, name: str) -> str: + return self._request.match_info[name] diff --git a/libraries/microsoft-agents-hosting-aiohttp/microsoft_agents/hosting/aiohttp/_start_agent_process.py b/libraries/microsoft-agents-hosting-aiohttp/microsoft_agents/hosting/aiohttp/_start_agent_process.py index bba42c8f9..c05da7db7 100644 --- a/libraries/microsoft-agents-hosting-aiohttp/microsoft_agents/hosting/aiohttp/_start_agent_process.py +++ b/libraries/microsoft-agents-hosting-aiohttp/microsoft_agents/hosting/aiohttp/_start_agent_process.py @@ -1,3 +1,6 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + from typing import Optional from aiohttp.web import Request, Response from microsoft_agents.hosting.core import error_resources diff --git a/libraries/microsoft-agents-hosting-aiohttp/microsoft_agents/hosting/aiohttp/cloud_adapter.py b/libraries/microsoft-agents-hosting-aiohttp/microsoft_agents/hosting/aiohttp/cloud_adapter.py index 659163409..bf8dfd190 100644 --- a/libraries/microsoft-agents-hosting-aiohttp/microsoft_agents/hosting/aiohttp/cloud_adapter.py +++ b/libraries/microsoft-agents-hosting-aiohttp/microsoft_agents/hosting/aiohttp/cloud_adapter.py @@ -1,5 +1,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. + from typing import Optional from aiohttp.web import Request, Response, json_response @@ -15,30 +16,6 @@ from .agent_http_adapter import AgentHttpAdapter -class AiohttpRequestAdapter: - """Adapter to make aiohttp Request compatible with HttpRequestProtocol.""" - - def __init__(self, request: Request): - self._request = request - - @property - def method(self) -> str: - return self._request.method - - @property - def headers(self): - return self._request.headers - - async def json(self): - return await self._request.json() - - def get_claims_identity(self): - return self._request.get("claims_identity") - - def get_path_param(self, name: str) -> str: - return self._request.match_info[name] - - class CloudAdapter(HttpAdapterBase, AgentHttpAdapter): """CloudAdapter for aiohttp web framework.""" diff --git a/libraries/microsoft-agents-hosting-fastapi/microsoft_agents/hosting/fastapi/__init__.py b/libraries/microsoft-agents-hosting-fastapi/microsoft_agents/hosting/fastapi/__init__.py index e72ee8d85..537951989 100644 --- a/libraries/microsoft-agents-hosting-fastapi/microsoft_agents/hosting/fastapi/__init__.py +++ b/libraries/microsoft-agents-hosting-fastapi/microsoft_agents/hosting/fastapi/__init__.py @@ -1,3 +1,6 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + from ._start_agent_process import start_agent_process from .agent_http_adapter import AgentHttpAdapter from .channel_service_route_table import channel_service_route_table diff --git a/libraries/microsoft-agents-hosting-fastapi/microsoft_agents/hosting/fastapi/_fastapi_request_adapter.py b/libraries/microsoft-agents-hosting-fastapi/microsoft_agents/hosting/fastapi/_fastapi_request_adapter.py new file mode 100644 index 000000000..d733a4502 --- /dev/null +++ b/libraries/microsoft-agents-hosting-fastapi/microsoft_agents/hosting/fastapi/_fastapi_request_adapter.py @@ -0,0 +1,28 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from fastapi import Request + + +class FastApiRequestAdapter: + """Adapter to make FastAPI Request compatible with HttpRequestProtocol.""" + + def __init__(self, request: Request): + self._request = request + + @property + def method(self) -> str: + return self._request.method + + @property + def headers(self): + return self._request.headers + + async def json(self): + return await self._request.json() + + def get_claims_identity(self): + return getattr(self._request.state, "claims_identity", None) + + def get_path_param(self, name: str) -> str: + return self._request.path_params.get(name, "") diff --git a/libraries/microsoft-agents-hosting-fastapi/microsoft_agents/hosting/fastapi/_start_agent_process.py b/libraries/microsoft-agents-hosting-fastapi/microsoft_agents/hosting/fastapi/_start_agent_process.py index ebaf2e439..24f982508 100644 --- a/libraries/microsoft-agents-hosting-fastapi/microsoft_agents/hosting/fastapi/_start_agent_process.py +++ b/libraries/microsoft-agents-hosting-fastapi/microsoft_agents/hosting/fastapi/_start_agent_process.py @@ -1,3 +1,6 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + from typing import Optional from fastapi import Request, Response from microsoft_agents.hosting.core import error_resources diff --git a/libraries/microsoft-agents-hosting-fastapi/microsoft_agents/hosting/fastapi/channel_service_route_table.py b/libraries/microsoft-agents-hosting-fastapi/microsoft_agents/hosting/fastapi/channel_service_route_table.py index b8a44b5e8..144244eb7 100644 --- a/libraries/microsoft-agents-hosting-fastapi/microsoft_agents/hosting/fastapi/channel_service_route_table.py +++ b/libraries/microsoft-agents-hosting-fastapi/microsoft_agents/hosting/fastapi/channel_service_route_table.py @@ -7,29 +7,7 @@ from microsoft_agents.hosting.core import ChannelApiHandlerProtocol from microsoft_agents.hosting.core.http import ChannelServiceRoutes - -class FastApiRequestAdapter: - """Adapter for FastAPI requests to use with ChannelServiceRoutes.""" - - def __init__(self, request: Request): - self._request = request - - @property - def method(self) -> str: - return self._request.method - - @property - def headers(self): - return self._request.headers - - async def json(self): - return await self._request.json() - - def get_claims_identity(self): - return getattr(self._request.state, "claims_identity", None) - - def get_path_param(self, name: str) -> str: - return self._request.path_params.get(name, "") +from ._fastapi_request_adapter import FastApiRequestAdapter def channel_service_route_table( diff --git a/libraries/microsoft-agents-hosting-fastapi/microsoft_agents/hosting/fastapi/cloud_adapter.py b/libraries/microsoft-agents-hosting-fastapi/microsoft_agents/hosting/fastapi/cloud_adapter.py index a94f81df1..99dff57b1 100644 --- a/libraries/microsoft-agents-hosting-fastapi/microsoft_agents/hosting/fastapi/cloud_adapter.py +++ b/libraries/microsoft-agents-hosting-fastapi/microsoft_agents/hosting/fastapi/cloud_adapter.py @@ -1,5 +1,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. + from typing import Optional from fastapi import Request, Response @@ -14,30 +15,7 @@ from microsoft_agents.hosting.core import ChannelServiceClientFactoryBase from .agent_http_adapter import AgentHttpAdapter - - -class FastApiRequestAdapter: - """Adapter to make FastAPI Request compatible with HttpRequestProtocol.""" - - def __init__(self, request: Request): - self._request = request - - @property - def method(self) -> str: - return self._request.method - - @property - def headers(self): - return self._request.headers - - async def json(self): - return await self._request.json() - - def get_claims_identity(self): - return getattr(self._request.state, "claims_identity", None) - - def get_path_param(self, name: str) -> str: - return self._request.path_params.get(name, "") +from ._fastapi_request_adapter import FastApiRequestAdapter class CloudAdapter(HttpAdapterBase, AgentHttpAdapter): @@ -46,8 +24,8 @@ class CloudAdapter(HttpAdapterBase, AgentHttpAdapter): def __init__( self, *, - connection_manager: Connections = None, - channel_service_client_factory: ChannelServiceClientFactoryBase = None, + connection_manager: Connections | None = None, + channel_service_client_factory: ChannelServiceClientFactoryBase | None = None, ): """ Initializes a new instance of the CloudAdapter class. From abf465d6a105f8d19467b48e537f7e5d6b75ebcd Mon Sep 17 00:00:00 2001 From: Rodrigo Brandao Date: Fri, 24 Jul 2026 14:07:54 -0700 Subject: [PATCH 2/2] Fixing missing import --- .../aiohttp/channel_service_route_table.py | 24 +------------------ .../hosting/aiohttp/cloud_adapter.py | 1 + 2 files changed, 2 insertions(+), 23 deletions(-) diff --git a/libraries/microsoft-agents-hosting-aiohttp/microsoft_agents/hosting/aiohttp/channel_service_route_table.py b/libraries/microsoft-agents-hosting-aiohttp/microsoft_agents/hosting/aiohttp/channel_service_route_table.py index eb9ace8da..676082f34 100644 --- a/libraries/microsoft-agents-hosting-aiohttp/microsoft_agents/hosting/aiohttp/channel_service_route_table.py +++ b/libraries/microsoft-agents-hosting-aiohttp/microsoft_agents/hosting/aiohttp/channel_service_route_table.py @@ -6,29 +6,7 @@ from microsoft_agents.hosting.core import ChannelApiHandlerProtocol from microsoft_agents.hosting.core.http import ChannelServiceRoutes - -class AiohttpRequestAdapter: - """Adapter for aiohttp requests to use with ChannelServiceRoutes.""" - - def __init__(self, request: Request): - self._request = request - - @property - def method(self) -> str: - return self._request.method - - @property - def headers(self): - return self._request.headers - - async def json(self): - return await self._request.json() - - def get_claims_identity(self): - return self._request.get("claims_identity") - - def get_path_param(self, name: str) -> str: - return self._request.match_info[name] +from ._aiohttp_request_adapter import AiohttpRequestAdapter def channel_service_route_table( diff --git a/libraries/microsoft-agents-hosting-aiohttp/microsoft_agents/hosting/aiohttp/cloud_adapter.py b/libraries/microsoft-agents-hosting-aiohttp/microsoft_agents/hosting/aiohttp/cloud_adapter.py index bf8dfd190..0dea335dd 100644 --- a/libraries/microsoft-agents-hosting-aiohttp/microsoft_agents/hosting/aiohttp/cloud_adapter.py +++ b/libraries/microsoft-agents-hosting-aiohttp/microsoft_agents/hosting/aiohttp/cloud_adapter.py @@ -14,6 +14,7 @@ from microsoft_agents.hosting.core import ChannelServiceClientFactoryBase from .agent_http_adapter import AgentHttpAdapter +from ._aiohttp_request_adapter import AiohttpRequestAdapter class CloudAdapter(HttpAdapterBase, AgentHttpAdapter):