-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(samples): introduce rust sample
- Loading branch information
Showing
4 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/target/ | ||
**/*.rs.bk | ||
Cargo.lock | ||
*.pdb | ||
.env | ||
.cargo/config.toml | ||
.idea/ | ||
.vscode/ | ||
*.swp | ||
*.swo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "rust" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
reqwest = { version = "0.12.12", features = ["json"] } | ||
serde = { version = "1.0.217", features = ["derive"] } | ||
serde_json = "1.0.134" | ||
tokio = { version = "1.42.0", features = ["full"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
extern crate reqwest; | ||
extern crate serde_json; | ||
|
||
mod models; | ||
|
||
use models::{GeocodeResult, ResultContainer}; | ||
use std::env; | ||
|
||
#[derive(serde::Serialize)] | ||
#[allow(dead_code)] | ||
enum Locators { | ||
All, | ||
AddressPoints, | ||
RoadCenterlines, | ||
} | ||
impl std::fmt::Display for Locators { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
match self { | ||
Locators::All => write!(f, "all"), | ||
Locators::AddressPoints => write!(f, "addressPoints"), | ||
Locators::RoadCenterlines => write!(f, "roadCenterlines"), | ||
} | ||
} | ||
} | ||
|
||
#[derive(serde::Serialize)] | ||
#[allow(dead_code)] | ||
enum Format { | ||
Esrijson, | ||
Geojson, | ||
} | ||
impl std::fmt::Display for Format { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
match self { | ||
Format::Esrijson => write!(f, "esrijson"), | ||
Format::Geojson => write!(f, "geojson"), | ||
} | ||
} | ||
} | ||
|
||
#[derive(serde::Serialize)] | ||
struct GeocodingOptions { | ||
accept_score: Option<i32>, | ||
pobox: Option<bool>, | ||
locators: Option<Locators>, | ||
suggest: Option<i8>, | ||
spatial_reference: Option<i32>, | ||
format: Option<Format>, | ||
callback: Option<String>, | ||
} | ||
impl Default for GeocodingOptions { | ||
fn default() -> Self { | ||
Self { | ||
accept_score: None, | ||
pobox: None, | ||
locators: None, | ||
suggest: None, | ||
spatial_reference: None, | ||
format: None, | ||
callback: None, | ||
} | ||
} | ||
} | ||
|
||
struct GeocodeService { | ||
api_key: String, | ||
} | ||
|
||
impl GeocodeService { | ||
fn new(api_key: String) -> Self { | ||
Self { api_key } | ||
} | ||
|
||
async fn locate( | ||
&self, | ||
street: &str, | ||
zone: &str, | ||
options: GeocodingOptions, | ||
) -> Result<ResultContainer<Option<GeocodeResult>>, reqwest::Error> { | ||
let url = format!( | ||
"https://api-1057643351324.us-central1.run.app/api/v1/geocode/{}/{}", | ||
street, zone | ||
); | ||
|
||
let response: ResultContainer<Option<GeocodeResult>> = reqwest::Client::new() | ||
.get(url) | ||
.query(&[("apiKey", self.api_key.clone())]) | ||
.query(&options) | ||
.send() | ||
.await? | ||
.json() | ||
.await?; | ||
|
||
// Return the response | ||
Ok(response) | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let api_key = env::var("UGRC_API_KEY").expect("UGRC_API_KEY must be set"); | ||
let geocode = GeocodeService::new(api_key); | ||
|
||
let result = geocode | ||
.locate( | ||
"123 south main street", | ||
"slc", | ||
GeocodingOptions { | ||
accept_score: Some(90), | ||
spatial_reference: Some(4326), | ||
..Default::default() | ||
}, | ||
) | ||
.await; | ||
|
||
println!("{:#?}", result); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
extern crate serde; | ||
|
||
use serde::Deserialize; | ||
|
||
#[derive(Debug, Deserialize)] | ||
#[allow(dead_code)] | ||
pub struct ResultContainer<T> { | ||
pub status: i32, | ||
pub message: Option<String>, | ||
pub result: T, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
#[allow(dead_code)] | ||
pub struct GeocodeResult { | ||
pub location: Location, | ||
pub score: f64, | ||
pub locator: String, | ||
#[serde(rename = "matchAddress")] | ||
pub match_address: Option<String>, | ||
#[serde(rename = "inputAddress")] | ||
pub input_address: String, | ||
#[serde(rename = "standardizedAddress")] | ||
pub standardized_address: Option<String>, | ||
#[serde(rename = "addressGrid")] | ||
pub address_grid: String, | ||
pub candidates: Option<Vec<Candidate>>, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
#[allow(dead_code)] | ||
pub struct Candidate { | ||
address: String, | ||
pub location: Location, | ||
pub score: f64, | ||
pub locator: String, | ||
#[serde(rename = "addressGrid")] | ||
pub address_grid: String, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
#[allow(dead_code)] | ||
pub struct Location { | ||
pub x: f64, | ||
pub y: f64, | ||
} |