Skip to content

Commit c808e91

Browse files
authored
Merge branch 'main' into refact/hide-json-models
2 parents 01205ef + a6615ce commit c808e91

13 files changed

+455
-113
lines changed

.github/dependabot.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ updates:
44
directory: "/"
55
# Check for updates every Monday
66
schedule:
7-
interval: "weekly"
7+
interval: "weekly"

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
run: cargo build --verbose --workspace
2121
# Tests: we only run library and documentation tests (integration tests are using the remote server!)
2222
- name: Run tests (unittests)
23-
run: cargo test --lib --verbose --workspace
23+
run: cargo test --verbose --workspace
2424
- name: Run tests (documentation tests)
2525
run: cargo test --doc --verbose --workspace
2626

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ exclude = [
1818
"README.tpl",
1919
]
2020

21+
[features]
22+
http_client = []
23+
2124
[dependencies]
2225
reqwest = { version = "0.12.9", features = ["json", "blocking"] }
2326
serde = { version = "1.0.216", features = ["derive"] }
@@ -28,6 +31,7 @@ url = "2.5.4"
2831
thiserror = "2.0.7"
2932

3033
[dev-dependencies]
34+
appconfiguration = {path = ".", features = ["http_client"]}
3135
dotenvy = "0.15.7"
3236
rstest = "0.23.0"
3337

src/client/app_configuration_http.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use crate::client::feature_snapshot::FeatureSnapshot;
1818
pub use crate::client::property_proxy::PropertyProxy;
1919
use crate::client::property_snapshot::PropertySnapshot;
2020
use crate::errors::{ConfigurationAccessError, Error, Result};
21-
use crate::TokenProvider;
22-
use crate::{ServerClientImpl, ServiceAddress};
21+
use crate::network::{ServiceAddress, TokenProvider};
22+
use crate::ServerClientImpl;
2323
use std::net::TcpStream;
2424
use std::sync::{Arc, Mutex};
2525
use std::thread;

src/client/app_configuration_ibm_cloud.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use crate::client::feature_snapshot::FeatureSnapshot;
1717
pub use crate::client::property_proxy::PropertyProxy;
1818
use crate::client::property_snapshot::PropertySnapshot;
1919
use crate::errors::Result;
20+
use crate::network::ServiceAddress;
2021
use crate::IBMCloudTokenProvider;
21-
use crate::ServiceAddress;
2222

2323
use super::AppConfigurationClientHttp;
2424
use super::{AppConfigurationClient, ConfigurationId};
@@ -91,17 +91,18 @@ impl AppConfigurationClient for AppConfigurationClientIBMCloud {
9191
mod tests {
9292

9393
use super::*;
94+
use crate::network::http_client::ServiceAddressProtocol;
9495

9596
#[test]
9697
fn test_ibm_service_address() {
9798
let service_address = AppConfigurationClientIBMCloud::create_service_address("region");
9899

99100
assert_eq!(
100-
service_address.base_url(crate::ServiceAddressProtocol::Https),
101+
service_address.base_url(ServiceAddressProtocol::Http),
101102
"https://region.apprapp.cloud.ibm.com/apprapp"
102103
);
103104
assert_eq!(
104-
service_address.base_url(crate::ServiceAddressProtocol::Wss),
105+
service_address.base_url(ServiceAddressProtocol::Ws),
105106
"wss://region.apprapp.cloud.ibm.com/apprapp"
106107
);
107108
}

src/client/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ pub(crate) mod property_proxy;
2424
pub(crate) mod property_snapshot;
2525

2626
pub use app_configuration_client::{AppConfigurationClient, ConfigurationId};
27-
pub(crate) use app_configuration_http::AppConfigurationClientHttp;
27+
pub use app_configuration_http::AppConfigurationClientHttp;
2828
pub use app_configuration_ibm_cloud::AppConfigurationClientIBMCloud;
2929
pub use app_configuration_offline::AppConfigurationOffline;

src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,12 @@ pub use entity::Entity;
9999
pub use errors::{Error, Result};
100100
pub use feature::Feature;
101101
pub(crate) use network::{IBMCloudTokenProvider, ServerClientImpl};
102-
pub use network::{ServiceAddress, ServiceAddressProtocol, TokenProvider};
103102
pub use property::Property;
104103
pub use value::Value;
105104

105+
#[cfg(feature = "http_client")]
106+
pub use client::AppConfigurationClientHttp;
107+
#[cfg(feature = "http_client")]
108+
pub use network::{ServiceAddress, TokenProvider};
106109
#[cfg(test)]
107110
mod tests;

src/network/http_client.rs

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,25 @@ use tungstenite::connect;
2929
use url::Url;
3030

3131
pub enum ServiceAddressProtocol {
32-
Https,
33-
Wss,
32+
Http,
33+
Ws,
3434
}
3535

3636
impl std::fmt::Display for ServiceAddressProtocol {
3737
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3838
match self {
39-
ServiceAddressProtocol::Https => write!(f, "https://"),
40-
ServiceAddressProtocol::Wss => write!(f, "wss://"),
39+
ServiceAddressProtocol::Http => write!(f, "http"),
40+
ServiceAddressProtocol::Ws => write!(f, "ws"),
4141
}
4242
}
4343
}
4444

45-
#[derive(Debug)]
45+
#[derive(Debug, Clone)]
4646
pub struct ServiceAddress {
4747
host: String,
4848
port: Option<u16>,
4949
endpoint: Option<String>,
50+
use_ssl: bool,
5051
}
5152

5253
impl ServiceAddress {
@@ -55,10 +56,20 @@ impl ServiceAddress {
5556
host,
5657
port,
5758
endpoint,
59+
use_ssl: true,
60+
}
61+
}
62+
63+
pub fn new_without_ssl(host: String, port: Option<u16>, endpoint: Option<String>) -> Self {
64+
Self {
65+
host,
66+
port,
67+
endpoint,
68+
use_ssl: false,
5869
}
5970
}
6071

61-
pub fn base_url(&self, protocol: ServiceAddressProtocol) -> String {
72+
pub(crate) fn base_url(&self, protocol: ServiceAddressProtocol) -> String {
6273
let port = if let Some(port) = self.port {
6374
format!(":{port}")
6475
} else {
@@ -70,8 +81,8 @@ impl ServiceAddress {
7081
} else {
7182
"".to_string()
7283
};
73-
74-
format!("{protocol}{}{port}{endpoint}", self.host)
84+
let ssl_suffix = if self.use_ssl { "s" } else { "" };
85+
format!("{protocol}{ssl_suffix}://{}{port}{endpoint}", self.host)
7586
}
7687
}
7788

@@ -102,7 +113,7 @@ impl ServerClientImpl {
102113
pub fn get_configuration(&self, collection: &ConfigurationId) -> Result<ConfigurationJson> {
103114
let url = format!(
104115
"{}/feature/v1/instances/{}/config",
105-
self.service_address.base_url(ServiceAddressProtocol::Https),
116+
self.service_address.base_url(ServiceAddressProtocol::Http),
106117
collection.guid
107118
);
108119
let client = Client::new();
@@ -119,7 +130,9 @@ impl ServerClientImpl {
119130
.send();
120131

121132
match r {
122-
Ok(response) => response.json().map_err(Error::ReqwestError),
133+
Ok(response) => response.json().map_err(|_| {
134+
Error::ProtocolError("Failed to deserialize JSON from server response".to_string())
135+
}),
123136
Err(e) => {
124137
// TODO: Identify if token expired, get new one and retry
125138
if false {
@@ -137,7 +150,7 @@ impl ServerClientImpl {
137150
) -> Result<(WebSocket<MaybeTlsStream<TcpStream>>, Response)> {
138151
let ws_url = format!(
139152
"{}/wsfeature",
140-
self.service_address.base_url(ServiceAddressProtocol::Wss)
153+
self.service_address.base_url(ServiceAddressProtocol::Ws)
141154
);
142155
let mut ws_url = Url::parse(&ws_url)
143156
.map_err(|e| Error::Other(format!("Cannot parse '{}' as URL: {}", ws_url, e)))?;
@@ -171,3 +184,52 @@ impl ServerClientImpl {
171184
Ok(connect(request)?)
172185
}
173186
}
187+
188+
#[cfg(test)]
189+
mod tests {
190+
use super::*;
191+
192+
#[test]
193+
fn test_non_ssl_base_url() {
194+
let address = ServiceAddress::new_without_ssl(
195+
"ibm.com".to_string(),
196+
None,
197+
Some("endpoint".to_string()),
198+
);
199+
assert_eq!(
200+
address.base_url(ServiceAddressProtocol::Http),
201+
"http://ibm.com/endpoint"
202+
);
203+
assert_eq!(
204+
address.base_url(ServiceAddressProtocol::Ws),
205+
"ws://ibm.com/endpoint"
206+
);
207+
}
208+
209+
#[test]
210+
fn test_ssl_base_url() {
211+
let address =
212+
ServiceAddress::new("ibm.com".to_string(), None, Some("endpoint".to_string()));
213+
assert_eq!(
214+
address.base_url(ServiceAddressProtocol::Http),
215+
"https://ibm.com/endpoint"
216+
);
217+
assert_eq!(
218+
address.base_url(ServiceAddressProtocol::Ws),
219+
"wss://ibm.com/endpoint"
220+
);
221+
}
222+
223+
#[test]
224+
fn test_url_with_port() {
225+
let address = ServiceAddress::new_without_ssl("ibm.com".to_string(), Some(12345), None);
226+
assert_eq!(
227+
address.base_url(ServiceAddressProtocol::Http),
228+
"http://ibm.com:12345"
229+
);
230+
assert_eq!(
231+
address.base_url(ServiceAddressProtocol::Ws),
232+
"ws://ibm.com:12345"
233+
);
234+
}
235+
}

src/network/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
mod http_client;
15+
pub(crate) mod http_client;
1616
mod token_provider;
1717

1818
pub(crate) use http_client::ServerClientImpl;
19-
pub use http_client::{ServiceAddress, ServiceAddressProtocol};
19+
pub use http_client::ServiceAddress;
2020
pub(crate) use token_provider::IBMCloudTokenProvider;
2121
pub use token_provider::TokenProvider;

tests/test_app_config.rs

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
 (0)