Reimplement GetFile request

This commit is contained in:
Waffle 2019-11-27 16:12:27 +03:00
parent 8056872c02
commit 4f77f8f371
3 changed files with 83 additions and 1 deletions

View file

@ -2,7 +2,7 @@ use crate::{
Bot,
requests::{
json, multipart,
payloads::{GetMe, GetUpdates, SendMessage, SendAnimation}
payloads::{GetMe, GetUpdates, SendMessage, SendAnimation, GetFile}
},
types::{ChatId, InputFile},
};
@ -48,4 +48,14 @@ impl Bot {
{
multipart::Request::new(self, SendAnimation::new(chat_id, animation))
}
/// For tg-method documentation see [`GetFile`]
///
/// [`GetFile`]: crate::requests::payloads::GetFile
pub fn get_file<F>(&self, file_id: F) -> json::Request<GetFile>
where
F: Into<String>,
{
json::Request::new(self, GetFile::new(file_id))
}
}

View file

@ -66,10 +66,14 @@ pub mod payloads {
mod send_animation;
mod get_file;
pub use self::{
get_me::GetMe,
send_message::SendMessage,
send_animation::SendAnimation,
get_file::GetFile,
};
}

View file

@ -0,0 +1,68 @@
use crate::{
bot::Bot,
network,
requests::{json, Method},
types::File,
};
use crate::requests::dynamic;
/// Use this method to get basic info about a file and prepare it for
/// downloading.
///
/// For the moment, bots can download files of up to `20MB` in size.
///
/// On success, a [`File`] object is returned.
///
/// The file can then be downloaded via the link
/// `https://api.telegram.org/file/bot<token>/<file_path>`, where `<file_path>`
/// is taken from the response. It is guaranteed that the link will be valid
/// for at least `1` hour. When the link expires, a new one can be requested by
/// calling [`GetFile`] again.
///
/// **Note**: This function may not preserve the original file name and MIME
/// type. You should save the file's MIME type and name (if available) when the
/// [`File`] object is received.
///
/// [`File`]: crate::types::file
/// [`GetFile`]: self::GetFile
#[serde_with_macros::skip_serializing_none]
#[derive(Debug, PartialEq, Eq, Hash, Clone, Deserialize, Serialize)]
pub struct GetFile {
/// File identifier to get info about
pub file_id: String,
}
impl Method for GetFile {
type Output = File;
const NAME: &'static str = "getFile";
}
impl json::Payload for GetFile {}
impl dynamic::Payload for GetFile {
fn kind(&self) -> dynamic::Kind {
dynamic::Kind::Json(serde_json::to_string(self).unwrap())
}
}
impl GetFile {
pub fn new<F>(file_id: F) -> Self
where
F: Into<String>
{
Self {
file_id: file_id.into(),
}
}
}
impl json::Request<'_, GetFile> {
pub fn file_id<F>(mut self, value: F) -> Self
where
F: Into<String>,
{
self.payload.file_id = value.into();
self
}
}