diff --git a/src/core/network/mod.rs b/src/core/network/mod.rs index a1a482ef..47d9f245 100644 --- a/src/core/network/mod.rs +++ b/src/core/network/mod.rs @@ -9,7 +9,7 @@ use bytes::Buf; use futures::StreamExt; use reqwest::r#async::Chunk; use reqwest::{ - r#async::{multipart::Form, Client}, + r#async::{multipart::Form, Client, Response}, StatusCode, }; use serde::{de::DeserializeOwned, Serialize}; @@ -49,22 +49,18 @@ pub async fn request_multipart( method_name: &str, params: Option
, ) -> ResponseResult { - let mut response = client - .post(&method_url(TELEGRAM_API_URL, token, method_name)) - .apply(|request_builder| match params { - Some(params) => request_builder.multipart(params), - None => request_builder, - }) - .send() - .await - .map_err(RequestError::NetworkError)?; - - let response = serde_json::from_str::>( - &response.text().await.map_err(RequestError::NetworkError)?, + process_response( + client + .post(&method_url(TELEGRAM_API_URL, token, method_name)) + .apply(|request_builder| match params { + Some(params) => request_builder.multipart(params), + None => request_builder, + }) + .send() + .await + .map_err(RequestError::NetworkError)?, ) - .map_err(RequestError::InvalidJson)?; - - response.into() + .await } pub async fn request_json( @@ -73,13 +69,20 @@ pub async fn request_json( method_name: &str, params: &P, ) -> ResponseResult { - let mut response = client - .post(&method_url(TELEGRAM_API_URL, token, method_name)) - .json(params) - .send() - .await - .map_err(RequestError::NetworkError)?; + process_response( + client + .post(&method_url(TELEGRAM_API_URL, token, method_name)) + .json(params) + .send() + .await + .map_err(RequestError::NetworkError)?, + ) + .await +} +async fn process_response( + mut response: Response, +) -> ResponseResult { let response = serde_json::from_str::>( &response.text().await.map_err(RequestError::NetworkError)?, ) @@ -92,14 +95,14 @@ pub async fn request_json( #[serde(untagged)] enum TelegramResponse { Ok { - /// Dummy field. Used for deserialization. + /// A dummy field. Used only for deserialization. #[allow(dead_code)] ok: bool, // TODO: True type result: R, }, Err { - /// Dummy field. Used for deserialization. + /// A dummy field. Used only for deserialization. #[allow(dead_code)] ok: bool, // TODO: False type diff --git a/src/core/requests/get_user_profile_photos.rs b/src/core/requests/get_user_profile_photos.rs index 2dfed773..0c6311ec 100644 --- a/src/core/requests/get_user_profile_photos.rs +++ b/src/core/requests/get_user_profile_photos.rs @@ -1,8 +1,19 @@ use crate::core::requests::RequestContext; -//TODO:: need implementation +//TODO: complete implementation after user_profile_fotos will be added to +// types/mod.rs +///Use this method to get a list of profile pictures for a user. Returns a +/// UserProfilePhotos object. #[derive(Debug, Clone, Serialize)] struct GetUserProfilePhotos<'a> { #[serde(skip_serializing)] ctx: RequestContext<'a>, + /// Unique identifier of the target user + user_id: i32, + /// Sequential number of the first photo to be returned. By default, all + /// photos are returned. + offset: Option, + ///Limits the number of photos to be retrieved. Values between 1—100 are + /// accepted. Defaults to 100. + limit: Option, } diff --git a/src/core/requests/kick_chat_member.rs b/src/core/requests/kick_chat_member.rs index 69c158a8..41d6dbc5 100644 --- a/src/core/requests/kick_chat_member.rs +++ b/src/core/requests/kick_chat_member.rs @@ -1,6 +1,10 @@ use crate::core::requests::RequestContext; //TODO:: need implementation - +/// Use this method to kick a user from a group, a supergroup or a channel. In +/// the case of supergroups and channels, the user will not be able to return to +/// the group on their own using invite links, etc., unless unbanned first. The +/// bot must be an administrator in the chat for this to work and must have the +/// appropriate admin rights. Returns True on success. #[derive(Debug, Clone, Serialize)] struct KickChatMember<'a> { #[serde(skip_serializing)] diff --git a/src/core/types/audio.rs b/src/core/types/audio.rs index 304d4946..66cc0878 100644 --- a/src/core/types/audio.rs +++ b/src/core/types/audio.rs @@ -1,17 +1,51 @@ use crate::core::types::PhotoSize; -#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize, Clone)] +#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone)] pub struct Audio { pub file_id: String, pub duration: u32, - #[serde(skip_serializing_if = "Option::is_none")] pub performer: Option, - #[serde(skip_serializing_if = "Option::is_none")] pub title: Option, - #[serde(skip_serializing_if = "Option::is_none")] pub mime_type: Option, - #[serde(skip_serializing_if = "Option::is_none")] pub file_size: Option, - #[serde(skip_serializing_if = "Option::is_none")] pub thumb: Option, } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn deserialize() { + let json = r#"{ + "file_id":"id", + "duration":60, + "performer":"Performer", + "title":"Title", + "mime_type":"MimeType", + "file_size":123456, + "thumb":{ + "file_id":"id", + "width":320, + "height":320, + "file_size":3452 + } + }"#; + let expected = Audio { + file_id: "id".to_string(), + duration: 60, + performer: Some("Performer".to_string()), + title: Some("Title".to_string()), + mime_type: Some("MimeType".to_string()), + file_size: Some(123456), + thumb: Some(PhotoSize { + file_id: "id".to_string(), + width: 320, + height: 320, + file_size: Some(3452) + }) + }; + let actual = serde_json::from_str::