use std::any::Any;
use std::collections::HashMap;
use std::error::Error;
use std::future::Future;
use anyhow::Result;
#[cfg(target_arch = "wasm32")]
use anyhow::{Context, anyhow};
use bytes::Bytes;
use http::{Request, Response};
use http_body::Body;
use omnia_wasi_sql::{DataType, Row};
pub trait Config: Send + Sync {
#[cfg(not(target_arch = "wasm32"))]
fn get(&self, key: &str) -> impl Future<Output = Result<String>> + Send;
#[cfg(target_arch = "wasm32")]
fn get(&self, key: &str) -> impl Future<Output = Result<String>> + Send {
async move {
let config = omnia_wasi_config::store::get(key).context("getting configuration")?;
config.ok_or_else(|| anyhow!("configuration not found"))
}
}
}
pub trait HttpRequest: Send + Sync {
#[cfg(not(target_arch = "wasm32"))]
fn fetch<T>(&self, request: Request<T>) -> impl Future<Output = Result<Response<Bytes>>> + Send
where
T: Body + Any + Send,
T::Data: Into<Vec<u8>>,
T::Error: Into<Box<dyn Error + Send + Sync + 'static>>;
#[cfg(target_arch = "wasm32")]
fn fetch<T>(&self, request: Request<T>) -> impl Future<Output = Result<Response<Bytes>>> + Send
where
T: Body + Any + Send,
T::Data: Into<Vec<u8>>,
T::Error: Into<Box<dyn Error + Send + Sync + 'static>>,
{
async move { omnia_wasi_http::handle(request).await }
}
}
#[derive(Clone, Debug)]
pub struct Message {
pub payload: Vec<u8>,
pub headers: HashMap<String, String>,
}
impl Message {
#[must_use]
pub fn new(payload: &[u8]) -> Self {
Self {
payload: payload.to_vec(),
headers: HashMap::new(),
}
}
}
pub trait Publish: Send + Sync {
#[cfg(not(target_arch = "wasm32"))]
fn send(&self, topic: &str, message: &Message) -> impl Future<Output = Result<()>> + Send;
#[cfg(target_arch = "wasm32")]
fn send(&self, topic: &str, message: &Message) -> impl Future<Output = Result<()>> + Send {
use omnia_wasi_messaging::producer;
use omnia_wasi_messaging::types::{self as wasi, Client};
async move {
let client =
Client::connect("host".to_string()).await.context("connecting to broker")?;
producer::send(&client, topic.to_string(), wasi::Message::new(&message.payload))
.await
.with_context(|| format!("sending message to {topic}"))
}
}
}
pub trait StateStore: Send + Sync {
#[cfg(not(target_arch = "wasm32"))]
fn get(&self, key: &str) -> impl Future<Output = Result<Option<Vec<u8>>>> + Send;
#[cfg(not(target_arch = "wasm32"))]
fn set(
&self, key: &str, value: &[u8], ttl_secs: Option<u64>,
) -> impl Future<Output = Result<Option<Vec<u8>>>> + Send;
#[cfg(not(target_arch = "wasm32"))]
fn delete(&self, key: &str) -> impl Future<Output = Result<()>> + Send;
#[cfg(target_arch = "wasm32")]
fn get(&self, key: &str) -> impl Future<Output = Result<Option<Vec<u8>>>> + Send {
async move {
let bucket =
omnia_wasi_keyvalue::cache::open("cache").await.context("opening cache")?;
bucket.get(key).await.context("reading state from cache")
}
}
#[cfg(target_arch = "wasm32")]
fn set(
&self, key: &str, value: &[u8], ttl_secs: Option<u64>,
) -> impl Future<Output = Result<Option<Vec<u8>>>> + Send {
async move {
let bucket =
omnia_wasi_keyvalue::cache::open("cache").await.context("opening cache")?;
bucket.set(key, value, ttl_secs).await.context("reading state from cache")
}
}
#[cfg(target_arch = "wasm32")]
fn delete(&self, key: &str) -> impl Future<Output = Result<()>> + Send {
async move {
let bucket =
omnia_wasi_keyvalue::cache::open("cache").await.context("opening cache")?;
bucket.delete(key).await.context("deleting entry from cache")
}
}
}
pub trait Identity: Send + Sync {
#[cfg(not(target_arch = "wasm32"))]
fn access_token(&self, identity: String) -> impl Future<Output = Result<String>> + Send;
#[cfg(target_arch = "wasm32")]
fn access_token(&self, identity: String) -> impl Future<Output = Result<String>> + Send {
use omnia_wasi_identity::credentials::get_identity;
async move {
let identity = wit_bindgen::block_on(get_identity(identity))?;
let access_token =
wit_bindgen::block_on(async move { identity.get_token(vec![]).await })?;
Ok(access_token.token)
}
}
}
pub trait TableStore: Send + Sync {
#[cfg(not(target_arch = "wasm32"))]
fn query(
&self, cnn_name: String, query: String, params: Vec<DataType>,
) -> impl Future<Output = Result<Vec<Row>>> + Send;
#[cfg(not(target_arch = "wasm32"))]
fn exec(
&self, cnn_name: String, query: String, params: Vec<DataType>,
) -> impl Future<Output = Result<u32>> + Send;
#[cfg(target_arch = "wasm32")]
fn query(
&self, cnn_name: String, query: String, params: Vec<DataType>,
) -> impl Future<Output = Result<Vec<Row>>> + Send {
use omnia_wasi_sql::types::{Connection, Statement};
async move {
let cnn = Connection::open(cnn_name)
.await
.map_err(|e| anyhow!("failed to open connection: {}", e.trace()))?;
let stmt = Statement::prepare(query, params)
.await
.map_err(|e| anyhow!("failed to prepare statement: {}", e.trace()))?;
let res = omnia_wasi_sql::readwrite::query(&cnn, &stmt)
.await
.map_err(|e| anyhow!("query failed: {}", e.trace()))?;
Ok(res)
}
}
#[cfg(target_arch = "wasm32")]
fn exec(
&self, cnn_name: String, query: String, params: Vec<DataType>,
) -> impl Future<Output = Result<u32>> + Send {
use omnia_wasi_sql::types::{Connection, Statement};
async move {
let cnn = Connection::open(cnn_name)
.await
.map_err(|e| anyhow!("failed to open connection: {}", e.trace()))?;
let stmt = Statement::prepare(query, params)
.await
.map_err(|e| anyhow!("failed to prepare statement: {}", e.trace()))?;
let res = omnia_wasi_sql::readwrite::exec(&cnn, &stmt)
.await
.map_err(|e| anyhow!("exec failed: {}", e.trace()))?;
Ok(res)
}
}
}
pub trait Broadcast: Send + Sync {
#[cfg(not(target_arch = "wasm32"))]
fn send(
&self, name: &str, data: &[u8], sockets: Option<Vec<String>>,
) -> impl Future<Output = Result<()>> + Send;
#[cfg(target_arch = "wasm32")]
fn send(
&self, name: &str, data: &[u8], sockets: Option<Vec<String>>,
) -> impl Future<Output = Result<()>> + Send {
async move {
let client = omnia_wasi_websocket::types::Client::connect(name.to_string())
.await
.map_err(|e| anyhow!("connecting to websocket: {e}"))?;
let event = omnia_wasi_websocket::types::Event::new(data);
omnia_wasi_websocket::client::send(&client, event, sockets)
.await
.map_err(|e| anyhow!("sending websocket event: {e}"))
}
}
}