kitsune2_bootstrap_client/
lib.rs

1//! A client for the Kitsune2 bootstrap server.
2
3#![deny(missing_docs)]
4
5use base64::Engine;
6use kitsune2_api::{AgentInfoSigned, DynVerifier, K2Error, K2Result, SpaceId};
7use std::sync::Arc;
8use url::Url;
9
10/// Send the agent info, for the given space, to the bootstrap server.
11///
12/// Note the `blocking_` prefix. This is a hint to the caller that if the function is used in
13/// an async context, it should be treated as a blocking operation.
14pub fn blocking_put(
15    mut server_url: Url,
16    agent_info: &AgentInfoSigned,
17) -> K2Result<()> {
18    server_url.set_path(&format!(
19        "bootstrap/{}/{}",
20        base64::prelude::BASE64_URL_SAFE_NO_PAD.encode(&**agent_info.space),
21        base64::prelude::BASE64_URL_SAFE_NO_PAD.encode(&**agent_info.agent),
22    ));
23
24    let encoded = agent_info.encode()?;
25    ureq::put(server_url.as_str())
26        .send_string(&encoded)
27        .map_err(|e| K2Error::other_src("Failed to put agent info", e))?;
28
29    Ok(())
30}
31
32/// Get all agent infos from the bootstrap server for the given space.
33///
34/// Note the `blocking_` prefix. This is a hint to the caller that if the function is used in
35/// an async context, it should be treated as a blocking operation.
36pub fn blocking_get(
37    mut server_url: Url,
38    space: SpaceId,
39    verifier: DynVerifier,
40) -> K2Result<Vec<Arc<AgentInfoSigned>>> {
41    server_url.set_path(&format!(
42        "bootstrap/{}",
43        base64::prelude::BASE64_URL_SAFE_NO_PAD.encode(&**space)
44    ));
45
46    let encoded = ureq::get(server_url.as_str())
47        .call()
48        .map_err(K2Error::other)?
49        .into_string()
50        .map_err(K2Error::other)?;
51
52    Ok(AgentInfoSigned::decode_list(&verifier, encoded.as_bytes())?
53        .into_iter()
54        .filter_map(|l| {
55            l.inspect_err(|err| {
56                tracing::debug!(?err, "failure decoding bootstrap agent info");
57            })
58            .ok()
59        })
60        .collect::<Vec<_>>())
61}