Skip to content

Commit

Permalink
Fix handle_url (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmjoy committed Jun 22, 2024
1 parent 0abc865 commit b9a1967
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 35 deletions.
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
12 changes: 6 additions & 6 deletions src/conf/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl PerformRequest for CachedFetchRequest {

fn path(&self) -> String {
format!(
"configfiles/{app_id}/{cluster_name}/{namespace_name}",
"/configfiles/{app_id}/{cluster_name}/{namespace_name}",
app_id = self.app_id,
cluster_name = self.cluster_name,
namespace_name = self.namespace_name
Expand Down Expand Up @@ -75,7 +75,7 @@ impl PerformRequest for CachedFetchRequest {

#[cfg(feature = "auth")]
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 @@ -134,7 +134,7 @@ impl PerformRequest for FetchRequest {

fn path(&self) -> String {
format!(
"configs/{app_id}/{cluster_name}/{namespace_name}",
"/configs/{app_id}/{cluster_name}/{namespace_name}",
app_id = self.app_id,
cluster_name = self.cluster_name,
namespace_name = self.namespace_name
Expand Down Expand Up @@ -165,7 +165,7 @@ impl PerformRequest for FetchRequest {

#[cfg(feature = "auth")]
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 @@ -217,7 +217,7 @@ impl PerformRequest for NotifyRequest {
type Response = Vec<Notification>;

fn path(&self) -> String {
"notifications/v2".to_string()
"/notifications/v2".to_string()
}

fn queries(&self) -> ApolloClientResult<Vec<(Cow<'_, str>, Cow<'_, str>)>> {
Expand Down Expand Up @@ -249,7 +249,7 @@ impl PerformRequest for NotifyRequest {

#[cfg(feature = "auth")]
fn access_key(&self) -> Option<&str> {
self.access_key.as_ref().map(|key| key.as_str())
self.access_key.as_deref()
}
}

Expand Down
39 changes: 24 additions & 15 deletions src/meta.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
//! Common api metadata.

use crate::errors::{ApolloClientResult, ApolloResponseError};
use async_trait::async_trait;
use http::Method;
use reqwest::{RequestBuilder, Response};
use std::{borrow::Cow, fmt, fmt::Display, time::Duration};
use url::Url;
use reqwest::Response;
use std::{fmt, fmt::Display, time::Duration};

#[allow(dead_code)]
pub(crate) const DEFAULT_CLUSTER_NAME: &str = "default";
Expand Down Expand Up @@ -57,6 +54,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,17 +64,22 @@ 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![])
}

/// Handle extras operator, such as set request body.
#[allow(unused_mut)]
fn request_builder(&self, mut request_builder: RequestBuilder) -> RequestBuilder {
fn request_builder(
&self,
mut request_builder: reqwest::RequestBuilder,
) -> reqwest::RequestBuilder {
//FIXME
//see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
#[cfg(all(feature = "auth", feature = "conf"))]
Expand All @@ -87,6 +90,7 @@ pub(crate) trait PerformRequest {
}

/// AppId
#[allow(dead_code)]
fn app_id(&self) -> Option<&str> {
None
}
Expand All @@ -103,7 +107,7 @@ pub(crate) trait PerformRequest {
///
/// https://www.apolloconfig.com/#/zh/usage/other-language-client-user-guide?id=_15-%e9%85%8d%e7%bd%ae%e8%ae%bf%e9%97%ae%e5%af%86%e9%92%a5
#[cfg(all(feature = "auth", feature = "conf"))]
fn signature(&self, mut request_builder: RequestBuilder) -> RequestBuilder {
fn signature(&self, mut request_builder: reqwest::RequestBuilder) -> reqwest::RequestBuilder {
use hmac::{Mac, SimpleHmac};
use sha1::Sha1;
type HmacWithSha1 = SimpleHmac<Sha1>;
Expand Down Expand Up @@ -139,21 +143,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,15 +171,18 @@ 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()?;

url.path_segments_mut()
.map_err(|_| crate::errors::ApolloClientError::UrlCannotBeABase)?
.extend(path.split('/'));
.extend(path.split('/').skip_while(|s| s.is_empty()));
if !query.is_empty() {
url.query_pairs_mut().extend_pairs(query);
}
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

0 comments on commit b9a1967

Please sign in to comment.