google_api_rust_client_unoffical/services/route_service/get_route/
mod.rs

1
2pub mod request_model;
3pub mod response_model;
4
5use std::collections::HashMap;
6
7use super::common_models::WayPoint;
8use request_model::ComputeRouteRequest;
9use anyhow::{Ok, Result};
10use reqwest::{header::HeaderValue, Client, Url};
11use response_model::ComputeRouteResponse;
12use serde_json::Value;
13
14use super::{RouteService, GET_ROUTE_URL};
15
16impl RouteService {
17
18    /// Get a route. <br>
19    /// See https://developers.google.com/maps/documentation/routes/compute_route_directions
20    ///
21    /// * `origin` -  Origin waypoint.
22    /// * `destination` -  destination waypoint.
23    /// * `response_masks` - response field mask. If not specified, all available fields will be included.<br>
24    ///     Example: vec!["routes.duration", "routes.distanceMeters"] to return only distanceMeters and duration field for the route.
25    ///     See https://developers.google.com/maps/documentation/routes/choose_fields for more details.
26    /// * `params` - Optional Additional Parameter. Keys accepted are the following.
27    ///    See [ComputeRouteRequestOptinalParams](ComputeRouteRequestOptinalParams) for type detail
28    ///     * `intermediates`
29    ///     * `travelMode`
30    ///     * `routingPreference`
31    ///     * `polylineQuality`
32    ///     * `polylineEncoding`
33    ///     * `departureTime`: A timestamp in RFC3339 UTC "Zulu" format. Examples: "2014-10-02T15:01:23Z" and "2014-10-02T15:01:23.045123456Z".
34    ///     * `arrivalTime`: A timestamp in RFC3339 UTC "Zulu" format. Examples: "2014-10-02T15:01:23Z" and "2014-10-02T15:01:23.045123456Z".
35    ///     * `computeAlternativeRoutes`
36    ///     * `routeModifiers`
37    ///     * `languageCode`
38    ///     * `regionCode`
39    ///     * `units`
40    ///     * `optimizeWaypointOrder`
41    ///     * `requestedReferenceRoutes`
42    ///     * `extraComputations`
43    ///     * `trafficModel`
44    ///     * `transitPreferences`
45    pub async fn get_route(&mut self, origin: &WayPoint, destination: &WayPoint, response_masks: Option<Vec<&str>>, params: Option<HashMap<String, Value>>) -> Result<ComputeRouteResponse>{
46
47        let base_url = Url::parse(&format!("{}", GET_ROUTE_URL))?;
48        let mut headers = self.base.create_headers().await?;
49
50        // add field mask
51        let mask_string = if let Some(masks) = response_masks {
52            masks.join(",")
53        } else {
54            "*".to_owned()
55        };
56        headers.insert("X-Goog-FieldMask", HeaderValue::from_str(&mask_string)?);
57
58        let request_body = ComputeRouteRequest::new(origin, destination, params)?;
59
60        let builder: reqwest::RequestBuilder = Client::new().post(base_url)
61                .headers(headers)
62                .body(serde_json::to_string(&request_body)?);
63
64        let body = self.base.make_request(builder).await?;
65
66        Ok(serde_json::from_str::<ComputeRouteResponse>(&body)?)
67
68    }
69}