webshift 0.2.0

Denoised web search library — fetch, clean, and rerank web content for AI agents.
Documentation
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! Generic configurable HTTP backend.
//!
//! Allows wiring up any REST search API through TOML configuration alone,
//! without writing Rust code. Covers the common case of a JSON endpoint that
//! accepts a query string and returns an array of results.
//!
//! Example `webshift.toml`:
//!
//! ```toml
//! [backends.http]
//! url          = "https://my-search.example.com/api/search"
//! method       = "GET"
//! query_param  = "q"
//! count_param  = "limit"
//! results_path = "data.items"     # dot-separated JSON path to results array
//! title_field  = "title"
//! url_field    = "link"
//! snippet_field = "description"
//!
//! [backends.http.headers]
//! "Authorization" = "Bearer my-secret-token"
//! ```

use std::collections::HashMap;

use super::{SearchBackend, SearchResult};
use crate::config::HttpBackendConfig;

#[derive(Debug)]
pub struct HttpBackend {
    config: HttpBackendConfig,
    client: reqwest::Client,
}

impl HttpBackend {
    pub fn new(config: &HttpBackendConfig) -> Result<Self, crate::WebshiftError> {
        if config.url.is_empty() {
            return Err(crate::WebshiftError::Backend(
                "Generic HTTP backend requires backends.http.url to be set".into(),
            ));
        }
        Ok(Self {
            config: config.clone(),
            client: reqwest::Client::builder()
                .timeout(std::time::Duration::from_secs(10))
                .build()
                .expect("failed to build HTTP client"),
        })
    }
}

/// Walk a dot-separated path through a JSON value.
/// `"data.items"` → `value["data"]["items"]`
fn json_path<'a>(mut val: &'a serde_json::Value, path: &str) -> Option<&'a serde_json::Value> {
    for key in path.split('.') {
        val = val.get(key)?;
    }
    Some(val)
}

fn jstr<'a>(val: &'a serde_json::Value, key: &str) -> &'a str {
    val.get(key).and_then(serde_json::Value::as_str).unwrap_or("")
}

#[async_trait::async_trait]
impl SearchBackend for HttpBackend {
    async fn search(
        &self,
        query: &str,
        num_results: usize,
        lang: Option<&str>,
    ) -> Result<Vec<SearchResult>, crate::WebshiftError> {
        let cfg = &self.config;

        // Build query params
        let mut params: Vec<(String, String)> = vec![
            (cfg.query_param.clone(), query.to_string()),
        ];
        if !cfg.count_param.is_empty() {
            params.push((cfg.count_param.clone(), num_results.to_string()));
        }
        if let Some(lang) = lang && !cfg.lang_param.is_empty() {
            params.push((cfg.lang_param.clone(), lang.to_string()));
        }
        // Merge extra static params from config
        for (k, v) in &cfg.extra_params {
            params.push((k.clone(), v.clone()));
        }

        let mut req = if cfg.method.eq_ignore_ascii_case("POST") {
            // For POST, send params as JSON body
            let body: HashMap<&str, &str> =
                params.iter().map(|(k, v)| (k.as_str(), v.as_str())).collect();
            self.client.post(&cfg.url).json(&body)
        } else {
            self.client.get(&cfg.url).query(&params)
        };

        // Attach headers
        for (k, v) in &cfg.headers {
            req = req.header(k.as_str(), v.as_str());
        }

        let resp = req
            .send()
            .await
            .map_err(|e| crate::WebshiftError::Backend(format!("http backend request failed: {e}")))?;

        let status = resp.status();
        if !status.is_success() {
            return Err(crate::WebshiftError::Backend(format!(
                "http backend HTTP {status}"
            )));
        }

        let data: serde_json::Value = resp
            .json()
            .await
            .map_err(|e| crate::WebshiftError::Backend(format!("http backend parse error: {e}")))?;

        let empty = vec![];
        let items = if cfg.results_path.is_empty() {
            data.as_array().unwrap_or(&empty)
        } else {
            json_path(&data, &cfg.results_path)
                .and_then(serde_json::Value::as_array)
                .unwrap_or(&empty)
        };

        let mut results = Vec::new();
        for item in items {
            if results.len() >= num_results {
                break;
            }
            results.push(SearchResult {
                title: jstr(item, &cfg.title_field).to_string(),
                url: jstr(item, &cfg.url_field).to_string(),
                snippet: jstr(item, &cfg.snippet_field).to_string(),
            });
        }

        Ok(results)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use wiremock::matchers::{method, path, query_param};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    fn base_config(url: &str) -> HttpBackendConfig {
        HttpBackendConfig {
            url: url.to_string(),
            ..Default::default()
        }
    }

    #[test]
    fn http_new_empty_url_returns_error() {
        let result = HttpBackend::new(&base_config(""));
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("backends.http.url"));
    }

    #[tokio::test]
    async fn http_search_parses_results_root_array() {
        let mock_server = MockServer::start().await;
        let url = format!("{}/search", mock_server.uri());

        // Root-level array (results_path is empty by default)
        let body = serde_json::json!([
            {"title": "Rust Lang", "url": "https://rust-lang.org", "snippet": "Systems programming"},
            {"title": "Tokio", "url": "https://tokio.rs", "snippet": "Async runtime for Rust"},
        ]);

        Mock::given(method("GET"))
            .and(path("/search"))
            .and(query_param("q", "rust"))
            .and(query_param("count", "5"))
            .respond_with(ResponseTemplate::new(200).set_body_json(&body))
            .mount(&mock_server)
            .await;

        let backend = HttpBackend::new(&base_config(&url)).unwrap();
        let results = backend.search("rust", 5, None).await.unwrap();

        assert_eq!(results.len(), 2);
        assert_eq!(results[0].title, "Rust Lang");
        assert_eq!(results[0].url, "https://rust-lang.org");
        assert_eq!(results[0].snippet, "Systems programming");
    }

    #[tokio::test]
    async fn http_search_nested_json_path() {
        let mock_server = MockServer::start().await;
        let url = format!("{}/api", mock_server.uri());

        let body = serde_json::json!({
            "data": {
                "items": [
                    {"title": "Nested Result", "url": "https://example.com", "snippet": "Found via path"},
                ]
            }
        });

        Mock::given(method("GET"))
            .and(path("/api"))
            .respond_with(ResponseTemplate::new(200).set_body_json(&body))
            .mount(&mock_server)
            .await;

        let config = HttpBackendConfig {
            url: url.clone(),
            results_path: "data.items".to_string(),
            ..Default::default()
        };
        let backend = HttpBackend::new(&config).unwrap();
        let results = backend.search("test", 10, None).await.unwrap();

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].title, "Nested Result");
    }

    #[tokio::test]
    async fn http_search_missing_json_path_key() {
        let mock_server = MockServer::start().await;
        let url = format!("{}/api", mock_server.uri());

        // Response doesn't contain the expected path
        let body = serde_json::json!({"other": "data"});

        Mock::given(method("GET"))
            .and(path("/api"))
            .respond_with(ResponseTemplate::new(200).set_body_json(&body))
            .mount(&mock_server)
            .await;

        let config = HttpBackendConfig {
            url: url.clone(),
            results_path: "data.items".to_string(),
            ..Default::default()
        };
        let backend = HttpBackend::new(&config).unwrap();
        let results = backend.search("test", 10, None).await.unwrap();

        assert!(results.is_empty());
    }

    #[tokio::test]
    async fn http_search_custom_field_names() {
        let mock_server = MockServer::start().await;
        let url = format!("{}/search", mock_server.uri());

        let body = serde_json::json!([
            {"name": "Custom Title", "link": "https://example.com", "description": "Custom snippet"},
        ]);

        Mock::given(method("GET"))
            .and(path("/search"))
            .respond_with(ResponseTemplate::new(200).set_body_json(&body))
            .mount(&mock_server)
            .await;

        let config = HttpBackendConfig {
            url: url.clone(),
            title_field: "name".to_string(),
            url_field: "link".to_string(),
            snippet_field: "description".to_string(),
            ..Default::default()
        };
        let backend = HttpBackend::new(&config).unwrap();
        let results = backend.search("test", 10, None).await.unwrap();

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].title, "Custom Title");
        assert_eq!(results[0].url, "https://example.com");
        assert_eq!(results[0].snippet, "Custom snippet");
    }

    #[tokio::test]
    async fn http_search_post_method() {
        let mock_server = MockServer::start().await;
        let url = format!("{}/search", mock_server.uri());

        let body = serde_json::json!([
            {"title": "POST Result", "url": "https://example.com", "snippet": "Via POST"},
        ]);

        Mock::given(method("POST"))
            .and(path("/search"))
            .respond_with(ResponseTemplate::new(200).set_body_json(&body))
            .mount(&mock_server)
            .await;

        let config = HttpBackendConfig {
            url: url.clone(),
            method: "POST".to_string(),
            ..Default::default()
        };
        let backend = HttpBackend::new(&config).unwrap();
        let results = backend.search("test", 10, None).await.unwrap();

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].title, "POST Result");
    }

    #[tokio::test]
    async fn http_search_custom_headers() {
        let mock_server = MockServer::start().await;
        let url = format!("{}/search", mock_server.uri());

        let body = serde_json::json!([
            {"title": "Authed", "url": "https://example.com", "snippet": "With auth"},
        ]);

        Mock::given(method("GET"))
            .and(path("/search"))
            .and(wiremock::matchers::header("Authorization", "Bearer secret"))
            .respond_with(ResponseTemplate::new(200).set_body_json(&body))
            .mount(&mock_server)
            .await;

        let mut headers = HashMap::new();
        headers.insert("Authorization".to_string(), "Bearer secret".to_string());

        let config = HttpBackendConfig {
            url: url.clone(),
            headers,
            ..Default::default()
        };
        let backend = HttpBackend::new(&config).unwrap();
        let results = backend.search("test", 10, None).await.unwrap();

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].title, "Authed");
    }

    #[tokio::test]
    async fn http_search_extra_params() {
        let mock_server = MockServer::start().await;
        let url = format!("{}/search", mock_server.uri());

        let body = serde_json::json!([]);

        Mock::given(method("GET"))
            .and(path("/search"))
            .and(query_param("q", "test"))
            .and(query_param("format", "json"))
            .respond_with(ResponseTemplate::new(200).set_body_json(&body))
            .mount(&mock_server)
            .await;

        let mut extra_params = HashMap::new();
        extra_params.insert("format".to_string(), "json".to_string());

        let config = HttpBackendConfig {
            url: url.clone(),
            extra_params,
            ..Default::default()
        };
        let backend = HttpBackend::new(&config).unwrap();
        let results = backend.search("test", 5, None).await.unwrap();

        assert!(results.is_empty());
    }

    #[tokio::test]
    async fn http_search_empty_count_param_omits_count() {
        let mock_server = MockServer::start().await;
        let url = format!("{}/search", mock_server.uri());

        let body = serde_json::json!([]);

        // When count_param is empty, only "q" should be sent
        Mock::given(method("GET"))
            .and(path("/search"))
            .and(query_param("q", "test"))
            .respond_with(ResponseTemplate::new(200).set_body_json(&body))
            .mount(&mock_server)
            .await;

        let config = HttpBackendConfig {
            url: url.clone(),
            count_param: String::new(),
            ..Default::default()
        };
        let backend = HttpBackend::new(&config).unwrap();
        let results = backend.search("test", 5, None).await.unwrap();

        assert!(results.is_empty());
    }

    #[tokio::test]
    async fn http_search_empty_results() {
        let mock_server = MockServer::start().await;
        let url = format!("{}/search", mock_server.uri());

        let body = serde_json::json!([]);

        Mock::given(method("GET"))
            .and(path("/search"))
            .respond_with(ResponseTemplate::new(200).set_body_json(&body))
            .mount(&mock_server)
            .await;

        let backend = HttpBackend::new(&base_config(&url)).unwrap();
        let results = backend.search("noresults", 5, None).await.unwrap();

        assert!(results.is_empty());
    }

    #[tokio::test]
    async fn http_search_http_error() {
        let mock_server = MockServer::start().await;
        let url = format!("{}/search", mock_server.uri());

        Mock::given(method("GET"))
            .and(path("/search"))
            .respond_with(ResponseTemplate::new(502))
            .mount(&mock_server)
            .await;

        let backend = HttpBackend::new(&base_config(&url)).unwrap();
        let result = backend.search("test", 5, None).await;

        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("502"));
    }

    #[tokio::test]
    async fn http_search_with_lang_param() {
        let mock_server = MockServer::start().await;
        let url = format!("{}/search", mock_server.uri());

        let body = serde_json::json!([
            {"title": "Italian", "url": "https://example.it", "snippet": "Risultato"},
        ]);

        Mock::given(method("GET"))
            .and(path("/search"))
            .and(query_param("lang", "it"))
            .respond_with(ResponseTemplate::new(200).set_body_json(&body))
            .mount(&mock_server)
            .await;

        let config = HttpBackendConfig {
            url: url.clone(),
            lang_param: "lang".to_string(),
            ..Default::default()
        };
        let backend = HttpBackend::new(&config).unwrap();
        let results = backend.search("test", 10, Some("it")).await.unwrap();

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].title, "Italian");
    }

    #[tokio::test]
    async fn http_search_empty_lang_param_omits_lang() {
        let mock_server = MockServer::start().await;
        let url = format!("{}/search", mock_server.uri());

        let body = serde_json::json!([]);

        // lang_param is empty by default, so even if lang is provided, no param is sent
        Mock::given(method("GET"))
            .and(path("/search"))
            .and(query_param("q", "test"))
            .respond_with(ResponseTemplate::new(200).set_body_json(&body))
            .mount(&mock_server)
            .await;

        let backend = HttpBackend::new(&base_config(&url)).unwrap();
        // Passing lang="it" but lang_param is empty, so it should be omitted
        let results = backend.search("test", 5, Some("it")).await.unwrap();

        assert!(results.is_empty());
    }
}