aboutsummaryrefslogtreecommitdiff
path: root/src/consul.rs
diff options
context:
space:
mode:
authorQuentin Dufour <quentin@deuxfleurs.fr>2020-05-22 10:33:09 +0200
committerQuentin Dufour <quentin@deuxfleurs.fr>2020-05-22 10:33:09 +0200
commitd583c17782977c3547da563a62e6f9f4fada15f3 (patch)
tree6887e6e681045dbd9dbfd41ecd3f699f073484ae /src/consul.rs
parent2a6b440270dc5d9b18061b69f8f700793d1ad0eb (diff)
downloaddiplonat-d583c17782977c3547da563a62e6f9f4fada15f3.tar.gz
diplonat-d583c17782977c3547da563a62e6f9f4fada15f3.zip
We are now able to watch
Diffstat (limited to 'src/consul.rs')
-rw-r--r--src/consul.rs24
1 files changed, 18 insertions, 6 deletions
diff --git a/src/consul.rs b/src/consul.rs
index e438605..b06f62d 100644
--- a/src/consul.rs
+++ b/src/consul.rs
@@ -1,6 +1,6 @@
use serde::{Serialize, Deserialize};
use std::collections::HashMap;
-use anyhow::Result;
+use anyhow::{Result, anyhow};
#[derive(Serialize, Deserialize, Debug)]
pub struct ServiceEntry {
@@ -14,20 +14,32 @@ pub struct CatalogNode {
pub struct Consul {
client: reqwest::Client,
- url: String
+ url: String,
+ idx: Option<u64>
}
impl Consul {
pub fn new(url: &str) -> Self {
return Self {
client: reqwest::Client::new(),
- url: url.to_string()
+ url: url.to_string(),
+ idx: None
};
}
- pub async fn catalog_node(&self, host: &str) -> Result<CatalogNode> {
- let url = format!("{}/v1/catalog/node/{}", self.url, host);
- let resp: CatalogNode = self.client.get(&url).send().await?.json().await?;
+ pub async fn watch_node(&mut self, host: &str) -> Result<CatalogNode> {
+ let url = match self.idx {
+ Some(i) => format!("{}/v1/catalog/node/{}?index={}", self.url, host, i),
+ None => format!("{}/v1/catalog/node/{}", self.url, host)
+ };
+
+ let http = self.client.get(&url).send().await?;
+ self.idx = match http.headers().get("X-Consul-Index") {
+ Some(v) => Some(v.to_str()?.parse::<u64>()?),
+ None => return Err(anyhow!("X-Consul-Index header not found"))
+ };
+
+ let resp: CatalogNode = http.json().await?;
return Ok(resp)
}
}