use crate::client::LatlngClient;
use crate::models::{AutosuggestResponse, CategoriesResponse, NearbyResponse, SearchResponse};
use crate::Result;
#[derive(Debug, Clone, Copy)]
pub struct PlacesClient<'a> {
client: &'a LatlngClient,
}
impl<'a> PlacesClient<'a> {
pub(crate) fn new(client: &'a LatlngClient) -> Self {
Self { client }
}
pub fn nearby(self, lat: f64, lon: f64) -> NearbyRequest<'a> {
NearbyRequest {
client: self.client,
lat,
lon,
radius: None,
category: None,
limit: None,
}
}
pub fn search(self, query: impl Into<String>) -> SearchRequest<'a> {
SearchRequest {
client: self.client,
query: query.into(),
lat: None,
lon: None,
category: None,
country: None,
limit: None,
}
}
pub fn autosuggest(self, query: impl Into<String>) -> AutosuggestRequest<'a> {
AutosuggestRequest {
client: self.client,
query: query.into(),
lat: None,
lon: None,
radius: None,
country: None,
bbox: None,
limit: None,
}
}
pub async fn categories(self) -> Result<CategoriesResponse> {
self.client.get_json("/v1/places/categories", &[]).await
}
}
#[derive(Debug, Clone)]
pub struct NearbyRequest<'a> {
client: &'a LatlngClient,
lat: f64,
lon: f64,
radius: Option<u32>,
category: Option<String>,
limit: Option<u32>,
}
impl<'a> NearbyRequest<'a> {
pub fn radius(mut self, radius: u32) -> Self {
self.radius = Some(radius);
self
}
pub fn category(mut self, category: impl Into<String>) -> Self {
self.category = Some(category.into());
self
}
pub fn limit(mut self, limit: u32) -> Self {
self.limit = Some(limit);
self
}
pub async fn send(self) -> Result<NearbyResponse> {
let mut params = vec![("lat", self.lat.to_string()), ("lon", self.lon.to_string())];
push_param(&mut params, "radius", self.radius);
push_param(&mut params, "type", self.category);
push_param(&mut params, "limit", self.limit);
self.client.get_json("/v1/places/nearby", ¶ms).await
}
}
#[derive(Debug, Clone)]
pub struct SearchRequest<'a> {
client: &'a LatlngClient,
query: String,
lat: Option<f64>,
lon: Option<f64>,
category: Option<String>,
country: Option<String>,
limit: Option<u32>,
}
impl<'a> SearchRequest<'a> {
pub fn bias(mut self, lat: f64, lon: f64) -> Self {
self.lat = Some(lat);
self.lon = Some(lon);
self
}
pub fn category(mut self, category: impl Into<String>) -> Self {
self.category = Some(category.into());
self
}
pub fn country(mut self, country: impl Into<String>) -> Self {
self.country = Some(country.into());
self
}
pub fn limit(mut self, limit: u32) -> Self {
self.limit = Some(limit);
self
}
pub async fn send(self) -> Result<SearchResponse> {
let mut params = vec![("q", self.query.to_string())];
push_param(&mut params, "lat", self.lat);
push_param(&mut params, "lon", self.lon);
push_param(&mut params, "type", self.category);
push_param(&mut params, "country", self.country);
push_param(&mut params, "limit", self.limit);
self.client.get_json("/v1/places/search", ¶ms).await
}
}
#[derive(Debug, Clone)]
pub struct AutosuggestRequest<'a> {
client: &'a LatlngClient,
query: String,
lat: Option<f64>,
lon: Option<f64>,
radius: Option<u32>,
country: Option<String>,
bbox: Option<String>,
limit: Option<u32>,
}
impl<'a> AutosuggestRequest<'a> {
pub fn bias(mut self, lat: f64, lon: f64) -> Self {
self.lat = Some(lat);
self.lon = Some(lon);
self
}
pub fn radius(mut self, radius: u32) -> Self {
self.radius = Some(radius);
self
}
pub fn country(mut self, country: impl Into<String>) -> Self {
self.country = Some(country.into());
self
}
pub fn bbox(mut self, bbox: impl Into<String>) -> Self {
self.bbox = Some(bbox.into());
self
}
pub fn limit(mut self, limit: u32) -> Self {
self.limit = Some(limit);
self
}
pub async fn send(self) -> Result<AutosuggestResponse> {
let mut params = vec![("q", self.query.to_string())];
push_param(&mut params, "lat", self.lat);
push_param(&mut params, "lon", self.lon);
push_param(&mut params, "radius", self.radius);
push_param(&mut params, "country", self.country);
push_param(&mut params, "bbox", self.bbox);
push_param(&mut params, "limit", self.limit);
self.client
.get_autosuggest_json("/autosuggest", ¶ms)
.await
}
}
fn push_param<'a, T>(params: &mut Vec<(&'a str, String)>, key: &'a str, value: Option<T>)
where
T: ToString,
{
if let Some(value) = value {
params.push((key, value.to_string()));
}
}