use crate::{
agents::ForAgent, errors::AtomicResult, plugins, urls, Db, Resource, Storelike, Value,
};
type HandleGet = fn(context: HandleGetContext) -> AtomicResult<Resource>;
type HandlePost = fn(context: HandlePostContext) -> AtomicResult<Resource>;
#[derive(Debug)]
pub struct HandleGetContext<'a> {
pub subject: url::Url,
pub store: &'a Db,
pub for_agent: &'a ForAgent,
}
#[derive(Debug)]
pub struct HandlePostContext<'a> {
pub subject: url::Url,
pub store: &'a Db,
pub for_agent: &'a ForAgent,
pub body: Vec<u8>,
}
#[derive(Clone)]
pub struct Endpoint {
pub path: String,
pub handle: Option<HandleGet>,
pub handle_post: Option<HandlePost>,
pub params: Vec<String>,
pub description: String,
pub shortname: String,
}
pub struct PostEndpoint {
pub path: String,
pub handle: Option<HandlePost>,
pub params: Vec<String>,
pub description: String,
pub shortname: String,
}
impl Endpoint {
pub fn to_resource(&self, store: &impl Storelike) -> AtomicResult<Resource> {
let subject = format!("{}{}", store.get_server_url(), self.path);
let mut resource = store.get_resource_new(&subject);
resource.set_string(urls::DESCRIPTION.into(), &self.description, store)?;
resource.set_string(urls::SHORTNAME.into(), &self.shortname, store)?;
let is_a = [urls::ENDPOINT.to_string()].to_vec();
resource.set(urls::IS_A.into(), is_a.into(), store)?;
let params_vec: Vec<String> = self.params.clone();
resource.set(
urls::ENDPOINT_PARAMETERS.into(),
Value::from(params_vec),
store,
)?;
Ok(resource)
}
}
pub fn default_endpoints() -> Vec<Endpoint> {
vec![
plugins::versioning::version_endpoint(),
plugins::versioning::all_versions_endpoint(),
plugins::path::path_endpoint(),
plugins::search::search_endpoint(),
plugins::files::upload_endpoint(),
plugins::files::download_endpoint(),
plugins::export::export_endpoint(),
#[cfg(feature = "html")]
plugins::bookmark::bookmark_endpoint(),
plugins::importer::import_endpoint(),
plugins::query::query_endpoint(),
#[cfg(debug_assertions)]
plugins::prunetests::prune_tests_endpoint(),
]
}