hubspot_rust_sdk/objects/
get.rs1use serde::Serialize;
2use serde_json::{json, Value};
3use crate::universals::{client::HubSpotClient, pagination::TurnPageMethod, requests::HttpMethod, utils::to_array};
4use super::types::{HubSpotObject, HubSpotObjectType};
5
6#[derive(Debug, Serialize)]
7pub struct GetBatchInput {
8 pub id: String
9}
10
11impl HubSpotClient {
12 pub async fn get(
15 &self,
16 object_type: HubSpotObjectType,
17 object_id: &str,
18 properties: Vec<&str>,
19 associations: Vec<&str>,
20 ) -> Result<HubSpotObject, String> {
21 HubSpotObject::from_value( self.request(
22 &format!(
23 "/crm/v3/objects/{object_type}/{object_id}{}",
24 query_params_to_string(properties, associations)
25 ),
26 &HttpMethod::Get,
27 None
28 ).await? )
29 }
30
31 pub async fn get_batch(
32 &self,
33 object_type: HubSpotObjectType,
34 ids: Vec<&str>,
35 properties: Vec<&str>
36 ) -> Result<Vec<HubSpotObject>, String> {
37 let inputs = ids
38 .iter()
39 .map(|id| GetBatchInput { id: id.to_string() })
40 .collect::<Vec<GetBatchInput>>();
41
42 let objects = self.request(
43 &format!("/crm/v3/objects/{object_type}/batch/read"),
44 &HttpMethod::Post,
45 Some(json!({
46 "properties": properties,
47 "inputs": inputs
48 }))
49 ).await?;
50
51 return to_array(&objects["results"])?
52 .into_iter()
53 .map(|v| HubSpotObject::from_value(v))
54 .collect::<Result<Vec<HubSpotObject>, String>>()
55 }
56
57 pub async fn get_many(
58 &self,
59 object_type: HubSpotObjectType,
60 properties: Vec<&str>,
61 associations: Vec<&str>,
62 max_amount: Option<usize>,
63 ) -> Result<Vec<HubSpotObject>, String> {
64 self.request_with_pagination(
65 format!("/crm/v3/objects/{object_type}{}", query_params_to_string(properties, associations)),
66 HttpMethod::Get,
67 None,
68 max_amount,
69 next_url
70 ).await
71 }
72}
73
74fn query_params_to_string(properties: Vec<&str>, associations: Vec<&str>) -> String {
75 if properties.is_empty() && associations.is_empty() {
76 return "".to_string();
77 }
78
79 let mut query_params = String::new();
80 query_params.push('?');
81
82 let mut first_param = true; if !properties.is_empty() {
85 push_params(&mut query_params, &properties, "properties");
86 first_param = false;
87 }
88
89 if !associations.is_empty() {
90 if !first_param {
91 query_params.push('&');
92 }
93 push_params(&mut query_params, &associations, "associations");
94 }
95
96 query_params
97}
98
99fn push_params(query_params: &mut String, params: &[&str], params_name: &str) {
100 query_params.push_str(&format!("{}={}", params_name, params.join(",")));
101}
102
103
104pub fn next_url(body: &Value) -> Option<TurnPageMethod> {
105 match body["paging"]["next"]["link"].as_str() {
106 Some(link) => Some(TurnPageMethod::NextUrl(link.to_string())),
107 None => None,
108 }
109}