def __init__(self, config): self._config = config self._api_client_factory = ApiClientFactory(config) self._pod_client = self._api_client_factory.get_pod_client() self._agent_client = self._api_client_factory.get_agent_client() self._authenticator_factory = AuthenticatorFactory( config, api_client_factory=self._api_client_factory) self._bot_session = None self._ext_app_authenticator = None self._service_factory = None self._user_service = None self._message_service = None self._connection_service = None self._stream_service = None self._application_service = None self._signal_service = None self._session_service = None self._datafeed_loop = None self._health_service = None self._presence_service = None self._activity_registry = None if self._config.bot.is_authentication_configured(): self._initialize_bot_services() else: logging.info( "Bot (service account) credentials have not been configured." "You can however use services in OBO mode if app authentication is configured." )
async def test_client_cert_configured_for_app(client_certificate_path, config): config.app.certificate.path = client_certificate_path client_factory = ApiClientFactory(config) assert client_factory.get_app_session_auth_client( ).configuration.cert_file == client_certificate_path
async def test_client_cert_not_configured(config): client_factory = ApiClientFactory(config) assert client_factory.get_session_auth_client( ).configuration.cert_file is None assert client_factory.get_key_auth_client().configuration.cert_file is None assert client_factory.get_app_session_auth_client( ).configuration.cert_file is None
async def test_client_cert_configured_for_app(config): client_cert_path = get_resource_filepath("cert/megabot.pem", as_text=True) config.app.certificate.path = client_cert_path client_factory = ApiClientFactory(config) assert client_factory.get_app_session_auth_client( ).configuration.cert_file == client_cert_path
def test_trust_store_configured(config): with patch("symphony.bdk.gen.rest.RESTClientObject"): truststore_path = "/path/to/truststore.pem" config.ssl = BdkSslConfig({"trustStore": {"path": truststore_path}}) client_factory = ApiClientFactory(config) assert client_factory.get_pod_client( ).configuration.ssl_ca_cert == truststore_path
def __init__( self, api_client_factory: ApiClientFactory, auth_session: AuthSession, config: BdkConfig ): self._pod_client = api_client_factory.get_pod_client() self._agent_client = api_client_factory.get_agent_client() self._auth_session = auth_session self._config = config
async def test_x_trace_id_in_default_headers(config, add_x_trace_id): default_headers = {"x-trace-id": "trace-id"} config.default_headers = default_headers with patch("symphony.bdk.core.client.api_client_factory.add_x_trace_id", return_value=add_x_trace_id) as mock: client_factory = ApiClientFactory(config) mock.assert_not_called() assert_default_headers(client_factory.get_pod_client().default_headers, default_headers) assert_default_headers( client_factory.get_login_client().default_headers, default_headers) assert_default_headers( client_factory.get_agent_client().default_headers, default_headers) assert_default_headers( client_factory.get_session_auth_client().default_headers, default_headers) assert_default_headers( client_factory.get_key_auth_client().default_headers, default_headers) assert_default_headers( client_factory.get_app_session_auth_client().default_headers, default_headers) assert_default_headers( client_factory.get_relay_client().default_headers, default_headers)
def __init__( self, api_client_factory: ApiClientFactory, auth_session: AuthSession, config: BdkConfig ): self._pod_client = api_client_factory.get_pod_client() self._agent_client = api_client_factory.get_agent_client() self._auth_session = auth_session self._config = config self._session_service = SessionService(SessionApi(self._pod_client), self._auth_session, self._config.retry)
async def test_host_configured(config): client_factory = ApiClientFactory(config) assert_host_configured_only(client_factory.get_pod_client(), POD) assert_host_configured_only(client_factory.get_login_client(), LOGIN) assert_host_configured_only(client_factory.get_agent_client(), AGENT) assert_host_configured_only(client_factory.get_session_auth_client(), SESSION_AUTH) assert_host_configured_only(client_factory.get_key_auth_client(), KEY_AUTH) assert_host_configured_only(client_factory.get_app_session_auth_client(), SESSION_AUTH) assert_host_configured_only(client_factory.get_relay_client(), RELAY)
async def test_global_user_agent_configured(config): custom_user_agent = "custom-user-agent" config.default_headers = {"user-agent": custom_user_agent} client_factory = ApiClientFactory(config) assert client_factory.get_pod_client().user_agent == custom_user_agent assert client_factory.get_login_client().user_agent == custom_user_agent assert client_factory.get_agent_client().user_agent == custom_user_agent assert client_factory.get_session_auth_client( ).user_agent == custom_user_agent assert client_factory.get_key_auth_client().user_agent == custom_user_agent assert client_factory.get_app_session_auth_client( ).user_agent == custom_user_agent assert client_factory.get_relay_client().user_agent == custom_user_agent
async def test_default_headers_at_km_level(config): default_headers = {"header_name": "header_value"} config.key_manager._default_headers = default_headers client_factory = ApiClientFactory(config) assert_default_headers(client_factory.get_pod_client().default_headers, {}) assert_default_headers(client_factory.get_login_client().default_headers, {}) assert_default_headers(client_factory.get_agent_client().default_headers, {}) assert_default_headers( client_factory.get_session_auth_client().default_headers, {}) assert_default_headers( client_factory.get_key_auth_client().default_headers, default_headers) assert_default_headers( client_factory.get_app_session_auth_client().default_headers, {}) assert_default_headers(client_factory.get_relay_client().default_headers, default_headers)
async def test_x_trace_id_not_in_default_headers(config, add_x_trace_id): with patch("symphony.bdk.core.client.api_client_factory.add_x_trace_id", return_value=add_x_trace_id) as mock: client_factory = ApiClientFactory(config) assert mock.call_count == 7 assert_default_headers(client_factory.get_pod_client().default_headers, {}) assert_default_headers( client_factory.get_login_client().default_headers, {}) assert_default_headers( client_factory.get_agent_client().default_headers, {}) assert_default_headers( client_factory.get_session_auth_client().default_headers, {}) assert_default_headers( client_factory.get_key_auth_client().default_headers, {}) assert_default_headers( client_factory.get_app_session_auth_client().default_headers, {}) assert_default_headers( client_factory.get_relay_client().default_headers, {})
async def test_proxy_configured(config): proxy_host = "proxy.com" proxy_port = 1234 config.proxy = BdkProxyConfig(proxy_host, proxy_port) client_factory = ApiClientFactory(config) assert_host_and_proxy_configured(client_factory.get_pod_client(), POD, proxy_host, proxy_port) assert_host_and_proxy_configured(client_factory.get_login_client(), LOGIN, proxy_host, proxy_port) assert_host_and_proxy_configured(client_factory.get_agent_client(), AGENT, proxy_host, proxy_port) assert_host_and_proxy_configured(client_factory.get_session_auth_client(), SESSION_AUTH, proxy_host, proxy_port) assert_host_and_proxy_configured(client_factory.get_key_auth_client(), KEY_AUTH, proxy_host, proxy_port) assert_host_and_proxy_configured( client_factory.get_app_session_auth_client(), SESSION_AUTH, proxy_host, proxy_port) assert_host_and_proxy_configured(client_factory.get_relay_client(), RELAY, proxy_host, proxy_port)
async def test_global_default_headers(config): config.default_headers = {"header_name": "header_value"} client_factory = ApiClientFactory(config) assert_default_headers(client_factory.get_pod_client().default_headers, config.default_headers) assert_default_headers(client_factory.get_login_client().default_headers, config.default_headers) assert_default_headers(client_factory.get_agent_client().default_headers, config.default_headers) assert_default_headers( client_factory.get_session_auth_client().default_headers, config.default_headers) assert_default_headers( client_factory.get_key_auth_client().default_headers, config.default_headers) assert_default_headers( client_factory.get_app_session_auth_client().default_headers, config.default_headers) assert_default_headers(client_factory.get_relay_client().default_headers, config.default_headers)
def __init__(self, config: BdkConfig, obo_session: OboAuthSession): """ :param config: the BDK configuration. :param obo_session: the OBO session to use. """ self._config = config self._obo_session = obo_session self._api_client_factory = ApiClientFactory(config) self._service_factory = OboServiceFactory(self._api_client_factory, self._obo_session, self._config) self._connection_service = self._service_factory.get_connection_service( ) self._message_service = self._service_factory.get_message_service() self._stream_service = self._service_factory.get_stream_service() self._user_service = self._service_factory.get_user_service() self._signal_service = self._service_factory.get_signal_service()
class SymphonyBdk: """BDK entry point """ async def __aenter__(self): return self async def __aexit__(self, exc_type, exc_val, exc_tb): await self.close_clients() def __init__(self, config): self._config = config self._api_client_factory = ApiClientFactory(config) self._pod_client = self._api_client_factory.get_pod_client() self._agent_client = self._api_client_factory.get_agent_client() self._authenticator_factory = AuthenticatorFactory( config, api_client_factory=self._api_client_factory) self._bot_session = None self._ext_app_authenticator = None self._service_factory = None self._user_service = None self._message_service = None self._connection_service = None self._stream_service = None self._application_service = None self._signal_service = None self._session_service = None self._datafeed_loop = None self._health_service = None self._presence_service = None self._activity_registry = None if self._config.bot.is_authentication_configured(): self._initialize_bot_services() else: logging.info( "Bot (service account) credentials have not been configured." "You can however use services in OBO mode if app authentication is configured." ) def _initialize_bot_services(self): self._bot_session = AuthSession( self._authenticator_factory.get_bot_authenticator()) self._service_factory = ServiceFactory(self._api_client_factory, self._bot_session, self._config) self._user_service = self._service_factory.get_user_service() self._message_service = self._service_factory.get_message_service() self._connection_service = self._service_factory.get_connection_service( ) self._stream_service = self._service_factory.get_stream_service() self._application_service = self._service_factory.get_application_service( ) self._signal_service = self._service_factory.get_signal_service() self._session_service = self._service_factory.get_session_service() self._datafeed_loop = self._service_factory.get_datafeed_loop() self._health_service = self._service_factory.get_health_service() self._presence_service = self._service_factory.get_presence_service() # creates ActivityRegistry that subscribes to DF Loop events self._activity_registry = ActivityRegistry(self._session_service) self._datafeed_loop.subscribe(self._activity_registry) @bot_service def bot_session(self) -> AuthSession: """Get the Bot authentication session. If the bot is not authenticated yet, perform the authentication for a new session. :return: The bot authentication session. """ return self._bot_session def app_authenticator(self) -> ExtensionAppAuthenticator: """Get the extension app authenticator. :return: a new instance of :class:`symphony.bdk.core.auth.ext_app_authenticator.ExtensionAppAuthenticator` """ if self._ext_app_authenticator is None: self._ext_app_authenticator = self._authenticator_factory.get_extension_app_authenticator( ) return self._ext_app_authenticator def obo(self, user_id: int = None, username: str = None) -> OboAuthSession: """Get the Obo authentication session. :return: The obo authentication session """ if user_id is not None: return self._authenticator_factory.get_obo_authenticator( ).authenticate_by_user_id(user_id) if username is not None: return self._authenticator_factory.get_obo_authenticator( ).authenticate_by_username(username) raise AuthInitializationError( "At least user_id or username should be given to OBO authenticate the " "extension app") def obo_services(self, obo_session: OboAuthSession) -> OboServices: """Return the entry point of all OBO-enabled services and endpoints. :param obo_session: the obo_session to use. :return: a new OboServices instance. """ return OboServices(self._config, obo_session) @bot_service def messages(self) -> MessageService: """Get the MessageService from the BDK entry point. :return: The MessageService instance. """ return self._message_service @bot_service def streams(self) -> StreamService: """Get the StreamService from the BDK entry point. :return: The StreamService instance. """ return self._stream_service @bot_service def datafeed(self) -> AbstractDatafeedLoop: """Get the Datafeed loop from the BDK entry point. :return: The Datafeed Loop instance. """ return self._datafeed_loop @bot_service def users(self) -> UserService: """Get the UserService from the BDK entry point. :return: The UserService instance. """ return self._user_service @bot_service def connections(self) -> ConnectionService: """Get the ConnectionService from the BDK entry point. :return: The ConnectionService instance. """ return self._connection_service @bot_service def applications(self) -> ApplicationService: """Get the ApplicationService from the BDK entry point. :return: The ApplicationService instance. """ return self._application_service @bot_service def signals(self) -> SignalService: """Get the SignalService from the BDK entry point. :return: The SignalService instance. """ return self._signal_service @bot_service def sessions(self) -> SessionService: """Get the SessionService from the BDK entry point. :return: The SessionService instance. """ return self._session_service @bot_service def health(self) -> HealthService: """Get the HealthService from the BDK entry point. :return: The HealthService instance. """ return self._health_service @bot_service def presence(self) -> PresenceService: """Get the PresenceService from the BDK entry point. :return: The PresenceService instance. """ return self._presence_service @bot_service def activities(self) -> ActivityRegistry: """Get the :class:`ActivityRegistry` from the BDK entry point. :return: The :class:`ActivityRegistry` instance. """ return self._activity_registry async def close_clients(self): """Close all the existing api clients created by the api client factory. """ await self._api_client_factory.close_clients()