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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
use crate::admin::AdminApiClient;
use crate::agreement_acceptances::{
    AgreementAcceptancesApiClient, AgreementAcceptancesIdApiClient,
};
use crate::agreements::{AgreementsApiClient, AgreementsIdApiClient};
use crate::api_default_imports::*;
use crate::app_catalogs::AppCatalogsApiClient;
use crate::applications::{ApplicationsApiClient, ApplicationsIdApiClient};
use crate::audit_logs::AuditLogsApiClient;
use crate::authentication_method_configurations::{
    AuthenticationMethodConfigurationsApiClient, AuthenticationMethodConfigurationsIdApiClient,
};
use crate::authentication_methods_policy::AuthenticationMethodsPolicyApiClient;
use crate::batch::BatchApiClient;
use crate::branding::BrandingApiClient;
use crate::certificate_based_auth_configuration::{
    CertificateBasedAuthConfigurationApiClient, CertificateBasedAuthConfigurationIdApiClient,
};
use crate::chats::{ChatsApiClient, ChatsIdApiClient};
use crate::communications::CommunicationsApiClient;
use crate::contracts::{ContractsApiClient, ContractsIdApiClient};
use crate::data_policy_operations::DataPolicyOperationsApiClient;
use crate::default_drive::DefaultDriveApiClient;
use crate::device_app_management::DeviceAppManagementApiClient;
use crate::device_management::DeviceManagementApiClient;
use crate::devices::{DevicesApiClient, DevicesIdApiClient};
use crate::directory::DirectoryApiClient;
use crate::directory_objects::{DirectoryObjectsApiClient, DirectoryObjectsIdApiClient};
use crate::directory_role_templates::{
    DirectoryRoleTemplatesApiClient, DirectoryRoleTemplatesIdApiClient,
};
use crate::directory_roles::{DirectoryRolesApiClient, DirectoryRolesIdApiClient};
use crate::domain_dns_records::{DomainDnsRecordsApiClient, DomainDnsRecordsIdApiClient};
use crate::domains::{DomainsApiClient, DomainsIdApiClient};
use crate::drives::{DrivesApiClient, DrivesIdApiClient};
use crate::education::EducationApiClient;
use crate::group_lifecycle_policies::{
    GroupLifecyclePoliciesApiClient, GroupLifecyclePoliciesIdApiClient,
};
use crate::groups::{GroupsApiClient, GroupsIdApiClient};
use crate::identity::{
    AllowedHostValidator, AuthorizationCodeAssertionCredential,
    AuthorizationCodeCertificateCredential, AuthorizationCodeCredential, BearerTokenCredential,
    ClientAssertionCredential, ClientCertificateCredential, ClientSecretCredential,
    ConfidentialClientApplication, DeviceCodeCredential, HostIs, OpenIdCredential,
    PublicClientApplication, ResourceOwnerPasswordCredential, Token,
};
use crate::identity_access::IdentityApiClient;
use crate::identity_governance::IdentityGovernanceApiClient;
use crate::identity_providers::{IdentityProvidersApiClient, IdentityProvidersIdApiClient};
use crate::invitations::InvitationsApiClient;
use crate::me::MeApiClient;
use crate::oauth2_permission_grants::{
    Oauth2PermissionGrantsApiClient, Oauth2PermissionGrantsIdApiClient,
};
use crate::organization::{OrganizationApiClient, OrganizationIdApiClient};
use crate::permission_grants::{PermissionGrantsApiClient, PermissionGrantsIdApiClient};
use crate::places::PlacesApiClient;
use crate::planner::PlannerApiClient;
use crate::policies::PoliciesApiClient;
use crate::reports::ReportsApiClient;
use crate::schema_extensions::{SchemaExtensionsApiClient, SchemaExtensionsIdApiClient};
use crate::service_principals::{ServicePrincipalsApiClient, ServicePrincipalsIdApiClient};
use crate::sites::{SitesApiClient, SitesIdApiClient};
use crate::solutions::SolutionsApiClient;
use crate::subscribed_skus::SubscribedSkusApiClient;
use crate::subscriptions::{SubscriptionsApiClient, SubscriptionsIdApiClient};
use crate::teams::{TeamsApiClient, TeamsIdApiClient};
use crate::teams_templates::{TeamsTemplatesApiClient, TeamsTemplatesIdApiClient};
use crate::teamwork::TeamworkApiClient;
use crate::users::{UsersApiClient, UsersIdApiClient};
use crate::{GRAPH_URL, GRAPH_URL_BETA};
use graph_core::identity::ForceTokenRefresh;
use lazy_static::lazy_static;

lazy_static! {
    static ref PARSED_GRAPH_URL: Url = Url::parse(GRAPH_URL).expect("Unable to set v1 endpoint");
    static ref PARSED_GRAPH_URL_BETA: Url =
        Url::parse(GRAPH_URL_BETA).expect("Unable to set beta endpoint");
}

// For backwards compatibility.
pub type Graph = GraphClient;

#[derive(Debug, Clone)]
pub struct GraphClient {
    client: Client,
    endpoint: Url,
    allowed_host_validator: AllowedHostValidator,
}

impl GraphClient {
    pub fn new<AT: ToString>(access_token: AT) -> GraphClient {
        GraphClient {
            client: Client::new(BearerTokenCredential::from(access_token.to_string())),
            endpoint: PARSED_GRAPH_URL.clone(),
            allowed_host_validator: AllowedHostValidator::default(),
        }
    }

    pub fn from_client_app<CA: ClientApplication + 'static>(client_app: CA) -> GraphClient {
        GraphClient {
            client: Client::new(client_app),
            endpoint: PARSED_GRAPH_URL.clone(),
            allowed_host_validator: AllowedHostValidator::default(),
        }
    }

    /// Use the v1 endpoint for the Microsoft Graph API. This is the default
    /// endpoint used by the client.
    ///
    /// # Example
    /// ```rust,ignore
    /// # use graph_rs_sdk::GRAPH_URL;
    /// use graph_rs_sdk::Graph;
    ///
    /// let mut client = Graph::new("ACCESS_TOKEN");
    ///
    /// client.v1()
    ///     .me()
    ///     .get_user()
    ///     .send()
    ///     .await?;
    /// ```
    pub fn v1(&mut self) -> &mut GraphClient {
        self.endpoint = PARSED_GRAPH_URL.clone();
        self
    }

    /// Use the v1 endpoint for the Microsoft Graph API. Same as calling
    /// `v1()` but takes a mutable reference to self and does not return
    /// self.
    ///
    /// # Example
    /// ```rust
    /// # use graph_rs_sdk::GRAPH_URL;
    /// use graph_rs_sdk::Graph;
    ///
    /// let mut client = Graph::new("ACCESS_TOKEN");
    /// client.use_v1();
    ///
    /// assert_eq!(client.url().to_string(), GRAPH_URL.to_string())
    /// ```
    pub fn use_v1(&mut self) {
        self.endpoint = PARSED_GRAPH_URL.clone();
    }

    /// Use the beta endpoint for the Microsoft Graph API
    ///
    /// # Example
    /// ```rust,ignore
    /// # use graph_rs_sdk::GRAPH_URL_BETA;
    /// use graph_rs_sdk::Graph;
    ///
    /// let mut client = Graph::new("ACCESS_TOKEN");
    ///
    /// client.beta()
    ///     .me()
    ///     .get_user()
    ///     .send()
    ///     .await?;
    /// ```
    pub fn beta(&mut self) -> &mut GraphClient {
        self.endpoint = PARSED_GRAPH_URL_BETA.clone();
        self
    }

    /// Use the beta endpoint for the Microsoft Graph API. Same as calling
    /// `beta()` but takes a mutable reference to self and does not return
    /// self.
    ///
    /// Example
    /// ```rust
    /// # use graph_rs_sdk::GRAPH_URL_BETA;
    /// use graph_rs_sdk::Graph;
    ///
    /// let mut client = Graph::new("ACCESS_TOKEN");
    /// client.use_beta();
    ///
    /// assert_eq!(client.url().to_string(), GRAPH_URL_BETA.to_string())
    /// ```
    pub fn use_beta(&mut self) {
        self.endpoint = PARSED_GRAPH_URL_BETA.clone();
    }

    pub fn url(&self) -> &Url {
        &self.endpoint
    }

    pub fn with_force_token_refresh(
        &mut self,
        force_token_refresh: ForceTokenRefresh,
    ) -> &mut Self {
        self.client.with_force_token_refresh(force_token_refresh);
        self
    }

    pub fn use_force_token_refresh(&mut self, force_token_refresh: ForceTokenRefresh) {
        self.client.with_force_token_refresh(force_token_refresh);
    }

    /// Set a custom endpoint for the Microsoft Graph API. Provide the scheme and host with an
    /// optional path. The path is not set by the sdk when using a custom endpoint.
    ///
    /// The scheme must be https:// and any other provided scheme will cause a panic.
    /// See [Microsoft Graph Service Root Endpoints](https://learn.microsoft.com/en-us/graph/deployments#microsoft-graph-and-graph-explorer-service-root-endpoints)
    ///
    /// Attempting to use an invalid host will cause the client to panic. This is done
    /// for increased security.
    ///
    /// Do not use a government host endpoint without authorization and any necessary clearances.
    /// Using any government host endpoint means you should expect every API call will be monitored
    /// and recorded.
    ///
    /// You should also assume China's Graph API operated by 21Vianet is being monitored
    /// by the Chinese government who is well known for the control it has over Chinese companies
    /// and for its surveillance state of Chinese citizens.
    /// And, according to Microsoft, **These services are subject to Chinese laws**. See
    /// [Microsoft 365 operated by 21Vianet](https://learn.microsoft.com/en-us/office365/servicedescriptions/office-365-platform-service-description/microsoft-365-operated-by-21vianet)
    ///
    /// Valid Hosts:
    /// * graph.microsoft.com (Default public endpoint worldwide)
    /// * graph.microsoft.us (U.S. Government)
    /// * dod-graph.microsoft.us (U.S. Department Of Defense)
    /// * graph.microsoft.de
    /// * microsoftgraph.chinacloudapi.cn (operated by 21Vianet)
    /// * canary.graph.microsoft.com
    ///
    /// Example
    /// ```rust,ignore
    /// use graph_rs_sdk::Graph;
    ///
    /// let mut client = Graph::new("ACCESS_TOKEN");
    ///
    /// client.custom_endpoint("https://graph.microsoft.com/v1.0")
    ///     .me()
    ///     .get_user()
    ///     .send()
    ///     .await?;
    /// ```
    pub fn custom_endpoint(&mut self, url: &Url) -> &mut GraphClient {
        self.use_endpoint(url);
        self
    }

    /// Set a custom endpoint for the Microsoft Graph API. Provide the scheme and host with an
    /// optional path. The path is not set by the sdk when using a custom endpoint.
    ///
    /// The scheme must be https:// and any other provided scheme will cause a panic.
    /// See [Microsoft Graph Service Root Endpoints](https://learn.microsoft.com/en-us/graph/deployments#microsoft-graph-and-graph-explorer-service-root-endpoints)
    ///
    /// Attempting to use an invalid host will cause the client to panic. This is done
    /// for increased security.
    ///
    /// Do not use a government host endpoint without authorization and any necessary clearances.
    /// Using any government host endpoint means you should expect every API call will be monitored
    /// and recorded.
    ///
    /// You should also assume China's Graph API operated by 21Vianet is being monitored
    /// by the Chinese government who is well known for the control it has over Chinese companies
    /// and for its surveillance state of Chinese citizens.
    /// And, according to Microsoft, **These services are subject to Chinese laws**. See
    /// [Microsoft 365 operated by 21Vianet](https://learn.microsoft.com/en-us/office365/servicedescriptions/office-365-platform-service-description/microsoft-365-operated-by-21vianet)
    ///
    /// Valid Hosts:
    /// * graph.microsoft.com (Default public endpoint worldwide)
    /// * graph.microsoft.us (U.S. Government)
    /// * dod-graph.microsoft.us (U.S. Department Of Defense)
    /// * graph.microsoft.de
    /// * microsoftgraph.chinacloudapi.cn (operated by 21Vianet)
    /// * canary.graph.microsoft.com
    ///
    /// Example
    /// ```rust
    /// use url::Url;
    /// use graph_rs_sdk::Graph;
    ///
    /// let mut client = Graph::new("ACCESS_TOKEN");
    /// client.use_endpoint(&Url::parse("https://graph.microsoft.com/v1.0").unwrap());
    ///
    /// assert_eq!(client.url().to_string(), "https://graph.microsoft.com/v1.0".to_string())
    /// ```
    pub fn use_endpoint(&mut self, url: &Url) {
        if url.query().is_some() {
            panic!(
                "Invalid query - provide only the scheme, host, and optional path of the Uri such as https://graph.microsoft.com/v1.0"
            );
        }

        match self.allowed_host_validator.validate_url(url) {
            HostIs::Valid => {
                self.endpoint = url.clone();
            }
            HostIs::Invalid => panic!("Invalid host"),
        }
    }

    #[cfg(feature = "test-util")]
    pub fn use_test_endpoint(&mut self, url: &Url) {
        self.endpoint = url.clone();
    }

    api_client_impl!(admin, AdminApiClient);

    api_client_impl!(app_catalogs, AppCatalogsApiClient);

    api_client_impl!(
        agreement_acceptances,
        AgreementAcceptancesApiClient,
        agreement_acceptance,
        AgreementAcceptancesIdApiClient
    );

    api_client_impl!(
        agreements,
        AgreementsApiClient,
        agreement,
        AgreementsIdApiClient
    );

    api_client_impl!(
        applications,
        ApplicationsApiClient,
        application,
        ApplicationsIdApiClient
    );

    api_client_impl!(audit_logs, AuditLogsApiClient);

    api_client_impl!(
        authentication_method_configurations,
        AuthenticationMethodConfigurationsApiClient,
        authentication_method_configuration,
        AuthenticationMethodConfigurationsIdApiClient
    );

    api_client_impl!(
        authentication_methods_policy,
        AuthenticationMethodsPolicyApiClient
    );

    api_client_impl!(branding, BrandingApiClient);

    api_client_impl!(
        certificate_based_auth_configurations,
        CertificateBasedAuthConfigurationApiClient,
        certificate_based_auth_configuration,
        CertificateBasedAuthConfigurationIdApiClient
    );

    api_client_impl!(chats, ChatsApiClient, chat, ChatsIdApiClient);

    api_client_impl!(communications, CommunicationsApiClient);

    api_client_impl!(
        contracts,
        ContractsApiClient,
        contract,
        ContractsIdApiClient
    );

    api_client_impl!(data_policy_operations, DataPolicyOperationsApiClient);

    api_client_impl!(device_app_management, DeviceAppManagementApiClient);

    api_client_impl!(device_management, DeviceManagementApiClient);

    api_client_impl!(devices, DevicesApiClient, device, DevicesIdApiClient);

    api_client_impl!(directory, DirectoryApiClient);

    api_client_impl!(
        directory_objects,
        DirectoryObjectsApiClient,
        directory_object,
        DirectoryObjectsIdApiClient
    );

    api_client_impl!(
        directory_role_templates,
        DirectoryRoleTemplatesApiClient,
        directory_role_template,
        DirectoryRoleTemplatesIdApiClient
    );

    api_client_impl!(
        directory_roles,
        DirectoryRolesApiClient,
        directory_role,
        DirectoryRolesIdApiClient
    );

    api_client_impl!(
        domain_dns_records,
        DomainDnsRecordsApiClient,
        domain_dns_record,
        DomainDnsRecordsIdApiClient
    );

    api_client_impl!(domains, DomainsApiClient, domain, DomainsIdApiClient);

    api_client_impl!(drives, DrivesApiClient, drive, DrivesIdApiClient);

    api_client_impl_link!(default_drive, DefaultDriveApiClient);

    api_client_impl_link!(education, EducationApiClient);

    api_client_impl!(groups, GroupsApiClient, group, GroupsIdApiClient);

    api_client_impl!(
        group_lifecycle_policies,
        GroupLifecyclePoliciesApiClient,
        group_lifecycle_policy,
        GroupLifecyclePoliciesIdApiClient
    );

    api_client_impl_link!(identity, IdentityApiClient);

    api_client_impl!(identity_governance, IdentityGovernanceApiClient);

    api_client_impl!(
        identity_providers,
        IdentityProvidersApiClient,
        identity_provider,
        IdentityProvidersIdApiClient
    );

    api_client_impl!(invitations, InvitationsApiClient);

    api_client_impl_link!(me, MeApiClient);

    api_client_impl!(
        oauth2_permission_grants,
        Oauth2PermissionGrantsApiClient,
        oauth2_permission_grant,
        Oauth2PermissionGrantsIdApiClient
    );

    api_client_impl!(
        organizations,
        OrganizationApiClient,
        organization,
        OrganizationIdApiClient
    );

    api_client_impl!(places, PlacesApiClient);

    api_client_impl!(
        permission_grants,
        PermissionGrantsApiClient,
        permission_grant,
        PermissionGrantsIdApiClient
    );

    api_client_impl!(planner, PlannerApiClient);

    api_client_impl!(policies, PoliciesApiClient);

    api_client_impl!(reports, ReportsApiClient);

    api_client_impl!(
        schema_extensions,
        SchemaExtensionsApiClient,
        schema_extension,
        SchemaExtensionsIdApiClient
    );

    api_client_impl!(
        service_principals,
        ServicePrincipalsApiClient,
        service_principal,
        ServicePrincipalsIdApiClient
    );

    api_client_impl!(sites, SitesApiClient, site, SitesIdApiClient);

    api_client_impl!(solutions, SolutionsApiClient);

    api_client_impl!(
        subscribed_skus,
        SubscribedSkusApiClient,
        subscribed_sku,
        SubscriptionsIdApiClient
    );

    api_client_impl!(
        subscriptions,
        SubscriptionsApiClient,
        subscription,
        SubscriptionsIdApiClient
    );

    api_client_impl!(teams, TeamsApiClient, team, TeamsIdApiClient);

    api_client_impl!(
        teams_templates,
        TeamsTemplatesApiClient,
        teams_template,
        TeamsTemplatesIdApiClient
    );

    api_client_impl_link!(teamwork, TeamworkApiClient);

    api_client_impl!(users, UsersApiClient, user, UsersIdApiClient);

    pub fn custom(&self, method: Method, body: Option<BodyRead>) -> RequestHandler {
        let body_result = body.map(|body| body.into_body());
        if let Some(b) = body_result {
            if let Err(err) = b {
                return RequestHandler::new(
                    self.client.clone(),
                    RequestComponents::new(ResourceIdentity::Custom, self.endpoint.clone(), method),
                    Some(err),
                    None,
                );
            } else if let Ok(body_read) = b {
                return RequestHandler::new(
                    self.client.clone(),
                    RequestComponents::new(ResourceIdentity::Custom, self.endpoint.clone(), method),
                    None,
                    Some(body_read),
                );
            }
        }

        RequestHandler::new(
            self.client.clone(),
            RequestComponents::new(ResourceIdentity::Custom, self.endpoint.clone(), method),
            None,
            None,
        )
    }

    pub fn batch<B: serde::Serialize>(&self, batch: &B) -> RequestHandler {
        BatchApiClient::new(
            self.client.clone(),
            ResourceProvisioner::resource_config_with_url(
                self.endpoint.clone(),
                ResourceIdentity::Batch,
            ),
            Handlebars::new(),
        )
        .batch(batch)
    }
}

impl From<&str> for GraphClient {
    fn from(token: &str) -> Self {
        GraphClient::from_client_app(BearerTokenCredential::from(token.to_string()))
    }
}

impl From<String> for GraphClient {
    fn from(token: String) -> Self {
        GraphClient::from_client_app(BearerTokenCredential::from(token))
    }
}

impl From<&Token> for GraphClient {
    fn from(token: &Token) -> Self {
        GraphClient::from_client_app(BearerTokenCredential::from(token.access_token.clone()))
    }
}

impl From<GraphClientConfiguration> for GraphClient {
    fn from(graph_client_builder: GraphClientConfiguration) -> Self {
        GraphClient {
            client: Client::from(graph_client_builder),
            endpoint: PARSED_GRAPH_URL.clone(),
            allowed_host_validator: AllowedHostValidator::default(),
        }
    }
}

impl From<&ConfidentialClientApplication<AuthorizationCodeCredential>> for GraphClient {
    fn from(value: &ConfidentialClientApplication<AuthorizationCodeCredential>) -> Self {
        GraphClient::from_client_app(value.clone())
    }
}

impl From<&ConfidentialClientApplication<AuthorizationCodeAssertionCredential>> for GraphClient {
    fn from(value: &ConfidentialClientApplication<AuthorizationCodeAssertionCredential>) -> Self {
        GraphClient::from_client_app(value.clone())
    }
}

impl From<&ConfidentialClientApplication<AuthorizationCodeCertificateCredential>> for GraphClient {
    fn from(value: &ConfidentialClientApplication<AuthorizationCodeCertificateCredential>) -> Self {
        GraphClient::from_client_app(value.clone())
    }
}

impl From<&ConfidentialClientApplication<ClientSecretCredential>> for GraphClient {
    fn from(value: &ConfidentialClientApplication<ClientSecretCredential>) -> Self {
        GraphClient::from_client_app(value.clone())
    }
}

impl From<&ConfidentialClientApplication<ClientCertificateCredential>> for GraphClient {
    fn from(value: &ConfidentialClientApplication<ClientCertificateCredential>) -> Self {
        GraphClient::from_client_app(value.clone())
    }
}

impl From<&ConfidentialClientApplication<ClientAssertionCredential>> for GraphClient {
    fn from(value: &ConfidentialClientApplication<ClientAssertionCredential>) -> Self {
        GraphClient::from_client_app(value.clone())
    }
}

impl From<&ConfidentialClientApplication<OpenIdCredential>> for GraphClient {
    fn from(value: &ConfidentialClientApplication<OpenIdCredential>) -> Self {
        GraphClient::from_client_app(value.clone())
    }
}

impl From<&PublicClientApplication<DeviceCodeCredential>> for GraphClient {
    fn from(value: &PublicClientApplication<DeviceCodeCredential>) -> Self {
        GraphClient::from_client_app(value.clone())
    }
}

impl From<&PublicClientApplication<ResourceOwnerPasswordCredential>> for GraphClient {
    fn from(value: &PublicClientApplication<ResourceOwnerPasswordCredential>) -> Self {
        GraphClient::from_client_app(value.clone())
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    #[should_panic]
    fn try_invalid_host() {
        let mut client = GraphClient::new("token");
        client.custom_endpoint(&Url::parse("https://example.org").unwrap());
    }

    #[test]
    #[should_panic]
    fn try_invalid_http_scheme() {
        let mut client = GraphClient::new("token");
        client.custom_endpoint(&Url::parse("http://example.org").unwrap());
    }

    #[test]
    #[should_panic]
    fn try_invalid_query() {
        let mut client = GraphClient::new("token");
        client.custom_endpoint(&Url::parse("https://example.org?user=name").unwrap());
    }

    #[test]
    #[should_panic]
    fn try_invalid_path() {
        let mut client = GraphClient::new("token");
        client.custom_endpoint(&Url::parse("https://example.org/v1").unwrap());
    }

    #[test]
    #[should_panic]
    fn try_invalid_host2() {
        let mut client = GraphClient::new("token");
        client.use_endpoint(&Url::parse("https://example.org").unwrap());
    }

    #[test]
    #[should_panic]
    fn try_invalid_scheme2() {
        let mut client = GraphClient::new("token");
        client.use_endpoint(&Url::parse("http://example.org").unwrap());
    }

    #[test]
    #[should_panic]
    fn try_invalid_query2() {
        let mut client = GraphClient::new("token");
        client.use_endpoint(&Url::parse("https://example.org?user=name").unwrap());
    }

    #[test]
    #[should_panic]
    fn try_invalid_path2() {
        let mut client = GraphClient::new("token");
        client.use_endpoint(&Url::parse("https://example.org/v1").unwrap());
    }

    #[test]
    fn try_valid_hosts() {
        let urls = [
            "https://graph.microsoft.com/v1.0",
            "https://graph.microsoft.us",
            "https://dod-graph.microsoft.us",
            "https://graph.microsoft.de",
            "https://microsoftgraph.chinacloudapi.cn",
            "https://canary.graph.microsoft.com",
        ];

        let mut client = Graph::new("token");

        for url in urls.iter() {
            client.custom_endpoint(&Url::parse(url).unwrap());
            assert_eq!(client.url().clone(), Url::parse(url).unwrap());
        }
    }
}

#[cfg(test)]
#[cfg(feature = "test-util")]
mod test_util_feature {
    use crate::{http::Url, Graph, GraphClientConfiguration, ODataQuery};
    use wiremock::matchers::{bearer_token, method, path, query_param};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    /// Tests the test-util feature and setting https-only to false.
    #[tokio::test]
    async fn can_set_test_endpoint() {
        let mock_server = MockServer::start().await;

        Mock::given(method("GET"))
            .and(path("/users"))
            .and(query_param("$top", "10"))
            .and(bearer_token("token"))
            .respond_with(ResponseTemplate::new(200))
            .mount(&mock_server)
            .await;

        let graph_client_configuration = GraphClientConfiguration::new()
            .access_token("token")
            .https_only(false);

        let mut client = Graph::from(graph_client_configuration);
        let uri = mock_server.uri();
        client.use_test_endpoint(&Url::parse(uri.as_str()).unwrap());

        let response = client.users().list_user().top("10").send().await.unwrap();
        let status = response.status();
        assert_eq!(status.as_u16(), 200);
    }

    #[tokio::test]
    #[should_panic]
    async fn test_util_feature_use_endpoint_panics() {
        let mock_server = MockServer::start().await;

        Mock::given(method("GET"))
            .and(path("/users"))
            .and(query_param("$top", "10"))
            .and(bearer_token("token"))
            .respond_with(ResponseTemplate::new(200))
            .mount(&mock_server)
            .await;

        let graph_client_configuration = GraphClientConfiguration::new()
            .access_token("token")
            .https_only(false);

        let mut client = Graph::from(graph_client_configuration);
        let uri = mock_server.uri();
        client.use_endpoint(&Url::parse(uri.as_str()).unwrap());

        let response = client.users().list_user().top("10").send().await.unwrap();
        let status = response.status();
        assert_eq!(status.as_u16(), 200);
    }
}