use crate::graph::pages::utils::{Fields, GetPostResponse};
use crate::prelude::errors::ClientErr;
use crate::prelude::{Data, HttpConnection};
use serde::{Deserialize, Serialize};
pub struct FeedApi {
base_url: String,
page_access_token: String,
}
impl FeedApi {
pub fn new(base_url: String, access_token: String) -> FeedApi {
FeedApi {
base_url,
page_access_token: access_token,
}
}
pub async fn post(&self, post_params: FeedPostFields) -> Result<FeedPostSuccess, ClientErr> {
let base_url = self.base_url.replace("EDGE", "feed");
let mut url = base_url
+ "?message="
+ &post_params.message
+ "&access_token="
+ &self.page_access_token;
if post_params.call_to_action.is_some() {
let type_ = "type:".to_owned()
+ format!("{:?}", &post_params.call_to_action.as_ref().unwrap().type_).as_str();
let link = "Link:".to_owned() + &post_params.call_to_action.unwrap().link;
let value = "value:{".to_owned() + &link + "}";
let built_url = "{".to_owned() + &type_ + "," + &value + "}";
url = "&call_to_action=".to_owned() + &built_url;
};
if !post_params.place.is_empty() {
if !post_params.tags.is_empty() {
let tag_list = build_tags(post_params.tags);
url = url + "&place=" + &post_params.place + "&tags=" + &tag_list;
} else {
url = url + "&place=" + &post_params.place
}
}
let resp = HttpConnection::get::<FeedPostSuccess>(url, "".to_string()).await?;
Ok(resp)
}
pub async fn get(self) -> Result<FeedData, ClientErr> {
let mut base_url = self.base_url.replace("EDGE", "feed");
base_url = "?fields=".to_string();
let field_count = Fields::default().fields.len();
for (count, field) in Fields::default().fields.into_iter().enumerate() {
if count < field_count - 1 {
base_url = base_url + &field + ",";
} else {
base_url = base_url + &field; }
}
let url = base_url + "&access_token=" + &self.page_access_token;
let resp = HttpConnection::get::<FeedData>(url, "".to_string()).await?;
Ok(resp)
}
}
#[derive(Deserialize, Serialize, Clone)]
pub struct FeedPostSuccess {
pub id: String,
}
#[derive(Deserialize, Debug, Default, Serialize)]
pub struct MediaPostSuccess {
id: String,
post_id: String,
}
#[derive(Deserialize, Debug, Serialize)]
pub struct FeedData {
data: Data<GetPostResponse>,
}
pub struct FeedPostFields {
pub link: String,
pub message: String,
pub tags: Vec<String>,
pub place: String,
pub call_to_action: Option<CallToAction>,
pub feeling: Option<Feeling>,
}
#[derive(Debug)]
pub struct CallToAction {
link: String,
type_: CallToActionType,
}
#[derive(Deserialize, Debug, Serialize)]
#[allow(warnings)]
pub enum CallToActionType {
BOOK_TRAVEL,
BUY_NOW,
CALL_NOW,
DOWNLOAD,
GET_DIRECTIONS,
GET_QUOTE,
INSTALL_APP,
INSTALL_MOBILE_APP,
LEARN_MORE,
LIKE_PAGE,
LISTEN_MUSIC,
MESSAGE_PAGE,
NO_BUTTON,
OPEN_LINK,
PLAY_GAME,
SHOP_NOW,
SIGN_UP,
SUBSCRIBE,
USE_APP,
USE_MOBIL,
WATCH_MOR,
WATCH_VID, }
pub struct Feeling {
og_action_type_id: String,
og_icon_id: String,
og_object_id: String,
}
fn build_tags(list: Vec<String>) -> String {
let mut url = "".to_string();
for value in list {
url = format!("{}{},", url, value)
}
url
}