Skip to content

Commit

Permalink
Merge branch 'master' into simplify-imports
Browse files Browse the repository at this point in the history
Conflicts:
	src/api.rs
	src/client_reqwest.rs
	src/client_ureq.rs
	src/lib.rs
  • Loading branch information
EdJoPaTo committed Sep 11, 2024
2 parents c670193 + 0cd7089 commit 0e35c8d
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 30 deletions.
6 changes: 2 additions & 4 deletions examples/async_reply_to_message_updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,12 @@ async fn process_message(message: Message, api: AsyncApi) {
let reply_parameters = ReplyParameters::builder()
.message_id(message.message_id)
.build();

let send_message_params = SendMessageParams::builder()
.chat_id(message.chat.id)
.text("hello")
.reply_parameters(reply_parameters)
.build();

if let Err(err) = api.send_message(&send_message_params).await {
println!("Failed to send message: {err:?}");
if let Err(error) = api.send_message(&send_message_params).await {
println!("Failed to send message: {error:?}");
}
}
19 changes: 4 additions & 15 deletions src/client_reqwest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ impl AsyncApi {
Self::builder().api_url(api_url).build()
}

pub fn encode_params<Params>(params: &Params) -> Result<String, Error>
where
Params: serde::ser::Serialize + std::fmt::Debug,
{
serde_json::to_string(params).map_err(|err| Error::Encode(format!("{err:?} : {params:?}")))
}

pub async fn decode_response<Output>(response: reqwest::Response) -> Result<Output, Error>
where
Output: serde::de::DeserializeOwned,
Expand All @@ -45,18 +38,14 @@ impl AsyncApi {
match response.text().await {
Ok(message) => {
if status_code == 200 {
Ok(Self::parse_json(&message)?)
Ok(crate::json::decode(&message)?)
} else {
Err(Error::Api(Self::parse_json(&message)?))
Err(Error::Api(crate::json::decode(&message)?))
}
}
Err(error) => Err(Error::Decode(format!("{error:?}"))),
}
}

fn parse_json<Output: serde::de::DeserializeOwned>(body: &str) -> Result<Output, Error> {
serde_json::from_str(body).map_err(|error| Error::Decode(format!("{error:?} : {body:?}")))
}
}

#[async_trait]
Expand All @@ -78,7 +67,7 @@ impl AsyncTelegramApi for AsyncApi {
.post(url)
.header("Content-Type", "application/json");
if let Some(params) = params {
let json_string = Self::encode_params(&params)?;
let json_string = crate::json::encode(&params)?;
prepared_request = prepared_request.body(json_string);
};
let response = prepared_request.send().await?;
Expand All @@ -95,7 +84,7 @@ impl AsyncTelegramApi for AsyncApi {
Params: serde::ser::Serialize + std::fmt::Debug + std::marker::Send,
Output: serde::de::DeserializeOwned,
{
let json_string = Self::encode_params(&params)?;
let json_string = crate::json::encode(&params)?;
let json_struct: Value = serde_json::from_str(&json_string).unwrap();
let file_keys: Vec<&str> = files.iter().map(|(key, _)| *key).collect();
let files_with_paths: Vec<(String, &str, String)> = files
Expand Down
14 changes: 3 additions & 11 deletions src/client_ureq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,12 @@ impl Api {
Self::builder().api_url(api_url).build()
}

pub fn encode_params<Params>(params: &Params) -> Result<String, Error>
where
Params: serde::ser::Serialize + std::fmt::Debug,
{
serde_json::to_string(params).map_err(|err| Error::Encode(format!("{err:?} : {params:?}")))
}

pub fn decode_response<Output>(response: Response) -> Result<Output, Error>
where
Output: serde::de::DeserializeOwned,
{
match response.into_string() {
Ok(message) => serde_json::from_str(&message)
.map_err(|error| Error::Decode(format!("{error:?} : {message:?}"))),
Ok(message) => crate::json::decode(&message),
Err(error) => Err(Error::Decode(format!("{error:?}"))),
}
}
Expand All @@ -62,7 +54,7 @@ impl TelegramApi for Api {
let response = match params {
None => prepared_request.call()?,
Some(data) => {
let json = Self::encode_params(&data)?;
let json = crate::json::encode(&data)?;
prepared_request.send_string(&json)?
}
};
Expand All @@ -79,7 +71,7 @@ impl TelegramApi for Api {
Params: serde::ser::Serialize + std::fmt::Debug,
Output: serde::de::DeserializeOwned,
{
let json_string = Self::encode_params(&params)?;
let json_string = crate::json::encode(&params)?;
let json_struct: Value = serde_json::from_str(&json_string).unwrap();
let file_keys: Vec<&str> = files.iter().map(|(key, _)| *key).collect();
let files_with_names: Vec<(&str, Option<&str>, PathBuf)> = files
Expand Down
1 change: 1 addition & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
#[serde(untagged)]
pub enum Error {
#[error("Http Error {code}: {message}")]
Expand Down
17 changes: 17 additions & 0 deletions src/json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::Error;

/// Shortcut for [`serde_json::from_str`] with [`crate::Error`].
pub fn decode<T>(string: &str) -> Result<T, Error>
where
T: serde::de::DeserializeOwned,
{
serde_json::from_str(string).map_err(|error| Error::Decode(format!("{error:?} : {string:?}")))
}

/// Shortcut for [`serde_json::to_string`] with [`crate::Error`].
pub fn encode<T>(value: &T) -> Result<String, Error>
where
T: serde::ser::Serialize + std::fmt::Debug,
{
serde_json::to_string(value).map_err(|error| Error::Encode(format!("{error:?} : {value:?}")))
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ mod client_reqwest;
#[cfg(feature = "http-client")]
mod client_ureq;
mod error;
#[cfg(feature = "serde_json")]
mod json;
pub mod objects;
pub mod parameters;
mod parse_mode;
Expand Down

0 comments on commit 0e35c8d

Please sign in to comment.