dweb/api/
mod.rs

1/*
2 Copyright (c) 2025 Mark Hughes
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Affero General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Affero General Public License for more details.
13
14 You should have received a copy of the GNU Affero General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>.
16*/
17
18use color_eyre::eyre::{eyre, Error, Result};
19
20///! A Rust interface to dweb server APIs
21///!
22///! TODO keep this and the with ports APIs in sync
23use crate::history::HistoryAddress;
24use crate::web::name::RecognisedName;
25use crate::web::request::main_server_request;
26
27/// The dweb::api is a native Rust API that handles http interaction with the dweb server.
28///
29/// The server can be accessed directly from any language, but this API simplifies the process
30/// for Rust apps.
31
32// IMPLEMENTATION: keep these wrappers slim and let the server
33// do parameter checks, and return the raw results immediately
34// for checking by the caller.
35
36// TODO break this out into modules here and in the with ports server
37
38pub const DWEB_API_ROUTE_V0: &str = "/dweb-0"; // Route for dweb API v0
39pub const DWEB_ANT_API_ROUTE_V0: &str = "/dweb-0"; // Route for dweb enhanced Autonommi API v0
40
41pub const ANT_API_ROUTE_V0: &str = "/ant-0"; // Route route for raw Autonomi API (dweb v0)
42
43pub const DWEB_API_ROUTE: &str = DWEB_API_ROUTE_V0;
44pub const DWEB_ANT_API_ROUTE: &str = DWEB_ANT_API_ROUTE_V0;
45pub const ANT_API_ROUTE: &str = ANT_API_ROUTE_V0;
46
47/// Register a name with the main server
48pub async fn name_register(
49    dweb_name: &str,
50    history_address: HistoryAddress,
51    host: Option<&String>,
52    port: Option<u16>,
53) -> Result<()> {
54    let url_path = format!(
55        "{DWEB_API_ROUTE}/name-register/{dweb_name}/{}",
56        history_address.to_hex()
57    );
58
59    match main_server_request(host, port, &url_path).await {
60        Ok(_json_value) => Ok(()),
61        Err(e) => Err(eyre!(Into::<Error>::into(e))),
62    }
63}
64
65/// Query the server for a list of recognised names
66pub async fn name_list(host: Option<&String>, port: Option<u16>) -> Result<Vec<RecognisedName>> {
67    let url_path = format!("{DWEB_API_ROUTE}/name-list");
68    match main_server_request(host, port, &url_path).await {
69        Ok(json) => {
70            let vec: Vec<RecognisedName> = serde_json::from_str(&json)?;
71            Ok(vec)
72        }
73        Err(e) => Err(eyre!(e)),
74    }
75}