Files
nym/common/client-libs/validator-client/src/validator_api/mod.rs
T
Bogdan-Ștefan Neacşu 973c30592f Use cached topology for clients (#674)
* Add validator-api common client

* Call validator-api from different clients for gateway topology

* Call validator-api from different clients for mixnode topology

* Use consts for the validator-api queries

* Rename the new query_validator function to query_validator_api

* Add mut to mixnode validator client

* Add refreshValidatorAPIGateways as a way to get the gateways...

... from validator-api

* Add refreshValidatorAPIGateways as a way to get the mixnodes...

... from validator-api

* Add yet another mut

* Change the port to validator-api service when querying the topology

* Add parsing check on the config phase...

... to make sure the validator URLs are in the correct format.

* Fix another clippy error

* Use all provided validators instead of just the first one

* The mutable reference was not actually needed, so remove it

* Use global variable for validator-api port

* Use url crate for checking the format and changing the port

* Use URL for parsing and move constants of validator-api to index.ts...

... until we find a way to link to the values from the validator-api
crate.

* Change global variables naming and have the API version into each API query

* Revert the changes to the index on connect...

... as they were working correctly before.

* Use all provided validators for mixnodes as well

* Remove location and layer
2021-07-09 11:50:41 +01:00

45 lines
1.1 KiB
Rust

pub mod error;
use crate::validator_api::error::ValidatorAPIClientError;
use serde::Deserialize;
use url::Url;
// TODO: This should be linked to the validator-api as well
pub(crate) const VALIDATOR_API_PORT: u16 = 8080;
pub(crate) const VALIDATOR_API_MIXNODES: &str = "v1/mixnodes";
pub(crate) const VALIDATOR_API_GATEWAYS: &str = "v1/gateways";
pub struct Client {
reqwest_client: reqwest::Client,
}
impl Client {
pub fn new() -> Self {
let reqwest_client = reqwest::Client::new();
Self { reqwest_client }
}
pub async fn query_validator_api<T>(
&self,
query: String,
validator_url: &Url,
) -> Result<T, ValidatorAPIClientError>
where
for<'a> T: Deserialize<'a>,
{
let mut validator_api_url = validator_url.clone();
validator_api_url
.set_port(Some(VALIDATOR_API_PORT))
.unwrap();
let query_url = format!("{}/{}", validator_api_url.as_str(), query);
Ok(self
.reqwest_client
.get(query_url)
.send()
.await?
.json()
.await?)
}
}