dimo_rust_sdk/graphql/
identity.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use crate::utils::request::{make_request, RequestParams};
use serde_json::Value;
use std::collections::HashMap;
use std::error::Error;

pub struct Identity {
    base_url: String,
}

impl Identity {
    pub fn new(base_url: &str) -> Self {
        Self {
            base_url: base_url.to_string(),
        }
    }

    pub async fn query(&self, query: &str) -> Result<Value, Box<dyn Error>> {
        let mut body = HashMap::new();
        body.insert("query".to_string(), Value::String(query.to_string()));

        let params = RequestParams {
            method: reqwest::Method::POST,
            base_url: self.base_url.clone(),
            path: "".to_string(),
            query_params: None,
            body: Some(body),
            headers: None,
        };

        make_request(params).await
    }

    pub async fn count_dimo_vehicles(&self) -> Result<Value, Box<dyn Error>> {
        let query = "
        {
            vehicles (first: 10) {
                totalCount
            }
        }
        ";
        self.query(query).await
    }

    pub async fn list_vehicle_definitions_per_address(
        &self,
        address: &str,
        limit: Option<i32>,
    ) -> Result<Value, Box<dyn Error>> {
        let limit = limit.unwrap_or(10);

        let query = format!(
            r#"
            {{
                vehicles(filterBy: {{owner: "{address}"}}, first: {limit}) {{
                    nodes {{
                        aftermarketDevice {{
                            tokenId
                            address
                        }}
                        syntheticDevice {{
                            address
                            tokenId
                        }}
                        definition {{
                            make
                            model
                            year
                        }}
                    }}
                }}
            }}
            "#,
            address = address,
            limit = limit
        );

        self.query(&query).await
    }
}