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
use crate::international_autocomplete_api::lookup::Lookup;
use crate::international_autocomplete_api::suggestion::SuggestionListing;
use crate::sdk::client::Client;
use crate::sdk::error::SmartyError;
use crate::sdk::options::Options;
use crate::sdk::send_request;
use reqwest::Method;
use smarty_rust_proc_macro::smarty_api;
use url::{ParseError, Url};

#[smarty_api(
    api_path = "v2/lookup/",
    default_url = "https://international-autocomplete.api.smarty.com/",
    lookup_style(lookup),
    lookup_type = "Lookup",
    result_type = "SuggestionListing",
    custom_send = true
)]
pub struct InternationalAutocompleteClient;

impl InternationalAutocompleteClient {
    /// Uses the lookup and the client in
    /// order to build a request and send the message
    /// to the server.
    pub async fn send(&self, lookup: &mut Lookup) -> Result<(), SmartyError> {
        let mut url = self.client.url.clone();
        if lookup.address_id != String::default() {
            url = url.join(&lookup.address_id)?;
        }
        let mut req = self.client.reqwest_client.request(Method::GET, url);
        req = self.client.build_request(req);
        req = req.query(&lookup.clone().into_param_array());

        let candidates = send_request::<SuggestionListing>(req).await?;

        lookup.results = candidates;

        Ok(())
    }
}