smarty_rust_sdk/us_enrichment_api/
client.rs

1use crate::sdk::client::Client;
2use crate::sdk::error::SmartyError;
3use crate::sdk::options::Options;
4use crate::sdk::{parse_response_json, send_request_full};
5use crate::us_enrichment_api::lookup::EnrichmentLookup;
6use reqwest::Method;
7use smarty_rust_proc_macro::smarty_api;
8
9use super::response::EnrichmentResponse;
10
11#[smarty_api(
12    api_path = "lookup",
13    default_url = "https://us-enrichment.api.smarty.com/",
14    lookup_style(lookup),
15    lookup_type = "EnrichmentLookup",
16    result_type = "Results",
17    custom_send = true
18)]
19pub struct USEnrichmentClient;
20
21impl USEnrichmentClient {
22    /// Uses the lookup and the client in
23    /// order to build a request and send the message
24    /// to the server.
25    pub async fn send<R: EnrichmentResponse>(
26        &self,
27        lookup: &mut EnrichmentLookup<R>,
28    ) -> Result<(), SmartyError> {
29        let mut url = self.client.url.clone();
30        url = url.join(&format!(
31            "/lookup/{}/{}",
32            lookup.smarty_key,
33            R::lookup_type()
34        ))?;
35
36        let mut req = self.client.reqwest_client.request(Method::GET, url);
37
38        if !lookup.etag.is_empty() {
39            req = req.header("ETag", &lookup.etag);
40        }
41
42        req = self.client.build_request(req);
43
44        println!("{req:?}");
45
46        let response = send_request_full(req).await?;
47
48        let etag = response
49            .headers()
50            .get("ETag")
51            .map(|x| x.to_str().expect("ETag should always be a string"))
52            .unwrap_or_default();
53        lookup.etag = etag.to_string();
54
55        let candidates = parse_response_json(response).await?;
56
57        lookup.set_results(candidates);
58
59        Ok(())
60    }
61}