Some more restructuring of the basic API. Closing #4 now, but surely this will still be refactored as more methods are added to the API.

This commit is contained in:
Tim Bruijnzeels
2019-01-02 16:19:41 +01:00
parent 3f613b800f
commit cfdff770b4
6 changed files with 205 additions and 13 deletions
+123
View File
@@ -0,0 +1,123 @@
//! Support for the Json API
use std::sync::Arc;
use crate::ext_serde;
use crate::provisioning::publisher::Publisher;
use crate::rpki::uri;
//------------ Link ----------------------------------------------------------
/// Defines a link element to include as part of a links array in a Json
/// response.
#[derive(Clone, Debug, Serialize)]
pub struct Link<'a> {
rel: &'a str,
link: String
}
//------------ PublisherSummaryInfo ------------------------------------------
/// Defines a summary of publisher information to be used in the publisher
/// list.
#[derive(Clone, Debug, Serialize)]
pub struct PublisherSummaryInfo<'a> {
id: &'a str,
links: Vec<Link<'a>>
}
impl<'a> PublisherSummaryInfo<'a> {
pub fn from(
publisher: &'a Publisher,
path_publishers: &'a str
) -> PublisherSummaryInfo<'a> {
let id = publisher.name().as_str();
let mut links = Vec::new();
let response_link = Link {
rel: "response-xml",
link: format!("{}/{}/response-xml", path_publishers, id)
};
let self_link = Link {
rel: "self",
link: format!("{}/{}", path_publishers, id)
};
links.push(response_link);
links.push(self_link);
PublisherSummaryInfo {
id,
links
}
}
}
//------------ PublisherList -------------------------------------------------
/// This type represents a list of (all) current publishers to show in the API
#[derive(Clone, Debug, Serialize)]
pub struct PublisherList<'a> {
publishers: Vec<PublisherSummaryInfo<'a>>
}
impl<'a> PublisherList<'a> {
pub fn from(
publishers: &'a Vec<Arc<Publisher>>,
path_publishers: &'a str
) -> PublisherList<'a> {
let publishers: Vec<PublisherSummaryInfo> = publishers.iter().map(|p|
PublisherSummaryInfo::from(&p, path_publishers)
).collect();
PublisherList {
publishers
}
}
pub fn publishers(&self) -> &Vec<PublisherSummaryInfo> {
&self.publishers
}
}
//------------ PublisherDetails ----------------------------------------------
#[derive(Clone, Debug, Serialize)]
pub struct PublisherDetails<'a> {
name: &'a str,
#[serde(serialize_with = "ext_serde::ser_rsync_uri")]
base_uri: &'a uri::Rsync,
#[serde(serialize_with = "ext_serde::ser_http_uri")]
service_uri: &'a uri::Http,
links: Vec<Link<'a>>
}
impl<'a> PublisherDetails<'a> {
pub fn from(
publisher: &'a Arc<Publisher>,
path_publishers: &'a str
) -> PublisherDetails<'a> {
let name = publisher.name().as_str();
let base_uri = publisher.base_uri();
let service_uri = publisher.service_uri();
let mut links = Vec::new();
links.push(Link {
rel: "response-xml",
link: format!("{}/{}/response-xml", path_publishers, name)
});
links.push(Link {
rel: "id-cert",
link: format!("{}/{}/id.cer", path_publishers, name)
});
PublisherDetails {
name,
base_uri,
service_uri,
links
}
}
}