1use crate::{
2 models::{
3 common::SearchParams,
4 person::{
5 BulkEnrichPersonParams, BulkEnrichPersonResponse, BulkRetrievePersonParams,
6 BulkRetrievePersonResponse, EnrichPersonParams, EnrichPersonResponse,
7 IdentifyPersonParams, IdentifyPersonResponse, RetrievePersonParams,
8 RetrievePersonResponse, SearchPersonResponse,
9 },
10 },
11 PDLClient, PDLError,
12};
13
14pub(crate) static PERSON_ENRICH_PATH: &str = "/person/enrich";
15pub(crate) static PERSON_BULK_ENRICH_PATH: &str = "/person/bulk";
16pub(crate) static PERSON_IDENTIFY_PATH: &str = "/person/identify";
17pub(crate) static PERSON_SEARCH_PATH: &str = "/person/search";
18pub(crate) static PERSON_RETRIEVE_PATH: &str = "/person/retrieve/";
19pub(crate) static PERSON_BULK_RETRIEVE_PATH: &str = "/person/retrieve/bulk";
20
21pub struct Person {
22 pub client: PDLClient,
23}
24
25impl Person {
26 pub fn enrich(&self, params: EnrichPersonParams) -> Result<EnrichPersonResponse, PDLError> {
27 params.validate()?;
28 self.client
29 .get::<EnrichPersonResponse, EnrichPersonParams>(PERSON_ENRICH_PATH, params)
30 }
31
32 pub fn bulk_enrich(
33 &self,
34 params: BulkEnrichPersonParams,
35 ) -> Result<Vec<BulkEnrichPersonResponse>, PDLError> {
36 self.client
37 .post::<Vec<BulkEnrichPersonResponse>, BulkEnrichPersonParams>(
38 PERSON_BULK_ENRICH_PATH,
39 params,
40 )
41 }
42
43 pub fn identify(
44 &self,
45 params: IdentifyPersonParams,
46 ) -> Result<IdentifyPersonResponse, PDLError> {
47 params.validate()?;
48 self.client
49 .get::<IdentifyPersonResponse, IdentifyPersonParams>(PERSON_IDENTIFY_PATH, params)
50 }
51
52 pub fn search(&self, params: SearchParams) -> Result<SearchPersonResponse, PDLError> {
53 params.validate()?;
54 self.client
55 .get::<SearchPersonResponse, SearchParams>(PERSON_SEARCH_PATH, params)
56 }
57
58 pub fn retrieve(
59 &self,
60 params: RetrievePersonParams,
61 ) -> Result<RetrievePersonResponse, PDLError> {
62 params.validate()?;
63 let url = PERSON_RETRIEVE_PATH.to_string() + ¶ms.person_id;
64 self.client
65 .get::<RetrievePersonResponse, RetrievePersonParams>(&url, params)
66 }
67
68 pub fn bulk_retrieve(
69 &self,
70 params: BulkRetrievePersonParams,
71 ) -> Result<Vec<BulkRetrievePersonResponse>, PDLError> {
72 self.client
73 .post::<Vec<BulkRetrievePersonResponse>, BulkRetrievePersonParams>(
74 PERSON_BULK_RETRIEVE_PATH,
75 params,
76 )
77 }
78}
79
80#[cfg(test)]
81mod tests {
82 use crate::{
83 client::{PDLCLientOptions, PDLClient},
84 models::common::AdditionalParams,
85 BaseParams, BulkEnrichPersonParams, BulkEnrichSinglePersonParams, BulkRetrievePersonParams,
86 BulkRetrieveSinglePersonParams, EnrichPersonParams, IdentifyPersonParams, PersonParams,
87 RetrievePersonParams, SearchBaseParams, SearchParams,
88 };
89
90 use super::Person;
91
92 #[test]
93 fn test_person_enrich() {
94 let api_key = std::env::var("PDL_API_KEY").unwrap();
95 let client = PDLClient::new(&api_key).build();
96
97 let person = Person { client };
98
99 let mut base_params = BaseParams::default();
100 base_params.pretty = Some(true);
101
102 let mut person_params = PersonParams::default();
103 person_params.profile = Some(vec!["http://linkedin.com/in/seanthorne".to_string()]);
104
105 let enrich_person_params = EnrichPersonParams {
106 base_params: Some(base_params),
107 person_params,
108 additional_params: None,
109 };
110
111 let resp = person.enrich(enrich_person_params).expect("ERROR");
112
113 assert_eq!(resp.status, 200);
114 assert_eq!(
115 resp.data.twitter_url,
116 Some("twitter.com/seanthorne5".to_string())
117 );
118 }
119
120 #[test]
121 fn test_person_enrich_sandbox() {
122 let api_key = std::env::var("PDL_API_KEY").unwrap();
123 let mut client_options = PDLCLientOptions::default();
124 client_options.sandbox = true;
125 let client = PDLClient::new(&api_key).options(client_options).build();
126
127 let person = Person { client };
128
129 let mut base_params = BaseParams::default();
130 base_params.pretty = Some(true);
131
132 let mut person_params = PersonParams::default();
133 person_params.email = Some(vec!["reneewillis74@aol.com".to_string()]);
134
135 let mut additional_params = AdditionalParams::default();
136 additional_params.min_likelihood = Some(6);
137
138 let enrich_person_params = EnrichPersonParams {
139 base_params: Some(base_params),
140 person_params,
141 additional_params: Some(additional_params),
142 };
143
144 let resp = person.enrich(enrich_person_params).expect("ERROR");
145
146 assert_eq!(resp.status, 200);
147 assert_eq!(
148 resp.data.twitter_url,
149 Some("twitter.com/rc1994".to_string())
150 );
151 }
152
153 #[test]
154 fn test_person_bulk_enrich() {
155 let api_key = std::env::var("PDL_API_KEY").unwrap();
156 let client = PDLClient::new(&api_key).build();
157
158 let person = Person { client };
159
160 let mut base_params = BaseParams::default();
161 base_params.pretty = Some(true);
162
163 let mut person_params_1 = PersonParams::default();
164 person_params_1.profile = Some(vec!["linkedin.com/in/seanthorne".to_string()]);
165 let mut person_params_2 = PersonParams::default();
166 person_params_2.profile =
167 Some(vec!["https://www.linkedin.com/in/haydenconrad/".to_string()]);
168
169 let mut additional_params = AdditionalParams::default();
170 additional_params.min_likelihood = Some(6);
171
172 let bulk_enrich_single_person_params_1 = BulkEnrichSinglePersonParams {
173 params: person_params_1,
174 metadata: None,
175 };
176
177 let bulk_enrich_single_person_params_2 = BulkEnrichSinglePersonParams {
178 params: person_params_2,
179 metadata: None,
180 };
181
182 let bulk_enrich_params = BulkEnrichPersonParams {
183 requires: None,
184 requests: vec![
185 bulk_enrich_single_person_params_1,
186 bulk_enrich_single_person_params_2,
187 ],
188 };
189
190 let resp = person.bulk_enrich(bulk_enrich_params).expect("ERROR");
191
192 assert_eq!(resp[0].status, 200);
193 assert_eq!(resp[1].status, 200);
194 }
195
196 #[test]
197 fn test_person_identify() {
198 let api_key = std::env::var("PDL_API_KEY").unwrap();
199 let client = PDLClient::new(&api_key).build();
200
201 let person = Person { client };
202
203 let mut base_params = BaseParams::default();
204 base_params.pretty = Some(true);
205
206 let mut person_params = PersonParams::default();
207 person_params.first_name = Some(vec!["sean".to_string()]);
208 person_params.last_name = Some(vec!["thorne".to_string()]);
209 person_params.company = Some(vec!["people data labs".to_string()]);
210
211 let indentify_person_params = IdentifyPersonParams {
212 base_params: Some(base_params),
213 person_params,
214 additional_params: None,
215 };
216
217 let resp = person.identify(indentify_person_params).expect("ERROR");
218
219 assert_eq!(resp.status, 200);
220 assert!(resp.matches.len() >= 1);
221 }
222
223 #[test]
224 fn test_person_identify_sandbox() {
225 let api_key = std::env::var("PDL_API_KEY").unwrap();
226 let mut client_options = PDLCLientOptions::default();
227 client_options.sandbox = true;
228 let client = PDLClient::new(&api_key).options(client_options).build();
229
230 let person = Person { client };
231
232 let mut base_params = BaseParams::default();
233 base_params.pretty = Some(true);
234
235 let mut person_params = PersonParams::default();
236 person_params.company = Some(vec!["adams group".to_string()]);
237
238 let indentify_person_params = IdentifyPersonParams {
239 base_params: Some(base_params),
240 person_params,
241 additional_params: None,
242 };
243
244 let resp = person.identify(indentify_person_params).expect("ERROR");
245
246 assert_eq!(resp.status, 200);
247 assert!(resp.matches.len() >= 1);
248 }
249
250 #[test]
251 fn test_person_search() {
252 let api_key = std::env::var("PDL_API_KEY").unwrap();
253 let client = PDLClient::new(&api_key).build();
254
255 let person = Person { client };
256
257 let mut base_params = BaseParams::default();
258 base_params.pretty = Some(true);
259
260 let mut search_base_params = SearchBaseParams::default();
261 search_base_params.sql = Some("SELECT * FROM person WHERE location_country='mexico' AND job_title_role='health' AND phone_numbers IS NOT NULL;".to_string());
262
263 let search_params = SearchParams {
264 base_params: Some(base_params),
265 search_base_params,
266 additional_params: None,
267 };
268
269 let resp = person.search(search_params).expect("ERROR");
270
271 assert_eq!(resp.status, 200);
272 assert_eq!(resp.data.unwrap().len(), 1);
273 }
274
275 #[test]
276 fn test_person_search_sandbox() {
277 let api_key = std::env::var("PDL_API_KEY").unwrap();
278 let mut client_options = PDLCLientOptions::default();
279 client_options.sandbox = true;
280 let client = PDLClient::new(&api_key).options(client_options).build();
281
282 let person = Person { client };
283
284 let num_results: usize = 3;
285
286 let mut base_params = BaseParams::default();
287 base_params.size = Some(num_results as i32);
288
289 let mut search_base_params = SearchBaseParams::default();
290 search_base_params.sql =
291 Some("SELECT * FROM person WHERE location_country='united states';".to_string());
292
293 let search_params = SearchParams {
294 base_params: Some(base_params),
295 search_base_params,
296 additional_params: None,
297 };
298
299 let resp = person.search(search_params).expect("ERROR");
300
301 assert_eq!(resp.status, 200);
302 assert_eq!(resp.data.unwrap().len(), num_results);
303 assert_eq!(resp.scroll_token.is_some() && !resp.scroll_token.as_ref().unwrap().is_empty(), true);
304 }
305
306 #[test]
307 fn test_person_retrieve() {
308 let api_key = std::env::var("PDL_API_KEY").unwrap();
309 let client = PDLClient::new(&api_key).build();
310
311 let person = Person { client };
312
313 let mut base_params = BaseParams::default();
314 base_params.pretty = Some(true);
315
316 let person_id = "qEnOZ5Oh0poWnQ1luFBfVw_0000".to_string();
317
318 let retrieve_person_params = RetrievePersonParams {
319 base_params: Some(base_params),
320 person_id: person_id.clone(),
321 };
322
323 let resp = person.retrieve(retrieve_person_params).expect("ERROR");
324
325 assert_eq!(resp.status, 200);
326 assert_eq!(resp.data.id, Some(person_id));
327 assert_eq!(resp.data.full_name, Some("sean thorne".to_string()));
328 }
329
330 #[test]
331 fn test_person_bulk_retrive() {
332 let api_key = std::env::var("PDL_API_KEY").unwrap();
333 let client = PDLClient::new(&api_key).build();
334
335 let person = Person { client };
336
337 let mut base_params = BaseParams::default();
338 base_params.pretty = Some(true);
339
340 let person_id_1 = "qEnOZ5Oh0poWnQ1luFBfVw_0000".to_string();
341 let person_id_2 = "9Grd31hT3RFKVzsyecBGPg_0000".to_string();
342
343 let bulk_rerieve_singe_person_param_1 = BulkRetrieveSinglePersonParams {
344 id: person_id_1,
345 metadata: None,
346 };
347
348 let bulk_rerieve_singe_person_param_2 = BulkRetrieveSinglePersonParams {
349 id: person_id_2,
350 metadata: None,
351 };
352
353 let bulk_retrieve_person_params = BulkRetrievePersonParams {
354 base_params: Some(base_params),
355 requests: vec![
356 bulk_rerieve_singe_person_param_1,
357 bulk_rerieve_singe_person_param_2,
358 ],
359 additional_params: None,
360 };
361
362 let resp = person
363 .bulk_retrieve(bulk_retrieve_person_params)
364 .expect("ERROR");
365 assert_eq!(resp[0].status, 200);
366 assert_eq!(resp[1].status, 200);
367 }
368}