diff --git a/src/conf/meta.rs b/src/conf/meta.rs index 511a232..0f49f7d 100644 --- a/src/conf/meta.rs +++ b/src/conf/meta.rs @@ -132,7 +132,7 @@ impl Display for IpValue { #[cfg(feature = "host-ip")] IpValue::HostIp => { Self::get_all_addrs() - .get(0) + .first() .map(|s| Cow::Owned(s.to_string())) .unwrap_or(Cow::Borrowed("127.0.0.1")) } diff --git a/src/conf/mod.rs b/src/conf/mod.rs index 0277743..34bab72 100644 --- a/src/conf/mod.rs +++ b/src/conf/mod.rs @@ -263,10 +263,10 @@ impl ApolloConfClient { /// Ok(()) /// } /// ``` - pub fn watch<'a>( - &'a self, + pub fn watch( + &self, request: WatchRequest, - ) -> impl Stream>>> + 'a + ) -> impl Stream>>> + '_ { let mut watch_notifications = request.create_notifications(); let mut fetch_notifications = watch_notifications.clone(); diff --git a/src/conf/requests.rs b/src/conf/requests.rs index 5ffc59c..61970e0 100644 --- a/src/conf/requests.rs +++ b/src/conf/requests.rs @@ -42,6 +42,7 @@ impl Default for CachedFetchRequest { } } +#[cfg(feature = "conf")] impl PerformRequest for CachedFetchRequest { type Response = Properties; @@ -69,13 +70,14 @@ impl PerformRequest for CachedFetchRequest { Ok(pairs) } + #[cfg(all(feature = "auth", feature = "conf"))] fn app_id(&self) -> Option<&str> { Some(&self.app_id) } - #[cfg(feature = "auth")] + #[cfg(all(feature = "auth", feature = "conf"))] fn access_key(&self) -> Option<&str> { - self.access_key.as_ref().map(|key| key.as_str()) + self.access_key.as_deref() } } @@ -129,6 +131,7 @@ impl FetchRequest { } } +#[cfg(feature = "conf")] impl PerformRequest for FetchRequest { type Response = FetchResponse; @@ -159,13 +162,14 @@ impl PerformRequest for FetchRequest { Ok(pairs) } + #[cfg(all(feature = "auth", feature = "conf"))] fn app_id(&self) -> Option<&str> { Some(&self.app_id) } - #[cfg(feature = "auth")] + #[cfg(all(feature = "auth", feature = "conf"))] fn access_key(&self) -> Option<&str> { - self.access_key.as_ref().map(|key| key.as_str()) + self.access_key.as_deref() } } @@ -213,6 +217,7 @@ impl NotifyRequest { } } +#[cfg(feature = "conf")] impl PerformRequest for NotifyRequest { type Response = Vec; @@ -243,13 +248,14 @@ impl PerformRequest for NotifyRequest { request_builder.timeout(self.timeout) } + #[cfg(all(feature = "auth", feature = "conf"))] fn app_id(&self) -> Option<&str> { Some(&self.app_id) } - #[cfg(feature = "auth")] + #[cfg(all(feature = "auth", feature = "conf"))] fn access_key(&self) -> Option<&str> { - self.access_key.as_ref().map(|key| key.as_str()) + self.access_key.as_deref() } } diff --git a/src/errors.rs b/src/errors.rs index 416a183..894a97a 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,6 +1,8 @@ //! Crate level errors. +#[cfg(feature = "conf")] use http::StatusCode; +#[cfg(feature = "conf")] use reqwest::Response; use std::str::Utf8Error; @@ -27,6 +29,7 @@ pub enum ApolloClientError { #[error(transparent)] IniParse(#[from] ini::ParseError), + #[cfg(feature = "conf")] #[error(transparent)] ApolloResponse(#[from] ApolloResponseError), @@ -38,6 +41,7 @@ pub enum ApolloClientError { } /// Apollo api response error, when http status is not success. +#[cfg(feature = "conf")] #[derive(thiserror::Error, Debug)] #[error(r#"error occurred when apollo response, status: {status}, body: "{body}""#)] pub struct ApolloResponseError { @@ -47,6 +51,7 @@ pub struct ApolloResponseError { pub body: String, } +#[cfg(feature = "conf")] impl ApolloResponseError { pub(crate) async fn from_response(response: Response) -> Result { if response.status().is_success() { diff --git a/src/meta.rs b/src/meta.rs index 8e90b92..b40a999 100644 --- a/src/meta.rs +++ b/src/meta.rs @@ -1,11 +1,10 @@ //! Common api metadata. +#[cfg(feature = "conf")] use crate::errors::{ApolloClientResult, ApolloResponseError}; -use async_trait::async_trait; -use http::Method; +#[cfg(feature = "conf")] use reqwest::{RequestBuilder, Response}; -use std::{borrow::Cow, fmt, fmt::Display, time::Duration}; -use url::Url; +use std::{fmt, fmt::Display, time::Duration}; #[allow(dead_code)] pub(crate) const DEFAULT_CLUSTER_NAME: &str = "default"; @@ -57,6 +56,7 @@ impl Display for NamespaceKind { } /// Common api request trait. +#[cfg(feature = "conf")] pub(crate) trait PerformRequest { /// The returned response after request is success. type Response: PerformResponse; @@ -66,11 +66,13 @@ pub(crate) trait PerformRequest { /// Request method. fn method(&self) -> http::Method { - Method::GET + http::Method::GET } /// Url queries. - fn queries(&self) -> ApolloClientResult, Cow<'_, str>)>> { + fn queries( + &self, + ) -> ApolloClientResult, std::borrow::Cow<'_, str>)>> { Ok(vec![]) } @@ -87,6 +89,7 @@ pub(crate) trait PerformRequest { } /// AppId + #[cfg(all(feature = "auth", feature = "conf"))] fn app_id(&self) -> Option<&str> { None } @@ -139,13 +142,15 @@ pub(crate) trait PerformRequest { } /// Common api response trait. -#[async_trait] +#[cfg(feature = "conf")] +#[async_trait::async_trait] pub(crate) trait PerformResponse: Sized { /// Create Self from response. async fn from_response(response: Response) -> ApolloClientResult; } -#[async_trait] +#[cfg(feature = "conf")] +#[async_trait::async_trait] impl PerformResponse for () { async fn from_response(_response: Response) -> ApolloClientResult { Ok(()) @@ -153,7 +158,7 @@ impl PerformResponse for () { } #[cfg(feature = "conf")] -#[async_trait] +#[async_trait::async_trait] impl PerformResponse for ini::Properties { async fn from_response(response: Response) -> ApolloClientResult { let content = response.text().await?; @@ -165,8 +170,11 @@ impl PerformResponse for ini::Properties { } /// Create request url from base url, mainly path and queries. -#[allow(dead_code)] -pub(crate) fn handle_url(request: &impl PerformRequest, base_url: Url) -> ApolloClientResult { +#[cfg(feature = "conf")] +pub(crate) fn handle_url( + request: &impl PerformRequest, + base_url: url::Url, +) -> ApolloClientResult { let mut url = base_url; let path = &request.path(); let query = &request.queries()?; @@ -182,7 +190,7 @@ pub(crate) fn handle_url(request: &impl PerformRequest, base_url: Url) -> Apollo } /// Validate response is successful or not. -#[allow(dead_code)] +#[cfg(feature = "conf")] pub(crate) async fn validate_response(response: Response) -> ApolloClientResult { ApolloResponseError::from_response(response) .await @@ -190,7 +198,7 @@ pub(crate) async fn validate_response(response: Response) -> ApolloClientResult< } /// Implement PerformResponse for response struct which content type is `application/json`. -#[allow(unused_macros)] +#[cfg(feature = "conf")] macro_rules! implement_json_perform_response_for { ($t:ty) => { #[async_trait::async_trait] diff --git a/src/open/requests.rs b/src/open/requests.rs index 7a3aa5b..49f2d96 100644 --- a/src/open/requests.rs +++ b/src/open/requests.rs @@ -15,7 +15,7 @@ use http::Method; use reqwest::RequestBuilder; use std::borrow::Cow; -const OPEN_API_PREFIX: &'static str = "openapi/v1"; +const OPEN_API_PREFIX: &str = "openapi/v1"; /// Request executed by [crate::open::OpenApiClient::execute]; pub(crate) trait PerformOpenRequest: PerformRequest {} @@ -49,17 +49,11 @@ impl PerformRequest for OpenEnvClusterRequest { impl PerformOpenRequest for OpenEnvClusterRequest {} /// Fetch app infos. -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Default)] pub struct OpenAppRequest { pub app_ids: Option>, } -impl Default for OpenAppRequest { - fn default() -> Self { - OpenAppRequest { app_ids: None } - } -} - impl PerformRequest for OpenAppRequest { type Response = Vec; diff --git a/src/utils.rs b/src/utils.rs index b25b906..e711066 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -28,7 +28,7 @@ pub(crate) fn get_all_addrs() -> &'static [std::net::IpAddr] { .map(|networks| { networks .values() - .map(|network| { + .flat_map(|network| { network .addrs .iter() @@ -50,7 +50,6 @@ pub(crate) fn get_all_addrs() -> &'static [std::net::IpAddr] { _ => None, }) }) - .flatten() .collect() }) .unwrap_or_default() diff --git a/tests/common.rs b/tests/common.rs index d5eeccd..9b51f16 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -97,7 +97,7 @@ fn setup_mysql() { let mut cmd = Command::new("mysql"); let output = cmd - .args(&["-h", "127.0.0.1", "-u", "root"]) + .args(["-h", "127.0.0.1", "-u", "root"]) .stdin(sql_file) .output() .unwrap();