use std::sync::Arc;
use serde_json::Value;
use crate::error::Result;
use crate::transport::{OnClientError, RequestOpts, Transport};
pub struct RoutingApi {
transport: Arc<Transport>,
}
impl RoutingApi {
pub(crate) fn new(transport: Arc<Transport>) -> Self {
Self { transport }
}
async fn post(&self, path: &str, body: Value) -> Result<Value> {
let raw = self
.transport
.do_request(RequestOpts {
method: reqwest::Method::POST,
path,
query: Vec::new(),
body: Some(body),
on_client_error: OnClientError::Throw,
empty: None,
})
.await?;
if raw.is_empty() {
return Ok(Value::Null);
}
Ok(serde_json::from_slice(&raw)?)
}
pub async fn route(&self, body: Value) -> Result<Value> {
self.post("/v2/route", body).await
}
pub async fn optimized(&self, body: Value) -> Result<Value> {
self.post("/v2/route/optimized", body).await
}
pub async fn matrix(&self, body: Value) -> Result<Value> {
self.post("/v2/route/matrix", body).await
}
pub async fn locate(&self, body: Value) -> Result<Value> {
self.post("/v2/route/locate", body).await
}
pub async fn elevation(&self, body: Value) -> Result<Value> {
self.post("/v2/route/elevation", body).await
}
}