dimo_rust_sdk/graphql/
identity.rs

1use crate::utils::request::{make_request, RequestParams};
2use serde_json::Value;
3use std::collections::HashMap;
4use std::error::Error;
5
6pub struct Identity {
7    base_url: String,
8}
9
10impl Identity {
11    pub fn new(base_url: &str) -> Self {
12        Self {
13            base_url: base_url.to_string(),
14        }
15    }
16
17    pub async fn query(&self, query: &str) -> Result<Value, Box<dyn Error>> {
18        let mut body = HashMap::new();
19        body.insert("query".to_string(), Value::String(query.to_string()));
20
21        let params = RequestParams {
22            method: reqwest::Method::POST,
23            base_url: self.base_url.clone(),
24            path: "".to_string(),
25            query_params: None,
26            body: Some(body),
27            headers: None,
28        };
29
30        make_request(params).await
31    }
32
33    pub async fn count_dimo_vehicles(&self) -> Result<Value, Box<dyn Error>> {
34        let query = "
35        {
36            vehicles (first: 10) {
37                totalCount
38            }
39        }
40        ";
41        self.query(query).await
42    }
43
44    pub async fn list_vehicle_definitions_per_address(
45        &self,
46        address: &str,
47        limit: Option<i32>,
48    ) -> Result<Value, Box<dyn Error>> {
49        let limit = limit.unwrap_or(10);
50
51        let query = format!(
52            r#"
53            {{
54                vehicles(filterBy: {{owner: "{address}"}}, first: {limit}) {{
55                    nodes {{
56                        aftermarketDevice {{
57                            tokenId
58                            address
59                        }}
60                        syntheticDevice {{
61                            address
62                            tokenId
63                        }}
64                        definition {{
65                            make
66                            model
67                            year
68                        }}
69                    }}
70                }}
71            }}
72            "#,
73            address = address,
74            limit = limit
75        );
76
77        self.query(&query).await
78    }
79}