diff --git a/src/lib.rs b/src/lib.rs index 426e3cb..f50fc65 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,7 @@ use std::{marker::PhantomData, net::IpAddr}; use futures_core::Stream; use futures_util::StreamExt; use mdns_sd::{ServiceDaemon, ServiceEvent, ServiceInfo}; +use reqwest::Client; use tracing::debug; use wot_td::{ @@ -41,6 +42,7 @@ pub struct Discoverer { mdns: ServiceDaemon, service_type: String, _other: PhantomData, + http_client: reqwest::Client, } /// Discovered Thing and its mDNS information @@ -80,7 +82,23 @@ impl Discovered { } } +impl Discoverer { + /// Creates a new Discoverer + pub fn new() -> Result { + let mdns = ServiceDaemon::new()?; + let service_type = "_wot._tcp.local.".to_owned(); + let http_client = Client::builder().build()?; + Ok(Self { + mdns, + service_type, + http_client, + _other: PhantomData, + }) + } +} + async fn get_thing( + client: &Client, info: ServiceInfo, ) -> Result> { let host = info.get_addresses().iter().next().ok_or(Error::NoAddress)?; @@ -99,7 +117,10 @@ async fn get_thing( debug!("Got {proto} {host} {port} {path}"); - let r = reqwest::get(format!("{proto}://{host}:{port}{path}")).await?; + let r = client + .get(format!("{proto}://{host}:{port}{path}")) + .send() + .await?; let thing = r.json().await?; let scheme = proto.to_owned(); @@ -112,19 +133,6 @@ async fn get_thing( Ok(d) } -impl Discoverer { - /// Creates a new Discoverer - pub fn new() -> Result { - let mdns = ServiceDaemon::new()?; - let service_type = "_wot._tcp.local.".to_owned(); - Ok(Self { - mdns, - service_type, - _other: PhantomData, - }) - } -} - impl Discoverer { /// Extend the [Discoverer] with a [ExtendableThing] pub fn ext(self) -> Discoverer @@ -135,12 +143,14 @@ impl Discoverer { let Discoverer { mdns, service_type, + http_client, _other, } = self; Discoverer { mdns, service_type, + http_client, _other: PhantomData, } } @@ -149,13 +159,18 @@ impl Discoverer { pub fn stream(&self) -> Result>>> { let receiver = self.mdns.browse(&self.service_type)?; - let s = receiver.into_stream().filter_map(|v| async move { - tracing::info!("{:?}", v); - if let ServiceEvent::ServiceResolved(info) = v { - let t = get_thing(info).await; - Some(t) - } else { - None + let http_client = self.http_client.clone(); + + let s = receiver.into_stream().filter_map(move |v| { + let client = http_client.clone(); + async move { + tracing::info!("{:?}", v); + if let ServiceEvent::ServiceResolved(info) = v { + let t = get_thing(&client, info).await; + Some(t) + } else { + None + } } });