iroh_docs/rpc/client/
authors.rs1use anyhow::Result;
6use futures_lite::{Stream, StreamExt};
7use quic_rpc::{client::BoxedConnector, Connector};
8
9use super::flatten;
10#[doc(inline)]
11pub use crate::engine::{Origin, SyncEvent, SyncReason};
12use crate::{
13 rpc::proto::{
14 AuthorCreateRequest, AuthorDeleteRequest, AuthorExportRequest, AuthorGetDefaultRequest,
15 AuthorImportRequest, AuthorListRequest, AuthorSetDefaultRequest, RpcService,
16 },
17 Author, AuthorId,
18};
19
20#[derive(Debug, Clone)]
22#[repr(transparent)]
23pub struct Client<C = BoxedConnector<RpcService>> {
24 pub(super) rpc: quic_rpc::RpcClient<RpcService, C>,
25}
26
27impl<C: Connector<RpcService>> Client<C> {
28 pub fn new(rpc: quic_rpc::RpcClient<RpcService, C>) -> Self {
30 Self { rpc }
31 }
32
33 pub async fn create(&self) -> Result<AuthorId> {
40 let res = self.rpc.rpc(AuthorCreateRequest).await??;
41 Ok(res.author_id)
42 }
43
44 pub async fn default(&self) -> Result<AuthorId> {
51 let res = self.rpc.rpc(AuthorGetDefaultRequest).await??;
52 Ok(res.author_id)
53 }
54
55 pub async fn set_default(&self, author_id: AuthorId) -> Result<()> {
62 self.rpc
63 .rpc(AuthorSetDefaultRequest { author_id })
64 .await??;
65 Ok(())
66 }
67
68 pub async fn list(&self) -> Result<impl Stream<Item = Result<AuthorId>>> {
72 let stream = self.rpc.server_streaming(AuthorListRequest {}).await?;
73 Ok(flatten(stream).map(|res| res.map(|res| res.author_id)))
74 }
75
76 pub async fn export(&self, author: AuthorId) -> Result<Option<Author>> {
80 let res = self.rpc.rpc(AuthorExportRequest { author }).await??;
81 Ok(res.author)
82 }
83
84 pub async fn import(&self, author: Author) -> Result<()> {
88 self.rpc.rpc(AuthorImportRequest { author }).await??;
89 Ok(())
90 }
91
92 pub async fn delete(&self, author: AuthorId) -> Result<()> {
98 self.rpc.rpc(AuthorDeleteRequest { author }).await??;
99 Ok(())
100 }
101}