|
10 | 10 | use alloc::string::String; |
11 | 11 | use alloc::vec::Vec; |
12 | 12 | use core::fmt; |
| 13 | +use core::time::Duration; |
| 14 | +#[cfg(not(target_arch = "wasm32"))] |
13 | 15 | use std::net::SocketAddr; |
14 | 16 |
|
15 | 17 | use reqwest::Client; |
@@ -54,6 +56,48 @@ impl From<reqwest::Error> for Error { |
54 | 56 | } |
55 | 57 | } |
56 | 58 |
|
| 59 | +/// NIP11 get options |
| 60 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 61 | +pub struct Nip11GetOptions { |
| 62 | + /// Proxy |
| 63 | + #[cfg(not(target_arch = "wasm32"))] |
| 64 | + pub proxy: Option<SocketAddr>, |
| 65 | + /// Timeout |
| 66 | + pub timeout: Duration, |
| 67 | +} |
| 68 | + |
| 69 | +impl Default for Nip11GetOptions { |
| 70 | + fn default() -> Self { |
| 71 | + Self { |
| 72 | + proxy: None, |
| 73 | + timeout: Duration::from_secs(60), |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +impl Nip11GetOptions { |
| 79 | + /// New default options |
| 80 | + #[inline] |
| 81 | + pub fn new() -> Self { |
| 82 | + Self::default() |
| 83 | + } |
| 84 | + |
| 85 | + /// Set proxy |
| 86 | + #[inline] |
| 87 | + #[cfg(not(target_arch = "wasm32"))] |
| 88 | + pub fn proxy(mut self, proxy: SocketAddr) -> Self { |
| 89 | + self.proxy = Some(proxy); |
| 90 | + self |
| 91 | + } |
| 92 | + |
| 93 | + /// Set timeout |
| 94 | + #[inline] |
| 95 | + pub fn timeout(mut self, timeout: Duration) -> Self { |
| 96 | + self.timeout = timeout; |
| 97 | + self |
| 98 | + } |
| 99 | +} |
| 100 | + |
57 | 101 | /// Relay information document |
58 | 102 | #[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] |
59 | 103 | pub struct RelayInformationDocument { |
@@ -181,27 +225,27 @@ pub struct FeeSchedule { |
181 | 225 | } |
182 | 226 |
|
183 | 227 | impl RelayInformationDocument { |
184 | | - /// Create new empty [`RelayInformationDocument`] |
| 228 | + /// Create a new empty [`RelayInformationDocument`]. |
185 | 229 | pub fn new() -> Self { |
186 | 230 | Self::default() |
187 | 231 | } |
188 | 232 |
|
189 | 233 | /// Get Relay Information Document |
190 | | - /// |
191 | | - /// **Proxy is ignored for WASM targets!** |
192 | | - pub async fn get(mut url: Url, _proxy: Option<SocketAddr>) -> Result<Self, Error> { |
| 234 | + pub async fn get(mut url: Url, opts: Nip11GetOptions) -> Result<Self, Error> { |
| 235 | + let mut builder = Client::builder(); |
| 236 | + |
| 237 | + // Set proxy |
193 | 238 | #[cfg(not(target_arch = "wasm32"))] |
194 | | - let client: Client = { |
195 | | - let mut builder = Client::builder(); |
196 | | - if let Some(proxy) = _proxy { |
197 | | - let proxy = format!("socks5h://{proxy}"); |
198 | | - builder = builder.proxy(Proxy::all(proxy)?); |
199 | | - } |
200 | | - builder.build()? |
201 | | - }; |
| 239 | + if let Some(proxy) = opts.proxy { |
| 240 | + let proxy = format!("socks5h://{proxy}"); |
| 241 | + builder = builder.proxy(Proxy::all(proxy)?); |
| 242 | + } |
| 243 | + |
| 244 | + // Set timeout |
| 245 | + builder = builder.timeout(opts.timeout); |
202 | 246 |
|
203 | | - #[cfg(target_arch = "wasm32")] |
204 | | - let client: Client = Client::new(); |
| 247 | + // Build client |
| 248 | + let client: Client = builder.build()?; |
205 | 249 |
|
206 | 250 | let url: &str = Self::with_http_scheme(&mut url)?; |
207 | 251 | let req = client.get(url).header("Accept", "application/nostr+json"); |
|
0 commit comments