-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclient.rs
More file actions
258 lines (217 loc) · 8.27 KB
/
Copy pathclient.rs
File metadata and controls
258 lines (217 loc) · 8.27 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
use anyhow::{Context, Result};
use reqwest::Client;
use url::Url;
use crate::models::{Post, PostDetailsResponse, SearchResponse};
/// HTTP client for the Discourse search API.
///
/// Authenticates via API key and converts search results into
/// [`terraphim_types::Document`]s for indexing.
pub struct DiscourseClient {
client: Client,
base_url: Url,
api_key: String,
api_username: String,
}
impl DiscourseClient {
/// Creates a new `DiscourseClient` authenticated with the given API key and username.
pub fn new(base_url: &str, api_key: &str, api_username: &str) -> Result<Self> {
let base_url = Url::parse(base_url).context("Failed to parse base URL")?;
println!("Initializing Discourse client for URL: {}", base_url);
let client = Client::new();
Ok(Self {
client,
base_url,
api_key: api_key.to_string(),
api_username: api_username.to_string(),
})
}
async fn fetch_post_details(&self, post_id: u64) -> Result<String> {
let url = self.base_url.join(&format!("posts/{}.json", post_id))?;
println!("Fetching post details from: {}", url);
let response = self
.client
.get(url)
.header("Api-Key", &self.api_key)
.header("Api-Username", &self.api_username)
.send()
.await
.context("Failed to fetch post details")?;
if !response.status().is_success() {
let error_text = response.text().await?;
anyhow::bail!("Failed to fetch post details: {}", error_text);
}
let details: PostDetailsResponse = response
.json()
.await
.context("Failed to parse post details")?;
Ok(details.post.cooked)
}
pub async fn search_posts(&self, query: &str, limit: u32) -> Result<Vec<Post>> {
let mut url = self.base_url.join("search.json")?;
url.query_pairs_mut()
.append_pair("q", query)
.append_pair("limit", &limit.to_string());
println!("Making request to: {}", url);
println!("Headers:");
println!(" Api-Username: {}", self.api_username);
println!(" Api-Key: {}", "*".repeat(8));
let response = self
.client
.get(url)
.header("Api-Key", &self.api_key)
.header("Api-Username", &self.api_username)
.send()
.await
.context("Failed to send request")?;
let status = response.status();
println!("Response status: {}", status);
if !status.is_success() {
let error_text = response.text().await?;
println!("Error response: {}", error_text);
anyhow::bail!("API request failed with status {}: {}", status, error_text);
}
let response_text = response.text().await?;
println!("Response body: {}", response_text);
let search_response: SearchResponse =
serde_json::from_str(&response_text).context("Failed to parse response JSON")?;
println!("Found {} posts in response", search_response.posts.len());
let mut posts = Vec::new();
for post in search_response.posts {
// Find corresponding topic
if let Some(topic) = search_response
.topics
.iter()
.find(|t| t.id == post.topic_id)
{
let url = self
.base_url
.join(&format!(
"t/{}/{}/{}",
topic.slug, topic.id, post.post_number
))
.context("Failed to construct post URL")?;
// Fetch full post content
let body = self.fetch_post_details(post.id).await.ok();
posts.push(Post {
id: post.id,
title: topic.fancy_title.clone(),
url: url.to_string(),
excerpt: post.blurb,
body,
created_at: post.created_at,
username: post.username,
});
}
}
println!("Processed {} valid posts", posts.len());
Ok(posts)
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
use wiremock::{
Mock, MockServer, ResponseTemplate,
matchers::{header, method, path, query_param},
};
fn can_bind_localhost() -> bool {
std::net::TcpListener::bind("127.0.0.1:0").is_ok()
}
async fn start_mock_server() -> Option<MockServer> {
if !can_bind_localhost() {
eprintln!("Skipping wiremock test: cannot bind to localhost");
return None;
}
Some(MockServer::start().await)
}
#[tokio::test]
async fn test_search_posts() {
let Some(mock_server) = start_mock_server().await else {
return;
};
let mock_response = json!({
"posts": [{
"id": 1,
"blurb": "Test post content",
"topic_id": 100,
"post_number": 1,
"username": "test_user",
"created_at": "2024-01-01T00:00:00Z"
}],
"topics": [{
"id": 100,
"title": "Test Topic",
"slug": "test-topic",
"posts_count": 1,
"fancy_title": "Test Topic Title"
}]
});
Mock::given(method("GET"))
.and(path("/search.json"))
.and(query_param("q", "test"))
.and(query_param("limit", "10"))
.and(header("Api-Key", "test_api_key"))
.and(header("Api-Username", "test_user"))
.respond_with(ResponseTemplate::new(200).set_body_json(&mock_response))
.mount(&mock_server)
.await;
let post_details_response = json!({
"post": {
"cooked": "<p>Test post content</p>"
}
});
Mock::given(method("GET"))
.and(path("/posts/1.json"))
.and(header("Api-Key", "test_api_key"))
.and(header("Api-Username", "test_user"))
.respond_with(ResponseTemplate::new(200).set_body_json(&post_details_response))
.mount(&mock_server)
.await;
let client = DiscourseClient::new(&mock_server.uri(), "test_api_key", "test_user").unwrap();
let posts = client.search_posts("test", 10).await.unwrap();
assert_eq!(posts.len(), 1);
assert_eq!(posts[0].id, 1);
assert_eq!(posts[0].username, "test_user");
assert_eq!(posts[0].title, "Test Topic Title");
assert!(posts[0].body.is_some());
assert_eq!(posts[0].body.as_ref().unwrap(), "<p>Test post content</p>");
}
#[tokio::test]
async fn test_fetch_post_details() {
let Some(mock_server) = start_mock_server().await else {
return;
};
// Test successful post fetch
let post_response = json!({
"post": {
"cooked": "<p>Detailed post content</p>"
}
});
Mock::given(method("GET"))
.and(path("/posts/42.json"))
.and(header("Api-Key", "test_api_key"))
.and(header("Api-Username", "test_user"))
.respond_with(ResponseTemplate::new(200).set_body_json(&post_response))
.mount(&mock_server)
.await;
let client = DiscourseClient::new(&mock_server.uri(), "test_api_key", "test_user").unwrap();
let content = client.fetch_post_details(42).await.unwrap();
assert_eq!(content, "<p>Detailed post content</p>");
// Test error response
Mock::given(method("GET"))
.and(path("/posts/404.json"))
.and(header("Api-Key", "test_api_key"))
.and(header("Api-Username", "test_user"))
.respond_with(ResponseTemplate::new(404).set_body_string("Post not found"))
.mount(&mock_server)
.await;
let error = client.fetch_post_details(404).await.unwrap_err();
assert!(error.to_string().contains("Post not found"));
}
#[test]
fn test_invalid_url() {
let result = DiscourseClient::new("not a url", "key", "user");
assert!(result.is_err());
}
}