use std::sync::Arc;
use bytes::Bytes;
use crate::api::{UploadOptions, UploadResult};
use crate::client::Inner;
use crate::pss::Subscription;
use crate::swarm::{
BatchId, Error, EthAddress, Identifier, PrivateKey, Reference, SPAN_LENGTH,
calculate_single_owner_chunk_address, make_single_owner_chunk,
};
#[derive(Clone, Debug)]
pub struct GsocApi {
pub(crate) inner: Arc<Inner>,
}
impl GsocApi {
pub(crate) fn new(inner: Arc<Inner>) -> Self {
Self { inner }
}
pub async fn send(
&self,
batch_id: &BatchId,
signer: &PrivateKey,
identifier: &Identifier,
data: &[u8],
opts: Option<&UploadOptions>,
) -> Result<UploadResult, Error> {
let soc = make_single_owner_chunk(identifier, data, signer)?;
let mut body = Vec::with_capacity(SPAN_LENGTH + soc.payload.len());
body.extend_from_slice(soc.span.as_bytes());
body.extend_from_slice(&soc.payload);
let owner = signer.public_key()?.address();
let file_api = crate::file::FileApi::new(self.inner.clone());
file_api
.upload_soc(
batch_id,
&owner,
identifier,
&soc.signature,
Bytes::from(body),
opts,
)
.await
}
pub async fn subscribe(
&self,
owner: &EthAddress,
identifier: &Identifier,
) -> Result<Subscription, Error> {
let addr = calculate_single_owner_chunk_address(identifier, owner)?;
let path = format!("gsoc/subscribe/{}", addr.to_hex());
Subscription::open(&self.inner, &path).await
}
pub fn soc_address(
&self,
identifier: &Identifier,
owner: &EthAddress,
) -> Result<Reference, Error> {
calculate_single_owner_chunk_address(identifier, owner)
}
}