use crate::client::FlareApiClient;
use std::error::Error;
#[derive(Default, Debug)]
pub struct FlareApiClientBuilder {
api_key: Option<String>,
tenant_id: Option<i64>,
}
impl FlareApiClientBuilder {
pub fn with_api_key(mut self, api_key: String) -> Self {
self.api_key = Some(api_key);
self
}
pub fn with_tenant_id(mut self, tenant_id: i64) -> Self {
self.tenant_id = Some(tenant_id);
self
}
pub fn build(self) -> Result<FlareApiClient, Box<dyn Error>> {
let api_key = match self.api_key {
Some(api_key) => api_key,
None => {
"".into()
}
};
if api_key.is_empty() {
return Err("API key is required".into());
}
let client = FlareApiClient {
_api_key: api_key,
_tenant_id: self.tenant_id,
};
Ok(client)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_build_no_api_key() {
let result = FlareApiClient::builder().build();
assert_eq!(result.unwrap_err().to_string(), "API key is required",);
}
#[test]
fn test_build_with_api_key() -> Result<(), Box<dyn Error>> {
let client = FlareApiClient::builder()
.with_api_key("fw-123".into())
.build()?;
assert_eq!(client._api_key, "fw-123");
assert_eq!(client._tenant_id, None);
Ok(())
}
#[test]
fn test_build_with_api_key_tenant_id() -> Result<(), Box<dyn Error>> {
let client = FlareApiClient::builder()
.with_api_key("fw-123".into())
.with_tenant_id(123)
.build()?;
assert_eq!(client._api_key, "fw-123");
assert_eq!(client._tenant_id, Some(123));
Ok(())
}
}