From c2d46196c80ad3104c561b436cf9fc32f18bc380 Mon Sep 17 00:00:00 2001 From: Flori Weber Date: Thu, 15 Jun 2023 19:00:47 +0200 Subject: [PATCH] Implement send_friend_request --- src/api/users/relationships.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/api/users/relationships.rs b/src/api/users/relationships.rs index d286ef6..ede39f0 100644 --- a/src/api/users/relationships.rs +++ b/src/api/users/relationships.rs @@ -1,6 +1,12 @@ use reqwest::Client; +use serde_json::to_string; -use crate::{api::deserialize_response, errors::ChorusLibError, instance::UserMeta, types}; +use crate::{ + api::{deserialize_response, handle_request_as_option}, + errors::ChorusLibError, + instance::UserMeta, + types, +}; impl UserMeta { /// Retrieves the mutual relationships between the authenticated user and the specified user. @@ -30,4 +36,24 @@ impl UserMeta { ) .await } + + /// Sends a friend request to a user. + /// + /// # Arguments + /// + /// * `schema` - A [`FriendRequestSendSchema`] struct that holds the information about the friend request to be sent. + /// + /// # Returns + /// This function returns an [`Option`] that holds a [`ChorusLibError`] if the request fails. + pub async fn send_friend_request( + &mut self, + schema: types::FriendRequestSendSchema, + ) -> Option { + let belongs_to = self.belongs_to.borrow(); + let url = format!("{}/users/@me/relationships/", belongs_to.urls.get_api()); + drop(belongs_to); + let body = to_string(&schema).unwrap(); + let request = Client::new().post(url).bearer_auth(self.token()).body(body); + handle_request_as_option(request, self, crate::api::limits::LimitType::Global).await + } }