async def _get_app_credentials(self, app_id: str, oauth_scope: str) -> AppCredentials: if not app_id: return MicrosoftAppCredentials.empty() # in the cache? cache_key = f"{app_id}{oauth_scope}" app_credentials = BotFrameworkHttpClient._APP_CREDENTIALS_CACHE.get( cache_key) if app_credentials: return app_credentials # create a new AppCredentials app_password = await self._credential_provider.get_app_password(app_id) app_credentials = (MicrosoftGovernmentAppCredentials( app_id, app_password, scope=oauth_scope) if self._credential_provider and self._channel_provider.is_government() else MicrosoftAppCredentials( app_id, app_password, oauth_scope=oauth_scope)) # put it in the cache BotFrameworkHttpClient._APP_CREDENTIALS_CACHE[ cache_key] = app_credentials return app_credentials
def _get_or_create_connector_client( self, service_url: str, credentials: AppCredentials) -> ConnectorClient: if not credentials: credentials = MicrosoftAppCredentials.empty() # Get ConnectorClient from cache or create. client_key = BotFrameworkAdapter.key_for_connector_client( service_url, credentials.microsoft_app_id, credentials.oauth_scope) client = self._connector_client_cache.get(client_key) if not client: client = ConnectorClient(credentials, base_url=service_url) client.config.add_user_agent(USER_AGENT) self._connector_client_cache[client_key] = client return client
def _create_streaming_connector_client( self, activity: Activity, request_handler: StreamingRequestHandler) -> ConnectorClient: empty_credentials = (MicrosoftAppCredentials.empty() if self._channel_provider and self._channel_provider.is_government() else MicrosoftGovernmentAppCredentials.empty()) streaming_driver = StreamingHttpDriver(request_handler) config = BotFrameworkConnectorConfiguration( empty_credentials, activity.service_url, pipeline_type=AsyncBfPipeline, driver=streaming_driver, ) streaming_driver.config = config connector_client = ConnectorClient(None, custom_configuration=config) return connector_client
async def create( self, service_url: str, audience: str # pylint: disable=unused-argument ) -> ConnectorClient: if not self._service_url: self._service_url = service_url elif service_url != self._service_url: raise RuntimeError( "This is a streaming scenario, all connectors from this factory must all be for the same url." ) # TODO: investigate if Driver and pipeline should be moved here streaming_driver = StreamingHttpDriver(self._request_handler) config = BotFrameworkConnectorConfiguration( MicrosoftAppCredentials.empty(), service_url, pipeline_type=AsyncBfPipeline, driver=streaming_driver, ) streaming_driver.config = config connector_client = ConnectorClient(None, custom_configuration=config) return connector_client
async def __get_app_credentials(self, app_id: str, oauth_scope: str) -> AppCredentials: if not app_id: return MicrosoftAppCredentials.empty() # get from the cache if it's there cache_key = BotFrameworkAdapter.key_for_app_credentials( app_id, oauth_scope) app_credentials = self._app_credential_map.get(cache_key) if app_credentials: return app_credentials # If app credentials were provided, use them as they are the preferred choice moving forward if self._credentials: self._app_credential_map[cache_key] = self._credentials return self._credentials # Credentials not found in cache, build them app_credentials = await self.__build_credentials(app_id, oauth_scope) # Cache the credentials for later use self._app_credential_map[cache_key] = app_credentials return app_credentials