Skip to content

Commit 66cd4e6

Browse files
authored
Allow passing WP.com base url (#752)
* Allow passing WP.com base url This is to support the app's UI tests, by allowing the app to swap the real WP.com public API URL with a mock server URL. * Add WpComBaseUrl type
1 parent a547ecd commit 66cd4e6

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

wp_api/src/wp_com/endpoint.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use crate::{
22
parsed_url::ParsedUrl,
33
request::endpoint::{ApiUrlResolver, AsNamespace, WpNamespace},
4+
wp_com::WpComBaseUrl,
45
};
56
use std::sync::Arc;
67
use strum::IntoEnumIterator;
7-
use url::Url;
88

99
pub mod jetpack_connection_endpoint;
1010
pub mod oauth2;
@@ -14,16 +14,16 @@ pub mod support_bots_endpoint;
1414
#[derive(uniffi::Object)]
1515
pub struct WpComDotOrgApiUrlResolver {
1616
pub base_url: ParsedUrl,
17-
pub site_url: String,
17+
pub site_id: String,
1818
}
1919

2020
#[uniffi::export]
2121
impl WpComDotOrgApiUrlResolver {
2222
#[uniffi::constructor]
23-
pub fn new(site_url: String) -> Self {
23+
pub fn new(site_id: String, base_url: WpComBaseUrl) -> Self {
2424
Self {
25-
base_url: wp_com_base_url(),
26-
site_url,
25+
base_url: base_url.parsed_url(),
26+
site_id,
2727
}
2828
}
2929
}
@@ -43,7 +43,7 @@ impl ApiUrlResolver for WpComDotOrgApiUrlResolver {
4343
Arc::new(
4444
self.base_url
4545
.by_extending_and_splitting_by_forward_slash(
46-
vec![namespace, "sites".to_string(), self.site_url.to_string()]
46+
vec![namespace, "sites".to_string(), self.site_id.to_string()]
4747
.into_iter()
4848
.chain(endpoint_segments),
4949
)
@@ -60,7 +60,7 @@ pub(crate) struct WpComApiClientInternalUrlResolver {
6060
impl WpComApiClientInternalUrlResolver {
6161
fn new() -> Self {
6262
Self {
63-
base_url: wp_com_base_url(),
63+
base_url: WpComBaseUrl::Production.parsed_url(),
6464
}
6565
}
6666
}
@@ -91,12 +91,6 @@ impl ApiUrlResolver for WpComApiClientInternalUrlResolver {
9191
}
9292
}
9393

94-
fn wp_com_base_url() -> ParsedUrl {
95-
Url::parse("https://public-api.wordpress.com")
96-
.expect("This is a valid URL")
97-
.into()
98-
}
99-
10094
#[cfg(test)]
10195
mod tests {
10296
use super::*;
@@ -109,7 +103,10 @@ mod tests {
109103
#[case] endpoint_segments: Vec<String>,
110104
#[case] expected_url: &str,
111105
) {
112-
let resolver = WpComDotOrgApiUrlResolver::new("example.wordpress.com".to_string());
106+
let resolver = WpComDotOrgApiUrlResolver::new(
107+
"example.wordpress.com".to_string(),
108+
WpComBaseUrl::Production,
109+
);
113110
assert_eq!(
114111
resolver
115112
.resolve(namespace.to_string(), endpoint_segments)

wp_api/src/wp_com/mod.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
use crate::prelude::*;
12
use crate::{impl_as_query_value_for_new_type, request::endpoint::AsNamespace};
23
use serde::{Deserialize, Serialize};
3-
use std::{num::ParseIntError, str::FromStr};
4+
use std::{num::ParseIntError, str::FromStr, sync::Arc};
45

56
pub mod client;
67
pub mod endpoint;
@@ -41,3 +42,21 @@ impl AsNamespace for WpComNamespace {
4142
}
4243
}
4344
}
45+
46+
#[derive(Debug, Default, PartialEq, Eq, uniffi::Enum)]
47+
pub enum WpComBaseUrl {
48+
#[default]
49+
Production,
50+
Custom(Arc<ParsedUrl>),
51+
}
52+
53+
impl WpComBaseUrl {
54+
pub fn parsed_url(&self) -> ParsedUrl {
55+
match self {
56+
WpComBaseUrl::Production => url::Url::parse("https://public-api.wordpress.com")
57+
.expect("This is a valid URL")
58+
.into(),
59+
WpComBaseUrl::Custom(url) => (**url).clone(),
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)