context_id/
mars.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5use serde_json::json;
6use url::Url;
7use viaduct::Request;
8
9const DEFAULT_MARS_API_ENDPOINT: &str = "https://ads.mozilla.org/v1/";
10
11pub trait MARSClient: Sync + Send {
12    fn delete(&self, context_id: String) -> crate::Result<()>;
13}
14
15pub struct SimpleMARSClient {
16    endpoint: String,
17}
18
19impl SimpleMARSClient {
20    pub fn new() -> Self {
21        Self {
22            endpoint: DEFAULT_MARS_API_ENDPOINT.to_string(),
23        }
24    }
25
26    #[allow(dead_code)]
27    fn new_with_endpoint(endpoint: String) -> Self {
28        Self { endpoint }
29    }
30}
31
32impl MARSClient for SimpleMARSClient {
33    fn delete(&self, context_id: String) -> crate::Result<()> {
34        let delete_url = Url::parse(&format!("{}/delete_user", self.endpoint))?;
35
36        let body = json!({
37            "context_id": context_id,
38        })
39        .to_string();
40        let request = Request::delete(delete_url).body(body);
41
42        let _ = request.send()?;
43        Ok(())
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50    use mockito::mock;
51
52    #[test]
53    fn test_delete() {
54        viaduct_reqwest::use_reqwest_backend();
55
56        let expected_context_id = "some-fake-context-id";
57        let expected_body = format!(r#"{{"context_id":"{}"}}"#, expected_context_id);
58
59        let m = mock("DELETE", "/delete_user")
60            .match_body(expected_body.as_str())
61            .create();
62
63        let client = SimpleMARSClient::new_with_endpoint(mockito::server_url());
64        let _ = client.delete(expected_context_id.to_string());
65        m.expect(1).assert();
66    }
67}