Debian Codegraph @

third_party/rust/context_id/src/mars.rs

raw
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
5 use serde_json::json;
6 use url::Url;
7 use viaduct::Request;
8
9 const DEFAULT_MARS_API_ENDPOINT: &str = "https://ads.mozilla.org/v1/";
10
11 pub trait MARSClient: Sync + Send {
12 fn delete(&self, context_id: String) -> crate::Result<()>;
13 }
14
15 pub struct SimpleMARSClient {
16 endpoint: String,
17 }
18
19 impl 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
32 impl 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)]
48 mod 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 }