use crate::{Client, Result, SortOrder, SourcesResults};
#[derive(Debug, Clone)]
#[must_use = "a SourcesRequest does nothing until you call `.send()`"]
pub struct SourcesRequest<'a> {
client: &'a Client,
sort_order: Option<SortOrder>,
limit: Option<u32>,
offset: Option<u32>,
}
impl<'a> SourcesRequest<'a> {
pub(crate) fn new(client: &'a Client) -> Self {
Self {
client,
sort_order: None,
limit: None,
offset: None,
}
}
pub fn sort_order(mut self, order: SortOrder) -> Self {
self.sort_order = Some(order);
self
}
pub fn limit(mut self, limit: u32) -> Self {
self.limit = Some(limit);
self
}
pub fn offset(mut self, offset: u32) -> Self {
self.offset = Some(offset);
self
}
pub async fn send(self) -> Result<SourcesResults> {
self.client.execute_sources(&self).await
}
pub(crate) fn query_params(&self) -> Vec<(&'static str, String)> {
let mut params: Vec<(&'static str, String)> = Vec::new();
if let Some(order) = self.sort_order {
params.push(("sort_order", order.query_code().to_owned()));
}
if let Some(limit) = self.limit {
params.push(("limit", limit.to_string()));
}
if let Some(offset) = self.offset {
params.push(("offset", offset.to_string()));
}
params
}
}
impl crate::paginate::sealed::Sealed for SourcesRequest<'_> {}
impl crate::paginate::Paginate for SourcesRequest<'_> {
type Page = SourcesResults;
const MAX_PAGE: u32 = 1000;
fn requested_limit(&self) -> Option<u32> {
self.limit
}
fn requested_offset(&self) -> Option<u32> {
self.offset
}
fn with_paging(self, limit: u32, offset: u32) -> Self {
self.limit(limit).offset(offset)
}
fn send_page(self) -> impl std::future::Future<Output = Result<Self::Page>> + Send {
self.send()
}
}