Skip to main content

facebook_api_rs/graph/pages/
post.rs

1//! The Post API let you perform operation (get post, update) on individual post
2//! in a profile's feed.
3//!
4//! The profile could be a user, page, app, or group.;
5//! Note: This end point does not allow you to create post or delete.
6//! for more details check facebook documentation on post Api.
7//! <https://developers.facebook.com/docs/graph-api/reference/post>
8use crate::graph::pages::utils::{Fields, From, GetPostResponse};
9use crate::prelude::errors::ClientErr;
10use crate::prelude::utils::Id;
11use crate::prelude::{Data, HttpConnection};
12use serde::{Deserialize, Serialize};
13
14/// The Post API let  you perform operation on individual post in a profile's
15/// feed. The profile could be a user, page, app, or group. (get post, update);
16/// Note: This end point does not allow you to create post or delete.
17/// for more details check facebook documentation on post Api.
18/// <https://developers.facebook.com/docs/graph-api/reference/page-post/>
19pub struct PostApi {
20    base_url: String,
21    page_access_token: String,
22}
23
24impl PostApi {
25    /// This is a static method used to create an instance of the feedApi
26    /// Note: this method is called inside of the Client method
27    pub fn new(base_url: String, access_token: String) -> PostApi {
28        PostApi {
29            base_url,
30            page_access_token: access_token,
31        }
32    }
33
34    ///  this method sends a get request to the facebook api (GET
35    /// /v23.0/{post-id}). it returns the data of the post_id  you have
36    /// provided
37    pub async fn get(self) -> Result<GetPostResponse, ClientErr> {
38        let mut url = self.base_url.replace("EDGE", "?fields=");
39
40        let field_count = Fields::default().fields.len();
41        for (count, field) in Fields::default().fields.into_iter().enumerate() {
42            if count < field_count - 1 {
43                url = url + &field + ",";
44            } else {
45                url = url + &field; // remove the comma in the last filed
46            }
47        }
48        let base_url = url + "&access_token=" + &self.page_access_token;
49
50        let resp = HttpConnection::get::<GetPostResponse>(base_url, "".to_string()).await?;
51        Ok(resp)
52    }
53
54    // TODO: impliment post uodated methods and test newly added methods
55    pub async fn update() {}
56
57    pub async fn comments(self) -> Result<Comments, ClientErr> {
58        let base_url = self.base_url.replace("EDGE", "commnets");
59        let url = base_url + "&access_token=" + &self.page_access_token;
60        let resp = HttpConnection::get::<Comments>(url, "".to_string()).await?;
61        Ok(resp)
62    }
63
64    pub async fn comment_on_post(self, message: String) -> Result<Id, ClientErr> {
65        let base_url = self.base_url.replace("EDGE", "commnets");
66        let url = base_url + "?message=" + &message + "&access_token=" + &self.page_access_token;
67
68        let resp = HttpConnection::post::<Id, String>(url, "".to_string()).await?;
69        Ok(resp)
70    }
71
72    pub async fn delete(self) -> Result<Id, ClientErr> {
73        let base_url = self.base_url.replace("EDGE", "");
74        let url = base_url + "&access_token=" + &self.page_access_token;
75        let resp = HttpConnection::delete::<Id>(url, "".to_string()).await?;
76        Ok(resp)
77    }
78}
79
80#[derive(Deserialize, Debug, Serialize)]
81pub struct Comments {
82    data: Data<Comment>,
83}
84
85#[derive(Deserialize, Debug, Serialize)]
86pub struct Comment {
87    from: From,
88    created_time: String,
89    message: String,
90    id: String,
91}