use anyhow::{anyhow, Result};
pub struct ConsulKV {
client: reqwest::Client,
url: String,
}
impl ConsulKV {
pub fn new(url: &str) -> Self {
Self {
client: reqwest::Client::new(),
url: url.to_string(),
}
}
pub async fn get_string(&self, key: &str) -> Result<String> {
let url = format!("{}/v1/kv/{}?raw", self.url, key);
let resp = self.client.get(&url).send().await?;
if resp.status() != reqwest::StatusCode::OK {
return Err(anyhow!("{} returned {}", url, resp.status()));
}
match resp.text().await {
Ok(s) => Ok(s),
Err(e) => Err(anyhow!("{}", e)),
}
}
pub async fn put_string(&self, key: &str, value: String) -> Result<()> {
let url = format!("{}/v1/kv/{}", self.url, key);
let resp = self.client.put(&url).body(value).send().await?;
match resp.status() {
reqwest::StatusCode::OK => Ok(()),
s => Err(anyhow!("{} returned {}", url, s)),
}
}
}