Add Message send()

This commit is contained in:
bitfl0wer 2023-05-03 16:27:04 +02:00
parent 7938addf67
commit 6858b1eb0d
1 changed files with 37 additions and 8 deletions

View File

@ -1,24 +1,53 @@
pub mod messages { pub mod messages {
use reqwest::Client; use reqwest::{Client, Response};
use serde_json::to_string; use serde_json::to_string;
use crate::api::limits::Limits; use crate::api::limits::Limits;
use crate::api::types::Message; use crate::api::types::Message;
use crate::api::User;
use crate::errors::InstanceServerError;
use crate::instance::Instance; use crate::instance::Instance;
use crate::limit::LimitedRequester; use crate::limit::LimitedRequester;
impl Message { impl Message {
/**
Sends a message to the Spacebar server.
# Arguments
* `url_api` - The URL of the Spacebar server's API.
* `message` - The [`Message`] that will be sent to the Spacebar server.
* `limits_user` - The [`Limits`] of the user.
* `limits_instance` - The [`Limits`] of the instance.
* `requester` - The [`LimitedRequester`] that will be used to make requests to the Spacebar server.
# Errors
* [`InstanceServerError`] - If the message cannot be sent.
*/
pub async fn send( pub async fn send(
url_api: &String, url_api: &String,
message: &Message, message: &Message,
limits: &Limits, limits_user: &mut Limits,
limits_instance: &mut Limits,
requester: &mut LimitedRequester, requester: &mut LimitedRequester,
) { ) -> Result<Response, InstanceServerError> {
let client = Client::new() let request = Client::new()
.post(url_api) .post(format!(
.body(to_string(message).unwrap()) "{}/channels/{}/messages",
.send() url_api, message.channel_id
.await; ))
.body(to_string(message).unwrap());
match requester
.send_request(
request,
crate::api::limits::LimitType::Channel,
limits_instance,
limits_user,
)
.await
{
Ok(result) => return Ok(result),
Err(e) => return Err(e),
};
} }
} }
impl User {}
} }