Skip to main content

LiteClient

Struct LiteClient 

Source
pub struct LiteClient { /* private fields */ }
Expand description

A light Swarm client: a read endpoint, plus an optional stamped node for uploads.

Implementations§

Source§

impl LiteClient

Source

pub fn read(endpoint: &str) -> Result<Self>

Build a read client pointed at endpoint (a public gateway or any Bee HTTP base URL).

Examples found in repository?
examples/read.rs (line 17)
9async fn main() -> anyhow::Result<()> {
10    let reference = std::env::args()
11        .nth(1)
12        .expect("usage: read <reference> [path]");
13    let path = std::env::args().nth(2);
14    let gateway = std::env::var("BEE_GATEWAY")
15        .unwrap_or_else(|_| "https://download.gateway.ethswarm.org".into());
16
17    let scout = LiteClient::read(&gateway)?;
18    let data = match &path {
19        Some(p) => scout.cat_path(&reference, p).await?,
20        None => scout.cat(&reference).await?,
21    };
22    print!("{}", String::from_utf8_lossy(&data));
23    Ok(())
24}
More examples
Hide additional examples
examples/upload_and_feed.rs (line 16)
10async fn main() -> anyhow::Result<()> {
11    let file = std::env::args().nth(1).expect("usage: upload_and_feed <file>");
12    let node = std::env::var("BEE_NODE").unwrap_or_else(|_| "http://localhost:1633".into());
13    let stamp = std::env::var("BEE_STAMP").expect("set BEE_STAMP to a usable postage batch id");
14
15    // Reads use the node here too; in practice the read endpoint can be a gateway.
16    let scout = LiteClient::read(&node)?.with_write(&node, &stamp)?;
17
18    let data = std::fs::read(&file)?;
19    let file_ref = scout.up_bytes(data).await?;
20    println!("uploaded:      {file_ref}");
21
22    // A throwaway feed identity for the demo; persist this key to keep the handle.
23    let (key, _owner, _pubkey) = scout::generate_key()?;
24    let handle = scout.publish(&key, "example-feed", &file_ref).await?;
25    println!("feed handle:   {handle}");
26    println!("read latest:   scout cat {handle} --gateway {node}");
27    Ok(())
28}
Source

pub fn with_write(self, node_endpoint: &str, stamp: &str) -> Result<Self>

Attach a write endpoint: a Bee node holding a usable postage batch (stamp, hex). Uploads go here; reads keep using the read endpoint.

Examples found in repository?
examples/upload_and_feed.rs (line 16)
10async fn main() -> anyhow::Result<()> {
11    let file = std::env::args().nth(1).expect("usage: upload_and_feed <file>");
12    let node = std::env::var("BEE_NODE").unwrap_or_else(|_| "http://localhost:1633".into());
13    let stamp = std::env::var("BEE_STAMP").expect("set BEE_STAMP to a usable postage batch id");
14
15    // Reads use the node here too; in practice the read endpoint can be a gateway.
16    let scout = LiteClient::read(&node)?.with_write(&node, &stamp)?;
17
18    let data = std::fs::read(&file)?;
19    let file_ref = scout.up_bytes(data).await?;
20    println!("uploaded:      {file_ref}");
21
22    // A throwaway feed identity for the demo; persist this key to keep the handle.
23    let (key, _owner, _pubkey) = scout::generate_key()?;
24    let handle = scout.publish(&key, "example-feed", &file_ref).await?;
25    println!("feed handle:   {handle}");
26    println!("read latest:   scout cat {handle} --gateway {node}");
27    Ok(())
28}
Source

pub async fn cat(&self, reference: &str) -> Result<Vec<u8>>

Download manifest-aware content at reference (GET /bzz/{ref}).

Examples found in repository?
examples/read.rs (line 20)
9async fn main() -> anyhow::Result<()> {
10    let reference = std::env::args()
11        .nth(1)
12        .expect("usage: read <reference> [path]");
13    let path = std::env::args().nth(2);
14    let gateway = std::env::var("BEE_GATEWAY")
15        .unwrap_or_else(|_| "https://download.gateway.ethswarm.org".into());
16
17    let scout = LiteClient::read(&gateway)?;
18    let data = match &path {
19        Some(p) => scout.cat_path(&reference, p).await?,
20        None => scout.cat(&reference).await?,
21    };
22    print!("{}", String::from_utf8_lossy(&data));
23    Ok(())
24}
Source

pub async fn cat_path(&self, reference: &str, path: &str) -> Result<Vec<u8>>

Download one path inside a collection (GET /bzz/{ref}/{path}).

Examples found in repository?
examples/read.rs (line 19)
9async fn main() -> anyhow::Result<()> {
10    let reference = std::env::args()
11        .nth(1)
12        .expect("usage: read <reference> [path]");
13    let path = std::env::args().nth(2);
14    let gateway = std::env::var("BEE_GATEWAY")
15        .unwrap_or_else(|_| "https://download.gateway.ethswarm.org".into());
16
17    let scout = LiteClient::read(&gateway)?;
18    let data = match &path {
19        Some(p) => scout.cat_path(&reference, p).await?,
20        None => scout.cat(&reference).await?,
21    };
22    print!("{}", String::from_utf8_lossy(&data));
23    Ok(())
24}
Source

pub async fn bytes(&self, reference: &str) -> Result<Vec<u8>>

Download raw bytes (GET /bytes/{ref}).

Source

pub async fn up_file( &self, name: &str, content_type: &str, data: Vec<u8>, ) -> Result<String>

Upload a file (manifest-wrapped, POST /bzz). Returns the bzz reference as hex.

Source

pub async fn up_bytes(&self, data: Vec<u8>) -> Result<String>

Upload raw bytes (POST /bytes). Returns the bytes reference as hex.

Examples found in repository?
examples/upload_and_feed.rs (line 19)
10async fn main() -> anyhow::Result<()> {
11    let file = std::env::args().nth(1).expect("usage: upload_and_feed <file>");
12    let node = std::env::var("BEE_NODE").unwrap_or_else(|_| "http://localhost:1633".into());
13    let stamp = std::env::var("BEE_STAMP").expect("set BEE_STAMP to a usable postage batch id");
14
15    // Reads use the node here too; in practice the read endpoint can be a gateway.
16    let scout = LiteClient::read(&node)?.with_write(&node, &stamp)?;
17
18    let data = std::fs::read(&file)?;
19    let file_ref = scout.up_bytes(data).await?;
20    println!("uploaded:      {file_ref}");
21
22    // A throwaway feed identity for the demo; persist this key to keep the handle.
23    let (key, _owner, _pubkey) = scout::generate_key()?;
24    let handle = scout.publish(&key, "example-feed", &file_ref).await?;
25    println!("feed handle:   {handle}");
26    println!("read latest:   scout cat {handle} --gateway {node}");
27    Ok(())
28}
Source

pub async fn up_dir( &self, folder: &str, index_document: Option<&str>, ) -> Result<String>

Upload a whole directory as a browsable Swarm collection (a mantaray manifest). index_document (e.g. index.html) is served as the default document at bzz/<ref>/. Returns the manifest reference. Needs a write endpoint (--stamp).

Source

pub async fn publish( &self, key_hex: &str, topic: &str, reference: &str, ) -> Result<String>

Point a feed (key owner + topic) at reference and ensure a feed manifest exists. Returns the manifest reference — a stable handle that always resolves to the feed’s latest content. Read it later with LiteClient::cat; re-publishing updates what that same handle serves. Needs a write endpoint (--stamp).

Examples found in repository?
examples/upload_and_feed.rs (line 24)
10async fn main() -> anyhow::Result<()> {
11    let file = std::env::args().nth(1).expect("usage: upload_and_feed <file>");
12    let node = std::env::var("BEE_NODE").unwrap_or_else(|_| "http://localhost:1633".into());
13    let stamp = std::env::var("BEE_STAMP").expect("set BEE_STAMP to a usable postage batch id");
14
15    // Reads use the node here too; in practice the read endpoint can be a gateway.
16    let scout = LiteClient::read(&node)?.with_write(&node, &stamp)?;
17
18    let data = std::fs::read(&file)?;
19    let file_ref = scout.up_bytes(data).await?;
20    println!("uploaded:      {file_ref}");
21
22    // A throwaway feed identity for the demo; persist this key to keep the handle.
23    let (key, _owner, _pubkey) = scout::generate_key()?;
24    let handle = scout.publish(&key, "example-feed", &file_ref).await?;
25    println!("feed handle:   {handle}");
26    println!("read latest:   scout cat {handle} --gateway {node}");
27    Ok(())
28}
Source

pub async fn share( &self, name: &str, content_type: &str, data: Vec<u8>, grantees: &[String], ) -> Result<ShareInfo>

Upload data under an ACT and grant the given compressed-pubkey grantees. Returns the refs to download it (file_ref + history + publisher) and to manage access (grantee_ref). Needs --stamp.

Source

pub async fn revoke( &self, grantee_ref: &str, history: &str, remove: &[String], ) -> Result<(String, String)>

Revoke remove grantees from a grantee list anchored at the upload’s history. Returns the new (grantee_ref, grantee_history). Needs --stamp.

Source

pub async fn grantees(&self, grantee_ref: &str) -> Result<Vec<String>>

List the grantees of a grantee-list reference. Read-only.

Source

pub async fn fetch_act( &self, file_ref: &str, publisher: &str, history: &str, ) -> Result<Vec<u8>>

Download ACT-protected content as publisher (their compressed pubkey) using the upload’s history. The Bee node decrypts — it must be the publisher or an authorised grantee. Read-only.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more