use candid::Principal;
use std::cell::RefCell;
use std::rc::Rc;
pub trait IcApi {
fn caller(&self) -> Principal;
fn id(&self) -> Principal;
fn time(&self) -> u64;
fn println(&self, message: &str);
}
pub struct DefaultIcApi;
impl IcApi for DefaultIcApi {
fn caller(&self) -> Principal {
ic_cdk::api::caller()
}
fn id(&self) -> Principal {
ic_cdk::api::id()
}
fn time(&self) -> u64 {
ic_cdk::api::time()
}
fn println(&self, message: &str) {
ic_cdk::println!("{}", message);
}
}
thread_local! {
static CURRENT_IC_API: RefCell<Rc<dyn IcApi>> = RefCell::new(Rc::new(DefaultIcApi));
}
pub fn set_ic_api(api: Rc<dyn IcApi>) {
CURRENT_IC_API.with(|current| {
*current.borrow_mut() = api;
});
}
pub fn get_ic_api() -> Rc<dyn IcApi> {
CURRENT_IC_API.with(|current| current.borrow().clone())
}