Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to fix CI error #17

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/conf/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Expand Down
6 changes: 3 additions & 3 deletions src/conf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,10 @@ impl ApolloConfClient {
/// Ok(())
/// }
/// ```
pub fn watch<'a>(
&'a self,
pub fn watch(
&self,
request: WatchRequest,
) -> impl Stream<Item = ApolloClientResult<HashMap<String, ApolloClientResult<FetchResponse>>>> + 'a
) -> impl Stream<Item = ApolloClientResult<HashMap<String, ApolloClientResult<FetchResponse>>>> + '_
{
let mut watch_notifications = request.create_notifications();
let mut fetch_notifications = watch_notifications.clone();
Expand Down
18 changes: 12 additions & 6 deletions src/conf/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl Default for CachedFetchRequest {
}
}

#[cfg(feature = "conf")]
impl PerformRequest for CachedFetchRequest {
type Response = Properties;

Expand Down Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -129,6 +131,7 @@ impl FetchRequest {
}
}

#[cfg(feature = "conf")]
impl PerformRequest for FetchRequest {
type Response = FetchResponse;

Expand Down Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -213,6 +217,7 @@ impl NotifyRequest {
}
}

#[cfg(feature = "conf")]
impl PerformRequest for NotifyRequest {
type Response = Vec<Notification>;

Expand Down Expand Up @@ -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()
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Crate level errors.

#[cfg(feature = "conf")]
use http::StatusCode;
#[cfg(feature = "conf")]
use reqwest::Response;
use std::str::Utf8Error;

Expand All @@ -27,6 +29,7 @@ pub enum ApolloClientError {
#[error(transparent)]
IniParse(#[from] ini::ParseError),

#[cfg(feature = "conf")]
#[error(transparent)]
ApolloResponse(#[from] ApolloResponseError),

Expand All @@ -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 {
Expand All @@ -47,6 +51,7 @@ pub struct ApolloResponseError {
pub body: String,
}

#[cfg(feature = "conf")]
impl ApolloResponseError {
pub(crate) async fn from_response(response: Response) -> Result<Response, Self> {
if response.status().is_success() {
Expand Down
34 changes: 21 additions & 13 deletions src/meta.rs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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;
Expand All @@ -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<Vec<(Cow<'_, str>, Cow<'_, str>)>> {
fn queries(
&self,
) -> ApolloClientResult<Vec<(std::borrow::Cow<'_, str>, std::borrow::Cow<'_, str>)>> {
Ok(vec![])
}

Expand All @@ -87,6 +89,7 @@ pub(crate) trait PerformRequest {
}

/// AppId
#[cfg(all(feature = "auth", feature = "conf"))]
fn app_id(&self) -> Option<&str> {
None
}
Expand Down Expand Up @@ -139,21 +142,23 @@ 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<Self>;
}

#[async_trait]
#[cfg(feature = "conf")]
#[async_trait::async_trait]
impl PerformResponse for () {
async fn from_response(_response: Response) -> ApolloClientResult<Self> {
Ok(())
}
}

#[cfg(feature = "conf")]
#[async_trait]
#[async_trait::async_trait]
impl PerformResponse for ini::Properties {
async fn from_response(response: Response) -> ApolloClientResult<Self> {
let content = response.text().await?;
Expand All @@ -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<Url> {
#[cfg(feature = "conf")]
pub(crate) fn handle_url(
request: &impl PerformRequest,
base_url: url::Url,
) -> ApolloClientResult<url::Url> {
let mut url = base_url;
let path = &request.path();
let query = &request.queries()?;
Expand All @@ -182,15 +190,15 @@ 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<Response> {
ApolloResponseError::from_response(response)
.await
.map_err(Into::into)
}

/// 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]
Expand Down
10 changes: 2 additions & 8 deletions src/open/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down Expand Up @@ -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<Vec<String>>,
}

impl Default for OpenAppRequest {
fn default() -> Self {
OpenAppRequest { app_ids: None }
}
}

impl PerformRequest for OpenAppRequest {
type Response = Vec<OpenAppResponse>;

Expand Down
3 changes: 1 addition & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -50,7 +50,6 @@ pub(crate) fn get_all_addrs() -> &'static [std::net::IpAddr] {
_ => None,
})
})
.flatten()
.collect()
})
.unwrap_or_default()
Expand Down
2 changes: 1 addition & 1 deletion tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading