1+ //! This Api is used to get instagram business account for any given facebook page
2+ //! <https://developers.facebook.com/docs/instagram-api/reference/page>
3+
14use seed:: fetch:: fetch;
25use seed:: prelude:: { Method , Request } ;
3-
46use seed:: { prelude:: * , * } ;
57use serde:: { Deserialize , Serialize } ;
8+ use seed:: prelude:: js_sys:: encode_uri;
9+ use urlencoding:: encode;
610
711#[ derive( Deserialize , Clone , Debug , Serialize ) ]
812pub struct InstaAccount {
@@ -16,11 +20,13 @@ pub struct InstaAccountId {
1620 pub id : String ,
1721}
1822
23+ #[ derive( Clone ) ]
1924pub struct InstagramApi {
2025 page_access_token : String ,
2126 base_url : String ,
2227}
2328
29+
2430impl InstagramApi {
2531 pub fn new ( page_access_token : String , base_url : String ) -> InstagramApi {
2632 InstagramApi {
@@ -29,7 +35,9 @@ impl InstagramApi {
2935 }
3036 }
3137
32- pub async fn insta_account ( self ) -> seed:: fetch:: Result < InstaAccount > {
38+ /// This method is use to get instagram business account id.
39+ /// for reference check <https://developers.facebook.com/docs/instagram-api/reference/page>
40+ pub async fn account_id ( self ) -> seed:: fetch:: Result < InstaAccount > {
3341 let base_url = self . base_url . replace ( "EDGE" , "?" ) ;
3442 let url = base_url
3543 + "fields=instagram_business_account"
@@ -39,4 +47,96 @@ impl InstagramApi {
3947 let request = Request :: new ( url) . method ( Method :: Get ) ;
4048 fetch ( request) . await ?. json :: < InstaAccount > ( ) . await
4149 }
50+
51+ /// This method is used to get instagram business account with its details (name, user, id ,etc).
52+ /// It accepts the instagram page id.
53+ /// for reference check <https://developers.facebook.com/docs/instagram-api/reference/ig-user>
54+ pub async fn account_details ( self ) -> seed:: fetch:: Result < InstagramAccount > {
55+ let mut url = self . base_url . replace ( "EDGE" , "?" ) ;
56+ let url_fields = Fields :: default ( ) . build_url_with_fields ( ) ; // build urlenconded url withe regired fields
57+
58+ let mut request_url = url
59+ + "fields="
60+ +url_fields. as_str ( )
61+ + "&access_token="
62+ + & self . page_access_token ;
63+ let request = Request :: new ( request_url) . method ( Method :: Get ) ;
64+ fetch ( request) . await ?. json :: < InstagramAccount > ( ) . await
65+ }
4266}
67+
68+
69+ #[ derive( Deserialize , Clone , Debug , Serialize ) ]
70+ pub struct InstagramAccount {
71+ //https://developers.facebook.com/docs/instagram-api/reference/ig-user/
72+ //This is a public fields, which means it can be returned if available.
73+ // biography: String,
74+
75+ ///This is a public fields, which means it can be returned if available.
76+ pub id : String ,
77+
78+ ///This is not a public fields, which means it may be returned depending on the user setting.
79+ pub ig_id : u32 ,
80+
81+ ///This is a public fields, which means it can be returned if available.
82+ pub followers_count : u32 ,
83+
84+ ///This is not a public fields, which means it may be returned depending on the user setting.
85+ pub follows_count : u32 ,
86+
87+ ///This is not a public fields, which means it may be returned depending on the user setting.
88+ pub media_count : u32 ,
89+
90+ ///This is not a public fields, which means it may be returned depending on the user setting.
91+ pub name : String ,
92+
93+ //This is not a public fields, which means it may be returned depending on the user setting.
94+ // profile_picture_url: String,
95+
96+ ///This is not a public fields, which means it may be returned depending on the user setting.
97+ pub username : String ,
98+
99+ //This is not a public fields, which means it may be returned depending on the user setting.
100+ // website : String,
101+ }
102+ pub struct Fields {
103+ pub ( crate ) fields : Vec < String > ,
104+ }
105+
106+ impl Default for Fields {
107+ /// These parameters are used as fields which are passed in as a query
108+ /// parameters to the get post request and feeds request
109+ fn default ( ) -> Self {
110+ let field_list = vec ! [
111+ "biography" ,
112+ "id" ,
113+ "ig_id" ,
114+ "followers_count" ,
115+ "follows_count" ,
116+ "media_count" ,
117+ "name" ,
118+ "profile_picture_url" ,
119+ "username" ,
120+ "website" ,
121+ ] ;
122+
123+ let fields = field_list. iter ( ) . map ( |& field| field. into ( ) ) . collect ( ) ;
124+ Self { fields }
125+ }
126+ }
127+
128+ impl Fields {
129+ pub fn build_url_with_fields ( self ) -> String {
130+ let mut url_fields= "" . to_string ( ) ;
131+ let fields_count = Fields :: default ( ) . fields . len ( ) ;
132+ for ( count, field) in Fields :: default ( ) . fields . into_iter ( ) . enumerate ( ) {
133+ if count < fields_count - 1 {
134+ url_fields = url_fields+ & field + "," ;
135+ } else {
136+ url_fields= url_fields+ & field; // remove the comma in the last filed
137+ }
138+ }
139+ url_fields = String :: from ( encode ( url_fields. as_str ( ) ) ) ; // encode the url
140+ url_fields
141+ }
142+ }
0 commit comments