From 8077d258967676167fc99282765468217f2766f5 Mon Sep 17 00:00:00 2001 From: Kacperek0 <47487512+Kacperek0@users.noreply.github.com> Date: Fri, 21 Nov 2025 17:09:25 +0100 Subject: [PATCH] Implement proxy support in CopilotClient Added support for HTTP and HTTPS proxies in CopilotClient. --- .../copilotstudio/client/copilot_client.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/libraries/microsoft-agents-copilotstudio-client/microsoft_agents/copilotstudio/client/copilot_client.py b/libraries/microsoft-agents-copilotstudio-client/microsoft_agents/copilotstudio/client/copilot_client.py index 68c26466..619e51c5 100644 --- a/libraries/microsoft-agents-copilotstudio-client/microsoft_agents/copilotstudio/client/copilot_client.py +++ b/libraries/microsoft-agents-copilotstudio-client/microsoft_agents/copilotstudio/client/copilot_client.py @@ -1,3 +1,4 @@ +import os import aiohttp from typing import AsyncIterable, Callable, Optional @@ -11,6 +12,8 @@ class CopilotClient: EVENT_STREAM_TYPE = "text/event-stream" APPLICATION_JSON_TYPE = "application/json" + HTTP_PROXY = os.getenv("HTTP_PROXY") + HTTPS_PROXY = os.getenv("HTTPS_PROXY") _current_conversation_id = "" @@ -24,12 +27,26 @@ def __init__( # TODO: Add logger # self.logger = logger self.conversation_id = "" + self.proxy = { + "http": self.HTTP_PROXY, + "https": self.HTTPS_PROXY, + } + + def _get_proxy(self) -> Optional[str]: + if self.proxy.get("https"): + return self.proxy["https"] + return self.proxy.get("http") async def post_request( self, url: str, data: dict, headers: dict ) -> AsyncIterable[Activity]: async with aiohttp.ClientSession() as session: - async with session.post(url, json=data, headers=headers) as response: + request_kwargs = {} + proxy = self._get_proxy() + if proxy: + request_kwargs["proxy"] = proxy + + async with session.post(url, json=data, headers=headers, **request_kwargs) as response: if response.status != 200: # self.logger(f"Error sending request: {response.status}") raise aiohttp.ClientError(