|
| 1 | +extern crate serde_json; |
| 2 | +extern crate smarty_rust_sdk; |
| 3 | +extern crate tokio; |
| 4 | + |
| 5 | +use smarty_rust_sdk::us_street_api::lookup::{Lookup, MatchStrategy}; |
| 6 | + |
| 7 | +use smarty_rust_sdk::sdk::authentication::SecretKeyCredential; |
| 8 | +use smarty_rust_sdk::sdk::batch::Batch; |
| 9 | +use smarty_rust_sdk::sdk::options::OptionsBuilder; |
| 10 | +use smarty_rust_sdk::us_street_api::client::USStreetAddressClient; |
| 11 | +use std::error::Error; |
| 12 | + |
| 13 | +#[tokio::main] |
| 14 | +async fn main() -> Result<(), Box<dyn Error>> { |
| 15 | + let lookup = Lookup { |
| 16 | + street: "1600 Amphitheatre Pkwy".to_string(), |
| 17 | + last_line: "Mountain View, CA".to_string(), |
| 18 | + max_candidates: 10, |
| 19 | + match_strategy: MatchStrategy::Enhanced, // Enhanced matching is required to return component analysis results. |
| 20 | + ..Default::default() |
| 21 | + }; |
| 22 | + |
| 23 | + let mut batch = Batch::default(); |
| 24 | + batch.push(lookup)?; |
| 25 | + |
| 26 | + let authentication = SecretKeyCredential::new( |
| 27 | + std::env::var("SMARTY_AUTH_ID").expect("Missing SMARTY_AUTH_ID env variable"), |
| 28 | + std::env::var("SMARTY_AUTH_TOKEN").expect("Missing SMARTY_AUTH_TOKEN env variable"), |
| 29 | + ); |
| 30 | + |
| 31 | + let options = OptionsBuilder::new(Some(authentication)) |
| 32 | + .with_component_analysis() // To add component analysis feature you need to specify when you create the options for the client. |
| 33 | + .build(); |
| 34 | + |
| 35 | + let client = USStreetAddressClient::new(options)?; |
| 36 | + |
| 37 | + client.send(&mut batch).await?; |
| 38 | + |
| 39 | + // Here is an example of how to access component analysis |
| 40 | + for record in batch.records() { |
| 41 | + if !record.results.is_empty() { |
| 42 | + println!("Component Analysis Results:\n {}",serde_json::to_string_pretty(&record.results[0].analysis.components)?); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + Ok(()) |
| 47 | +} |
0 commit comments