Skip to content

Commit b9a1967

Browse files
authored
Fix handle_url (#16)
1 parent 0abc865 commit b9a1967

File tree

6 files changed

+37
-35
lines changed

6 files changed

+37
-35
lines changed

src/conf/meta.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl Display for IpValue {
132132
#[cfg(feature = "host-ip")]
133133
IpValue::HostIp => {
134134
Self::get_all_addrs()
135-
.get(0)
135+
.first()
136136
.map(|s| Cow::Owned(s.to_string()))
137137
.unwrap_or(Cow::Borrowed("127.0.0.1"))
138138
}

src/conf/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,10 @@ impl ApolloConfClient {
263263
/// Ok(())
264264
/// }
265265
/// ```
266-
pub fn watch<'a>(
267-
&'a self,
266+
pub fn watch(
267+
&self,
268268
request: WatchRequest,
269-
) -> impl Stream<Item = ApolloClientResult<HashMap<String, ApolloClientResult<FetchResponse>>>> + 'a
269+
) -> impl Stream<Item = ApolloClientResult<HashMap<String, ApolloClientResult<FetchResponse>>>> + '_
270270
{
271271
let mut watch_notifications = request.create_notifications();
272272
let mut fetch_notifications = watch_notifications.clone();

src/conf/requests.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl PerformRequest for CachedFetchRequest {
4747

4848
fn path(&self) -> String {
4949
format!(
50-
"configfiles/{app_id}/{cluster_name}/{namespace_name}",
50+
"/configfiles/{app_id}/{cluster_name}/{namespace_name}",
5151
app_id = self.app_id,
5252
cluster_name = self.cluster_name,
5353
namespace_name = self.namespace_name
@@ -75,7 +75,7 @@ impl PerformRequest for CachedFetchRequest {
7575

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

@@ -134,7 +134,7 @@ impl PerformRequest for FetchRequest {
134134

135135
fn path(&self) -> String {
136136
format!(
137-
"configs/{app_id}/{cluster_name}/{namespace_name}",
137+
"/configs/{app_id}/{cluster_name}/{namespace_name}",
138138
app_id = self.app_id,
139139
cluster_name = self.cluster_name,
140140
namespace_name = self.namespace_name
@@ -165,7 +165,7 @@ impl PerformRequest for FetchRequest {
165165

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

@@ -217,7 +217,7 @@ impl PerformRequest for NotifyRequest {
217217
type Response = Vec<Notification>;
218218

219219
fn path(&self) -> String {
220-
"notifications/v2".to_string()
220+
"/notifications/v2".to_string()
221221
}
222222

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

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

src/meta.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
//! Common api metadata.
22
33
use crate::errors::{ApolloClientResult, ApolloResponseError};
4-
use async_trait::async_trait;
5-
use http::Method;
6-
use reqwest::{RequestBuilder, Response};
7-
use std::{borrow::Cow, fmt, fmt::Display, time::Duration};
8-
use url::Url;
4+
use reqwest::Response;
5+
use std::{fmt, fmt::Display, time::Duration};
96

107
#[allow(dead_code)]
118
pub(crate) const DEFAULT_CLUSTER_NAME: &str = "default";
@@ -57,6 +54,7 @@ impl Display for NamespaceKind {
5754
}
5855

5956
/// Common api request trait.
57+
#[cfg(feature = "conf")]
6058
pub(crate) trait PerformRequest {
6159
/// The returned response after request is success.
6260
type Response: PerformResponse;
@@ -66,17 +64,22 @@ pub(crate) trait PerformRequest {
6664

6765
/// Request method.
6866
fn method(&self) -> http::Method {
69-
Method::GET
67+
http::Method::GET
7068
}
7169

7270
/// Url queries.
73-
fn queries(&self) -> ApolloClientResult<Vec<(Cow<'_, str>, Cow<'_, str>)>> {
71+
fn queries(
72+
&self,
73+
) -> ApolloClientResult<Vec<(std::borrow::Cow<'_, str>, std::borrow::Cow<'_, str>)>> {
7474
Ok(vec![])
7575
}
7676

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

8992
/// AppId
93+
#[allow(dead_code)]
9094
fn app_id(&self) -> Option<&str> {
9195
None
9296
}
@@ -103,7 +107,7 @@ pub(crate) trait PerformRequest {
103107
///
104108
/// 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
105109
#[cfg(all(feature = "auth", feature = "conf"))]
106-
fn signature(&self, mut request_builder: RequestBuilder) -> RequestBuilder {
110+
fn signature(&self, mut request_builder: reqwest::RequestBuilder) -> reqwest::RequestBuilder {
107111
use hmac::{Mac, SimpleHmac};
108112
use sha1::Sha1;
109113
type HmacWithSha1 = SimpleHmac<Sha1>;
@@ -139,21 +143,23 @@ pub(crate) trait PerformRequest {
139143
}
140144

141145
/// Common api response trait.
142-
#[async_trait]
146+
#[cfg(feature = "conf")]
147+
#[async_trait::async_trait]
143148
pub(crate) trait PerformResponse: Sized {
144149
/// Create Self from response.
145150
async fn from_response(response: Response) -> ApolloClientResult<Self>;
146151
}
147152

148-
#[async_trait]
153+
#[cfg(feature = "conf")]
154+
#[async_trait::async_trait]
149155
impl PerformResponse for () {
150156
async fn from_response(_response: Response) -> ApolloClientResult<Self> {
151157
Ok(())
152158
}
153159
}
154160

155161
#[cfg(feature = "conf")]
156-
#[async_trait]
162+
#[async_trait::async_trait]
157163
impl PerformResponse for ini::Properties {
158164
async fn from_response(response: Response) -> ApolloClientResult<Self> {
159165
let content = response.text().await?;
@@ -165,15 +171,18 @@ impl PerformResponse for ini::Properties {
165171
}
166172

167173
/// Create request url from base url, mainly path and queries.
168-
#[allow(dead_code)]
169-
pub(crate) fn handle_url(request: &impl PerformRequest, base_url: Url) -> ApolloClientResult<Url> {
174+
#[cfg(feature = "conf")]
175+
pub(crate) fn handle_url(
176+
request: &impl PerformRequest,
177+
base_url: url::Url,
178+
) -> ApolloClientResult<url::Url> {
170179
let mut url = base_url;
171180
let path = &request.path();
172181
let query = &request.queries()?;
173182

174183
url.path_segments_mut()
175184
.map_err(|_| crate::errors::ApolloClientError::UrlCannotBeABase)?
176-
.extend(path.split('/'));
185+
.extend(path.split('/').skip_while(|s| s.is_empty()));
177186
if !query.is_empty() {
178187
url.query_pairs_mut().extend_pairs(query);
179188
}

src/open/requests.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use http::Method;
1515
use reqwest::RequestBuilder;
1616
use std::borrow::Cow;
1717

18-
const OPEN_API_PREFIX: &'static str = "openapi/v1";
18+
const OPEN_API_PREFIX: &str = "openapi/v1";
1919

2020
/// Request executed by [crate::open::OpenApiClient::execute];
2121
pub(crate) trait PerformOpenRequest: PerformRequest {}
@@ -49,17 +49,11 @@ impl PerformRequest for OpenEnvClusterRequest {
4949
impl PerformOpenRequest for OpenEnvClusterRequest {}
5050

5151
/// Fetch app infos.
52-
#[derive(Clone, Debug)]
52+
#[derive(Clone, Debug, Default)]
5353
pub struct OpenAppRequest {
5454
pub app_ids: Option<Vec<String>>,
5555
}
5656

57-
impl Default for OpenAppRequest {
58-
fn default() -> Self {
59-
OpenAppRequest { app_ids: None }
60-
}
61-
}
62-
6357
impl PerformRequest for OpenAppRequest {
6458
type Response = Vec<OpenAppResponse>;
6559

src/utils.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub(crate) fn get_all_addrs() -> &'static [std::net::IpAddr] {
2828
.map(|networks| {
2929
networks
3030
.values()
31-
.map(|network| {
31+
.flat_map(|network| {
3232
network
3333
.addrs
3434
.iter()
@@ -50,7 +50,6 @@ pub(crate) fn get_all_addrs() -> &'static [std::net::IpAddr] {
5050
_ => None,
5151
})
5252
})
53-
.flatten()
5453
.collect()
5554
})
5655
.unwrap_or_default()

0 commit comments

Comments
 (0)