diff options
author | Alex Auvolat <alex@adnab.me> | 2023-04-21 13:10:41 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2023-04-21 13:10:41 +0200 |
commit | 45e12c3bcd1198bdfce3a4f7f000ef4177067a5c (patch) | |
tree | 603808716ac2fb6b6b639acdcefe6e76fc48e3b6 /src/kv.rs | |
parent | a11ed03031cb59b412950d62960fc27cf919e2bf (diff) | |
download | df-consul-45e12c3bcd1198bdfce3a4f7f000ef4177067a5c.tar.gz df-consul-45e12c3bcd1198bdfce3a4f7f000ef4177067a5c.zip |
add kv_get_prefixv0.3.4
Diffstat (limited to 'src/kv.rs')
-rw-r--r-- | src/kv.rs | 38 |
1 files changed, 37 insertions, 1 deletions
@@ -1,10 +1,19 @@ +use std::collections::HashMap; + use anyhow::{anyhow, Result}; use bytes::Bytes; use log::*; use reqwest::StatusCode; use serde::{Deserialize, Serialize}; -use crate::Consul; +use crate::{Consul, WithIndex}; + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] +#[serde(rename_all = "PascalCase")] +pub struct KvGetPrefixEntry { + pub key: String, + pub value: String, +} impl Consul { pub async fn kv_get(&self, key: &str) -> Result<Option<Bytes>> { @@ -37,6 +46,33 @@ impl Consul { } } + pub async fn kv_get_prefix( + &self, + key_prefix: &str, + last_index: Option<usize>, + ) -> Result<WithIndex<HashMap<String, Bytes>>> { + debug!("kv_get_prefix {} index={:?}", key_prefix, last_index); + let results: WithIndex<Vec<KvGetPrefixEntry>> = self + .get_with_index( + format!( + "{}/v1/kv/{}{}?recurse", + self.url, self.kv_prefix, key_prefix + ), + last_index, + ) + .await?; + + let mut res = HashMap::new(); + for ent in results.value { + res.insert(ent.key, Bytes::from(base64::decode(&ent.value)?)); + } + + Ok(WithIndex { + value: res, + index: results.index, + }) + } + pub async fn kv_put(&self, key: &str, bytes: Bytes) -> Result<()> { debug!("kv_put {}", key); |