use std::collections::HashMap;
use serde_json::Value;
use crate::analytics::Analytics;
use crate::error::{BilldogEngError, Result};
use crate::flags::Flags;
use crate::llm::Llm;
use crate::messaging::Messaging;
use crate::surveys::Surveys;
use crate::transport::{Transport, TransportConfig};
use crate::types::{
BilldogEngOptions, DispatchParams, FlagEvalOptions, FlagValue, Groups, LlmTraceParams,
Properties,
};
pub struct BilldogEng {
pub options: BilldogEngOptions,
analytics: Analytics,
flags: Flags,
pub surveys: Surveys,
pub messaging: Messaging,
pub llm: Llm,
}
impl BilldogEng {
pub fn new(api_key: &str, options: BilldogEngOptions) -> Self {
assert!(!api_key.is_empty(), "BilldogEng: apiKey is required");
let transport = Transport::new(TransportConfig {
host: options.host.clone(),
request_timeout_ms: options.request_timeout_ms,
gzip: options.gzip,
max_retries: options.max_retries,
enable_logging: options.enable_logging,
});
let analytics = Analytics::new(
transport.clone(),
api_key.to_string(),
options.flush_at,
options.flush_interval_ms,
options.max_queue_size,
options.enable_logging,
options.group_type_index.clone(),
);
let flags = Flags::new(
transport.clone(),
api_key.to_string(),
options.local_evaluation,
options.enable_logging,
);
let surveys = Surveys::new(transport.clone(), api_key.to_string());
let messaging = Messaging::new(transport.clone());
let llm = Llm::new(transport, api_key.to_string());
Self {
options,
analytics,
flags,
surveys,
messaging,
llm,
}
}
pub fn capture(
&self,
distinct_id: &str,
event: &str,
properties: Properties,
groups: Groups,
) {
self.analytics.capture(distinct_id, event, properties, groups);
}
pub fn identify(&self, distinct_id: &str, properties: Properties) {
self.analytics.identify(distinct_id, properties);
}
pub fn group_identify(&self, group_type: &str, group_key: &str, properties: Properties) {
self.analytics.group_identify(group_type, group_key, properties);
}
pub fn alias(&self, distinct_id: &str, alias: &str) {
self.analytics.alias(distinct_id, alias);
}
pub fn flush(&self) -> Result<()> {
self.analytics.flush()
}
pub fn queue_length(&self) -> usize {
self.analytics.queue_length()
}
pub fn get_feature_flag(
&self,
key: &str,
distinct_id: &str,
opts: &FlagEvalOptions,
) -> Result<Option<FlagValue>> {
self.flags.get_feature_flag(key, distinct_id, opts)
}
pub fn is_feature_enabled(
&self,
key: &str,
distinct_id: &str,
opts: &FlagEvalOptions,
) -> Result<bool> {
self.flags.is_feature_enabled(key, distinct_id, opts)
}
pub fn get_feature_flag_payload(
&self,
key: &str,
distinct_id: &str,
opts: &FlagEvalOptions,
) -> Result<Option<Value>> {
self.flags.get_feature_flag_payload(key, distinct_id, opts)
}
pub fn get_all_flags(
&self,
distinct_id: &str,
opts: &FlagEvalOptions,
) -> Result<HashMap<String, FlagValue>> {
self.flags.get_all_flags(distinct_id, opts)
}
pub fn reload_feature_flag_definitions(&self) -> Result<()> {
self.flags.reload_feature_flag_definitions()
}
pub fn feature_flags(&self) -> &Flags {
&self.flags
}
pub fn dispatch_message(&self, params: &DispatchParams) -> Result<Value> {
self.messaging.dispatch(params)
}
pub fn capture_trace(&self, params: &LlmTraceParams) -> Result<Value> {
self.llm.capture_trace(params)
}
pub fn shutdown(&self) -> Result<()> {
self.analytics.shutdown()
}
}
impl Drop for BilldogEng {
fn drop(&mut self) {
let _: std::result::Result<(), BilldogEngError> = self.analytics.shutdown();
}
}