lens_client/lens/
mod.rs

1pub mod auth;
2pub mod follow;
3pub mod profile;
4
5pub struct LensClient {
6    pub queries: crate::graphql::queries::Queries,
7    pub endpoint: String,
8    pub chain: crate::Chain,
9    pub net: crate::Net,
10    pub access_token: Option<String>,
11    pub refresh_token: Option<String>,
12}
13
14/// Constructs a new LensClient
15impl LensClient {
16    /// Create a new LensClient
17    /// # Arguments
18    /// * `chain` - The chain of the Lens server
19    /// * `net` - The net of the Lens server
20    /// # Return
21    /// * `LensClient` - The new LensClient
22    pub fn new(chain: crate::Chain, net: crate::Net) -> LensClient {
23        match chain {
24            crate::Chain::Polygon => match net {
25                crate::Net::Mumbai => LensClient {
26                    queries: crate::graphql::queries::Queries::new(),
27                    endpoint: String::from("https://api-mumbai.lens.dev"),
28                    chain: chain,
29                    net: net,
30                    access_token: None,
31                    refresh_token: None,
32                },
33                crate::Net::Main => LensClient {
34                    queries: crate::graphql::queries::Queries::new(),
35                    endpoint: String::from("https://api.lens.dev"),
36                    chain: chain,
37                    net: net,
38                    access_token: None,
39                    refresh_token: None,
40                },
41            },
42        }
43    }
44
45    /// Get the profiles of a user by address
46    /// # Arguments
47    /// * `address` - The address of the user
48    /// # Returns
49    /// * `Result<profile::default::Profile, String>` - The default profile of the user
50    pub fn get_default_profile_by_address(
51        &self,
52        address: String,
53    ) -> Result<profile::default::Profile, String> {
54        let profile = crate::methods::profile::get_default_profile_by_address(self, address);
55        profile
56    }
57
58    /// Get the profiles of a user by address
59    /// # Arguments
60    /// * `address` - The address of the user
61    /// # Returns
62    /// * `Result<profile::AddressProfiles, String>` - The profiles of the user
63    pub fn get_profiles_by_address(
64        &self,
65        address: String,
66    ) -> Result<profile::AddressProfiles, String> {
67        let profile = crate::methods::profile::get_profiles_by_address(self, address);
68        profile
69    }
70
71    /// Get status of follow of a address to a profile id
72    /// # Arguments
73    /// * `address` - The address of the user
74    /// * `followee` - The profile id of the followee
75    /// # Returns
76    /// * `Result<follow::DFollow, String>` - The follow status of the user
77    pub fn does_follow(
78        &self,
79        address: String,
80        followee: String,
81    ) -> Result<follow::DFollow, String> {
82        let follow = crate::methods::follow::does_follow(self, address, followee);
83        follow
84    }
85
86    /// Get the followees of a user by address
87    /// # Arguments
88    /// * `address` - The address of the user
89    /// * `limit` - The amount of the returned followees
90    /// # Returns
91    /// * `Result<follow::following::FollowingData, String>` - The followees of the user
92    pub fn get_following(
93        &self,
94        address: String,
95        limit: u64,
96    ) -> Result<follow::following::FollowingData, String> {
97        let follow = crate::methods::follow::get_following(self, address, limit);
98        follow
99    }
100
101    /// Get the followers of a user by address
102    /// # Arguments
103    /// * `profile_id` - The profile id of the user
104    /// * `limit` - The amount of the returned followers
105    /// # Returns
106    /// * `Result<follow::followers::FollowersData, String>` - The followers of the user
107    pub fn get_followers(
108        &self,
109        profile_id: String,
110        limit: u64,
111    ) -> Result<follow::followers::FollowersData, String> {
112        let follow = crate::methods::follow::get_followers_by_profile_id(self, profile_id, limit);
113        follow
114    }
115
116    /// Retrieve the callenge to sign to login to Lens
117    /// # Arguments
118    /// * `address` - The address of the user
119    /// # Returns
120    /// * `Result<auth::challenge::Challenge, String>` - The challenge to sign to login to Lens
121    pub fn challenge(&self, address: String) -> Result<auth::AuthChallenge, String> {
122        let auth = crate::methods::auth::challenge(self, address);
123        auth
124    }
125
126    /// Sign to login to Lens
127    /// # Arguments
128    /// * `address` - The address of the user
129    /// * `signature` - The signature of the challenge to login to Lens
130    /// # Returns
131    /// * `Result<auth::login::Login, String>` - The auth token to login to Lens
132    pub fn login(
133        &mut self,
134        address: String,
135        signature: String,
136    ) -> Result<auth::login::Login, String> {
137        let auth = crate::methods::auth::login(self, address, signature);
138        self.access_token = Some(auth.clone().unwrap().data.authenticate.access_token);
139        self.refresh_token = Some(auth.clone().unwrap().data.authenticate.refresh_token);
140
141        auth
142    }
143
144    /// Create a Lens profile
145    /// # Arguments
146    /// * `handle` - The handle of the user
147    /// # Returns
148    /// * `Result<crate::lens::profile::create::CreateProfileData, String>` - Data with transaction hash of profile creation
149    pub fn create_profile(
150        &self,
151        handle: String,
152    ) -> Result<crate::lens::profile::create::CreateProfileData, String> {
153        let created_profile = crate::methods::profile::create_profile(
154            self,
155            handle,
156            crate::lens::follow::FollowModule {
157                follow_type: crate::lens::follow::FollowModuleType::Free,
158                currency: None,
159                value: None,
160                recipient: None,
161            },
162        );
163        created_profile
164    }
165
166    pub fn make_request(
167        &self,
168        q: crate::graphql::Query,
169        headers: Option<Vec<serde_json::Value>>,
170    ) -> Result<surf::Response, Option<String>> {
171        let mut res = Err(None);
172        let u = self.endpoint.clone();
173        async_std::task::block_on(async {
174            let mut p = surf::post(u);
175            if headers.is_some() {
176                let hh = headers.unwrap();
177                for h in hh {
178                    p = p.header(h["k"].as_str().unwrap(), h["v"].as_str().unwrap());
179                }
180            }
181            let request = p.body_json(&q).unwrap();
182            let rr = request.send().await.unwrap();
183            res = Ok(rr);
184        });
185        res
186    }
187}