pub struct Client { /* private fields */ }Expand description
Implementations§
Source§impl Client
impl Client
Sourcepub fn node_id(&self) -> NodeId
pub fn node_id(&self) -> NodeId
node_id returns the local node’s identifier derived from the connecting keypair.
Sourcepub fn peer_identity(&self) -> Identity
pub fn peer_identity(&self) -> Identity
peer_identity returns the verified identity of the remote server as established
during the NWEP mutual-authentication handshake.
Sourcepub fn peer_node_id(&self) -> NodeId
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.
Sourcepub fn fetch(
&self,
method: &str,
path: &str,
body: &[u8],
) -> Result<Response, Error>
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.
Sourcepub fn fetch_with_headers(
&self,
method: &str,
path: &str,
body: &[u8],
headers: &[Header],
) -> Result<Response, Error>
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.
Sourcepub fn get(&self, path: &str) -> Result<Response, Error>
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?
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}Sourcepub fn post(&self, path: &str, body: &[u8]) -> Result<Response, Error>
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.
Sourcepub fn close(&self)
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?
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}