-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathendpoint.rs
More file actions
151 lines (137 loc) · 4.44 KB
/
endpoint.rs
File metadata and controls
151 lines (137 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use crate::{
parsed_url::ParsedUrl,
request::endpoint::{ApiUrlResolver, AsNamespace, WpNamespace},
wp_com::WpComBaseUrl,
};
use std::sync::Arc;
use strum::IntoEnumIterator;
pub mod extensions;
pub mod followers_endpoint;
pub mod jetpack_connection_endpoint;
pub mod languages_endpoint;
pub mod me_endpoint;
pub mod oauth2;
pub mod sites_endpoint;
pub mod stats_city_views_endpoint;
pub mod stats_clicks_endpoint;
pub mod stats_country_views_endpoint;
pub mod stats_devices_browser_endpoint;
pub mod stats_devices_platform_endpoint;
pub mod stats_devices_screensize_endpoint;
pub mod stats_file_downloads_endpoint;
pub mod stats_referrers_endpoint;
pub mod stats_region_views_endpoint;
pub mod stats_search_terms_endpoint;
pub mod stats_top_authors_endpoint;
pub mod stats_top_posts_endpoint;
pub mod stats_video_plays_endpoint;
pub mod stats_visits_endpoint;
pub mod subscribers_endpoint;
pub mod support_bots_endpoint;
pub mod support_eligibility_endpoint;
pub mod support_tickets_endpoint;
#[derive(uniffi::Object)]
pub struct WpComDotOrgApiUrlResolver {
pub base_url: ParsedUrl,
pub site_id: String,
}
#[uniffi::export]
impl WpComDotOrgApiUrlResolver {
#[uniffi::constructor]
pub fn new(site_id: String, base_url: WpComBaseUrl) -> Self {
Self {
base_url: base_url.parsed_url(),
site_id,
}
}
}
#[uniffi::export]
impl ApiUrlResolver for WpComDotOrgApiUrlResolver {
fn can_resolve(&self, namespace: String) -> bool {
WpNamespace::iter().any(|n| n.namespace_value() == namespace)
}
fn resolve(&self, namespace: String, endpoint_segments: Vec<String>) -> Arc<ParsedUrl> {
assert!(
self.can_resolve(namespace.clone()),
"`WpComDotOrgApiUrlResolver` doesn't support the namespace `{}`. The supported namespaces are: {:?}",
namespace,
WpNamespace::iter()
);
// The API root endpoint needs special handling for WordPress.com
if namespace == WpNamespace::None.namespace_value() && endpoint_segments.is_empty() {
let url_string = format!(
"https://public-api.wordpress.com/wp-json/?rest_route=/sites/{}/",
self.site_id
);
let parsed_url =
ParsedUrl::parse(&url_string).expect("WordPress.com API root URL is valid");
return Arc::new(parsed_url);
}
Arc::new(
self.base_url
.by_extending_and_splitting_by_forward_slash(
vec![namespace, "sites".to_string(), self.site_id.to_string()]
.into_iter()
.chain(endpoint_segments),
)
.into(),
)
}
}
#[derive(Debug)]
pub(crate) struct WpComApiClientInternalUrlResolver {
pub base_url: ParsedUrl,
}
impl WpComApiClientInternalUrlResolver {
fn new() -> Self {
Self {
base_url: WpComBaseUrl::Production.parsed_url(),
}
}
}
impl Default for WpComApiClientInternalUrlResolver {
fn default() -> Self {
Self::new()
}
}
impl ApiUrlResolver for WpComApiClientInternalUrlResolver {
fn can_resolve(&self, namespace: String) -> bool {
!WpNamespace::iter().any(|n| n.namespace_value() == namespace)
}
fn resolve(&self, namespace: String, endpoint_segments: Vec<String>) -> Arc<ParsedUrl> {
assert!(
self.can_resolve(namespace.clone()),
"`WpComApiClient` doesn't support the namespace `{namespace}`. Try using `WpApiClient` instead.",
);
Arc::new(
self.base_url
.by_extending_and_splitting_by_forward_slash(
vec![namespace].into_iter().chain(endpoint_segments),
)
.into(),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use rstest::rstest;
#[rstest]
#[case("/wp/v2", vec!["posts".to_string()], "https://public-api.wordpress.com/wp/v2/sites/example.wordpress.com/posts")]
fn wp_com_dot_org_api_url_resolver(
#[case] namespace: &str,
#[case] endpoint_segments: Vec<String>,
#[case] expected_url: &str,
) {
let resolver = WpComDotOrgApiUrlResolver::new(
"example.wordpress.com".to_string(),
WpComBaseUrl::Production,
);
assert_eq!(
resolver
.resolve(namespace.to_string(), endpoint_segments)
.url(),
expected_url
);
}
}