use super::{ClientPortalBackend, FakeBackend, FakeFixtureStore, IbkrBackend};
use crate::internal::audit::AuditHmacKey;
use crate::internal::cpapi::ClientPortalClient;
use crate::internal::domain::{BrokerBackendKind, ErrorCode, GatewayError};
use std::path::PathBuf;
use std::sync::Arc;
use url::Url;
#[derive(Clone, Debug)]
pub struct BackendFactoryConfig {
pub backend: BrokerBackendKind,
pub fixture_root: PathBuf,
pub client_portal_base_url: Option<Url>,
pub verify_tls: bool,
pub audit_hmac_key: Arc<AuditHmacKey>,
}
pub fn create_backend(config: BackendFactoryConfig) -> Result<Box<dyn IbkrBackend>, GatewayError> {
match config.backend {
BrokerBackendKind::Fake => Ok(Box::new(FakeBackend::new(FakeFixtureStore::new(
config.fixture_root,
)))),
BrokerBackendKind::ClientPortalGateway => {
let Some(base_url) = config.client_portal_base_url else {
return Err(GatewayError::new(
ErrorCode::ConfigMissingBrokerBaseUrl,
"Client Portal Gateway base URL is required",
false,
Some("Configure broker.client_portal_gateway.base_url".to_string()),
));
};
Ok(Box::new(ClientPortalBackend::new(
ClientPortalClient::new(base_url, config.verify_tls)?,
config.audit_hmac_key,
)))
}
}
}