aleo_rust/api/mod.rs
1// Copyright (C) 2019-2023 Aleo Systems Inc.
2// This file is part of the Aleo SDK library.
3
4// The Aleo SDK library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU 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// The Aleo SDK library 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 General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Aleo SDK library. If not, see <https://www.gnu.org/licenses/>.
16
17//! API clients for interacting with Aleo Network endpoints
18
19use super::*;
20
21pub mod blocking;
22pub use blocking::*;
23
24/// Aleo API client for interacting with the Aleo Beacon API
25#[derive(Clone, Debug)]
26pub struct AleoAPIClient<N: Network> {
27 client: ureq::Agent,
28 base_url: String,
29 network_id: String,
30 _network: PhantomData<N>,
31}
32
33impl<N: Network> AleoAPIClient<N> {
34 pub fn new(base_url: &str, chain: &str) -> Result<Self> {
35 let client = ureq::Agent::new();
36 ensure!(
37 base_url.starts_with("http://") || base_url.starts_with("https://"),
38 "specified url {base_url} invalid, the base url must start with or https:// (or http:// if doing local development)"
39 );
40 Ok(AleoAPIClient {
41 client,
42 base_url: base_url.to_string(),
43 network_id: chain.to_string(),
44 _network: PhantomData,
45 })
46 }
47
48 pub fn testnet3() -> Self {
49 Self::new("https://api.explorer.aleo.org/v1", "testnet3").unwrap()
50 }
51
52 pub fn local_testnet3(port: &str) -> Self {
53 Self::new(&format!("http://0.0.0.0:{}", port), "testnet3").unwrap()
54 }
55
56 /// Get base URL
57 pub fn base_url(&self) -> &str {
58 &self.base_url
59 }
60
61 /// Get network ID being interacted with
62 pub fn network_id(&self) -> &str {
63 &self.network_id
64 }
65}