1pub mod environments;
2pub mod graphql;
3pub mod rest;
4pub mod utils;
5
6pub use serde_json::Value;
7pub use utils::credentials::get_credentials;
8use environments::dimo_environment;
9use graphql::{Identity, Telemetry};
10use rest::{
11 attestation::AttestationClient,
12 auth::{AccessToken, AuthClient},
13 devicedefinitions::DeviceDefinitions,
14 devices::Devices,
15 tokenexchange::TokenExchange,
16 trips::Trips,
17 valuations::Valuations,
18};
19
20#[derive(Clone, Copy)]
21pub enum Environment {
22 Production,
23 Dev,
24}
25
26pub struct DIMO {
27 pub attestation: AttestationClient,
28 pub auth: AuthClient,
29 pub devicedefinitions: DeviceDefinitions,
30 pub devices: Devices,
31 pub tokenexchange: TokenExchange,
32 pub trips: Trips,
33 pub valuations: Valuations,
34 pub identity: Identity,
35 pub telemetry: Telemetry,
36}
37
38impl DIMO {
39 pub fn new(env: Environment) -> Self {
40 let routes = match env {
41 Environment::Production => dimo_environment::PRODUCTION.routes,
42 Environment::Dev => dimo_environment::DEV.routes,
43 };
44 let constants = match env {
45 Environment::Production => dimo_environment::PRODUCTION.constants,
46 Environment::Dev => dimo_environment::DEV.constants,
47 };
48
49 Self {
50 attestation: AttestationClient::new(routes.attestation.to_string()),
51 auth: AuthClient::new(routes.auth.to_string()),
52 devicedefinitions: DeviceDefinitions::new(routes.device_definitions),
53 devices: Devices::new(routes.devices),
54 tokenexchange: TokenExchange::new(routes.token_exchange, constants.nft_address),
55 trips: Trips::new(routes.trips),
56 valuations: Valuations::new(routes.valuations),
57 identity: Identity::new(routes.identity),
58 telemetry: Telemetry::new(routes.telemetry),
59 }
60 }
61
62 pub async fn get_token(&mut self) -> Result<AccessToken, Box<dyn std::error::Error>> {
63 let creds = get_credentials()?;
64
65 let challenge = self
66 .auth
67 .generate_challenge(&creds.client_id, &creds.domain)
68 .await
69 .expect("error generating challenge");
70
71 let state = challenge.state;
72 let challenge = challenge.challenge;
73 let signature = self
74 .auth
75 .sign_challenge(&challenge, &creds.private_key)
76 .expect("error signing challenge");
77
78 let token = self
79 .auth
80 .submit_challenge(&creds.client_id, &creds.domain, &state, &signature)
81 .await
82 .expect("error submitting challenge");
83
84 Ok(token)
85 }
86}