aboutsummaryrefslogtreecommitdiff
path: root/src/consul_kv.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/consul_kv.rs')
-rw-r--r--src/consul_kv.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/consul_kv.rs b/src/consul_kv.rs
new file mode 100644
index 0000000..5b3d0ef
--- /dev/null
+++ b/src/consul_kv.rs
@@ -0,0 +1,41 @@
+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)),
+ }
+ }
+}