use crate::places::{
query::{
ExpectedResponse, NearPointQueryParams, PlacesClient, PlacesError, PointResponse,
WithinExtentQueryParams,
},
PlaceResult,
};
use std::sync::Arc;
#[derive(Debug, Clone)]
pub struct NearPointQuery {
pub client: Arc<PlacesClient>,
pub params: NearPointQueryParams,
pub results: <Vec<PlaceResult> as IntoIterator>::IntoIter,
pub next_page: Option<String>,
}
impl NearPointQuery {
pub fn new(
client: Arc<PlacesClient>,
params: NearPointQueryParams,
) -> Result<Self, PlacesError> {
let c = client
.client
.get(format!("{}/places/near-point", client.base_url))
.query(¶ms.clone().prepare())
.header("X-Esri-Authorization", format!("Bearer {}", client.token));
let resp = c
.send()
.map_err(PlacesError::RequestError)?
.json::<ExpectedResponse>()
.map_err(PlacesError::RequestError)?;
let point_response = match resp {
ExpectedResponse::Point(point_response) => point_response,
ExpectedResponse::Error(error_response) => {
return Err(PlacesError::ApiError(error_response))
}
};
let next_page = match point_response.pagination {
Some(p) => p.next_url,
None => None,
};
Ok(Self {
client,
params,
results: point_response.results.into_iter(),
next_page,
})
}
pub fn try_next(&mut self) -> Result<Option<PlaceResult>, PlacesError> {
if let Some(place_res) = self.results.next() {
return Ok(Some(place_res));
}
if self.next_page.is_none() {
return Ok(None);
}
let next_page = self
.client
.client
.get(self.next_page.as_ref().unwrap())
.header(
"X-Esri-Authorization",
format!("Bearer {}", self.client.token),
)
.send()
.map_err(PlacesError::RequestError)?
.json::<PointResponse>()
.map_err(PlacesError::RequestError)?;
self.results = next_page.results.into_iter();
self.next_page = match next_page.pagination {
Some(p) => p.next_url,
None => None,
};
Ok(self.results.next())
}
}
impl Iterator for NearPointQuery {
type Item = Result<PlaceResult, PlacesError>;
fn next(&mut self) -> Option<Self::Item> {
match self.try_next() {
Ok(Some(place_res)) => Some(Ok(place_res)),
Ok(None) => None,
Err(e) => Some(Err(e)),
}
}
}
#[derive(Debug, Clone)]
pub struct WithinExtentQuery {
pub client: Arc<PlacesClient>,
pub params: WithinExtentQueryParams,
pub results: <Vec<PlaceResult> as IntoIterator>::IntoIter,
pub next_page: Option<String>,
}
impl WithinExtentQuery {
pub fn new(
client: Arc<PlacesClient>,
params: WithinExtentQueryParams,
) -> Result<Self, PlacesError> {
let c = client
.client
.get(format!("{}/places/within-extent", client.base_url))
.query(¶ms.clone().prepare())
.header("X-Esri-Authorization", format!("Bearer {}", client.token));
let resp = c
.send()
.map_err(PlacesError::RequestError)?
.json::<ExpectedResponse>()
.map_err(PlacesError::RequestError)?;
let point_response = match resp {
ExpectedResponse::Point(point_response) => point_response,
ExpectedResponse::Error(error_response) => {
return Err(PlacesError::ApiError(error_response))
}
};
let next_page = match point_response.pagination {
Some(p) => p.next_url,
None => None,
};
Ok(Self {
client,
params,
results: point_response.results.into_iter(),
next_page,
})
}
pub fn try_next(&mut self) -> Result<Option<PlaceResult>, PlacesError> {
if let Some(place_res) = self.results.next() {
return Ok(Some(place_res));
}
if self.next_page.is_none() {
return Ok(None);
}
let next_page = self
.client
.client
.get(self.next_page.as_ref().unwrap())
.header(
"X-Esri-Authorization",
format!("Bearer {}", self.client.token),
)
.send()
.map_err(PlacesError::RequestError)?
.json::<PointResponse>()
.map_err(PlacesError::RequestError)?;
self.results = next_page.results.into_iter();
self.next_page = match next_page.pagination {
Some(p) => p.next_url,
None => None,
};
Ok(self.results.next())
}
}
impl Iterator for WithinExtentQuery {
type Item = Result<PlaceResult, PlacesError>;
fn next(&mut self) -> Option<Self::Item> {
match self.try_next() {
Ok(Some(place_res)) => Some(Ok(place_res)),
Ok(None) => None,
Err(e) => Some(Err(e)),
}
}
}