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/with_index.rs | |
parent | a11ed03031cb59b412950d62960fc27cf919e2bf (diff) | |
download | df-consul-45e12c3bcd1198bdfce3a4f7f000ef4177067a5c.tar.gz df-consul-45e12c3bcd1198bdfce3a4f7f000ef4177067a5c.zip |
add kv_get_prefixv0.3.4
Diffstat (limited to 'src/with_index.rs')
-rw-r--r-- | src/with_index.rs | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/src/with_index.rs b/src/with_index.rs index adce169..ba94779 100644 --- a/src/with_index.rs +++ b/src/with_index.rs @@ -1,14 +1,39 @@ -use std::fmt::{Debug, Display}; +use std::fmt::{Debug, Display, Write}; use anyhow::{bail, Result}; +use log::*; use reqwest::Response; +use serde::Deserialize; + +use crate::Consul; + +impl Consul { + pub(crate) async fn get_with_index<T: for<'de> Deserialize<'de>>( + &self, + mut url: String, + last_index: Option<usize>, + ) -> Result<WithIndex<T>> { + if let Some(i) = last_index { + if url.contains('?') { + write!(&mut url, "&index={}", i).unwrap(); + } else { + write!(&mut url, "?index={}", i).unwrap(); + } + } + debug!("GET {} as {}", url, std::any::type_name::<T>()); + + let http = self.client.get(&url).send().await?; + + Ok(WithIndex::<T>::index_from(&http)?.value(http.json().await?)) + } +} /// Wraps the returned value of an [API call with blocking /// possibility](https://developer.hashicorp.com/consul/api-docs/features/blocking) with the /// returned Consul index pub struct WithIndex<T> { - value: T, - index: usize, + pub(crate) value: T, + pub(crate) index: usize, } impl<T> WithIndex<T> { |