Skip to main content

Client

Struct Client 

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

Client is a handle to an established NWEP connection.

Client is cheap to clone; all clones share the same underlying QUIC connection and event loop. Use fetch, get, or post to make requests, and close when done.

Implementations§

Source§

impl Client

Source

pub fn node_id(&self) -> NodeId

node_id returns the local node’s identifier derived from the connecting keypair.

Source

pub fn peer_identity(&self) -> Identity

peer_identity returns the verified identity of the remote server as established during the NWEP mutual-authentication handshake.

Source

pub fn peer_node_id(&self) -> NodeId

peer_node_id returns the remote server’s node identifier.

This is a convenience shorthand for client.peer_identity().node_id.

Source

pub fn fetch( &self, method: &str, path: &str, body: &[u8], ) -> Result<Response, Error>

fetch sends a request with the given method, path, and body, blocking until the complete response is received or the request timeout elapses.

§Errors

Returns Err if the connection is closed, the timeout elapses, or the server returns an error response. Use is_ok on the returned Response to distinguish application-level errors from transport errors.

Source

pub fn fetch_with_headers( &self, method: &str, path: &str, body: &[u8], headers: &[Header], ) -> Result<Response, Error>

fetch_with_headers sends a request with the given method, path, body, and additional request headers, blocking until the complete response is received.

§Errors

Returns Err if the connection is closed, the timeout elapses, or the C layer fails to open a stream.

Source

pub fn get(&self, path: &str) -> Result<Response, Error>

get sends a read request to path with no body.

This is a convenience wrapper around fetch using the NWEP read method.

Examples found in repository?
examples/client.rs (line 36)
4fn main() {
5    let url = std::env::args().nth(1).unwrap_or_else(|| {
6        eprintln!("usage: client <web://url/path>");
7        process::exit(1);
8    });
9
10    nwep::init().unwrap_or_else(|e| {
11        eprintln!("init: {e}");
12        process::exit(1);
13    });
14
15    nwep::log::set_level(nwep::log::LogLevel::Warn);
16    nwep::log::set_stderr(true);
17
18    let parsed = Url::parse(&url).unwrap_or_else(|e| {
19        eprintln!("parse url: {e}");
20        process::exit(1);
21    });
22    let path = if parsed.path.is_empty() { "/".to_string() } else { parsed.path.clone() };
23
24    let keypair = Keypair::generate().unwrap_or_else(|e| {
25        eprintln!("keygen: {e}");
26        process::exit(1);
27    });
28
29    let client = ClientBuilder::new()
30        .connect(keypair, &url)
31        .unwrap_or_else(|e| {
32            eprintln!("connect: {e}");
33            process::exit(1);
34        });
35
36    let resp = client.get(&path).unwrap_or_else(|e| {
37        eprintln!("get: {e}");
38        process::exit(1);
39    });
40
41    println!("{}", resp.status);
42    println!("{}", String::from_utf8_lossy(&resp.body));
43
44    client.close();
45}
Source

pub fn post(&self, path: &str, body: &[u8]) -> Result<Response, Error>

post sends a write request to path with the given body bytes.

This is a convenience wrapper around fetch using the NWEP write method.

Source

pub fn close(&self)

Sends the shutdown signal and blocks until the event loop thread has exited and all resources (C client, state, pending requests) have been freed.

Examples found in repository?
examples/client.rs (line 44)
4fn main() {
5    let url = std::env::args().nth(1).unwrap_or_else(|| {
6        eprintln!("usage: client <web://url/path>");
7        process::exit(1);
8    });
9
10    nwep::init().unwrap_or_else(|e| {
11        eprintln!("init: {e}");
12        process::exit(1);
13    });
14
15    nwep::log::set_level(nwep::log::LogLevel::Warn);
16    nwep::log::set_stderr(true);
17
18    let parsed = Url::parse(&url).unwrap_or_else(|e| {
19        eprintln!("parse url: {e}");
20        process::exit(1);
21    });
22    let path = if parsed.path.is_empty() { "/".to_string() } else { parsed.path.clone() };
23
24    let keypair = Keypair::generate().unwrap_or_else(|e| {
25        eprintln!("keygen: {e}");
26        process::exit(1);
27    });
28
29    let client = ClientBuilder::new()
30        .connect(keypair, &url)
31        .unwrap_or_else(|e| {
32            eprintln!("connect: {e}");
33            process::exit(1);
34        });
35
36    let resp = client.get(&path).unwrap_or_else(|e| {
37        eprintln!("get: {e}");
38        process::exit(1);
39    });
40
41    println!("{}", resp.status);
42    println!("{}", String::from_utf8_lossy(&resp.body));
43
44    client.close();
45}

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, 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, 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.