Files
nym/explorer-api/src/client.rs
T
Jon Häggblad 4587d5da26 Add nym- prefix to network-defaults (#3069)
* network-defaults: update cargo metadata

* Add nym- prefix to network-defaults crate

* Some manual updating

* rustfmt
2023-02-21 14:26:28 +01:00

43 lines
1.5 KiB
Rust

use nym_network_defaults::{
var_names::{NYM_API, NYXD},
NymNetworkDetails,
};
use reqwest::Url;
use std::{str::FromStr, sync::Arc};
use validator_client::nyxd::QueryNyxdClient;
// since this is just a query client, we don't need any locking mechanism to keep sequence numbers in check
// nor we need to access any of its methods taking mutable reference (like updating api URL)
// when that becomes a requirement, we would simply put an extra RwLock (or Mutex) in here
#[derive(Clone)]
pub(crate) struct ThreadsafeValidatorClient(
pub(crate) Arc<validator_client::Client<QueryNyxdClient>>,
);
impl ThreadsafeValidatorClient {
pub(crate) fn new() -> Self {
new_validator_client()
}
pub(crate) fn api_endpoint(&self) -> &Url {
self.0.nym_api.current_url()
}
}
pub(crate) fn new_validator_client() -> ThreadsafeValidatorClient {
let nyxd_url = Url::from_str(&std::env::var(NYXD).expect("nyxd validator not set"))
.expect("nyxd validator not in url format");
let api_url = Url::from_str(&std::env::var(NYM_API).expect("nyxd validator not set"))
.expect("nyxd validator not in url format");
let details = NymNetworkDetails::new_from_env();
let client_config = validator_client::Config::try_from_nym_network_details(&details)
.expect("failed to construct valid validator client config with the provided network")
.with_urls(nyxd_url, api_url);
ThreadsafeValidatorClient(Arc::new(
validator_client::Client::new_query(client_config).expect("Failed to connect to nyxd!"),
))
}