use crate::prelude::errors::ClientErr;
use crate::prelude::search::Location;
use crate::prelude::structs::Engagement;
use crate::prelude::{HttpConnection, InstagramAccount};
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Debug, Clone, Default)]
pub struct PageAccount {
access_token: String,
category: String,
category_list: Vec<ListDetails>,
name: String,
about: Option<String>,
id: String,
business: String,
can_post: bool,
cover: CoverPage,
emails: Vec<String>,
engagement: Engagement,
fan_count: u32,
followers_count: u32,
connected_page_backed_instagram_account: InstagramAccount,
instagram_business_account: InstagramAccount,
pub location: Location,
phone: String,
rating_count: u32,
tasks: Vec<String>,
}
#[derive(Deserialize, Debug, Clone, Default)]
pub struct Account {
#[serde(default)]
access_token: String,
category: String,
category_list: Vec<ListDetails>,
name: String,
id: String,
tasks: Vec<String>,
}
#[derive(Deserialize, Debug, Clone, Default, Serialize)]
pub struct ListDetails {
pub id: String,
pub name: String,
}
#[derive(Deserialize, Debug, Clone, Default)]
pub struct Accounts {
pub data: Vec<Account>,
}
impl Account {
pub fn access_token(&self) -> &String {
&self.access_token
}
pub fn category(&self) -> &String {
&self.category
}
pub fn category_list(&self) -> &Vec<ListDetails> {
&self.category_list
}
pub fn name(&self) -> &String {
&self.name
}
pub fn id(&self) -> &String {
&self.id
}
pub fn tasks(&self) -> &Vec<String> {
&self.tasks
}
}
impl PageAccount {
pub fn access_token(&self) -> &String {
&self.access_token
}
pub fn category(&self) -> &String {
&self.category
}
pub fn category_list(&self) -> &Vec<ListDetails> {
&self.category_list
}
pub fn name(&self) -> &String {
&self.name
}
pub fn id(&self) -> &String {
&self.id
}
pub fn tasks(&self) -> &Vec<String> {
&self.tasks
}
pub fn about(&self) -> &Option<String> {
&self.about
}
pub fn business(&self) -> &str {
&self.business
}
pub fn can_post(&self) -> bool {
self.can_post
}
pub fn cover(&self) -> &CoverPage {
&self.cover
}
pub fn emails(&self) -> &Vec<String> {
&self.emails
}
pub fn engagement(&self) -> &Engagement {
&self.engagement
}
pub fn fan_count(&self) -> u32 {
self.fan_count
}
pub fn followers_count(&self) -> u32 {
self.followers_count
}
pub fn connected_page_backed_instagram_account(&self) -> &InstagramAccount {
&self.connected_page_backed_instagram_account
}
pub fn instagram_business_account(&self) -> &InstagramAccount {
&self.instagram_business_account
}
pub fn phone(&self) -> &str {
&self.phone
}
pub fn rating_count(&self) -> u32 {
self.rating_count
}
}
pub struct AccountsAPI {
url: String,
}
impl AccountsAPI {
pub fn new(base_url: String) -> AccountsAPI {
AccountsAPI {
url: base_url.replace("EDGE", "accounts"),
}
}
pub async fn get(&self) -> Result<Accounts, ClientErr> {
let resp = HttpConnection::get::<Accounts>(self.url.to_string(), "".to_string()).await?;
Ok(resp)
}
}
#[derive(Deserialize, Debug, Clone, Default, Serialize)]
pub struct CoverPage {
id: String,
source: String,
}
impl CoverPage {
pub fn id(&self) -> &str {
&self.id
}
pub fn source(&self) -> &str {
&self.source
}
}
#[cfg(test)]
mod test {
use super::*;
use serde_json::Value;
#[test]
fn test_object() {
let data = r#"{
"data": [
{
"access_token": "Dummy",
"category": "Musician/Band",
"category_list": [
{
"id": "ID",
"name": "Musician/Band"
}
],
"name": "business_name",
"id": "12345",
"tasks": [
]
}
]
}"#;
let v: Accounts = serde_json::from_str(data).unwrap();
assert_eq!(v.data.first().unwrap().name, "business_name".to_string());
}
}