use std::future::{Future, IntoFuture};
use std::pin::Pin;
use serde::Serialize;
use rustigram_types::update::Update;
use rustigram_types::webhook::WebhookInfo;
use crate::client::BotClient;
use crate::error::Result;
#[derive(Serialize, Default)]
pub struct GetUpdatesParams {
#[serde(skip_serializing_if = "Option::is_none")]
pub offset: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub limit: Option<u8>,
#[serde(skip_serializing_if = "Option::is_none")]
pub timeout: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub allowed_updates: Option<Vec<String>>,
}
pub struct GetUpdates {
client: BotClient,
params: GetUpdatesParams,
}
impl GetUpdates {
pub(crate) fn new(client: BotClient) -> Self {
Self {
client,
params: GetUpdatesParams::default(),
}
}
pub fn offset(mut self, offset: i64) -> Self {
self.params.offset = Some(offset);
self
}
pub fn limit(mut self, limit: u8) -> Self {
self.params.limit = Some(limit.clamp(1, 100));
self
}
pub fn timeout(mut self, secs: u32) -> Self {
self.params.timeout = Some(secs);
self
}
pub fn allowed_updates(mut self, types: Vec<impl Into<String>>) -> Self {
self.params.allowed_updates = Some(types.into_iter().map(Into::into).collect());
self
}
}
impl IntoFuture for GetUpdates {
type Output = Result<Vec<Update>>;
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move { self.client.post_json("getUpdates", &self.params).await })
}
}
#[derive(Serialize)]
pub struct SetWebhookParams {
url: String,
#[serde(skip_serializing_if = "Option::is_none")]
ip_address: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
max_connections: Option<u8>,
#[serde(skip_serializing_if = "Option::is_none")]
allowed_updates: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
drop_pending_updates: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
secret_token: Option<String>,
}
pub struct SetWebhook {
client: BotClient,
params: SetWebhookParams,
}
impl SetWebhook {
pub(crate) fn new(client: BotClient, url: impl Into<String>) -> Self {
Self {
client,
params: SetWebhookParams {
url: url.into(),
ip_address: None,
max_connections: None,
allowed_updates: None,
drop_pending_updates: None,
secret_token: None,
},
}
}
pub fn ip_address(mut self, ip: impl Into<String>) -> Self {
self.params.ip_address = Some(ip.into());
self
}
pub fn max_connections(mut self, n: u8) -> Self {
self.params.max_connections = Some(n.clamp(1, 100));
self
}
pub fn allowed_updates(mut self, types: Vec<impl Into<String>>) -> Self {
self.params.allowed_updates = Some(types.into_iter().map(Into::into).collect());
self
}
pub fn drop_pending_updates(mut self, v: bool) -> Self {
self.params.drop_pending_updates = Some(v);
self
}
pub fn secret_token(mut self, token: impl Into<String>) -> Self {
self.params.secret_token = Some(token.into());
self
}
}
impl IntoFuture for SetWebhook {
type Output = Result<bool>;
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move { self.client.post_json("setWebhook", &self.params).await })
}
}
#[derive(Serialize, Default)]
struct DeleteWebhookParams {
#[serde(skip_serializing_if = "Option::is_none")]
drop_pending_updates: Option<bool>,
}
pub struct DeleteWebhook {
client: BotClient,
params: DeleteWebhookParams,
}
impl DeleteWebhook {
pub(crate) fn new(client: BotClient) -> Self {
Self {
client,
params: DeleteWebhookParams::default(),
}
}
pub fn drop_pending_updates(mut self, v: bool) -> Self {
self.params.drop_pending_updates = Some(v);
self
}
}
impl IntoFuture for DeleteWebhook {
type Output = Result<bool>;
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move { self.client.post_json("deleteWebhook", &self.params).await })
}
}
pub struct GetWebhookInfo {
client: BotClient,
}
impl GetWebhookInfo {
pub(crate) fn new(client: BotClient) -> Self {
Self { client }
}
}
impl IntoFuture for GetWebhookInfo {
type Output = Result<WebhookInfo>;
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move {
self.client
.post_json("getWebhookInfo", &serde_json::json!({}))
.await
})
}
}