From fcd698e033f5399494ea5023f8e62644629c3871 Mon Sep 17 00:00:00 2001 From: Syfaro Date: Sat, 11 Feb 2023 00:01:45 -0500 Subject: [PATCH] Move client nats behind feature. --- .gitignore | 1 + bkapi-client/Cargo.toml | 21 ++++++----- bkapi-client/src/lib.rs | 78 +++------------------------------------- bkapi-client/src/nats.rs | 74 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 84 deletions(-) create mode 100644 bkapi-client/src/nats.rs diff --git a/.gitignore b/.gitignore index fedaa2b..08ca83d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target .env +.vscode/ diff --git a/bkapi-client/Cargo.toml b/bkapi-client/Cargo.toml index 109ee45..595b04c 100644 --- a/bkapi-client/Cargo.toml +++ b/bkapi-client/Cargo.toml @@ -5,20 +5,19 @@ authors = ["Syfaro "] edition = "2018" publish = false +[features] +nats = ["async-nats"] + [dependencies] +async-nats = { version = "0.27", optional = true } +futures = "0.3" +opentelemetry = "0.18" +opentelemetry-http = "0.7" +reqwest = { version = "0.11", features = ["json"] } +serde = { version = "1", features = ["derive"] } +serde_json = "1" tracing = "0.1" tracing-opentelemetry = "0.18" -opentelemetry = "0.18" -opentelemetry-http = "0.7" - -futures = "0.3" - -reqwest = { version = "0.11", features = ["json"] } -async-nats = "0.27" - -serde = { version = "1", features = ["derive"] } -serde_json = "1" - [dev-dependencies] tokio = { version = "1", features = ["full"] } diff --git a/bkapi-client/src/lib.rs b/bkapi-client/src/lib.rs index bc3f2fd..f0cb21a 100644 --- a/bkapi-client/src/lib.rs +++ b/bkapi-client/src/lib.rs @@ -7,6 +7,11 @@ use futures::TryStreamExt; use serde::{Deserialize, Serialize}; +#[cfg(feature = "nats")] +mod nats; +#[cfg(feature = "nats")] +pub use nats::{BKApiNatsClient, HashDistance}; + /// A search result, containing the searched information and all of the results. #[derive(Clone, Debug, Serialize, Deserialize)] pub struct SearchResults { @@ -110,79 +115,6 @@ impl InjectContext for reqwest::RequestBuilder { } } -/// The BKApi client, operating over NATS instead of HTTP. -#[derive(Clone)] -pub struct BKApiNatsClient { - client: async_nats::Client, -} - -/// A hash and distance. -#[derive(serde::Serialize, serde::Deserialize)] -pub struct HashDistance { - /// Hash to search. - pub hash: i64, - /// Maximum distance from hash to include in results. - pub distance: u32, -} - -impl BKApiNatsClient { - const NATS_SUBJECT: &str = "bkapi.search"; - - /// Create a new client with a given NATS client. - pub fn new(client: async_nats::Client) -> Self { - Self { client } - } - - /// Search for a single hash. - pub async fn search( - &self, - hash: i64, - distance: i32, - ) -> Result { - let hashes = [HashDistance { - hash, - distance: distance as u32, - }]; - - self.search_many(&hashes) - .await - .map(|mut results| results.remove(0)) - } - - /// Search many hashes at once. - pub async fn search_many( - &self, - hashes: &[HashDistance], - ) -> Result, async_nats::Error> { - let payload = serde_json::to_vec(hashes).unwrap(); - - let message = self - .client - .request(Self::NATS_SUBJECT.to_string(), payload.into()) - .await?; - - let results: Vec> = serde_json::from_slice(&message.payload).unwrap(); - - let results = results - .into_iter() - .zip(hashes) - .map(|(results, search)| SearchResults { - hash: search.hash, - distance: search.distance as u64, - hashes: results - .into_iter() - .map(|result| SearchResult { - hash: result.hash, - distance: result.distance as u64, - }) - .collect(), - }) - .collect(); - - Ok(results) - } -} - #[cfg(test)] mod tests { fn get_test_endpoint() -> String { diff --git a/bkapi-client/src/nats.rs b/bkapi-client/src/nats.rs new file mode 100644 index 0000000..affe75d --- /dev/null +++ b/bkapi-client/src/nats.rs @@ -0,0 +1,74 @@ +use crate::{SearchResult, SearchResults}; + +/// The BKApi client, operating over NATS instead of HTTP. +#[derive(Clone)] +pub struct BKApiNatsClient { + client: async_nats::Client, +} + +/// A hash and distance. +#[derive(serde::Serialize, serde::Deserialize)] +pub struct HashDistance { + /// Hash to search. + pub hash: i64, + /// Maximum distance from hash to include in results. + pub distance: u32, +} + +impl BKApiNatsClient { + const NATS_SUBJECT: &str = "bkapi.search"; + + /// Create a new client with a given NATS client. + pub fn new(client: async_nats::Client) -> Self { + Self { client } + } + + /// Search for a single hash. + pub async fn search( + &self, + hash: i64, + distance: i32, + ) -> Result { + let hashes = [HashDistance { + hash, + distance: distance as u32, + }]; + + self.search_many(&hashes) + .await + .map(|mut results| results.remove(0)) + } + + /// Search many hashes at once. + pub async fn search_many( + &self, + hashes: &[HashDistance], + ) -> Result, async_nats::Error> { + let payload = serde_json::to_vec(hashes).unwrap(); + + let message = self + .client + .request(Self::NATS_SUBJECT.to_string(), payload.into()) + .await?; + + let results: Vec> = serde_json::from_slice(&message.payload).unwrap(); + + let results = results + .into_iter() + .zip(hashes) + .map(|(results, search)| SearchResults { + hash: search.hash, + distance: search.distance as u64, + hashes: results + .into_iter() + .map(|result| SearchResult { + hash: result.hash, + distance: result.distance as u64, + }) + .collect(), + }) + .collect(); + + Ok(results) + } +}