vim_rs 0.4.4

Rust Bindings for the VMware by Broadcom vCenter VI JSON API
Documentation
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
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
use std::sync::Arc;
use std::time::Instant;

use tokio::sync::RwLock;
use super::super::types::structs;
use super::wire_log;
use log::{warn, debug, trace};

use bytes::Bytes;
use std::future::Future;
use std::pin::Pin;
use std::ffi::OsStr;
use crate::mo;
use crate::types::struct_enum::StructType;
use crate::types::structs::{ManagedObjectReference, ServiceContent};

const LIB_NAME: &str = env!("CARGO_PKG_NAME");
const LIB_VERSION: &str = env!("CARGO_PKG_VERSION");
// See build.rs for the RUSTC_VERSION
const RUSTC_VERSION: &str = env!("RUSTC_VERSION");

/// Compatible API releases i.e. current and older API releases that can be negotiated with a server
pub const COMPATIBLE_API_RELEASES: [&str; 4] = ["9.0.0.0", "8.0.3.0", "8.0.2.0", "8.0.1.0"];

/// The default API version found in the OpenAPI specification
pub const API_RELEASE: &str = "9.0.0.0";

/// The header key for the session key
const AUTHN_HEADER: &str = "vmware-api-session-id";

const SERVICE_INSTANCE_MOID: &str = "ServiceInstance";

#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// A typed vSphere API fault returned by the server.
    ///
    /// For SOAP transport this is produced *only* when the server's
    /// `<soapenv:Fault>` carries a typed element inside `<detail>`
    /// (e.g. `RequestCanceled`, `InvalidLogin`, `VAppPropertyFault`). For
    /// JSON transport this is produced when the error body deserializes as
    /// a `MethodFault`. Envelope-level SOAP errors that are *not* part of
    /// the vSphere type hierarchy are surfaced as [`Error::SoapFault`].
    #[error("MethodFault: {0:?}")]
    MethodFault(structs::MethodFault),
    #[error("Reqwest error: {0}")]
    ReqwestError(#[from] reqwest::Error),
    /// A parse error returned by the server.
    ///
    /// This is produced when the server's response is not valid XML or JSON.
    /// This is also produced when the server's response is a SOAP envelope fault.
    #[error("Parse error: {0}")]
    ParseError(String),
    #[error("Missing or Invalid session key")]
    MissingOrInvalidSessionKey,
    #[error("Invalid object type {0} expected: {1}")]
    InvalidObjectType(String, String),
    #[error("Cannot negotiate compatible API release. Attempted with: {0:?}")]
    CannotNegotiateAPIRelease(Vec<String>),
}

/// Convenience alias used in generated MO stubs for error construction.
pub type VimError = Error;

pub type Result<T> = std::result::Result<T, Error>;

/// Returns `true` if `err` is a `RequestCanceled` SOAP fault (for example after
/// [`crate::mo::PropertyCollector::cancel_wait_for_updates`]).
#[must_use]
pub fn is_request_canceled_error(err: &Error) -> bool {
    matches!(
        err,
        Error::MethodFault(f) if f.type_ == Some(StructType::RequestCanceled)
    )
}

#[cfg(test)]
mod is_request_canceled_tests {
    use super::{is_request_canceled_error, Error};
    use crate::types::struct_enum::StructType;
    use crate::types::structs::MethodFault;

    #[test]
    fn detects_request_canceled_fault() {
        let err = Error::MethodFault(MethodFault {
            fault_cause: None,
            fault_message: None,
            type_: Some(StructType::RequestCanceled),
            extra_fields_: Default::default(),
        });
        assert!(is_request_canceled_error(&err));
    }

    #[test]
    fn rejects_non_fault_errors() {
        assert!(!is_request_canceled_error(&Error::ParseError("x".into())));
    }
}

/// A boxed future used by the object-safe `VimClient` trait.
pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;

/// Transport protocol used by a `VimClient` instance.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Transport {
    Json,
    #[cfg(feature = "xml")]
    Soap,
}

/// Wire representation of a managed-object property value before it is narrowed to a concrete type.
///
/// JSON transport returns raw bytes from the VI JSON API. SOAP transport returns the value already
/// parsed as [`crate::types::vim_any::VimAny`] from `RetrievePropertiesEx` (no re-serialization).
#[derive(Debug)]
pub enum PropertyValue {
    Json(Bytes),
    #[cfg(feature = "xml")]
    Parsed(crate::types::vim_any::VimAny),
}

/// Narrow a [`PropertyValue`] to `T`: JSON deserializes once; SOAP downcasts the in-memory `VimAny`.
pub fn extract_property<T: miniserde::Deserialize + 'static>(pv: PropertyValue) -> Result<T> {
    match pv {
        PropertyValue::Json(bytes) => {
            let text = std::str::from_utf8(&bytes)
                .map_err(|e| Error::ParseError(e.to_string()))?;
            miniserde::json::from_str(text).map_err(|_| {
                Error::ParseError(format!(
                    "JSON property decode failed for {}",
                    std::any::type_name::<T>()
                ))
            })
        }
        #[cfg(feature = "xml")]
        PropertyValue::Parsed(any) => any
            .into_any()
            .downcast::<T>()
            .map(|b| *b)
            .map_err(|_| {
                Error::ParseError(format!(
                    "SOAP property type mismatch for {}",
                    std::any::type_name::<T>()
                ))
            }),
    }
}

/// Object-safe client abstraction for generated managed-object stubs (`crate::mo::*`).
///
/// The trait exposes high-level operations that are transport-agnostic.
/// Generated MO proxies use [`extract_property`] for GET properties and the free [`unmarshal`] /
/// [`unmarshal_array`] functions for POST method bodies, keyed on [`Self::transport`].
pub trait VimClient: Send + Sync {
    /// Access vSphere `ServiceContent` (root managed object references).
    fn service_content(&self) -> &ServiceContent;

    /// The transport protocol this client uses.
    fn transport(&self) -> Transport;

    /// API version string (negotiated release for JSON, about.api_version for SOAP).
    fn api_release(&self) -> String;

    /// Invoke a method and return the response body bytes.
    fn invoke<'a>(
        &'a self,
        svc: &'a str,
        mo_type: &'a str,
        mo_id: &'a str,
        method_name: &'a str,
        params: Option<&'a (dyn miniserde::Serialize + Send + Sync)>,
    ) -> BoxFuture<'a, Result<Bytes>>;

    /// Invoke a method that may return an empty body (`None`).
    fn invoke_optional<'a>(
        &'a self,
        svc: &'a str,
        mo_type: &'a str,
        mo_id: &'a str,
        method_name: &'a str,
        params: Option<&'a (dyn miniserde::Serialize + Send + Sync)>,
    ) -> BoxFuture<'a, Result<Option<Bytes>>>;

    /// Invoke a void method (no response body expected).
    fn invoke_void<'a>(
        &'a self,
        svc: &'a str,
        mo_type: &'a str,
        mo_id: &'a str,
        method_name: &'a str,
        params: Option<&'a (dyn miniserde::Serialize + Send + Sync)>,
    ) -> BoxFuture<'a, Result<()>>;

    /// Fetch a single property by name (or `None` if empty).
    ///
    /// Managed-object stubs call [`extract_property`] on the result. Application code typically uses
    /// [`Client::fetch_property`] on the public facade instead.
    fn fetch_property_raw<'a>(
        &'a self,
        svc: &'a str,
        mo_type: &'a str,
        mo_id: &'a str,
        property: &'a str,
    ) -> BoxFuture<'a, Result<Option<PropertyValue>>>;
}

/// Deserialize response bytes according to the transport format.
///
/// This is a free function (not a trait method) so that it can be generic on `T`
/// without breaking object-safety of `VimClient`.
pub fn unmarshal<T: miniserde::Deserialize>(transport: Transport, bytes: &[u8]) -> Result<T> {
    let text = std::str::from_utf8(bytes)
        .map_err(|e| Error::ParseError(e.to_string()))?;
    match transport {
        Transport::Json => miniserde::json::from_str(text)
            .map_err(|_| Error::ParseError(format!(
                "JSON deserialization failed for {}", std::any::type_name::<T>()))),
        #[cfg(feature = "xml")]
        Transport::Soap => {
            // Method responses are SOAP envelopes with `<returnval>`; property fetch returns raw object XML.
            // Uses the feature-gated tolerant dispatchers: with `vcsim_compat`
            // enabled, malformed elements are dropped instead of failing the
            // whole parse; with the feature off this is identical to the
            // historical strict `vim_response` / `from_xml` path.
            crate::xml::soap::vim_response_internal(text)
                .or_else(|_| crate::xml::de::from_xml_internal(text))
                .map_err(|_| Error::ParseError(format!(
                    "XML deserialization failed for {}", std::any::type_name::<T>())))
        },
    }
}

/// Deserialize results encoded as a **list of sibling** `<returnval>` elements per item (SOAP) or
/// the equivalent JSON array (JSON).
///
/// vSphere uses this shape for paginated reads such as *ReadNextEvents*, *ReadPreviousEvents*,
/// *QueryEvents*, *ReadNextTasks*, *ReadPreviousTasks*, and *ReadNextTasksByViewSpec*. For those
/// APIs, do **not** use [`unmarshal`] with `Vec<…>` on SOAP: [`unmarshal`] only drives the first
/// `<returnval>` via [`crate::xml::soap::vim_response`].
pub fn unmarshal_array<U: miniserde::Deserialize>(
    transport: Transport,
    bytes: &[u8],
) -> Result<Vec<U>> {
    let text = std::str::from_utf8(bytes)
        .map_err(|e| Error::ParseError(e.to_string()))?;
    match transport {
        Transport::Json => miniserde::json::from_str(text).map_err(|_| {
            Error::ParseError(format!(
                "JSON deserialization failed for Vec<{}>",
                std::any::type_name::<U>()
            ))
        }),
        #[cfg(feature = "xml")]
        Transport::Soap => crate::xml::soap::vim_response_list_internal(text).map_err(|_| {
            Error::ParseError(format!(
                "XML deserialization failed for Vec<{}>",
                std::any::type_name::<U>()
            ))
        }),
    }
}

/// Convenience handle type used by generated bindings.
pub type VimClientHandle = Arc<dyn VimClient>;

/// Transport selection for [`ClientBuilder::build`].
///
/// Requires the `xml` feature for `Soap` and `Auto` variants.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TransportMode {
    /// Use the VI JSON API (vCenter only). This is the default.
    #[default]
    Json,
    /// Use SOAP/XML transport (works on both ESXi and vCenter).
    #[cfg(feature = "xml")]
    Soap,
    /// Auto-detect: try the Hello System API (JSON). If it fails, fall back to SOAP.
    #[cfg(feature = "xml")]
    Auto,
}

/// Explicit wire capture mode for VI JSON / SOAP traffic.
///
/// Emits on log targets `vim_rs::wire::json` and `vim_rs::wire::soap`. Summary metadata uses
/// [`log::Level::Debug`]; full request/response bodies use [`log::Level::Trace`] when
/// [`WireLoggingMode::Detailed`] applies and the managed object type is not denylisted.
///
/// **Denylist:** traffic for managed object type `SessionManager` (login, logout, session APIs)
/// never logs bodies, even in detailed mode; emitted lines use summary-style fields and may include
/// `body_logging=denylisted`.
///
/// When any wire mode other than [`WireLoggingMode::Off`] is active, legacy transport `trace!` lines
/// that duplicate full request/response dumps are suppressed for those paths.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum WireLoggingMode {
    /// Default: no dedicated wire log lines; existing library `debug!` / `trace!` behavior unchanged.
    #[default]
    Off,
    /// Request/response metadata only (sizes, paths, status, duration) at [`log::Level::Debug`].
    Summary,
    /// Adds full bodies at [`log::Level::Trace`] where allowed (`SessionManager` remains summary-only).
    Detailed,
}

impl WireLoggingMode {
    /// `true` when dedicated wire logs may be emitted (Summary or Detailed).
    pub const fn is_enabled(self) -> bool {
        !matches!(self, Self::Off)
    }
    /// `true` when full bodies may be logged at [`log::Level::Trace`] (subject to denylist).
    pub const fn is_detailed(self) -> bool {
        matches!(self, Self::Detailed)
    }
}

pub struct ClientBuilder {
    server_address: String,
    compatible_api_releases: Option<Vec<String>>,
    api_release: Option<String>,
    http_client: Option<reqwest::Client>,
    insecure: Option<bool>,
    app_name: Option<String>,
    app_version: Option<String>,
    user_name: Option<String>,
    password: Option<String>,
    locale: Option<String>,
    transport_mode: TransportMode,
    wire_logging: WireLoggingMode,
}

impl ClientBuilder {
    /// Create a new client builder for a VI/JSON API at given FQDN or IP address
    ///
    /// * `server_address` - vCenter server FQDN or IP address
    pub fn new(server_address: &str) -> Self {
        Self {
            server_address: server_address.to_string(),
            compatible_api_releases: None,
            api_release: None,
            http_client: None,
            insecure: None,
            app_name: None,
            app_version: None,
            user_name: None,
            password: None,
            locale: None,
            transport_mode: TransportMode::default(),
            wire_logging: WireLoggingMode::default(),
        }
    }

    /// Opt-in transport logging on dedicated log targets (`vim_rs::wire::json`, `vim_rs::wire::soap`).
    ///
    /// Defaults to [`WireLoggingMode::Off`]. Summary records use [`log::Level::Debug`]; full bodies in
    /// [`WireLoggingMode::Detailed`] use [`log::Level::Trace`] except for `SessionManager` traffic,
    /// which stays summary-only. Prefer filtering by target (e.g. `RUST_LOG=vim_rs::wire::json=debug`)
    /// instead of raising global trace for the entire crate.
    pub fn wire_logging(mut self, mode: WireLoggingMode) -> Self {
        self.wire_logging = mode;
        self
    }

    /// Set the transport mode. Requires the `xml` feature for `Soap` and `Auto`.
    pub fn transport(mut self, mode: TransportMode) -> Self {
        self.transport_mode = mode;
        self
    }

    /// Set the compatible API releases. The default is set from the openapi spec. If `api_release`
    /// is not explicitly set then this value or `COMPATIBLE_API_RELEASES` will be used to call the
    /// vCenter [Hello System](https://developer.broadcom.com/xapis/vsphere-automation-api/latest/vcenter/api/vcenter/system__action=hello/post/index)
    /// API to negotiate an API release.
    /// * `compatible_api_releases` - List of compatible API releases
    pub fn compatible_api_releases(mut self, releases: Vec<&str>) -> Self {
        self.compatible_api_releases = Some(releases.iter().map(|s| s.to_string()).collect());
        self
    }

    /// Set the vCenter API release version. The default value from the OpenAPI spec can be used
    /// by setting here the `API_RELEASE` constant. If this is set then the Hello System API will
    /// not be called to negotiate the API release.
    /// * `api_release` - API release version
    pub fn api_release(mut self, api_release: &str) -> Self {
        self.api_release = Some(api_release.to_string());
        self
    }

    /// Set the reqwest::Client instance to use for HTTP requests.
    /// This resets the insecure flag. Use the http_client methods to set the certificate and
    /// hostname verification behavior.
    /// * `http_client` - preconfigured reqwest::Client instance
    pub fn http_client(mut self, http_client: reqwest::Client) -> Self {
        self.http_client = Some(http_client);
        self.insecure = None;
        self
    }

    /// Set the insecure flag to allow invalid certificates and hostnames.
    /// This resets the http_client. A new reqwest::Client instance will be created instead.
    /// * `insecure` - Allow invalid certificates and hostnames
    pub fn insecure(mut self, insecure: bool) -> Self {
        warn!("!!! WARNING !!! Insecure mode enabled. TLS certificate and hostname verification is disabled. !!! WARNING !!!");
        self.insecure = Some(insecure);
        self.http_client = None;
        self
    }

    /// Set app name and version. This will be used to compose the User-Agent header. User Agent
    /// value is seen in the vSphere UI under Monitoring for the vCenter system for troubleshooting.
    /// The easiest is to use cargo environment variables during build time.
    /// ```rust
    /// const APP_NAME: &str = env!("CARGO_PKG_NAME");
    /// const APP_VERSION: &str = env!("CARGO_PKG_VERSION");
    /// ```
    /// * `app_name` - Name of the application
    /// * `app_version` - Version of the application
    pub fn app_details(mut self, app_name: &str, app_version: &str) -> Self {
        self.app_name = Some(app_name.to_string());
        self.app_version = Some(app_version.to_string());
        self
    }

    /// Set the username and password for basic login.
    /// * `user_name` - Username for login
    /// * `password` - Password for login
    pub fn basic_authn(mut self, user_name: &str, password: &str) -> Self {
        self.user_name = Some(user_name.to_string());
        self.password = Some(password.to_string());
        self
    }

    /// Set the locale for the session. The default is "en".
    /// * `locale` - Locale for the session
    pub fn locale(mut self, locale: &str) -> Self {
        self.locale = Some(locale.to_string());
        self
    }

    /// Build a connected [`Client`] facade for the configured [`TransportMode`].
    ///
    /// - **Json** (default): VI JSON API via Hello System negotiation (unless `api_release` is set).
    /// - **Soap** (`xml` feature): SOAP/XML to `/sdk`.
    /// - **Auto** (`xml` feature): Hello System first; falls back to SOAP if unavailable.
    pub async fn build(self) -> Result<Arc<Client>> {
        #[cfg(feature = "xml")]
        {
            match self.transport_mode {
                TransportMode::Soap => return Self::build_soap_facade(self).await,
                TransportMode::Auto => return Self::build_auto_facade(self).await,
                TransportMode::Json => {}
            }
        }
        Self::build_json(self).await
    }

    async fn build_json(self) -> Result<Arc<Client>> {
        let wire_logging = self.wire_logging;
        let http_client = match self.http_client {
            Some(client) => client,
            None => {
                let mut builder = reqwest::ClientBuilder::new();
                if let Some(insecure) = self.insecure {
                    builder = builder.danger_accept_invalid_certs(insecure)
                                     .danger_accept_invalid_hostnames(insecure);
                }
                builder.build()?
            },
        };
        let session_key = Arc::new(RwLock::new(None));

        let user_agent = user_agent(self.app_name.as_deref(), self.app_version.as_deref());

        // Negotiate the API release if not set
        let api_release = match self.api_release {
            Some(release) => release,
            None => {
                let releases = self.compatible_api_releases
                    .unwrap_or_else(|| COMPATIBLE_API_RELEASES.iter().map(|s| s.to_string()).collect());
                let spec = HelloSpec {
                    api_releases: &releases,
                };
                let path = format!("https://{}/api/vcenter/system?action=hello", self.server_address);
                let json_body = miniserde::json::to_string(&spec);
                let started = Instant::now();
                if wire_logging.is_enabled() {
                    let mode_l = wire_log::wire_mode_label(wire_logging, "");
                    let msg = format!(
                        "wire=json mode={} phase=request kind=negotiate path={} body_bytes={}",
                        mode_l,
                        path,
                        json_body.len()
                    );
                    wire_log::log_json_line(wire_logging, "", false, &msg);
                }
                let req = http_client.post(&path)
                    .header("Content-Type", "application/json")
                    .header("User-Agent", &user_agent)
                    .body(json_body);
                let res = match req.send().await {
                    Ok(r) => r,
                    Err(e) => {
                        wire_log::log_json_negotiate_transport_failure(
                            wire_logging,
                            &path,
                            started.elapsed(),
                            &e,
                        );
                        return Err(Error::ReqwestError(e));
                    }
                };
                let status = res.status();
                // Same classification as `Response::error_for_status()` (4xx/5xx → reqwest::Error).
                let http_err = res.error_for_status_ref().err();
                let body = res.text().await.map_err(Error::ReqwestError)?;
                let elapsed = started.elapsed();
                if wire_logging.is_enabled() {
                    let mode_l = wire_log::wire_mode_label(wire_logging, "");
                    let detailed = wire_logging.is_detailed();
                    let mut msg = format!(
                        "wire=json mode={} phase=response kind=negotiate path={} status={} body_bytes={} duration_ms={}",
                        mode_l,
                        path,
                        status.as_u16(),
                        body.len(),
                        elapsed.as_millis()
                    );
                    if detailed {
                        msg.push_str(&format!(" body={}", body));
                    }
                    wire_log::log_json_line(wire_logging, "", detailed, &msg);
                }
                if let Some(e) = http_err {
                    return Err(Error::ReqwestError(e));
                }
                let result: HelloResult = miniserde::json::from_str(&body)
                    .map_err(|_| Error::ParseError("Failed to parse HelloResult".to_string()))?;
                let api_release = result.api_release;
                // Throw error if api_release is empty string indicating no compatible API release
                // was found.
                if api_release.is_empty() {
                    return Err(Error::CannotNegotiateAPIRelease(releases));
                }
                debug!("Negotiated API release: {}", api_release);
                api_release
            },
        };

        let base_url = format!("https://{}/sdk/vim25/{}", self.server_address, api_release);

        let bootstrap = Arc::new(JsonClient {
            http_client: http_client.clone(),
            session_key: session_key.clone(),
            api_release: api_release.clone(),
            base_url: base_url.clone(),
            user_agent: user_agent.clone(),
            service_content: None,
            wire_logging: wire_logging,
        });

        let service_instance = mo::ServiceInstance::new(bootstrap.clone(), SERVICE_INSTANCE_MOID);
        let content = service_instance.content().await?;
        debug!("ServiceInstance content obtained from: {}", content.about.full_name);
        trace!("ServiceInstance content: {:?}", content);

        let sm_id = content.session_manager.as_ref().map(|moid| moid.value.clone());
        let json = Arc::new(JsonClient {
            http_client: http_client.clone(),
            session_key: session_key.clone(),
            api_release: api_release.clone(),
            base_url: base_url.clone(),
            user_agent: user_agent.clone(),
            service_content: Some(content),
            wire_logging: wire_logging,
        });

        if let (Some(ref sm_id), Some(ref user_name), Some(ref password)) = (sm_id, self.user_name, self.password) {
            let sm = mo::SessionManager::new(json.clone(), sm_id);
            let session = sm.login(user_name, password, self.locale.as_deref()).await?;
            debug!("Session created for: {:?}", session.user_name);
        }
        Ok(Arc::new(Client { inner: json }))
    }

    #[cfg(feature = "xml")]
    async fn build_soap_facade(self) -> Result<Arc<Client>> {
        let http_client = match self.http_client {
            Some(client) => client,
            None => {
                let mut builder = reqwest::ClientBuilder::new()
                    .cookie_store(true);
                if let Some(insecure) = self.insecure {
                    builder = builder.danger_accept_invalid_certs(insecure)
                                     .danger_accept_invalid_hostnames(insecure);
                }
                builder.build()?
            },
        };
        let ua = user_agent(self.app_name.as_deref(), self.app_version.as_deref());
        let api_release = match self.api_release {
            Some(release) => release.clone(),
            None => API_RELEASE.to_string(),
        };
        let mut soap = crate::xml::client::SoapClient::new(
            http_client,
            &self.server_address,
            &api_release,
            &ua,
            self.wire_logging,
        );
        soap.bootstrap().await?;

        let sm_id = soap.service_content().session_manager.as_ref().map(|m| m.value.clone());
        let soap = Arc::new(soap);

        if let (Some(ref sm_id), Some(ref user_name), Some(ref password)) = (sm_id, self.user_name, self.password) {
            let sm = mo::SessionManager::new(soap.clone(), sm_id);
            let session = sm.login(user_name, password, self.locale.as_deref()).await?;
            debug!("SOAP session created for: {:?}", session.user_name);
        }
        Ok(Arc::new(Client { inner: soap }))
    }

    #[cfg(feature = "xml")]
    async fn build_auto_facade(self) -> Result<Arc<Client>> {
        let wl = self.wire_logging;
        let ua = user_agent(self.app_name.as_deref(), self.app_version.as_deref());
        let http_client_for_probe = {
            let mut builder = reqwest::ClientBuilder::new();
            if let Some(insecure) = self.insecure {
                builder = builder.danger_accept_invalid_certs(insecure)
                                 .danger_accept_invalid_hostnames(insecure);
            }
            builder.build()?
        };

        let releases = self.compatible_api_releases.clone()
            .unwrap_or_else(|| COMPATIBLE_API_RELEASES.iter().map(|s| s.to_string()).collect());
        let hello_url = format!("https://{}/api/vcenter/system?action=hello", self.server_address);
        let spec = HelloSpec { api_releases: &releases };
        let json_body = miniserde::json::to_string(&spec);
        let started = Instant::now();
        if wl.is_enabled() {
            let mode_l = wire_log::wire_mode_label(wl, "");
            let msg = format!(
                "wire=json mode={} phase=request kind=probe path={} body_bytes={}",
                mode_l,
                hello_url,
                json_body.len()
            );
            wire_log::log_json_line(wl, "", false, &msg);
        }

        let hello_ok = match http_client_for_probe
            .post(&hello_url)
            .header("Content-Type", "application/json")
            .header("User-Agent", &ua)
            .body(json_body)
            .send()
            .await
        {
            Ok(res) => {
                let status = res.status();
                let body = res.text().await.unwrap_or_default();
                let elapsed = started.elapsed();
                if wl.is_enabled() {
                    let mode_l = wire_log::wire_mode_label(wl, "");
                    let detailed = wl.is_detailed();
                    let mut msg = format!(
                        "wire=json mode={} phase=response kind=probe path={} status={} body_bytes={} duration_ms={}",
                        mode_l,
                        hello_url,
                        status.as_u16(),
                        body.len(),
                        elapsed.as_millis()
                    );
                    if detailed {
                        msg.push_str(&format!(" body={}", body));
                    }
                    wire_log::log_json_line(wl, "", detailed, &msg);
                }
                if status.is_success() {
                    miniserde::json::from_str::<HelloResult>(&body)
                        .ok()
                        .filter(|r| !r.api_release.is_empty())
                } else {
                    None
                }
            }
            Err(e) => {
                if wl.is_enabled() {
                    let mode_l = wire_log::wire_mode_label(wl, "");
                    let msg = format!(
                        "wire=json mode={} phase=response kind=probe path={} error=transport body_bytes=0 duration_ms={} detail={}",
                        mode_l,
                        hello_url,
                        started.elapsed().as_millis(),
                        e
                    );
                    wire_log::log_json_line(wl, "", false, &msg);
                }
                None
            }
        };

        if hello_ok.is_some() {
            debug!("Auto mode: Hello API succeeded, using JSON transport");
            Self::build_json(self.transport(TransportMode::Json)).await
        } else {
            debug!("Auto mode: Hello API unavailable, falling back to SOAP");
            Self::build_soap_facade(self).await
        }
    }
}

/// User-facing session handle: **only** the public methods that existed on `Client` in vim_rs **0.4.0**
/// (`service_content`, `api_release`, `fetch_property`), independent of wire format.
///
/// Internally holds an [`Arc`] to a [`VimClient`] implementation (crate-private JSON or SOAP
/// client). Logout runs when the last strong reference to that inner client is dropped.
pub struct Client {
    inner: Arc<dyn VimClient>,
}

impl Client {
    /// Get the VIM service instance content. This is the main part of the VI JSON API that contains
    /// the core virtualization objects and services. There are additional storage APIs under VSAN,
    /// PBM, VSLM and SMS. There is also ESX Agent Manager API for managing agent virtual machines.
    pub fn service_content(&self) -> &ServiceContent {
        self.inner.service_content()
    }

    /// Get the currently used API release. This may be lower than `API_RELEASE` and should be used
    /// to downgrade client expectations. For example if client is using library 8.0.3.0 with
    /// vCenter 8.0.1.0 the negotiated release will be 8.0.1.0 and the client should not call APIs
    /// or set parameters that are only available in 8.0.3.0.
    pub fn api_release(&self) -> String {
        self.inner.api_release()
    }

    /// Fetch a managed object property by name into the requested type.
    ///
    /// Works for both JSON and SOAP transports via [`extract_property`].
    pub async fn fetch_property<T>(&self, obj: ManagedObjectReference, property: &str) -> Result<T>
    where
        T: miniserde::Deserialize + 'static,
    {
        let type_name: &str = obj.r#type.as_str();
        let id = &obj.value;
        let pv_opt = self
            .inner
            .fetch_property_raw("", type_name, id, property)
            .await?;
        let pv = pv_opt.ok_or_else(|| {
            Error::ParseError(format!("property {property} was empty"))
        })?;
        extract_property(pv)
    }
}

impl VimClient for Client {
    fn service_content(&self) -> &ServiceContent {
        self.inner.service_content()
    }

    fn transport(&self) -> Transport {
        self.inner.transport()
    }

    fn api_release(&self) -> String {
        self.inner.api_release()
    }

    fn invoke<'a>(
        &'a self,
        svc: &'a str,
        mo_type: &'a str,
        mo_id: &'a str,
        method_name: &'a str,
        params: Option<&'a (dyn miniserde::Serialize + Send + Sync)>,
    ) -> BoxFuture<'a, Result<Bytes>> {
        self.inner.invoke(svc, mo_type, mo_id, method_name, params)
    }

    fn invoke_optional<'a>(
        &'a self,
        svc: &'a str,
        mo_type: &'a str,
        mo_id: &'a str,
        method_name: &'a str,
        params: Option<&'a (dyn miniserde::Serialize + Send + Sync)>,
    ) -> BoxFuture<'a, Result<Option<Bytes>>> {
        self.inner
            .invoke_optional(svc, mo_type, mo_id, method_name, params)
    }

    fn invoke_void<'a>(
        &'a self,
        svc: &'a str,
        mo_type: &'a str,
        mo_id: &'a str,
        method_name: &'a str,
        params: Option<&'a (dyn miniserde::Serialize + Send + Sync)>,
    ) -> BoxFuture<'a, Result<()>> {
        self.inner.invoke_void(svc, mo_type, mo_id, method_name, params)
    }

    fn fetch_property_raw<'a>(
        &'a self,
        svc: &'a str,
        mo_type: &'a str,
        mo_id: &'a str,
        property: &'a str,
    ) -> BoxFuture<'a, Result<Option<PropertyValue>>> {
        self.inner
            .fetch_property_raw(svc, mo_type, mo_id, property)
    }
}

pub(crate) struct JsonWireCtx<'a> {
    pub svc: &'a str,
    pub mo_type: &'a str,
    pub mo_id: &'a str,
    pub name: &'a str,
    pub path: &'a str,
    pub is_property_get: bool,
}

fn json_method_path(svc: &str, mo_type: &str, mo_id: &str, method_name: &str) -> String {
    if svc.is_empty() {
        format!("/{mo_type}/{mo_id}/{method_name}")
    } else {
        format!("/{svc}/{mo_type}/{mo_id}/{method_name}")
    }
}

pub(crate) struct JsonClient {
    http_client: reqwest::Client,
    session_key: Arc<RwLock<Option<String>>>,
    api_release: String,
    base_url: String,
    user_agent: String,
    service_content: Option<ServiceContent>,
    wire_logging: WireLoggingMode,
}

/// VI JSON API implementation (Hello + `/sdk/vim25/{release}`). Crate-private; users hold [`Client`].
///
/// Manages the session key header and logs out when the last [`Arc`] to this value is dropped.
impl JsonClient {
    pub(crate) fn service_content(&self) -> &ServiceContent {
        self.service_content.as_ref().expect("JsonClient missing ServiceContent")
    }

    pub(crate) fn api_release(&self) -> String {
        self.api_release.clone()
    }

    fn get_request(&self, path: &str) -> reqwest::RequestBuilder
    {
        debug!("GET request: {}", path);
        let url = format!("{}{}", self.base_url, path);
        self.http_client.get(&url)
    }

    /// Prepare POST request without a body
    pub(crate) fn post_bare(&self, path: &str) -> reqwest::RequestBuilder
    {
        debug!("POST request (void): {}", path);
        let url = format!("{}{}", self.base_url, path);
        self.http_client.post(&url)
    }

    fn build_post_request(
        &self,
        svc: &str,
        mo_type: &str,
        mo_id: &str,
        method_name: &str,
        params: Option<&(dyn miniserde::Serialize + Send + Sync)>,
    ) -> reqwest::RequestBuilder {
        let path = if svc.is_empty() {
            format!("/{mo_type}/{mo_id}/{method_name}")
        } else {
            format!("/{svc}/{mo_type}/{mo_id}/{method_name}")
        };
        match params {
            Some(payload) => {
                debug!("POST request: {}", path);
                let json_body = miniserde::json::to_string(payload);
                if self.wire_logging.is_enabled() {
                    let mode_l = wire_log::wire_mode_label(self.wire_logging, mo_type);
                    let deny = wire_log::body_logging_note(self.wire_logging, mo_type);
                    let deny_s = deny.unwrap_or("");
                    let deny_sep = if deny_s.is_empty() { "" } else { " " };
                    let mut msg = format!(
                        "wire=json mode={} phase=request svc=\"{}\" mo={} id={} method={} path={} body_bytes={}{}{}",
                        mode_l,
                        svc,
                        mo_type,
                        mo_id,
                        method_name,
                        path,
                        json_body.len(),
                        deny_sep,
                        deny_s
                    );
                    if wire_log::bodies_allowed(self.wire_logging, mo_type) {
                        msg.push_str(&format!(" body={}", json_body));
                    }
                    wire_log::log_json_line(
                        self.wire_logging,
                        mo_type,
                        wire_log::bodies_allowed(self.wire_logging, mo_type),
                        &msg,
                    );
                } else if log::log_enabled!(log::Level::Trace)
                    && !wire_log::suppress_legacy_transport_trace(self.wire_logging)
                {
                    trace!("POST payload: {}", json_body);
                }
                let url = format!("{}{}", self.base_url, path);
                self.http_client
                    .post(&url)
                    .header("Content-Type", "application/json")
                    .body(json_body)
            }
            None => {
                if self.wire_logging.is_enabled() {
                    let mode_l = wire_log::wire_mode_label(self.wire_logging, mo_type);
                    let deny = wire_log::body_logging_note(self.wire_logging, mo_type);
                    let deny_s = deny.unwrap_or("");
                    let deny_sep = if deny_s.is_empty() { "" } else { " " };
                    let msg = format!(
                        "wire=json mode={} phase=request svc=\"{}\" mo={} id={} method={} path={} body_bytes=0{}{}",
                        mode_l,
                        svc,
                        mo_type,
                        mo_id,
                        method_name,
                        path,
                        deny_sep,
                        deny_s
                    );
                    wire_log::log_json_line(self.wire_logging, mo_type, false, &msg);
                }
                self.post_bare(&path)
            }
        }
    }

    /// Execute a request that does not return a response body
    pub(crate) async fn execute_void(
        &self,
        mut req: reqwest::RequestBuilder,
        ctx: Option<JsonWireCtx<'_>>,
        started: Instant,
    ) -> Result<()> {
        req = self.prepare(req).await;
        let res = match req.send().await {
            Ok(r) => r,
            Err(e) => {
                if self.wire_logging.is_enabled() {
                    if let Some(c) = ctx.as_ref() {
                        wire_log::log_json_transport_failure(
                            self.wire_logging,
                            c.svc,
                            c.mo_type,
                            c.mo_id,
                            c.name,
                            c.path,
                            c.is_property_get,
                            started.elapsed(),
                            &e,
                        );
                    }
                }
                return Err(Error::ReqwestError(e));
            }
        };
        let res = self
            .process_response(res, Some(started), ctx.as_ref())
            .await?;
        if self.wire_logging.is_enabled() {
            if let Some(c) = ctx.as_ref() {
                let mode_l = wire_log::wire_mode_label(self.wire_logging, c.mo_type);
                let deny = wire_log::body_logging_note(self.wire_logging, c.mo_type);
                let deny_s = deny.unwrap_or("");
                let deny_sep = if deny_s.is_empty() { "" } else { " " };
                let name_field = format!("method={}", c.name);
                let msg = format!(
                    "wire=json mode={} phase=response svc=\"{}\" mo={} id={} {} path={} status={} body_bytes=0 duration_ms={}{}{}",
                    mode_l,
                    c.svc,
                    c.mo_type,
                    c.mo_id,
                    name_field,
                    c.path,
                    res.status().as_u16(),
                    started.elapsed().as_millis(),
                    deny_sep,
                    deny_s
                );
                wire_log::log_json_line(self.wire_logging, c.mo_type, false, &msg);
            }
        }
        Ok(())
    }

    /// Add authn header to request
    async fn prepare(&self, mut req: reqwest::RequestBuilder) -> reqwest::RequestBuilder {
        let session_key = self.session_key.read().await;
        if let Some(value) = session_key.as_ref() {
            req = req.header(AUTHN_HEADER, value);
        }
        req = req.header("User-Agent", &self.user_agent);
        req
    }

    /// Handle authn header update and error unmarsalling
    async fn process_response(
        &self,
        res: reqwest::Response,
        started: Option<Instant>,
        ctx: Option<&JsonWireCtx<'_>>,
    ) -> Result<reqwest::Response> {
        if res.status().is_success() && res.headers().contains_key(AUTHN_HEADER) {
            let session_key = res.headers().get(AUTHN_HEADER).unwrap().to_str().map_err(|_| Error::MissingOrInvalidSessionKey)?.to_string();
            let mut key_holder = self.session_key.write().await;
            *key_holder = Some(session_key);
        }
        if !res.status().is_success() {
            warn!("HTTP error: {}", res.status());
            let status = res.status();
            let body = res.text().await?;
            if let (Some(start), Some(c)) = (started, ctx) {
                if self.wire_logging.is_enabled() {
                    wire_log::log_json_http_error(
                        self.wire_logging,
                        c.svc,
                        c.mo_type,
                        c.mo_id,
                        c.name,
                        c.path,
                        c.is_property_get,
                        status,
                        &body,
                        start.elapsed(),
                    );
                }
            }
            let fault: structs::MethodFault = miniserde::json::from_str(&body)
                .map_err(|_| Error::ParseError(format!("Failed to parse MethodFault from error response: {}", &body[..body.len().min(200)])))?;
            return Err(Error::MethodFault(fault));
        }
        Ok(res)
    }
}

impl VimClient for JsonClient {
    fn service_content(&self) -> &ServiceContent {
        JsonClient::service_content(self)
    }

    fn transport(&self) -> Transport {
        Transport::Json
    }

    fn api_release(&self) -> String {
        JsonClient::api_release(self)
    }

    fn invoke<'a>(
        &'a self,
        svc: &'a str,
        mo_type: &'a str,
        mo_id: &'a str,
        method_name: &'a str,
        params: Option<&'a (dyn miniserde::Serialize + Send + Sync)>,
    ) -> BoxFuture<'a, Result<Bytes>> {
        Box::pin(async move {
            let path_str = json_method_path(svc, mo_type, mo_id, method_name);
            let ctx = JsonWireCtx {
                svc,
                mo_type,
                mo_id,
                name: method_name,
                path: path_str.as_str(),
                is_property_get: false,
            };
            let started = Instant::now();
            let req = self.build_post_request(svc, mo_type, mo_id, method_name, params);
            let req = self.prepare(req).await;
            let res = match req.send().await {
                Ok(r) => r,
                Err(e) => {
                    wire_log::log_json_transport_failure(
                        self.wire_logging,
                        svc,
                        mo_type,
                        mo_id,
                        method_name,
                        path_str.as_str(),
                        false,
                        started.elapsed(),
                        &e,
                    );
                    return Err(Error::ReqwestError(e));
                }
            };
            let res = self
                .process_response(res, Some(started), Some(&ctx))
                .await?;
            let http_status = res.status();
            let bytes = res.bytes().await?;
            if self.wire_logging.is_enabled() {
                let mode_l = wire_log::wire_mode_label(self.wire_logging, mo_type);
                let deny = wire_log::body_logging_note(self.wire_logging, mo_type);
                let deny_s = deny.unwrap_or("");
                let deny_sep = if deny_s.is_empty() { "" } else { " " };
                let body_lossy = wire_log::sanitize_utf8(&bytes);
                let mut msg = format!(
                    "wire=json mode={} phase=response svc=\"{}\" mo={} id={} method={} path={} status={} body_bytes={} duration_ms={}{}{}",
                    mode_l,
                    svc,
                    mo_type,
                    mo_id,
                    method_name,
                    path_str,
                    http_status.as_u16(),
                    bytes.len(),
                    started.elapsed().as_millis(),
                    deny_sep,
                    deny_s
                );
                if wire_log::bodies_allowed(self.wire_logging, mo_type) {
                    msg.push_str(&format!(" body={}", body_lossy));
                }
                wire_log::log_json_line(
                    self.wire_logging,
                    mo_type,
                    wire_log::bodies_allowed(self.wire_logging, mo_type),
                    &msg,
                );
            } else if log::log_enabled!(log::Level::Trace)
                && !wire_log::suppress_legacy_transport_trace(self.wire_logging)
            {
                let body = String::from_utf8_lossy(&bytes);
                trace!(
                    "JSON response from {}/{}: {}...",
                    mo_type,
                    method_name,
                    &body[..body.len().min(2000)]
                );
            }
            Ok(bytes)
        })
    }

    fn invoke_optional<'a>(
        &'a self,
        svc: &'a str,
        mo_type: &'a str,
        mo_id: &'a str,
        method_name: &'a str,
        params: Option<&'a (dyn miniserde::Serialize + Send + Sync)>,
    ) -> BoxFuture<'a, Result<Option<Bytes>>> {
        Box::pin(async move {
            let path_str = json_method_path(svc, mo_type, mo_id, method_name);
            let ctx = JsonWireCtx {
                svc,
                mo_type,
                mo_id,
                name: method_name,
                path: path_str.as_str(),
                is_property_get: false,
            };
            let started = Instant::now();
            let req = self.build_post_request(svc, mo_type, mo_id, method_name, params);
            let req = self.prepare(req).await;
            let res = match req.send().await {
                Ok(r) => r,
                Err(e) => {
                    wire_log::log_json_transport_failure(
                        self.wire_logging,
                        svc,
                        mo_type,
                        mo_id,
                        method_name,
                        path_str.as_str(),
                        false,
                        started.elapsed(),
                        &e,
                    );
                    return Err(Error::ReqwestError(e));
                }
            };
            let res = self
                .process_response(res, Some(started), Some(&ctx))
                .await?;
            let http_status = res.status();
            let bytes = res.bytes().await?;
            if self.wire_logging.is_enabled() && !bytes.is_empty() {
                let mode_l = wire_log::wire_mode_label(self.wire_logging, mo_type);
                let deny = wire_log::body_logging_note(self.wire_logging, mo_type);
                let deny_s = deny.unwrap_or("");
                let deny_sep = if deny_s.is_empty() { "" } else { " " };
                let body_lossy = wire_log::sanitize_utf8(&bytes);
                let mut msg = format!(
                    "wire=json mode={} phase=response svc=\"{}\" mo={} id={} method={} path={} status={} body_bytes={} duration_ms={}{}{}",
                    mode_l,
                    svc,
                    mo_type,
                    mo_id,
                    method_name,
                    path_str,
                    http_status.as_u16(),
                    bytes.len(),
                    started.elapsed().as_millis(),
                    deny_sep,
                    deny_s
                );
                if wire_log::bodies_allowed(self.wire_logging, mo_type) {
                    msg.push_str(&format!(" body={}", body_lossy));
                }
                wire_log::log_json_line(
                    self.wire_logging,
                    mo_type,
                    wire_log::bodies_allowed(self.wire_logging, mo_type),
                    &msg,
                );
            } else if log::log_enabled!(log::Level::Trace)
                && !bytes.is_empty()
                && !wire_log::suppress_legacy_transport_trace(self.wire_logging)
            {
                let body = String::from_utf8_lossy(&bytes);
                trace!(
                    "JSON response from {}/{}: {}...",
                    mo_type,
                    method_name,
                    &body[..body.len().min(2000)]
                );
            }
            if bytes.is_empty() {
                Ok(None)
            } else {
                Ok(Some(bytes))
            }
        })
    }

    fn invoke_void<'a>(
        &'a self,
        svc: &'a str,
        mo_type: &'a str,
        mo_id: &'a str,
        method_name: &'a str,
        params: Option<&'a (dyn miniserde::Serialize + Send + Sync)>,
    ) -> BoxFuture<'a, Result<()>> {
        Box::pin(async move {
            let path_str = json_method_path(svc, mo_type, mo_id, method_name);
            let ctx = JsonWireCtx {
                svc,
                mo_type,
                mo_id,
                name: method_name,
                path: path_str.as_str(),
                is_property_get: false,
            };
            let started = Instant::now();
            let req = self.build_post_request(svc, mo_type, mo_id, method_name, params);
            JsonClient::execute_void(self, req, Some(ctx), started).await
        })
    }

    fn fetch_property_raw<'a>(
        &'a self,
        svc: &'a str,
        mo_type: &'a str,
        mo_id: &'a str,
        property: &'a str,
    ) -> BoxFuture<'a, Result<Option<PropertyValue>>> {
        Box::pin(async move {
            let path = if svc.is_empty() {
                format!("/{mo_type}/{mo_id}/{property}")
            } else {
                format!("/{svc}/{mo_type}/{mo_id}/{property}")
            };
            let ctx = JsonWireCtx {
                svc,
                mo_type,
                mo_id,
                name: property,
                path: path.as_str(),
                is_property_get: true,
            };
            let started = Instant::now();
            if self.wire_logging.is_enabled() {
                let mode_l = wire_log::wire_mode_label(self.wire_logging, mo_type);
                let deny = wire_log::body_logging_note(self.wire_logging, mo_type);
                let deny_s = deny.unwrap_or("");
                let deny_sep = if deny_s.is_empty() { "" } else { " " };
                let msg = format!(
                    "wire=json mode={} phase=request svc=\"{}\" mo={} id={} property={} path={} body_bytes=0{}{}",
                    mode_l,
                    svc,
                    mo_type,
                    mo_id,
                    property,
                    path,
                    deny_sep,
                    deny_s
                );
                wire_log::log_json_line(self.wire_logging, mo_type, false, &msg);
            }
            let req = self.get_request(&path);
            let req = self.prepare(req).await;
            let res = match req.send().await {
                Ok(r) => r,
                Err(e) => {
                    wire_log::log_json_transport_failure(
                        self.wire_logging,
                        svc,
                        mo_type,
                        mo_id,
                        property,
                        path.as_str(),
                        true,
                        started.elapsed(),
                        &e,
                    );
                    return Err(Error::ReqwestError(e));
                }
            };
            let res = self
                .process_response(res, Some(started), Some(&ctx))
                .await?;
            let http_status = res.status();
            let bytes = res.bytes().await?;
            if self.wire_logging.is_enabled() && !bytes.is_empty() {
                let mode_l = wire_log::wire_mode_label(self.wire_logging, mo_type);
                let deny = wire_log::body_logging_note(self.wire_logging, mo_type);
                let deny_s = deny.unwrap_or("");
                let deny_sep = if deny_s.is_empty() { "" } else { " " };
                let body_lossy = wire_log::sanitize_utf8(&bytes);
                let mut msg = format!(
                    "wire=json mode={} phase=response svc=\"{}\" mo={} id={} property={} path={} status={} body_bytes={} duration_ms={}{}{}",
                    mode_l,
                    svc,
                    mo_type,
                    mo_id,
                    property,
                    path,
                    http_status.as_u16(),
                    bytes.len(),
                    started.elapsed().as_millis(),
                    deny_sep,
                    deny_s
                );
                if wire_log::bodies_allowed(self.wire_logging, mo_type) {
                    msg.push_str(&format!(" body={}", body_lossy));
                }
                wire_log::log_json_line(
                    self.wire_logging,
                    mo_type,
                    wire_log::bodies_allowed(self.wire_logging, mo_type),
                    &msg,
                );
            } else if log::log_enabled!(log::Level::Trace)
                && !bytes.is_empty()
                && !wire_log::suppress_legacy_transport_trace(self.wire_logging)
            {
                let body = String::from_utf8_lossy(&bytes);
                trace!(
                    "JSON fetch_property_raw {}/{}: {}...",
                    mo_type,
                    property,
                    &body[..body.len().min(2000)]
                );
            }
            if bytes.is_empty() {
                Ok(None)
            } else {
                Ok(Some(PropertyValue::Json(bytes)))
            }
        })
    }
}


/// Log out the JSON session when the last strong reference to this [`JsonClient`] is dropped.
impl Drop for JsonClient {
    fn drop(&mut self) {
        debug!("Disposing VIM client.");

        let session_key = Arc::clone(&self.session_key);
        let http_client = &self.http_client.clone();
        let base_url = self.base_url.clone();
        let wire_logging = self.wire_logging;

        let sm_id = self.service_content.as_ref().and_then(|content| content.session_manager.as_ref().map(|moid| moid.value.clone()));
        let sm_id = match sm_id {
            Some(id) => id,
            None => {
                debug!("No session manager found. Skipping logout.");
                return;
            },
        };

        tokio::task::block_in_place(|| {
            tokio::runtime::Handle::current().block_on(async move {
                debug!("Terminating VIM session as needed.");
                let key = {
                    let session_key = session_key.read().await;
                    session_key.clone()
                };
                let Some(key) = key else {
                    debug!("No session key present. Skipping logout.");
                    return;
                };
                debug!("Session is present. Sending logout request...");

                let path = format!("{base_url}/SessionManager/{moId}/Logout",
                                    base_url = base_url,
                                    moId = sm_id);
                if wire_logging.is_enabled() {
                    let mode_l = wire_log::wire_mode_label(wire_logging, "SessionManager");
                    let msg = format!(
                        "wire=json mode={} phase=request kind=logout mo=SessionManager id={} method=Logout path={} body_bytes=0 body_logging=denylisted",
                        mode_l,
                        sm_id,
                        path
                    );
                    wire_log::log_json_line(wire_logging, "SessionManager", false, &msg);
                }
                let req = http_client.post(&path)
                                        .header(AUTHN_HEADER, key);
                let started = Instant::now();
                match req.send().await {
                    Ok(resp) => {
                        let status = resp.status();
                        let body = resp.text().await.unwrap_or_default();
                        let dur = started.elapsed();
                        if wire_logging.is_enabled() {
                            let mode_l = wire_log::wire_mode_label(wire_logging, "SessionManager");
                            let http_note = if status.is_success() {
                                ""
                            } else {
                                " error=http_failure"
                            };
                            let msg = format!(
                                "wire=json mode={} phase=response kind=logout mo=SessionManager id={} method=Logout status={} body_bytes={} duration_ms={} body_logging=denylisted{}",
                                mode_l,
                                sm_id,
                                status.as_u16(),
                                body.len(),
                                dur.as_millis(),
                                http_note
                            );
                            wire_log::log_json_line(wire_logging, "SessionManager", false, &msg);
                        }
                        if status.is_success() {
                            debug!("Session logged out successfully");
                        } else {
                            match miniserde::json::from_str::<structs::MethodFault>(&body) {
                                Ok(fault) => warn!("Failed to logout session(HTTP code: {}). MethodFault: {:?}", status, fault),
                                Err(_) => warn!("Failed to logout session(HTTP code: {}). Cannot parse MethodFault: {}", status, &body[..body.len().min(200)]),
                            }
                        }
                    }
                    Err(e) => {
                        if wire_logging.is_enabled() {
                            let mode_l = wire_log::wire_mode_label(wire_logging, "SessionManager");
                            let msg = format!(
                                "wire=json mode={} phase=response kind=logout mo=SessionManager id={} method=Logout error=transport duration_ms={} body_logging=denylisted detail={}",
                                mode_l,
                                sm_id,
                                started.elapsed().as_millis(),
                                e
                            );
                            wire_log::log_json_line(wire_logging, "SessionManager", false, &msg);
                        }
                        warn!("Failed to logout session. Cannot execute logout request: {}", e);
                    }
                }
            });
        });
    }
}

fn user_agent(app_name: Option<&str>, app_version: Option<&str>) -> String {
    let app_name: String = if app_name.is_some() {
        app_name.unwrap().to_string()
    } else {
        get_executable_name().unwrap_or_else(|| "unknown".to_string())
    };
    let Some(appv) = app_version else {
        return format!(
            "{} ({}/{}; {}; {}; rustc/{})",
            app_name,
            LIB_NAME,
            LIB_VERSION,
            std::env::consts::OS,
            std::env::consts::ARCH,
            RUSTC_VERSION
        );
    };
    format!(
        "{}/{} ({}/{}; {}; {}; rustc/{})",
        app_name,
        appv,
        LIB_NAME,
        LIB_VERSION,
        std::env::consts::OS,
        std::env::consts::ARCH,
        RUSTC_VERSION
    )
}

fn get_executable_name() -> Option<String> {
    std::env::current_exe()
        .ok()
        .as_ref()
        .and_then(|path| path.file_name())
        .and_then(OsStr::to_str)
        .map(|s| s.to_owned())
}


/// The Hello System API request. This is not full-fledged binding but a simple request to
/// negotiate the API release version.
/// See [Hello System](https://developer.broadcom.com/xapis/vsphere-automation-api/latest/vcenter/api/vcenter/system__action=hello/post/index)
struct HelloSpec<'a> {
    /// List of API release IDs that the client can work with in order of preference. The server will select the first mutually supported release ID.
    api_releases: &'a Vec<String>,
}

impl miniserde::Serialize for HelloSpec<'_> {
    fn begin(&self) -> miniserde::ser::Fragment<'_> {
        use miniserde::ser::Fragment;
        Fragment::Map(Box::new(HelloSpecSerializer { data: self, seq: 0 }))
    }
}

struct HelloSpecSerializer<'a> {
    data: &'a HelloSpec<'a>,
    seq: usize,
}

impl miniserde::ser::Map for HelloSpecSerializer<'_> {
    fn next(&mut self) -> Option<(std::borrow::Cow<'_, str>, &dyn miniserde::Serialize)> {
        let result = match self.seq {
            0 => Some((std::borrow::Cow::Borrowed("api_releases"), &self.data.api_releases as &dyn miniserde::Serialize)),
            _ => None,
        };
        self.seq += 1;
        result
    }
}

impl std::fmt::Debug for HelloSpec<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("HelloSpec")
            .field("api_releases", &self.api_releases)
            .finish()
    }
}

/// The Hello System API response. This is not full-fledged binding but a simple response to
/// negotiate the API release version.
struct HelloResult {
    /// The ID of a mutually-supported API release. This ID should be used in subsequent API calls
    /// to the current vCenter system. If there is no mutually-supported API release, the value will
    /// be an empty string, e.g. "". Typically, this is a case where one of the parties is much
    /// older than the other party.
    api_release: String,
}

miniserde::make_place!(Place);

impl miniserde::Deserialize for HelloResult {
    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
        Place::new(out)
    }
}

impl miniserde::de::Visitor for Place<HelloResult> {
    fn map(&mut self) -> miniserde::Result<Box<dyn miniserde::de::Map + '_>> {
        Ok(Box::new(HelloResultFields {
            api_release: None,
            __out: &mut self.out,
        }))
    }
}

struct HelloResultFields<'a> {
    api_release: Option<String>,
    __out: &'a mut Option<HelloResult>,
}

impl miniserde::de::Map for HelloResultFields<'_> {
    fn key(&mut self, k: &str) -> miniserde::Result<&mut dyn miniserde::de::Visitor> {
        match k {
            "api_release" => Ok(miniserde::Deserialize::begin(&mut self.api_release)),
            _ => Ok(<dyn miniserde::de::Visitor>::ignore()),
        }
    }

    fn finish(&mut self) -> miniserde::Result<()> {
        let api_release = self.api_release.take().ok_or(miniserde::Error)?;
        *self.__out = Some(HelloResult { api_release });
        Ok(())
    }
}

impl std::fmt::Debug for HelloResult {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("HelloResult")
            .field("api_release", &self.api_release)
            .finish()
    }
}

/// Shared by wire-logging transport tests (`core` + `xml` client).
#[cfg(test)]
pub(crate) fn test_dead_port_http_client() -> reqwest::Client {
    #[cfg(feature = "xml")]
    {
        return reqwest::Client::builder()
            .cookie_store(true)
            .connect_timeout(std::time::Duration::from_millis(500))
            .timeout(std::time::Duration::from_secs(3))
            .build()
            .expect("reqwest test client");
    }
    #[cfg(not(feature = "xml"))]
    {
        reqwest::Client::builder()
            .connect_timeout(std::time::Duration::from_millis(500))
            .timeout(std::time::Duration::from_secs(3))
            .build()
            .expect("reqwest test client")
    }
}

/// Unbound TCP address used to force `send()` transport failures quickly.
#[cfg(test)]
pub(crate) const TEST_WIRE_DEAD_ADDR: &str = "127.0.0.1:65433";

#[cfg(test)]
pub(crate) fn test_minimal_service_content_for_tests() -> ServiceContent {
    let json = r#"{
        "_typeName": "ServiceContent",
        "rootFolder": {"_typeName":"ManagedObjectReference","type":"Folder","value":"root-1"},
        "propertyCollector": {"_typeName":"ManagedObjectReference","type":"PropertyCollector","value":"pc-1"},
        "viewManager": {"_typeName":"ManagedObjectReference","type":"ViewManager","value":"vmgr-1"},
        "about": {
            "_typeName":"AboutInfo",
            "name":"n",
            "fullName":"f",
            "vendor":"v",
            "version":"1",
            "build":"b",
            "osType":"o",
            "productLineId":"p",
            "apiType":"VirtualCenter",
            "apiVersion":"1"
        }
    }"#;
    miniserde::json::from_str(json).expect("fixture ServiceContent")
}

#[cfg(test)]
pub(crate) fn test_json_client_wire_transport() -> Arc<JsonClient> {
    let base_url = format!("https://{}/sdk/vim25/{}", TEST_WIRE_DEAD_ADDR, API_RELEASE);
    Arc::new(JsonClient {
        http_client: test_dead_port_http_client(),
        session_key: Arc::new(RwLock::new(None)),
        api_release: API_RELEASE.to_string(),
        base_url,
        user_agent: "wire-transport-test".to_string(),
        service_content: Some(test_minimal_service_content_for_tests()),
        wire_logging: WireLoggingMode::Summary,
    })
}

/// [`ServiceContent`] with `sessionManager` set (for JSON logout-on-`Drop` tests).
#[cfg(test)]
pub(crate) fn test_service_content_with_session_manager_for_tests() -> ServiceContent {
    let json = r#"{
        "_typeName": "ServiceContent",
        "rootFolder": {"_typeName":"ManagedObjectReference","type":"Folder","value":"root-1"},
        "propertyCollector": {"_typeName":"ManagedObjectReference","type":"PropertyCollector","value":"pc-1"},
        "viewManager": {"_typeName":"ManagedObjectReference","type":"ViewManager","value":"vmgr-1"},
        "sessionManager": {"_typeName":"ManagedObjectReference","type":"SessionManager","value":"sm-wire-test"},
        "about": {
            "_typeName":"AboutInfo",
            "name":"n",
            "fullName":"f",
            "vendor":"v",
            "version":"1",
            "build":"b",
            "osType":"o",
            "productLineId":"p",
            "apiType":"VirtualCenter",
            "apiVersion":"1"
        }
    }"#;
    miniserde::json::from_str(json).expect("fixture ServiceContent with session manager")
}

/// JSON VI client pointing at `http_origin` (e.g. `http://127.0.0.1:PORT`) for local stub servers.
#[cfg(test)]
pub(crate) fn test_json_client_http_origin(
    http_origin: &str,
    session_key: Option<String>,
) -> Arc<JsonClient> {
    let base_url = format!("{http_origin}/sdk/vim25/{API_RELEASE}");
    Arc::new(JsonClient {
        http_client: reqwest::Client::builder()
            .timeout(std::time::Duration::from_secs(5))
            .build()
            .expect("reqwest test client"),
        session_key: Arc::new(RwLock::new(session_key)),
        api_release: API_RELEASE.to_string(),
        base_url,
        user_agent: "wire-http-test".to_string(),
        service_content: Some(test_service_content_with_session_manager_for_tests()),
        wire_logging: WireLoggingMode::Summary,
    })
}

/// Wire logging integration tests: transport failures, HTTP error responses (`log_json_http_error`), and
/// logout-on-`Drop` paths (JSON + SOAP). Uses a multi-threaded Tokio runtime for every async test because
/// `JsonClient` / `SoapClient` `Drop` uses `block_in_place` + `block_on`. Serializes tests that share the
/// global `log` sink and a localhost HTTP stub on a background OS thread for deterministic responses.
#[cfg(test)]
mod wire_logging_transport_tests {
    use std::sync::{Mutex, Once};

    use std::time::Instant;

    use super::super::wire_log;
    use super::{
        test_dead_port_http_client, test_json_client_wire_transport, ClientBuilder, Error, JsonClient,
        VimClient, WireLoggingMode, TEST_WIRE_DEAD_ADDR,
    };
    use miniserde::Serialize;

    /// Matches [`crate::core::wire_log::TARGET_SOAP`] without importing the `xml`-gated symbol.
    const SOAP_WIRE_TARGET: &str = "vim_rs::wire::soap";

    static LOG_INIT: Once = Once::new();
    static SERIAL: Mutex<()> = Mutex::new(());
    static WIRE_LINES: Mutex<Vec<String>> = Mutex::new(Vec::new());

    struct CaptureWireLogger;

    impl log::Log for CaptureWireLogger {
        fn enabled(&self, _: &log::Metadata<'_>) -> bool {
            true
        }

        fn log(&self, record: &log::Record<'_>) {
            let t = record.target();
            if t == wire_log::TARGET_JSON || t == SOAP_WIRE_TARGET {
                WIRE_LINES
                    .lock()
                    .expect("wire log capture")
                    .push(record.args().to_string());
            }
        }

        fn flush(&self) {}
    }

    fn init_wire_capture() {
        LOG_INIT.call_once(|| {
            let _ = log::set_logger(&CaptureWireLogger);
            log::set_max_level(log::LevelFilter::Trace);
        });
    }

    fn clear_wire_lines() {
        WIRE_LINES.lock().expect("wire lines").clear();
    }

    fn joined_wire_output() -> String {
        WIRE_LINES.lock().expect("wire lines").join("\n")
    }

    /// Empty JSON object `{}` for `invoke` paths that use a POST body.
    struct EmptyJsonObject;
    struct EmptyJsonMap;

    impl Serialize for EmptyJsonObject {
        fn begin(&self) -> miniserde::ser::Fragment<'_> {
            miniserde::ser::Fragment::Map(Box::new(EmptyJsonMap))
        }
    }

    impl miniserde::ser::Map for EmptyJsonMap {
        fn next(&mut self) -> Option<(std::borrow::Cow<'_, str>, &dyn Serialize)> {
            None
        }
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn json_hello_negotiate_and_invoke_paths_log_transport_failure() {
        let _serial = SERIAL.lock().expect("serial");
        init_wire_capture();
        clear_wire_lines();

        // Hello System negotiation (no fixed api_release → must POST hello).
        let hello_err = ClientBuilder::new(TEST_WIRE_DEAD_ADDR)
            .http_client(test_dead_port_http_client())
            .wire_logging(WireLoggingMode::Summary)
            .build()
            .await;
        assert!(
            matches!(hello_err.as_ref(), Err(Error::ReqwestError(_))),
            "expected transport error from hello, got {:?}",
            hello_err.as_ref().err()
        );
        let out = joined_wire_output();
        assert!(
            out.contains("phase=request kind=negotiate"),
            "hello request line missing: {out}"
        );
        assert!(
            out.contains("phase=response kind=negotiate") && out.contains("error=transport"),
            "hello transport response line missing: {out}"
        );

        clear_wire_lines();
        let jc = test_json_client_wire_transport();
        let empty = EmptyJsonObject;

        let invoke_err = jc
            .invoke(
                "",
                "VirtualMachine",
                "vm-1",
                "RefreshStorageInfo",
                Some(&empty as &(dyn Serialize + Send + Sync)),
            )
            .await;
        assert!(matches!(invoke_err, Err(Error::ReqwestError(_))));
        let out = joined_wire_output();
        assert!(
            out.contains("phase=request") && out.contains("method=RefreshStorageInfo"),
            "invoke request missing: {out}"
        );
        assert!(
            out.contains("phase=response") && out.contains("error=transport"),
            "invoke transport response missing: {out}"
        );

        clear_wire_lines();
        let opt_err = jc
            .invoke_optional(
                "",
                "VirtualMachine",
                "vm-1",
                "SomeMethod",
                Some(&empty as &(dyn Serialize + Send + Sync)),
            )
            .await;
        assert!(matches!(opt_err, Err(Error::ReqwestError(_))));
        let out = joined_wire_output();
        assert!(out.contains("error=transport"), "invoke_optional: {out}");

        clear_wire_lines();
        let void_err = jc
            .invoke_void("", "VirtualMachine", "vm-1", "Destroy", None)
            .await;
        assert!(matches!(void_err, Err(Error::ReqwestError(_))));
        let out = joined_wire_output();
        assert!(
            out.contains("phase=request") && out.contains("body_bytes=0"),
            "void request: {out}"
        );
        assert!(out.contains("error=transport"), "void transport: {out}");

        clear_wire_lines();
        let fetch_err = jc
            .fetch_property_raw("", "VirtualMachine", "vm-1", "name")
            .await;
        assert!(matches!(fetch_err, Err(Error::ReqwestError(_))));
        let out = joined_wire_output();
        assert!(
            out.contains("property=name") && out.contains("error=transport"),
            "fetch_property_raw: {out}"
        );
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn json_execute_void_helper_logs_transport_failure() {
        let _serial = SERIAL.lock().expect("serial");
        init_wire_capture();
        clear_wire_lines();

        let jc = test_json_client_wire_transport();
        let path_str = super::json_method_path("", "Folder", "group-d1", "Destroy");
        let ctx = super::JsonWireCtx {
            svc: "",
            mo_type: "Folder",
            mo_id: "group-d1",
            name: "Destroy",
            path: path_str.as_str(),
            is_property_get: false,
        };
        let started = Instant::now();
        let req = jc.build_post_request("", "Folder", "group-d1", "Destroy", None);
        let req = jc.prepare(req).await;
        let err = JsonClient::execute_void(jc.as_ref(), req, Some(ctx), started).await;
        assert!(matches!(err, Err(Error::ReqwestError(_))));
        let out = joined_wire_output();
        assert!(out.contains("method=Destroy") && out.contains("error=transport"), "{out}");
    }

    #[cfg(feature = "xml")]
    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn json_auto_probe_logs_transport_on_hello_send_failure() {
        let _serial = SERIAL.lock().expect("serial");
        init_wire_capture();
        clear_wire_lines();

        let err = ClientBuilder::new(TEST_WIRE_DEAD_ADDR)
            .http_client(test_dead_port_http_client())
            .wire_logging(WireLoggingMode::Summary)
            .transport(super::TransportMode::Auto)
            .build()
            .await;
        assert!(err.is_err(), "expected auto build to fail");
        let out = joined_wire_output();
        assert!(
            out.contains("kind=probe") && out.contains("error=transport"),
            "auto probe transport line missing: {out}"
        );
    }

    /// One-shot HTTP/1.1 stub on a background thread (avoids deadlocks with `Drop` + `block_on`).
    fn spawn_http_stub_once(status: u16, body: &[u8]) -> (String, std::thread::JoinHandle<()>) {
        use std::io::{Read, Write};
        use std::net::TcpListener;
        use std::thread;
        use std::time::Duration;

        let listener = TcpListener::bind("127.0.0.1:0").expect("bind http stub");
        let port = listener.local_addr().expect("stub addr").port();
        let body = body.to_vec();
        let h = thread::spawn(move || {
            let (mut stream, _) = listener.accept().expect("stub accept");
            let _ = stream.set_read_timeout(Some(Duration::from_secs(5)));
            let mut buf = vec![0u8; 32768];
            let _ = stream.read(&mut buf);
            let status_text = match status {
                200 => "OK",
                403 => "Forbidden",
                500 => "Internal Server Error",
                503 => "Service Unavailable",
                _ => "Error",
            };
            let head = format!(
                "HTTP/1.1 {} {}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
                status,
                status_text,
                body.len()
            );
            let mut out = head.into_bytes();
            out.extend_from_slice(&body);
            let _ = stream.write_all(&out);
        });
        (format!("http://127.0.0.1:{port}"), h)
    }

    /// Minimal [`MethodFault`] JSON (parses via `process_response`).
    const SAMPLE_FAULT_JSON: &str = r#"{"_typeName":"VAppPropertyFault","id":"x","category":"string","label":"l","type":"string","value":"v"}"#;

    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn json_invoke_non_success_emits_wire_http_error_line() {
        let _serial = SERIAL.lock().expect("serial");
        init_wire_capture();
        clear_wire_lines();

        let (origin, stub) = spawn_http_stub_once(500, SAMPLE_FAULT_JSON.as_bytes());
        let jc = super::test_json_client_http_origin(&origin, Some("sess".into()));
        let empty = EmptyJsonObject;
        let err = jc
            .invoke(
                "",
                "VirtualMachine",
                "vm-1",
                "SomeMethod",
                Some(&empty as &(dyn Serialize + Send + Sync)),
            )
            .await;
        assert!(matches!(err, Err(Error::MethodFault(_))));
        stub.join().expect("stub thread");
        let out = joined_wire_output();
        assert!(out.contains("phase=request") && out.contains("method=SomeMethod"), "{out}");
        assert!(
            out.contains("status=500") && out.contains("phase=response"),
            "expected log_json_http_error-style line: {out}"
        );
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn json_fetch_property_non_success_emits_wire_http_error_line() {
        let _serial = SERIAL.lock().expect("serial");
        init_wire_capture();
        clear_wire_lines();

        let (origin, stub) = spawn_http_stub_once(403, SAMPLE_FAULT_JSON.as_bytes());
        let jc = super::test_json_client_http_origin(&origin, Some("sess".into()));
        let err = jc
            .fetch_property_raw("", "HostSystem", "host-9", "name")
            .await;
        assert!(matches!(err, Err(Error::MethodFault(_))));
        stub.join().expect("stub thread");
        let out = joined_wire_output();
        assert!(out.contains("property=name") && out.contains("status=403"), "{out}");
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn json_drop_logout_emits_wire_lines_on_http_success() {
        let _serial = SERIAL.lock().expect("serial");
        init_wire_capture();
        clear_wire_lines();

        let (origin, stub) = spawn_http_stub_once(200, b"");
        let jc = super::test_json_client_http_origin(&origin, Some("sk-drop-ok".into()));
        drop(jc);
        stub.join().expect("stub thread");
        let out = joined_wire_output();
        assert!(out.contains("kind=logout") && out.contains("phase=request"), "{out}");
        assert!(
            out.contains("status=200") && !out.contains("error=http_failure"),
            "{out}"
        );
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn json_drop_logout_emits_wire_lines_on_http_non_success() {
        let _serial = SERIAL.lock().expect("serial");
        init_wire_capture();
        clear_wire_lines();

        let (origin, stub) = spawn_http_stub_once(503, b"x");
        let jc = super::test_json_client_http_origin(&origin, Some("sk-drop-bad".into()));
        drop(jc);
        stub.join().expect("stub thread");
        let out = joined_wire_output();
        assert!(
            out.contains("kind=logout") && out.contains("error=http_failure"),
            "{out}"
        );
        assert!(out.contains("status=503"), "{out}");
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn json_drop_logout_emits_wire_transport_line() {
        let _serial = SERIAL.lock().expect("serial");
        init_wire_capture();
        clear_wire_lines();

        let origin = format!("http://{}", super::TEST_WIRE_DEAD_ADDR);
        let jc = super::test_json_client_http_origin(&origin, Some("sk-drop-tr".into()));
        drop(jc);
        let out = joined_wire_output();
        assert!(
            out.contains("kind=logout") && out.contains("error=transport"),
            "{out}"
        );
    }

    #[cfg(feature = "xml")]
    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn soap_bootstrap_logs_transport_failure() {
        let _serial = SERIAL.lock().expect("serial");
        init_wire_capture();
        clear_wire_lines();

        use crate::xml::client::SoapClient;

        let mut soap = SoapClient::new(
            test_dead_port_http_client(),
            TEST_WIRE_DEAD_ADDR,
            super::API_RELEASE,
            "soap-bootstrap-wire-test",
            WireLoggingMode::Summary,
        );
        let err = soap.bootstrap().await;
        assert!(matches!(err, Err(Error::ReqwestError(_))));
        let out = joined_wire_output();
        assert!(
            out.contains("RetrieveServiceContent")
                && out.contains("phase=request")
                && out.contains("error=transport"),
            "SOAP bootstrap wire: {out}"
        );
    }

    #[cfg(feature = "xml")]
    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn soap_invoke_logs_transport_failure() {
        let _serial = SERIAL.lock().expect("serial");
        init_wire_capture();
        clear_wire_lines();

        use crate::xml::client::soap_test_client_with_service_content;

        let soap = soap_test_client_with_service_content();
        let err = VimClient::invoke(
            &soap,
            "",
            "VirtualMachine",
            "vm-wire",
            "RefreshStorageInfo",
            None,
        )
        .await;
        assert!(matches!(err, Err(Error::ReqwestError(_))));
        let out = joined_wire_output();
        assert!(
            out.contains("RefreshStorageInfo") && out.contains("error=transport"),
            "SOAP invoke wire: {out}"
        );
    }

    #[cfg(feature = "xml")]
    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn soap_drop_logout_emits_wire_on_http_success() {
        let _serial = SERIAL.lock().expect("serial");
        init_wire_capture();
        clear_wire_lines();

        let (origin, stub) = spawn_http_stub_once(200, b"");
        let endpoint = format!("{}/sdk", origin.trim_end_matches('/'));
        let soap = crate::xml::client::soap_test_client_for_logout_drop(
            reqwest::Client::builder()
                .timeout(std::time::Duration::from_secs(5))
                .build()
                .unwrap(),
            endpoint,
        );
        drop(soap);
        stub.join().expect("stub thread");
        let out = joined_wire_output();
        assert!(
            out.contains("wire=soap")
                && out.contains("kind=logout")
                && out.contains("status=200")
                && !out.contains("error=http_failure"),
            "{out}"
        );
    }

    #[cfg(feature = "xml")]
    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn soap_drop_logout_emits_wire_on_http_non_success() {
        let _serial = SERIAL.lock().expect("serial");
        init_wire_capture();
        clear_wire_lines();

        let (origin, stub) = spawn_http_stub_once(502, b"err");
        let endpoint = format!("{}/sdk", origin.trim_end_matches('/'));
        let soap = crate::xml::client::soap_test_client_for_logout_drop(
            reqwest::Client::builder()
                .timeout(std::time::Duration::from_secs(5))
                .build()
                .unwrap(),
            endpoint,
        );
        drop(soap);
        stub.join().expect("stub thread");
        let out = joined_wire_output();
        assert!(
            out.contains("error=http_failure") && out.contains("status=502"),
            "{out}"
        );
    }

    #[cfg(feature = "xml")]
    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn soap_drop_logout_emits_wire_transport_line() {
        let _serial = SERIAL.lock().expect("serial");
        init_wire_capture();
        clear_wire_lines();

        let endpoint = format!("http://{}/sdk", super::TEST_WIRE_DEAD_ADDR);
        let soap = crate::xml::client::soap_test_client_for_logout_drop(
            reqwest::Client::builder()
                .timeout(std::time::Duration::from_secs(2))
                .build()
                .unwrap(),
            endpoint,
        );
        drop(soap);
        let out = joined_wire_output();
        assert!(
            out.contains("kind=logout") && out.contains("error=transport"),
            "{out}"
        );
    }
}