use crate::error::Error;
use super::client::SeedClient;
use super::models::{
CustodyEpoch, Identity, OtaCheckNowAck, OtaConfig, PairCreate, PairCreateResponse, PairStatus,
Status, StoreIngest, StoreIngestAck, StoreQuery, StoreQueryResult, StoreStatus, WitnessChain,
};
#[derive(Debug)]
pub struct SeedSession<'c> {
pub(crate) client: &'c SeedClient,
pub(crate) pinned_peer: String,
}
impl<'c> SeedSession<'c> {
pub fn pinned_peer(&self) -> &str {
&self.pinned_peer
}
pub async fn status(&self) -> Result<Status, Error> {
self.client
.request_on_peer_get("/status", Some(&self.pinned_peer))
.await
}
pub async fn identity(&self) -> Result<Identity, Error> {
self.client
.request_on_peer_get("/identity", Some(&self.pinned_peer))
.await
}
pub fn pair(&self) -> SessionPair<'_, 'c> {
SessionPair { session: self }
}
pub fn store(&self) -> SessionStore<'_, 'c> {
SessionStore { session: self }
}
pub fn witness(&self) -> SessionWitness<'_, 'c> {
SessionWitness { session: self }
}
pub fn custody(&self) -> SessionCustody<'_, 'c> {
SessionCustody { session: self }
}
pub fn ota(&self) -> SessionOta<'_, 'c> {
SessionOta { session: self }
}
}
#[derive(Debug)]
pub struct SessionPair<'s, 'c> {
session: &'s SeedSession<'c>,
}
impl<'s, 'c> SessionPair<'s, 'c> {
pub async fn status(&self) -> Result<PairStatus, Error> {
self.session
.client
.request_on_peer_get("/pair/status", Some(&self.session.pinned_peer))
.await
}
pub async fn create(&self, req: PairCreate) -> Result<PairCreateResponse, Error> {
self.session
.client
.request_on_peer_post("/pair", &req, false, Some(&self.session.pinned_peer))
.await
}
}
#[derive(Debug)]
pub struct SessionStore<'s, 'c> {
session: &'s SeedSession<'c>,
}
impl<'s, 'c> SessionStore<'s, 'c> {
pub async fn status(&self) -> Result<StoreStatus, Error> {
self.session
.client
.request_on_peer_get("/store/status", Some(&self.session.pinned_peer))
.await
}
pub async fn query(&self, req: StoreQuery) -> Result<StoreQueryResult, Error> {
self.session
.client
.request_on_peer_post("/store/query", &req, true, Some(&self.session.pinned_peer))
.await
}
pub async fn ingest(&self, req: StoreIngest) -> Result<StoreIngestAck, Error> {
self.session
.client
.request_on_peer_post(
"/store/ingest",
&req,
false,
Some(&self.session.pinned_peer),
)
.await
}
}
#[derive(Debug)]
pub struct SessionWitness<'s, 'c> {
session: &'s SeedSession<'c>,
}
impl<'s, 'c> SessionWitness<'s, 'c> {
pub async fn chain(&self) -> Result<WitnessChain, Error> {
self.session
.client
.request_on_peer_get("/witness/chain", Some(&self.session.pinned_peer))
.await
}
}
#[derive(Debug)]
pub struct SessionCustody<'s, 'c> {
session: &'s SeedSession<'c>,
}
impl<'s, 'c> SessionCustody<'s, 'c> {
pub async fn epoch(&self) -> Result<CustodyEpoch, Error> {
self.session
.client
.request_on_peer_get("/custody/epoch", Some(&self.session.pinned_peer))
.await
}
}
#[derive(Debug)]
pub struct SessionOta<'s, 'c> {
session: &'s SeedSession<'c>,
}
impl<'s, 'c> SessionOta<'s, 'c> {
pub async fn config(&self) -> Result<OtaConfig, Error> {
self.session
.client
.request_on_peer_get("/ota/config", Some(&self.session.pinned_peer))
.await
}
pub async fn check_now(&self) -> Result<OtaCheckNowAck, Error> {
self.session
.client
.request_on_peer_post(
"/ota/check-now",
&serde_json::Value::Null,
false,
Some(&self.session.pinned_peer),
)
.await
}
}