ya_client/
net.rs

1use crate::error::Error;
2use crate::model::net::*;
3use crate::web::{WebClient, WebInterface};
4use actix_codec::Framed;
5use awc::http::Method;
6use awc::ws::Codec;
7use awc::BoxedSocket;
8use std::ops::Not;
9
10pub const NET_URL_ENV_VAR: &str = "YAGNA_NET_URL";
11
12pub type Result<T> = std::result::Result<T, Error>;
13
14/// Bindings for Requestor part of the Net API.
15#[derive(Clone)]
16pub struct NetApi {
17    client: WebClient,
18}
19
20impl WebInterface for NetApi {
21    const API_URL_ENV_VAR: &'static str = "YAGNA_NET_URL";
22    const API_SUFFIX: &'static str = ya_client_model::net::NET_API_V2_NET_PATH;
23
24    fn from_client(client: WebClient) -> Self {
25        NetApi { client }
26    }
27}
28
29impl NetApi {
30    /// Retrieves connection status.
31    pub async fn get_status(&self) -> Result<Status> {
32        self.client.get("status").send().json().await
33    }
34}
35
36/// Bindings for Requestor part of the Net VPN API.
37#[deprecated(since = "0.7.0", note = "Please use `NetVpnApi` instead")]
38pub type NetRequestorApi = NetVpnApi;
39
40/// Bindings for Requestor part of the Net VPN API.
41#[derive(Clone)]
42pub struct NetVpnApi {
43    client: WebClient,
44}
45
46impl WebInterface for NetVpnApi {
47    const API_URL_ENV_VAR: &'static str = "YAGNA_NET_URL";
48    const API_SUFFIX: &'static str = ya_client_model::net::NET_API_V2_VPN_PATH;
49
50    fn from_client(client: WebClient) -> Self {
51        NetVpnApi { client }
52    }
53}
54
55impl NetVpnApi {
56    /// Retrieves requestor's virtual private networks.
57    pub async fn get_networks(&self) -> Result<Vec<Network>> {
58        self.client.get("net").send().json().await
59    }
60
61    /// Registers a new virtual private network overlay on the network.
62    pub async fn create_network(&self, network: &NewNetwork) -> Result<Network> {
63        self.client.post("net").send_json(&network).json().await
64    }
65
66    /// Retrieves a requestor's virtual private network.
67    pub async fn get_network(&self, network_id: &str) -> Result<Network> {
68        let url = url_format!("net/{network_id}");
69        self.client.get(&url).send().json().await
70    }
71
72    /// Unregisters an existing virtual private network overlay on the network.
73    pub async fn remove_network(&self, network_id: &str) -> Result<()> {
74        let url = url_format!("net/{network_id}");
75        self.client.delete(&url).send().json().await
76    }
77
78    /// Retrieves requestor's addresses in a virtual private network.
79    pub async fn get_addresses(&self, network_id: &str) -> Result<Vec<Address>> {
80        let url = url_format!("net/{network_id}/addresses");
81        self.client.get(&url).send().json().await
82    }
83
84    /// Assigns a new address of the requestor in an existing private network.
85    pub async fn add_address(&self, network_id: &str, address: &Address) -> Result<()> {
86        let url = url_format!("net/{network_id}/addresses");
87        self.client.post(&url).send_json(&address).json().await
88    }
89
90    /// Retrieves nodes within a virtual private network.
91    pub async fn get_nodes(&self, network_id: &str) -> Result<Vec<Node>> {
92        let url = url_format!("net/{network_id}/nodes");
93        self.client.get(&url).send().json().await
94    }
95
96    /// Registers a node in a virtual private network.
97    pub async fn add_node(&self, network_id: &str, node: &Node) -> Result<()> {
98        let url = url_format!("net/{network_id}/nodes");
99        self.client.post(&url).send_json(&node).json().await
100    }
101
102    /// Unregisters an existing node in a virtual private network.
103    pub async fn remove_node(&self, network_id: &str, node_id: &str) -> Result<()> {
104        let url = url_format!("net/{network_id}/nodes/{node_id}");
105        self.client.post(&url).send().json().await
106    }
107
108    /// Lists TCP connections
109    pub async fn list_tcp(&self, network_id: &str) -> Result<Vec<Connection>> {
110        let url = url_format!("net/{network_id}/tcp");
111        self.client.get(&url).send().json().await
112    }
113
114    /// Creates a new TCP connection
115    pub async fn connect_tcp(
116        &self,
117        network_id: &str,
118        ip: &str,
119        port: u16,
120    ) -> Result<Framed<BoxedSocket, Codec>> {
121        let url = url_format!("net/{network_id}/tcp/{ip}/{port}");
122        let (mut res, conn) = self.client.ws(&url).await?;
123
124        let status = res.status();
125        if status.is_success().not() && status.is_informational().not() {
126            let body = res.body().limit(16384).await?;
127            return Err(Error::HttpError {
128                code: status,
129                msg: String::from_utf8(body.to_vec())?,
130                method: Method::GET,
131                url,
132            });
133        }
134
135        Ok(conn)
136    }
137}